Mercurial > mercurial > hgweb_xdwtools.cgi
comparison xdwgrep2.cpp @ 2:f96c40f7aeae
add xdwgrep2.
| author | pyon@macmini |
|---|---|
| date | Mon, 23 Oct 2017 20:15:22 +0900 |
| parents | |
| children | edfa39292d78 |
comparison
equal
deleted
inserted
replaced
| 1:9341384c2785 | 2:f96c40f7aeae |
|---|---|
| 1 /* Makefile | |
| 2 debug: xdwgrep2.c | |
| 3 gcc -g -O0 -I. xdwgrep2.c xdwapi.lib | |
| 4 #date | |
| 5 #./a.exe target.xdw | |
| 6 #date | |
| 7 | |
| 8 release: xdwgrep2.c | |
| 9 gcc -I. xdwgrep2.c xdwapi.lib -static -o xdwgrep2.exe | |
| 10 strip xdwgrep2.exe | |
| 11 | |
| 12 */ | |
| 13 | |
| 14 #include <stdio.h> | |
| 15 #include <string.h> | |
| 16 #include <stdlib.h> | |
| 17 #include <time.h> | |
| 18 #include <io.h> | |
| 19 #include <windows.h> | |
| 20 #include <xdw_api.h> | |
| 21 | |
| 22 #define MAXKWORD 256 | |
| 23 | |
| 24 void print_error( int code ) { | |
| 25 fprintf( stderr, "Error code : %d\n", code ); | |
| 26 switch ( code ) { | |
| 27 case XDW_E_NOT_INSTALLED: | |
| 28 fprintf( stderr, "DocuWorksがインストールされていません。" ); | |
| 29 break; | |
| 30 case XDW_E_FILE_NOT_FOUND: | |
| 31 fprintf( stderr, "指定されたファイルが見つかりません。" ); | |
| 32 break; | |
| 33 case XDW_E_FILE_EXISTS: | |
| 34 fprintf( stderr, "指定されたファイルはすでに存在します。" ); | |
| 35 break; | |
| 36 case XDW_E_ACCESSDENIED: | |
| 37 case XDW_E_INVALID_NAME: | |
| 38 case XDW_E_BAD_NETPATH: | |
| 39 fprintf( stderr, "指定されたファイルを開くことができません。" ); | |
| 40 break; | |
| 41 case XDW_E_BAD_FORMAT: | |
| 42 fprintf( stderr, "指定されたファイルは正しいフォーマットではありません。" ); | |
| 43 break; | |
| 44 case XDW_E_INVALID_ACCESS: | |
| 45 fprintf( stderr, "指定された操作をする権利がありません。" ); | |
| 46 break; | |
| 47 default: | |
| 48 fprintf( stderr, "エラーが発生しました。" ); | |
| 49 break; | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 void print_now( char *msg ) { | |
| 54 time_t now = time( NULL ); | |
| 55 struct tm *ts = localtime( &now ); | |
| 56 | |
| 57 char buf[80]; | |
| 58 strftime( buf, sizeof( buf ), "%H:%M:%S %Z", ts ); | |
| 59 printf( "%s\t%s\n", buf, msg ); | |
| 60 } | |
| 61 | |
| 62 int xdw_grep( char infile[ _MAX_PATH ], char outfile[ _MAX_PATH ], char keyword[ MAXKWORD ], int inv ) { | |
| 63 | |
| 64 char in_path[ _MAX_PATH ], out_path[ _MAX_PATH ], tmp_path[ _MAX_PATH ]; | |
| 65 _fullpath( in_path, infile, _MAX_PATH ); | |
| 66 _fullpath( out_path, outfile, _MAX_PATH ); | |
| 67 _fullpath( tmp_path, "tempXXXX.xdw", _MAX_PATH ); | |
| 68 | |
| 69 remove( tmp_path ); | |
| 70 remove( out_path ); | |
| 71 | |
| 72 char buf[ _MAX_PATH ]; | |
| 73 sprintf( buf, "copy %s %s", in_path, tmp_path ); | |
| 74 system( buf ); | |
| 75 | |
| 76 int api_result = 0; | |
| 77 | |
| 78 // 文書ハンドルを開く | |
| 79 XDW_DOCUMENT_HANDLE h = NULL; | |
| 80 XDW_OPEN_MODE_EX mode = { sizeof( XDW_OPEN_MODE_EX ), XDW_OPEN_UPDATE, XDW_AUTH_NODIALOGUE }; | |
| 81 api_result = XDW_OpenDocumentHandle( tmp_path, &h, ( XDW_OPEN_MODE* )&mode ); | |
| 82 if ( api_result < 0 ) { | |
| 83 print_error( api_result ); | |
| 84 return 1; | |
| 85 } | |
| 86 | |
| 87 XDW_DOCUMENT_INFO info = { sizeof( XDW_DOCUMENT_INFO ), 0, 0, 0 }; // 総ページ数を得る | |
| 88 XDW_GetDocumentInformation( h, &info ); | |
| 89 int last_page = info.nPages; | |
| 90 | |
| 91 XDW_FOUND_HANDLE pFoundHandle = NULL; | |
| 92 for ( int i = last_page; i >= 1; i-- ) { | |
| 93 api_result = XDW_FindTextInPage( h, i, keyword, NULL, &pFoundHandle, NULL ); | |
| 94 | |
| 95 if ( inv == 0 && pFoundHandle == NULL ) | |
| 96 api_result = XDW_DeletePage( h, i, NULL ); | |
| 97 else if ( inv == 1 && pFoundHandle != NULL ) | |
| 98 api_result = XDW_DeletePage( h, i, NULL ); | |
| 99 | |
| 100 XDW_CloseFoundHandle( pFoundHandle ); | |
| 101 } | |
| 102 // 文書ハンドルを閉じる | |
| 103 XDW_SaveDocument( h, NULL ); | |
| 104 XDW_CloseDocumentHandle( h, NULL ); | |
| 105 | |
| 106 // 最適化 | |
| 107 api_result = XDW_OptimizeDocument( tmp_path, out_path, NULL ); | |
| 108 if ( api_result < 0 ) { | |
| 109 print_error( api_result ); | |
| 110 exit( 1 ); | |
| 111 } | |
| 112 remove( tmp_path ); | |
| 113 | |
| 114 return 0; | |
| 115 } | |
| 116 | |
| 117 int main( int argc, char* argv[] ) { | |
| 118 | |
| 119 /* オプションの解析 */ | |
| 120 char prog[128]; | |
| 121 strcpy( prog, argv[0] ); | |
| 122 | |
| 123 int pnow = 0; | |
| 124 int match = 1; | |
| 125 int unmatch = 0; | |
| 126 char c; | |
| 127 while ( --argc > 0 && ( *++argv )[0] == '-' ) { | |
| 128 while ( c = *++argv[0] ) { | |
| 129 switch ( c ) { | |
| 130 case 'l': | |
| 131 pnow = 1; | |
| 132 print_now( "start." ); | |
| 133 break; | |
| 134 case 'v': | |
| 135 match = 0; | |
| 136 unmatch = 1; | |
| 137 break; | |
| 138 case 'w': | |
| 139 unmatch = 1; | |
| 140 break; | |
| 141 default: | |
| 142 printf("error: illegal option '%c'.\n", c ); | |
| 143 exit( 1 ); | |
| 144 } | |
| 145 } | |
| 146 } | |
| 147 | |
| 148 if ( argc < 2 ) { | |
| 149 fprintf( stderr, "%s keyword infile\n", prog ); | |
| 150 fprintf( stderr, "%s -v keyword infile\n", prog ); | |
| 151 fprintf( stderr, "%s -w keyword infile\n", prog ); | |
| 152 fprintf( stderr, "%s -l keyword infile\n", prog ); | |
| 153 exit( 1 ); | |
| 154 } | |
| 155 | |
| 156 /* 検索 */ | |
| 157 if ( pnow ) print_now( "searching xdw-file." ); | |
| 158 | |
| 159 if ( match == 1 ) // unmatch == 0|1 | |
| 160 xdw_grep( argv[1], "mout.xdw", argv[0], 0 ); | |
| 161 else // match == 0 && unmatch == 1 | |
| 162 xdw_grep( argv[1], "uout.xdw", argv[0], 1 ); | |
| 163 | |
| 164 if ( match == 1 && unmatch == 1 ) | |
| 165 xdw_grep( argv[1], "uout.xdw", argv[0], 1 ); | |
| 166 | |
| 167 if ( pnow ) print_now( "done." ); | |
| 168 return 0; | |
| 169 } | |
| 170 |
