2
|
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
|
7
|
14 #include <stdbool.h>
|
2
|
15 #include <stdio.h>
|
|
16 #include <string.h>
|
|
17 #include <stdlib.h>
|
|
18 #include <time.h>
|
|
19 #include <io.h>
|
|
20 #include <windows.h>
|
|
21 #include <xdw_api.h>
|
|
22
|
|
23 #define MAXKWORD 256
|
7
|
24 #define MAXLINE 256
|
2
|
25
|
|
26 void print_error( int code ) {
|
|
27 fprintf( stderr, "Error code : %d\n", code );
|
|
28 switch ( code ) {
|
|
29 case XDW_E_NOT_INSTALLED:
|
|
30 fprintf( stderr, "DocuWorksがインストールされていません。" );
|
|
31 break;
|
|
32 case XDW_E_FILE_NOT_FOUND:
|
|
33 fprintf( stderr, "指定されたファイルが見つかりません。" );
|
|
34 break;
|
|
35 case XDW_E_FILE_EXISTS:
|
|
36 fprintf( stderr, "指定されたファイルはすでに存在します。" );
|
|
37 break;
|
|
38 case XDW_E_ACCESSDENIED:
|
|
39 case XDW_E_INVALID_NAME:
|
|
40 case XDW_E_BAD_NETPATH:
|
|
41 fprintf( stderr, "指定されたファイルを開くことができません。" );
|
|
42 break;
|
|
43 case XDW_E_BAD_FORMAT:
|
|
44 fprintf( stderr, "指定されたファイルは正しいフォーマットではありません。" );
|
|
45 break;
|
|
46 case XDW_E_INVALID_ACCESS:
|
|
47 fprintf( stderr, "指定された操作をする権利がありません。" );
|
|
48 break;
|
|
49 default:
|
|
50 fprintf( stderr, "エラーが発生しました。" );
|
|
51 break;
|
|
52 }
|
|
53 }
|
|
54
|
|
55 void print_now( char *msg ) {
|
|
56 time_t now = time( NULL );
|
|
57 struct tm *ts = localtime( &now );
|
|
58
|
|
59 char buf[80];
|
|
60 strftime( buf, sizeof( buf ), "%H:%M:%S %Z", ts );
|
|
61 printf( "%s\t%s\n", buf, msg );
|
|
62 }
|
|
63
|
7
|
64 /* 指定された単語で検索 */
|
2
|
65 int xdw_grep( char infile[ _MAX_PATH ], char outfile[ _MAX_PATH ], char keyword[ MAXKWORD ], int inv ) {
|
|
66
|
|
67 char in_path[ _MAX_PATH ], out_path[ _MAX_PATH ], tmp_path[ _MAX_PATH ];
|
|
68 _fullpath( in_path, infile, _MAX_PATH );
|
|
69 _fullpath( out_path, outfile, _MAX_PATH );
|
|
70 _fullpath( tmp_path, "tempXXXX.xdw", _MAX_PATH );
|
|
71
|
|
72 remove( tmp_path );
|
|
73 remove( out_path );
|
|
74
|
|
75 char buf[ _MAX_PATH ];
|
|
76 sprintf( buf, "copy %s %s", in_path, tmp_path );
|
|
77 system( buf );
|
|
78
|
|
79 int api_result = 0;
|
|
80
|
|
81 // 文書ハンドルを開く
|
|
82 XDW_DOCUMENT_HANDLE h = NULL;
|
|
83 XDW_OPEN_MODE_EX mode = { sizeof( XDW_OPEN_MODE_EX ), XDW_OPEN_UPDATE, XDW_AUTH_NODIALOGUE };
|
|
84 api_result = XDW_OpenDocumentHandle( tmp_path, &h, ( XDW_OPEN_MODE* )&mode );
|
|
85 if ( api_result < 0 ) {
|
|
86 print_error( api_result );
|
|
87 return 1;
|
|
88 }
|
|
89
|
|
90 XDW_DOCUMENT_INFO info = { sizeof( XDW_DOCUMENT_INFO ), 0, 0, 0 }; // 総ページ数を得る
|
|
91 XDW_GetDocumentInformation( h, &info );
|
|
92 int last_page = info.nPages;
|
|
93
|
|
94 XDW_FOUND_HANDLE pFoundHandle = NULL;
|
|
95 for ( int i = last_page; i >= 1; i-- ) {
|
|
96 api_result = XDW_FindTextInPage( h, i, keyword, NULL, &pFoundHandle, NULL );
|
|
97
|
|
98 if ( inv == 0 && pFoundHandle == NULL )
|
|
99 api_result = XDW_DeletePage( h, i, NULL );
|
|
100 else if ( inv == 1 && pFoundHandle != NULL )
|
|
101 api_result = XDW_DeletePage( h, i, NULL );
|
|
102
|
|
103 XDW_CloseFoundHandle( pFoundHandle );
|
|
104 }
|
|
105 // 文書ハンドルを閉じる
|
|
106 XDW_SaveDocument( h, NULL );
|
|
107 XDW_CloseDocumentHandle( h, NULL );
|
|
108
|
|
109 // 最適化
|
|
110 api_result = XDW_OptimizeDocument( tmp_path, out_path, NULL );
|
|
111 if ( api_result < 0 ) {
|
|
112 print_error( api_result );
|
|
113 exit( 1 );
|
|
114 }
|
|
115 remove( tmp_path );
|
|
116
|
|
117 return 0;
|
|
118 }
|
|
119
|
7
|
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
|
2
|
214 int main( int argc, char* argv[] ) {
|
|
215
|
|
216 /* オプションの解析 */
|
|
217 char prog[128];
|
|
218 strcpy( prog, argv[0] );
|
|
219
|
7
|
220 bool pnow = false;
|
|
221 bool match = true;
|
|
222 bool unmatch = false;
|
|
223 bool list = false;
|
2
|
224 char c;
|
|
225 while ( --argc > 0 && ( *++argv )[0] == '-' ) {
|
|
226 while ( c = *++argv[0] ) {
|
|
227 switch ( c ) {
|
|
228 case 'l':
|
7
|
229 pnow = true;
|
2
|
230 print_now( "start." );
|
|
231 break;
|
|
232 case 'v':
|
7
|
233 match = false;
|
|
234 unmatch = true;
|
2
|
235 break;
|
|
236 case 'w':
|
7
|
237 unmatch = true;
|
|
238 break;
|
|
239 case 'i':
|
|
240 list = true;
|
2
|
241 break;
|
|
242 default:
|
3
|
243 fprintf( stderr, "error: illegal option '%c'.\n", c );
|
2
|
244 exit( 1 );
|
|
245 }
|
|
246 }
|
|
247 }
|
|
248
|
7
|
249
|
|
250 if ( list ) {
|
|
251 xdw_grep_list( argv[0], "mout.xdw", unmatch );
|
|
252 exit( 0 );
|
|
253 }
|
|
254
|
2
|
255 if ( argc < 2 ) {
|
|
256 fprintf( stderr, "%s keyword infile\n", prog );
|
|
257 fprintf( stderr, "%s -v keyword infile\n", prog );
|
|
258 fprintf( stderr, "%s -w keyword infile\n", prog );
|
|
259 fprintf( stderr, "%s -l keyword infile\n", prog );
|
7
|
260 fprintf( stderr, "%s -i infile\n", prog );
|
|
261 fprintf( stderr, "%s -i -v infile\n", prog );
|
2
|
262 exit( 1 );
|
|
263 }
|
|
264
|
|
265 /* 検索 */
|
|
266 if ( pnow ) print_now( "searching xdw-file." );
|
|
267
|
7
|
268 if ( match ) // unmatch == 0|1
|
2
|
269 xdw_grep( argv[1], "mout.xdw", argv[0], 0 );
|
|
270 else // match == 0 && unmatch == 1
|
|
271 xdw_grep( argv[1], "uout.xdw", argv[0], 1 );
|
|
272
|
7
|
273 if ( match && unmatch )
|
2
|
274 xdw_grep( argv[1], "uout.xdw", argv[0], 1 );
|
|
275
|
|
276 if ( pnow ) print_now( "done." );
|
|
277 return 0;
|
|
278 }
|
|
279
|