view xdwgrep2.cpp @ 9:cc5262d43399

small changes.
author pyon@macmini
date Thu, 05 Apr 2018 21:06:13 +0900
parents 61ce4754737e
children
line wrap: on
line source

/* Makefile
debug: xdwgrep2.cpp
	gcc -g -O0 -I. xdwgrep2.cpp xdwapi.lib
	#date
	#./a.exe target.xdw
	#date

release: xdwgrep2.cpp
	gcc -I. xdwgrep2.cpp xdwapi.lib -static -o xdwgrep2.exe
	strip xdwgrep2.exe

*/

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <io.h>
#include <windows.h>
#include <xdw_api.h>

#define MAXKWORD  256
#define MAXLINE  4096

void print_error( int code ) {
	fprintf( stderr, "Error code : %d\n", code );
	switch ( code ) {
	case XDW_E_NOT_INSTALLED:
		fprintf( stderr, "DocuWorksがインストールされていません。" );
		break;
	case XDW_E_FILE_NOT_FOUND:
		fprintf( stderr, "指定されたファイルが見つかりません。" );
		break;
	case XDW_E_FILE_EXISTS:
		fprintf( stderr, "指定されたファイルはすでに存在します。" );
		break;
	case XDW_E_ACCESSDENIED:
	case XDW_E_INVALID_NAME:
	case XDW_E_BAD_NETPATH:
		fprintf( stderr, "指定されたファイルを開くことができません。" );
		break;
	case XDW_E_BAD_FORMAT:
		fprintf( stderr, "指定されたファイルは正しいフォーマットではありません。" );
		break;
	case XDW_E_INVALID_ACCESS:
		fprintf( stderr, "指定された操作をする権利がありません。" );
		break;
	default:
		fprintf( stderr, "エラーが発生しました。" );
		break;
	}
}

void print_now( const char *msg ) {
   time_t now = time( NULL );
   struct tm *ts = localtime( &now );

   char buf[80];
   strftime( buf, sizeof( buf ), "%H:%M:%S %Z", ts );
   printf( "%s\t%s\n", buf, msg );
}

/* 指定された単語で検索 */
int xdw_grep( const char infile[ _MAX_PATH ], const char outfile[ _MAX_PATH ], const char keyword[ MAXKWORD ], int inv ) {

	char in_path[ _MAX_PATH ], out_path[ _MAX_PATH ], tmp_path[ _MAX_PATH ];
	_fullpath( in_path,  infile,  _MAX_PATH );
	_fullpath( out_path, outfile, _MAX_PATH );
	_fullpath( tmp_path, "tempXXXX.xdw", _MAX_PATH );

	remove( tmp_path );
	remove( out_path );

	char buf[ _MAX_PATH ];
	sprintf( buf, "copy %s %s", in_path, tmp_path );
	system( buf );

	int api_result = 0;

	// 文書ハンドルを開く
	XDW_DOCUMENT_HANDLE h = NULL;
	XDW_OPEN_MODE_EX mode = { sizeof( XDW_OPEN_MODE_EX ), XDW_OPEN_UPDATE, XDW_AUTH_NODIALOGUE };
	api_result = XDW_OpenDocumentHandle( tmp_path, &h, ( XDW_OPEN_MODE* )&mode );
	if ( api_result < 0 ) {
		print_error( api_result );
		return 1;
	}

	XDW_DOCUMENT_INFO info = { sizeof( XDW_DOCUMENT_INFO ), 0, 0, 0 }; // 総ページ数を得る
	XDW_GetDocumentInformation( h, &info );
	int last_page = info.nPages;

	XDW_FOUND_HANDLE pFoundHandle = NULL;
	for ( int i = last_page; i >= 1; i-- ) {
		api_result = XDW_FindTextInPage( h, i, keyword, NULL, &pFoundHandle, NULL );

		if ( inv == 0 && pFoundHandle == NULL )
			api_result = XDW_DeletePage( h, i, NULL );
		else if ( inv == 1 && pFoundHandle != NULL )
			api_result = XDW_DeletePage( h, i, NULL );

		XDW_CloseFoundHandle( pFoundHandle );
	}
	// 文書ハンドルを閉じる
	XDW_SaveDocument( h, NULL );
	XDW_CloseDocumentHandle( h, NULL );

	// 最適化
	api_result = XDW_OptimizeDocument( tmp_path, out_path, NULL );
	if ( api_result < 0 ) {
		print_error( api_result );
		exit( 1 );
	}
	remove( tmp_path );

	return 0;
}

/* 検索する単語のリストをファイルから読み込む */
int xdw_grep_list( const char infile[ _MAX_PATH ], const char outfile[ _MAX_PATH ], bool inv ) {

	char in_path[ _MAX_PATH ], out_path[ _MAX_PATH ], tmp_path[ _MAX_PATH ];
	_fullpath( in_path,  infile,  _MAX_PATH );
	_fullpath( out_path, outfile, _MAX_PATH );
	_fullpath( tmp_path, "tempXXXX.xdw", _MAX_PATH );

	remove( tmp_path );
	remove( out_path );

	char buf[ _MAX_PATH ];
	sprintf( buf, "copy %s %s", in_path, tmp_path );
	system( buf );

	/* リストの取り込み */
	char *al = (char*)malloc( MAXLINE * sizeof( char ) * MAXKWORD );
	int alN = 0;
	FILE *fp;

	if ( al == NULL ) {
		fprintf( stderr, "can't allocate memory\n" );
		exit( 1 );
	}

	if ( ( fp = fopen( "grep.list", "r" ) ) == NULL ) {
		fprintf( stderr, "can't open file [grep.list]\n" );
		exit ( 1 );
	}
	char *p;
	while ( fgets( buf, sizeof buf, fp ) ) {
		if ( !strncmp( buf, "#",  1 ) ) continue;
		if ( !strncmp( buf, "//", 2 ) ) continue;
		if ( !strcmp( buf, "\n" )     ) continue;

		if ( ( p = strchr( buf, '\n' ) ) != NULL ) {
			*p = '\0';
		}
		strncpy( &al[ alN * MAXKWORD ], buf, MAXKWORD );
		alN++;
	}
	fclose( fp );
	int api_result = 0;

	// 文書ハンドルを開く
	XDW_DOCUMENT_HANDLE h = NULL;
	XDW_OPEN_MODE_EX mode = { sizeof( XDW_OPEN_MODE_EX ), XDW_OPEN_UPDATE, XDW_AUTH_NODIALOGUE };
	api_result = XDW_OpenDocumentHandle( tmp_path, &h, ( XDW_OPEN_MODE* )&mode );
	if ( api_result < 0 ) {
		print_error( api_result );
		return 1;
	}

	XDW_DOCUMENT_INFO info = { sizeof( XDW_DOCUMENT_INFO ), 0, 0, 0 }; // 総ページ数を得る
	XDW_GetDocumentInformation( h, &info );
	int last_page = info.nPages;

	XDW_FOUND_HANDLE pFoundHandle = NULL;
	bool f[9999];
	for ( int p = 0; p < last_page; p++ )
		f[p] = false;

	for ( int p = 0; p < last_page; p++ ) {
		for ( int i = 0; i < alN; i++ ) {
			api_result = XDW_FindTextInPage( h, p + 1, &al[ i * MAXKWORD ], NULL, &pFoundHandle, NULL );
			if ( pFoundHandle ) f[p] = true;
		}
	}
	XDW_CloseFoundHandle( pFoundHandle );

	if ( inv ) 
		for ( int p = 0; p < last_page; p++ )
			f[p] = !f[p];

	for ( int p = last_page; p >= 1; p-- ) {
		if ( !f[ p - 1 ] )
			api_result = XDW_DeletePage( h, p, NULL );
	}
	// 文書ハンドルを閉じる
	XDW_SaveDocument( h, NULL );
	XDW_CloseDocumentHandle( h, NULL );

	// 最適化
	api_result = XDW_OptimizeDocument( tmp_path, out_path, NULL );
	if ( api_result < 0 ) {
		print_error( api_result );
		exit( 1 );
	}
	remove( tmp_path );

	return 0;
}


int main( int argc, char* argv[] ) {

	/* オプションの解析 */
	char prog[128];
	strcpy( prog, argv[0] );

	bool pnow = false;
	bool match = true;
	bool unmatch = false;
	bool list = false;
	char c;
	while ( --argc > 0 && ( *++argv )[0] == '-' ) {
		while ( c = *++argv[0] ) {
			switch ( c ) {
				case 'l':
					pnow = true;
					print_now( "start." );
					break;
				case 'v':
					match   = false;
					unmatch = true;
					break;
				case 'w':
					unmatch = true;
					break;
				case 'i':
					list = true;
					break;
				default:
					fprintf( stderr, "error: illegal option '%c'.\n", c );
					exit( 1 );
			}
		}
	}


	if ( list ) {
		xdw_grep_list( argv[0], "mout.xdw", unmatch );
		exit( 0 );
	}

	if ( argc < 2 ) {
		fprintf( stderr, "%s keyword infile\n",    prog );
		fprintf( stderr, "%s -v keyword infile\n", prog );
		fprintf( stderr, "%s -w keyword infile\n", prog );
		fprintf( stderr, "%s -l keyword infile\n", prog );
		fprintf( stderr, "%s -i infile\n", prog );
		fprintf( stderr, "%s -i -v infile\n", prog );
		exit( 1 );
	}

	/* 検索 */
	if ( pnow ) print_now( "searching xdw-file." );

	if ( match ) // unmatch == 0|1
		xdw_grep( argv[1], "mout.xdw", argv[0], 0 );
	else	// match == 0 && unmatch == 1
		xdw_grep( argv[1], "uout.xdw", argv[0], 1 );

	if ( match && unmatch ) 
		xdw_grep( argv[1], "uout.xdw", argv[0], 1 );

	if ( pnow ) print_now( "done." );
	return 0;
}