changeset 7:8de5b1bd9506

update xdwgrep2.
author pyon@macmini
date Sat, 24 Mar 2018 08:51:45 +0900
parents a4af60a0ec32
children 61ce4754737e
files xdwaddatn2.cpp xdwaddpage2.cpp xdwgrep2.cpp
diffstat 3 files changed, 374 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xdwaddatn2.cpp	Sat Mar 24 08:51:45 2018 +0900
@@ -0,0 +1,254 @@
+/* Makefile
+debug: xdwaddatn2.c
+	#rm -rf tempXXXX
+	#cls
+	g++ -g -O0 -I. xdwaddatn2.c xdwapi.lib
+	#date
+	#./a.exe -m -i target.xdw
+	#date
+
+release: xdwaddatn2.c
+	g++ -I. xdwaddatn2.c xdwapi.lib -static -o xdwaddatn2.exe
+	strip xdwaddatn2.exe
+
+clean:
+	rm -rf tempXXXX
+*/
+
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <time.h>
+#include <glob.h>
+#include <io.h>
+#include <windows.h>
+#include <xdw_api.h>
+#include <xdwapian.h>
+
+#define MAXCOL  1024
+#define MAXLINE 9999
+#define BLOCKSZ   64
+
+void print_error( int 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:
+		fprintf( stderr, "指定されたファイルを開くことができません。" );
+		break;
+	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;
+	case XDW_E_SHARING_VIOLATION:
+		fprintf( stderr, "ファイルが使用中です。" );
+		break;
+	default:
+		fprintf( stderr, "エラーが発生しました。" );
+		break;
+	}
+	fprintf( stderr, "[ Error code : %d ]\n", code );
+}
+
+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 add_annotation( XDW_DOCUMENT_HANDLE h, int page, int x, int y, char* string, int* sz, int tr )
+{
+	// テキストアノテーションを作成する
+	XDW_ANNOTATION_HANDLE annoation;
+	int api_result = XDW_AddAnnotation( h, XDW_AID_TEXT, page, x, y, NULL, &annoation, NULL );
+	if ( api_result < 0 ) return api_result;
+
+	// 作成したアノテーションに文字列を設定する
+	api_result = XDW_SetAnnotationAttribute( h, annoation, XDW_ATN_Text, XDW_ATYPE_STRING, string, 0, NULL ); // 文字テキスト
+	api_result = XDW_SetAnnotationAttribute( h, annoation, XDW_ATN_FontSize, XDW_ATYPE_INT, reinterpret_cast<char*>(sz), 0, NULL ); // フォントサイズ
+    if ( tr ) {
+		int color = XDW_COLOR_NONE;
+		api_result = XDW_SetAnnotationAttribute( h, annoation, XDW_ATN_BackColor, XDW_ATYPE_INT, reinterpret_cast<char*>(&color), 0, NULL );
+    }
+
+	return api_result;
+}
+
+
+int main( int argc, char* argv[] ) {
+
+	/* オプションの解析 */
+	char prog[128];
+	strcpy( prog, argv[0] );
+
+	bool pnow  = false;
+	bool mmode = false;
+	bool csv   = false;
+	char c;
+	while ( --argc > 0 && ( *++argv )[0] == '-' ) {
+		while ( c = *++argv[0] ) {
+			switch ( c ) {
+				case 'v':
+					pnow = true;
+					print_now( "start." );
+					break;
+				case 'm':	/* matched page: now writing... */
+					mmode = true;
+					break;
+				case 'p':	/* page number: now writing... */
+					break;
+				case 'i':	/* csv file: now writing... */
+					csv = true;
+					break;
+				default:
+					fprintf( stderr, "error: illegal option '%c'.\n", c );
+					exit( 1 );
+			}
+		}
+	}
+
+	if ( argc < 1 ) {
+		fprintf( stderr, "%s -m mtxt x y txt sz tr infile\n", prog );
+		fprintf( stderr, "%s -p page x y txt sz tr infile\n", prog );
+		fprintf( stderr, "%s -m -i infile\n", prog );
+		fprintf( stderr, "%s -p -i infile\n", prog );
+		fprintf( stderr, "%s -v -p -i infile\n", prog );
+		fprintf( stderr, "%s -v -m -i infile\n", prog );
+		exit( 1 );
+	}
+
+	/* リストの取り込み */
+	char *al = (char*)malloc( MAXLINE * sizeof( char ) * MAXCOL );
+	char buf[ MAXCOL ];
+	int alN = 0;
+	if ( csv ) {
+		if ( pnow ) print_now( "reading list." );
+		FILE *fp;
+
+		if ( al == NULL ) {
+			fprintf( stderr, "can't allocate memory\n" );
+			exit( 1 );
+		}
+
+		if ( ( fp = fopen( "addatn.list", "r" ) ) == NULL ) {
+			fprintf( stderr, "%s: can't open file [addatn.list]\n", argv[0] );
+			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 * MAXCOL ], buf, MAXCOL );
+			alN++;
+		}
+		fclose( fp );
+	}
+
+	/* 本処理:アノテーションを貼付け */
+	if ( pnow ) print_now( "analizing xdw-file." );
+
+	char in_path[ _MAX_PATH ];
+	sprintf( buf, "copy %s %s", argv[0], "tmpXXXX.xdw" );
+	system( buf );
+	_fullpath( in_path, "tmpXXXX.xdw", _MAX_PATH );
+
+	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( in_path, &h, (XDW_OPEN_MODE*)&mode );
+	if ( api_result < 0 ) {
+		print_error( api_result );
+		return 0;
+	}
+
+	if ( !csv ) {
+		if ( mmode ) {
+			for ( int i = 0; i < alN; i++ ) {
+			}
+		}
+		else {
+		}
+	}
+	else {
+		if ( pnow ) print_now( "csv mode." );
+		XDW_DOCUMENT_INFO info = { sizeof( XDW_DOCUMENT_INFO ), 0, 0, 0 }; // 総ページ数を得る
+		XDW_GetDocumentInformation( h, &info );
+		int last_page = info.nPages;
+
+		int x, y, sz, tr;
+		char *str, *m, *s;
+		if ( mmode ) {
+			XDW_FOUND_HANDLE pFoundHandle = NULL;
+			for ( int p = 0; p < last_page; p++ ) {
+				for ( int i = 0; i < alN; i++ ) {
+					strncpy( buf, &al[ i * MAXCOL ], MAXCOL );
+					m = strtok( buf, "," );
+					api_result = XDW_FindTextInPage( h, p + 1, m, NULL, &pFoundHandle, NULL );
+					if ( pFoundHandle ) {
+						x = atoi( strtok( NULL, "," ) );
+						y = atoi( strtok( NULL, "," ) );
+						s = strtok( NULL, "," );
+						sz = atoi( strtok( NULL, "," ) );
+						tr = atoi( strtok( NULL, "," ) );
+						api_result = add_annotation( h, p + 1, x, y, s, &sz, tr );
+						if ( api_result < 0 ) {
+							print_error( api_result );
+							break;
+						}
+					}
+				}
+			}
+			XDW_CloseFoundHandle( pFoundHandle );
+		}
+		else {
+		}
+	}
+
+	api_result = XDW_SaveDocument( h, NULL ); // 変更をファイルに反映する
+	XDW_CloseDocumentHandle( h, NULL ); // 文書ハンドルを閉じる
+
+	/* 最適化 */
+	if ( pnow ) print_now( "optimizing." );
+	char out_path[ _MAX_PATH ];
+	_fullpath( out_path, "out.xdw", _MAX_PATH );
+	remove( out_path );
+	api_result = XDW_OptimizeDocument( in_path, out_path, NULL );
+	if ( api_result < 0 ) {
+		print_error( api_result );
+		exit( 1 );
+	}
+
+	/* 後処理 */
+	if ( pnow ) print_now( "cleaning." );
+	remove( in_path );
+	free( al );
+
+	if ( pnow ) print_now( "done." );
+	return 0;
+}
+
--- a/xdwaddpage2.cpp	Thu Nov 09 18:42:22 2017 +0900
+++ b/xdwaddpage2.cpp	Sat Mar 24 08:51:45 2018 +0900
@@ -1,10 +1,10 @@
 /* Makefile
 debug: xdwaddpage2.c
 	#cls
-	gcc -g -O0 -I. xdwaddpage2.c xdwapi.lib
+	g++ -g -O0 -I. xdwaddpage2.c xdwapi.lib
 
 release: xdwaddpage2.c
-	gcc -I. xdwaddpage2.c xdwapi.lib -static -o xdwaddpage2.exe
+	g++ -I. xdwaddpage2.c xdwapi.lib -static -o xdwaddpage2.exe
 	strip xdwaddpage2.exe
 
 clean:
--- a/xdwgrep2.cpp	Thu Nov 09 18:42:22 2017 +0900
+++ b/xdwgrep2.cpp	Sat Mar 24 08:51:45 2018 +0900
@@ -11,6 +11,7 @@
 
 */
 
+#include <stdbool.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
@@ -20,6 +21,7 @@
 #include <xdw_api.h>
 
 #define MAXKWORD  256
+#define MAXLINE   256
 
 void print_error( int code ) {
 	fprintf( stderr, "Error code : %d\n", code );
@@ -59,6 +61,7 @@
    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 ];
@@ -114,29 +117,127 @@
 	return 0;
 }
 
+/* 検索する単語のリストをファイルから読み込む */
+int xdw_grep_list( char infile[ _MAX_PATH ], 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] );
 
-	int pnow = 0;
-	int match   = 1;
-	int unmatch = 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 = 1;
+					pnow = true;
 					print_now( "start." );
 					break;
 				case 'v':
-					match   = 0;
-					unmatch = 1;
+					match   = false;
+					unmatch = true;
 					break;
 				case 'w':
-					unmatch = 1;
+					unmatch = true;
+					break;
+				case 'i':
+					list = true;
 					break;
 				default:
 					fprintf( stderr, "error: illegal option '%c'.\n", c );
@@ -145,23 +246,31 @@
 		}
 	}
 
+
+	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 == 1 ) // unmatch == 0|1
+	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 == 1 && unmatch == 1 ) 
+	if ( match && unmatch ) 
 		xdw_grep( argv[1], "uout.xdw", argv[0], 1 );
 
 	if ( pnow ) print_now( "done." );