Mercurial > mercurial > hgweb_xdwtools.cgi
comparison xdwgrep2.cpp @ 7:8de5b1bd9506
update xdwgrep2.
| author | pyon@macmini |
|---|---|
| date | Sat, 24 Mar 2018 08:51:45 +0900 |
| parents | edfa39292d78 |
| children | 61ce4754737e |
comparison
equal
deleted
inserted
replaced
| 6:a4af60a0ec32 | 7:8de5b1bd9506 |
|---|---|
| 9 gcc -I. xdwgrep2.c xdwapi.lib -static -o xdwgrep2.exe | 9 gcc -I. xdwgrep2.c xdwapi.lib -static -o xdwgrep2.exe |
| 10 strip xdwgrep2.exe | 10 strip xdwgrep2.exe |
| 11 | 11 |
| 12 */ | 12 */ |
| 13 | 13 |
| 14 #include <stdbool.h> | |
| 14 #include <stdio.h> | 15 #include <stdio.h> |
| 15 #include <string.h> | 16 #include <string.h> |
| 16 #include <stdlib.h> | 17 #include <stdlib.h> |
| 17 #include <time.h> | 18 #include <time.h> |
| 18 #include <io.h> | 19 #include <io.h> |
| 19 #include <windows.h> | 20 #include <windows.h> |
| 20 #include <xdw_api.h> | 21 #include <xdw_api.h> |
| 21 | 22 |
| 22 #define MAXKWORD 256 | 23 #define MAXKWORD 256 |
| 24 #define MAXLINE 256 | |
| 23 | 25 |
| 24 void print_error( int code ) { | 26 void print_error( int code ) { |
| 25 fprintf( stderr, "Error code : %d\n", code ); | 27 fprintf( stderr, "Error code : %d\n", code ); |
| 26 switch ( code ) { | 28 switch ( code ) { |
| 27 case XDW_E_NOT_INSTALLED: | 29 case XDW_E_NOT_INSTALLED: |
| 57 char buf[80]; | 59 char buf[80]; |
| 58 strftime( buf, sizeof( buf ), "%H:%M:%S %Z", ts ); | 60 strftime( buf, sizeof( buf ), "%H:%M:%S %Z", ts ); |
| 59 printf( "%s\t%s\n", buf, msg ); | 61 printf( "%s\t%s\n", buf, msg ); |
| 60 } | 62 } |
| 61 | 63 |
| 64 /* 指定された単語で検索 */ | |
| 62 int xdw_grep( char infile[ _MAX_PATH ], char outfile[ _MAX_PATH ], char keyword[ MAXKWORD ], int inv ) { | 65 int xdw_grep( char infile[ _MAX_PATH ], char outfile[ _MAX_PATH ], char keyword[ MAXKWORD ], int inv ) { |
| 63 | 66 |
| 64 char in_path[ _MAX_PATH ], out_path[ _MAX_PATH ], tmp_path[ _MAX_PATH ]; | 67 char in_path[ _MAX_PATH ], out_path[ _MAX_PATH ], tmp_path[ _MAX_PATH ]; |
| 65 _fullpath( in_path, infile, _MAX_PATH ); | 68 _fullpath( in_path, infile, _MAX_PATH ); |
| 66 _fullpath( out_path, outfile, _MAX_PATH ); | 69 _fullpath( out_path, outfile, _MAX_PATH ); |
| 112 remove( tmp_path ); | 115 remove( tmp_path ); |
| 113 | 116 |
| 114 return 0; | 117 return 0; |
| 115 } | 118 } |
| 116 | 119 |
| 120 /* 検索する単語のリストをファイルから読み込む */ | |
| 121 int xdw_grep_list( char infile[ _MAX_PATH ], char outfile[ _MAX_PATH ], bool inv ) { | |
| 122 | |
| 123 char in_path[ _MAX_PATH ], out_path[ _MAX_PATH ], tmp_path[ _MAX_PATH ]; | |
| 124 _fullpath( in_path, infile, _MAX_PATH ); | |
| 125 _fullpath( out_path, outfile, _MAX_PATH ); | |
| 126 _fullpath( tmp_path, "tempXXXX.xdw", _MAX_PATH ); | |
| 127 | |
| 128 remove( tmp_path ); | |
| 129 remove( out_path ); | |
| 130 | |
| 131 char buf[ _MAX_PATH ]; | |
| 132 sprintf( buf, "copy %s %s", in_path, tmp_path ); | |
| 133 system( buf ); | |
| 134 | |
| 135 /* リストの取り込み */ | |
| 136 char *al = (char*)malloc( MAXLINE * sizeof( char ) * MAXKWORD ); | |
| 137 int alN = 0; | |
| 138 FILE *fp; | |
| 139 | |
| 140 if ( al == NULL ) { | |
| 141 fprintf( stderr, "can't allocate memory\n" ); | |
| 142 exit( 1 ); | |
| 143 } | |
| 144 | |
| 145 if ( ( fp = fopen( "grep.list", "r" ) ) == NULL ) { | |
| 146 fprintf( stderr, "can't open file [grep.list]\n" ); | |
| 147 exit ( 1 ); | |
| 148 } | |
| 149 char *p; | |
| 150 while ( fgets( buf, sizeof buf, fp ) ) { | |
| 151 if ( !strncmp( buf, "#", 1 ) ) continue; | |
| 152 if ( !strncmp( buf, "//", 2 ) ) continue; | |
| 153 if ( !strcmp( buf, "\n" ) ) continue; | |
| 154 | |
| 155 if ( ( p = strchr( buf, '\n' ) ) != NULL ) { | |
| 156 *p = '\0'; | |
| 157 } | |
| 158 strncpy( &al[ alN * MAXKWORD ], buf, MAXKWORD ); | |
| 159 alN++; | |
| 160 } | |
| 161 fclose( fp ); | |
| 162 int api_result = 0; | |
| 163 | |
| 164 // 文書ハンドルを開く | |
| 165 XDW_DOCUMENT_HANDLE h = NULL; | |
| 166 XDW_OPEN_MODE_EX mode = { sizeof( XDW_OPEN_MODE_EX ), XDW_OPEN_UPDATE, XDW_AUTH_NODIALOGUE }; | |
| 167 api_result = XDW_OpenDocumentHandle( tmp_path, &h, ( XDW_OPEN_MODE* )&mode ); | |
| 168 if ( api_result < 0 ) { | |
| 169 print_error( api_result ); | |
| 170 return 1; | |
| 171 } | |
| 172 | |
| 173 XDW_DOCUMENT_INFO info = { sizeof( XDW_DOCUMENT_INFO ), 0, 0, 0 }; // 総ページ数を得る | |
| 174 XDW_GetDocumentInformation( h, &info ); | |
| 175 int last_page = info.nPages; | |
| 176 | |
| 177 XDW_FOUND_HANDLE pFoundHandle = NULL; | |
| 178 bool f[9999]; | |
| 179 for ( int p = 0; p < last_page; p++ ) | |
| 180 f[p] = false; | |
| 181 | |
| 182 for ( int p = 0; p < last_page; p++ ) { | |
| 183 for ( int i = 0; i < alN; i++ ) { | |
| 184 api_result = XDW_FindTextInPage( h, p + 1, &al[ i * MAXKWORD ], NULL, &pFoundHandle, NULL ); | |
| 185 if ( pFoundHandle ) f[p] = true; | |
| 186 } | |
| 187 } | |
| 188 XDW_CloseFoundHandle( pFoundHandle ); | |
| 189 | |
| 190 if ( inv ) | |
| 191 for ( int p = 0; p < last_page; p++ ) | |
| 192 f[p] = !f[p]; | |
| 193 | |
| 194 for ( int p = last_page; p >= 1; p-- ) { | |
| 195 if ( !f[ p - 1 ] ) | |
| 196 api_result = XDW_DeletePage( h, p, NULL ); | |
| 197 } | |
| 198 // 文書ハンドルを閉じる | |
| 199 XDW_SaveDocument( h, NULL ); | |
| 200 XDW_CloseDocumentHandle( h, NULL ); | |
| 201 | |
| 202 // 最適化 | |
| 203 api_result = XDW_OptimizeDocument( tmp_path, out_path, NULL ); | |
| 204 if ( api_result < 0 ) { | |
| 205 print_error( api_result ); | |
| 206 exit( 1 ); | |
| 207 } | |
| 208 remove( tmp_path ); | |
| 209 | |
| 210 return 0; | |
| 211 } | |
| 212 | |
| 213 | |
| 117 int main( int argc, char* argv[] ) { | 214 int main( int argc, char* argv[] ) { |
| 118 | 215 |
| 119 /* オプションの解析 */ | 216 /* オプションの解析 */ |
| 120 char prog[128]; | 217 char prog[128]; |
| 121 strcpy( prog, argv[0] ); | 218 strcpy( prog, argv[0] ); |
| 122 | 219 |
| 123 int pnow = 0; | 220 bool pnow = false; |
| 124 int match = 1; | 221 bool match = true; |
| 125 int unmatch = 0; | 222 bool unmatch = false; |
| 223 bool list = false; | |
| 126 char c; | 224 char c; |
| 127 while ( --argc > 0 && ( *++argv )[0] == '-' ) { | 225 while ( --argc > 0 && ( *++argv )[0] == '-' ) { |
| 128 while ( c = *++argv[0] ) { | 226 while ( c = *++argv[0] ) { |
| 129 switch ( c ) { | 227 switch ( c ) { |
| 130 case 'l': | 228 case 'l': |
| 131 pnow = 1; | 229 pnow = true; |
| 132 print_now( "start." ); | 230 print_now( "start." ); |
| 133 break; | 231 break; |
| 134 case 'v': | 232 case 'v': |
| 135 match = 0; | 233 match = false; |
| 136 unmatch = 1; | 234 unmatch = true; |
| 137 break; | 235 break; |
| 138 case 'w': | 236 case 'w': |
| 139 unmatch = 1; | 237 unmatch = true; |
| 238 break; | |
| 239 case 'i': | |
| 240 list = true; | |
| 140 break; | 241 break; |
| 141 default: | 242 default: |
| 142 fprintf( stderr, "error: illegal option '%c'.\n", c ); | 243 fprintf( stderr, "error: illegal option '%c'.\n", c ); |
| 143 exit( 1 ); | 244 exit( 1 ); |
| 144 } | 245 } |
| 145 } | 246 } |
| 146 } | 247 } |
| 147 | 248 |
| 249 | |
| 250 if ( list ) { | |
| 251 xdw_grep_list( argv[0], "mout.xdw", unmatch ); | |
| 252 exit( 0 ); | |
| 253 } | |
| 254 | |
| 148 if ( argc < 2 ) { | 255 if ( argc < 2 ) { |
| 149 fprintf( stderr, "%s keyword infile\n", prog ); | 256 fprintf( stderr, "%s keyword infile\n", prog ); |
| 150 fprintf( stderr, "%s -v keyword infile\n", prog ); | 257 fprintf( stderr, "%s -v keyword infile\n", prog ); |
| 151 fprintf( stderr, "%s -w keyword infile\n", prog ); | 258 fprintf( stderr, "%s -w keyword infile\n", prog ); |
| 152 fprintf( stderr, "%s -l keyword infile\n", prog ); | 259 fprintf( stderr, "%s -l keyword infile\n", prog ); |
| 260 fprintf( stderr, "%s -i infile\n", prog ); | |
| 261 fprintf( stderr, "%s -i -v infile\n", prog ); | |
| 153 exit( 1 ); | 262 exit( 1 ); |
| 154 } | 263 } |
| 155 | 264 |
| 156 /* 検索 */ | 265 /* 検索 */ |
| 157 if ( pnow ) print_now( "searching xdw-file." ); | 266 if ( pnow ) print_now( "searching xdw-file." ); |
| 158 | 267 |
| 159 if ( match == 1 ) // unmatch == 0|1 | 268 if ( match ) // unmatch == 0|1 |
| 160 xdw_grep( argv[1], "mout.xdw", argv[0], 0 ); | 269 xdw_grep( argv[1], "mout.xdw", argv[0], 0 ); |
| 161 else // match == 0 && unmatch == 1 | 270 else // match == 0 && unmatch == 1 |
| 162 xdw_grep( argv[1], "uout.xdw", argv[0], 1 ); | 271 xdw_grep( argv[1], "uout.xdw", argv[0], 1 ); |
| 163 | 272 |
| 164 if ( match == 1 && unmatch == 1 ) | 273 if ( match && unmatch ) |
| 165 xdw_grep( argv[1], "uout.xdw", argv[0], 1 ); | 274 xdw_grep( argv[1], "uout.xdw", argv[0], 1 ); |
| 166 | 275 |
| 167 if ( pnow ) print_now( "done." ); | 276 if ( pnow ) print_now( "done." ); |
| 168 return 0; | 277 return 0; |
| 169 } | 278 } |
