view xdwgrep2.cpp @ 3:edfa39292d78

small fix.
author pyon@macmini
date Tue, 24 Oct 2017 19:29:36 +0900
parents f96c40f7aeae
children 8de5b1bd9506
line wrap: on
line source

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

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

*/

#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

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( 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( char infile[ _MAX_PATH ], char outfile[ _MAX_PATH ], 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 main( int argc, char* argv[] ) {

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

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

	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 );
		exit( 1 );
	}

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

	if ( match == 1 ) // 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 == 1 && unmatch == 1 ) 
		xdw_grep( argv[1], "uout.xdw", argv[0], 1 );

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