Mercurial > mercurial > hgweb_xdwtools.cgi
comparison xdwsort.cpp @ 1:9341384c2785
added xdwsort.
| author | pyon@macmini |
|---|---|
| date | Mon, 16 Oct 2017 20:51:59 +0900 |
| parents | |
| children | edfa39292d78 |
comparison
equal
deleted
inserted
replaced
| 0:c1ebc8b218f2 | 1:9341384c2785 |
|---|---|
| 1 /* Makefile | |
| 2 debug: xdwsort.cpp | |
| 3 #rm -rf tempXXXX | |
| 4 #cls | |
| 5 gcc -g -O0 -I. xdwsort.cpp xdwapi.lib | |
| 6 #date | |
| 7 #./a.exe target.xdw | |
| 8 #date | |
| 9 | |
| 10 release: xdwsort.cpp | |
| 11 gcc -I. xdwsort.cpp xdwapi.lib -static -o xdwsort.exe | |
| 12 strip xdwsort.exe | |
| 13 | |
| 14 clean: | |
| 15 rm -rf tempXXXX | |
| 16 */ | |
| 17 | |
| 18 #include <stdio.h> | |
| 19 #include <string.h> | |
| 20 #include <stdlib.h> | |
| 21 #include <time.h> | |
| 22 #include <glob.h> | |
| 23 #include <io.h> | |
| 24 #include <windows.h> | |
| 25 #include <xdw_api.h> | |
| 26 | |
| 27 #define MAXCOL 1024 | |
| 28 #define MAXLINE 9999 | |
| 29 #define BLOCKSZ 64 | |
| 30 | |
| 31 void print_error( int code ) { | |
| 32 fprintf( stderr, "Error code : %d\n", code ); | |
| 33 switch ( code ) { | |
| 34 case XDW_E_NOT_INSTALLED: | |
| 35 fprintf( stderr, "DocuWorksがインストールされていません。" ); | |
| 36 break; | |
| 37 case XDW_E_FILE_NOT_FOUND: | |
| 38 fprintf( stderr, "指定されたファイルが見つかりません。" ); | |
| 39 break; | |
| 40 case XDW_E_FILE_EXISTS: | |
| 41 fprintf( stderr, "指定されたファイルはすでに存在します。" ); | |
| 42 break; | |
| 43 case XDW_E_ACCESSDENIED: | |
| 44 case XDW_E_INVALID_NAME: | |
| 45 case XDW_E_BAD_NETPATH: | |
| 46 fprintf( stderr, "指定されたファイルを開くことができません。" ); | |
| 47 break; | |
| 48 case XDW_E_BAD_FORMAT: | |
| 49 fprintf( stderr, "指定されたファイルは正しいフォーマットではありません。" ); | |
| 50 break; | |
| 51 case XDW_E_INVALID_ACCESS: | |
| 52 fprintf( stderr, "指定された操作をする権利がありません。" ); | |
| 53 break; | |
| 54 default: | |
| 55 fprintf( stderr, "エラーが発生しました。" ); | |
| 56 break; | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 void print_now( char *msg ) { | |
| 61 time_t now = time( NULL ); | |
| 62 struct tm *ts = localtime( &now ); | |
| 63 | |
| 64 char buf[80]; | |
| 65 strftime( buf, sizeof( buf ), "%H:%M:%S %Z", ts ); | |
| 66 printf( "%s\t%s\n", buf, msg ); | |
| 67 } | |
| 68 | |
| 69 int main( int argc, char* argv[] ) { | |
| 70 | |
| 71 /* オプションの解析 */ | |
| 72 char prog[128]; | |
| 73 strcpy( prog, argv[0] ); | |
| 74 | |
| 75 int pnow = 0; | |
| 76 char c; | |
| 77 while ( --argc > 0 && ( *++argv )[0] == '-' ) { | |
| 78 while ( c = *++argv[0] ) { | |
| 79 switch ( c ) { | |
| 80 case 'v': | |
| 81 pnow = 1; | |
| 82 print_now( "start." ); | |
| 83 break; | |
| 84 case 'l': /* now writing... */ | |
| 85 break; | |
| 86 default: | |
| 87 printf("error: illegal option '%c'.\n", c ); | |
| 88 exit( 1 ); | |
| 89 } | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 if ( argc < 1 ) { | |
| 94 fprintf( stderr, "%s infile\n", prog ); | |
| 95 fprintf( stderr, "%s -v infile\n", prog ); | |
| 96 exit( 1 ); | |
| 97 } | |
| 98 | |
| 99 /* ソートリストの取り込み */ | |
| 100 if ( pnow ) print_now( "reading sort-list." ); | |
| 101 | |
| 102 FILE *fp; | |
| 103 char buf[ MAXCOL ]; | |
| 104 char *sl = (char*)malloc( MAXLINE * sizeof( char ) * MAXCOL ); | |
| 105 | |
| 106 if ( sl == NULL ) { | |
| 107 fprintf( stderr, "can't allocate memory\n" ); | |
| 108 exit( 1 ); | |
| 109 } | |
| 110 | |
| 111 if ( ( fp = fopen( "sort.list", "r" ) ) == NULL ) { | |
| 112 fprintf( stderr, "%s: can't open file [sort.list]\n", argv[0] ); | |
| 113 exit ( 1 ); | |
| 114 } | |
| 115 | |
| 116 char *p; | |
| 117 int slN = 0; | |
| 118 while ( fgets( buf, sizeof buf, fp ) ) { | |
| 119 if ( !strncmp( buf, "#", 1 ) ) continue; | |
| 120 if ( !strncmp( buf, "//", 2 ) ) continue; | |
| 121 if ( !strcmp( buf, "\n" ) ) continue; | |
| 122 | |
| 123 if ( ( p = strchr( buf, '\n' ) ) != NULL ) { | |
| 124 *p = '\0'; | |
| 125 } | |
| 126 strncpy( &sl[ slN * MAXCOL ], buf, MAXCOL ); | |
| 127 slN++; | |
| 128 } | |
| 129 fclose( fp ); | |
| 130 /* | |
| 131 for ( int j = 0; j < slN; j++ ) { | |
| 132 printf( "%d : %s\n", j, &sl[ j * MAXCOL ] ); | |
| 133 } | |
| 134 exit( 0 ); | |
| 135 */ | |
| 136 | |
| 137 /* 本処理:並び順を決定しつつ抽出作業 */ | |
| 138 if ( pnow ) print_now( "analizing xdw-file." ); | |
| 139 | |
| 140 char in_path[ _MAX_PATH ]; | |
| 141 _fullpath( in_path, argv[0], _MAX_PATH ); | |
| 142 | |
| 143 int api_result = 0; | |
| 144 | |
| 145 XDW_DOCUMENT_HANDLE h = NULL; // 文書ハンドルを開く | |
| 146 XDW_OPEN_MODE_EX mode = { | |
| 147 sizeof( XDW_OPEN_MODE_EX ), XDW_OPEN_READONLY, XDW_AUTH_NODIALOGUE | |
| 148 }; | |
| 149 api_result = XDW_OpenDocumentHandle( in_path, &h, (XDW_OPEN_MODE*)&mode ); | |
| 150 if ( api_result < 0 ) { | |
| 151 print_error( api_result ); | |
| 152 return 0; | |
| 153 } | |
| 154 | |
| 155 XDW_DOCUMENT_INFO info = { sizeof( XDW_DOCUMENT_INFO ), 0, 0, 0 }; // 総ページ数を得る | |
| 156 XDW_GetDocumentInformation( h, &info ); | |
| 157 int last_page = info.nPages; | |
| 158 | |
| 159 int *table = (int*)malloc( sizeof( int ) * last_page ); | |
| 160 for ( int p = 0; p < last_page; p++ ) { | |
| 161 *( table + p ) = 9999; | |
| 162 } | |
| 163 int index = 0; | |
| 164 XDW_FOUND_HANDLE pFoundHandle = NULL; | |
| 165 for ( int i = 0; i < slN; i++ ) { | |
| 166 for ( int p = 0; p < last_page; p++ ) { | |
| 167 if ( *( table + p ) != 9999 ) continue; | |
| 168 api_result = XDW_FindTextInPage( h, p + 1, &sl[ i * MAXCOL ], NULL, &pFoundHandle, NULL ); | |
| 169 if ( api_result < 0 ) { | |
| 170 print_error( api_result ); | |
| 171 exit( 1 ); | |
| 172 } | |
| 173 if ( pFoundHandle != NULL ) { | |
| 174 *( table + p ) = ++index; | |
| 175 //printf( "found : %s at %d\n", &sl[ i * MAXCOL ], p + 1 ); | |
| 176 pFoundHandle = NULL; | |
| 177 } | |
| 178 } | |
| 179 } | |
| 180 free( sl ); | |
| 181 | |
| 182 if ( pnow ) print_now( "extracting pages." ); | |
| 183 mkdir( "tempXXXX" ); | |
| 184 for ( int p = 0; p < last_page; p++ ) { | |
| 185 if ( *( table + p ) == 9999 ) { | |
| 186 *( table + p ) = ++index; | |
| 187 } | |
| 188 //printf( "%d\n", *( table + p ) ); | |
| 189 sprintf( buf, "tempXXXX/%04d.xdw", *( table + p ) ); | |
| 190 _fullpath( in_path, buf, _MAX_PATH ); | |
| 191 api_result = XDW_GetPage( h, p + 1, in_path, NULL ); | |
| 192 if ( api_result < 0 ) { | |
| 193 print_error( api_result ); | |
| 194 exit( 1 ); | |
| 195 } | |
| 196 } | |
| 197 free( table ); | |
| 198 | |
| 199 XDW_CloseDocumentHandle( h, NULL ); // 文書ハンドルを閉じる | |
| 200 | |
| 201 /* マージ */ | |
| 202 if ( pnow ) print_now( "merging pages." ); | |
| 203 | |
| 204 /* ブロック版 */ | |
| 205 char *blk_path = (char*)malloc( BLOCKSZ * sizeof( char ) * _MAX_PATH ); | |
| 206 const char **blk_path_addr = (const char**)malloc( ( last_page / BLOCKSZ + 1 ) * sizeof( char* ) * _MAX_PATH ); | |
| 207 | |
| 208 int bn = 0; | |
| 209 // ブロック毎の処理 | |
| 210 for ( int p = 0, m = 0; p < last_page; p++ ) { | |
| 211 m = p % BLOCKSZ; | |
| 212 if ( m == 0 && p > 0 ) { | |
| 213 sprintf( buf, "tempXXXX/b%04d.xdw", ++bn ); | |
| 214 _fullpath( in_path, buf, _MAX_PATH ); | |
| 215 api_result = XDW_MergeXdwFiles( blk_path_addr, BLOCKSZ, in_path, NULL ); | |
| 216 if ( api_result < 0 ) { | |
| 217 print_error( api_result ); | |
| 218 exit( 1 ); | |
| 219 } | |
| 220 } | |
| 221 sprintf( buf, "tempXXXX/%04d.xdw", p + 1 ); | |
| 222 _fullpath( in_path, buf, _MAX_PATH ); | |
| 223 strncpy( &blk_path[ m * _MAX_PATH ], buf, _MAX_PATH ); | |
| 224 blk_path_addr[m] = &blk_path[ m * _MAX_PATH ]; | |
| 225 } | |
| 226 if ( last_page % BLOCKSZ != 0 ) { | |
| 227 sprintf( buf, "tempXXXX/b%04d.xdw", ++bn ); | |
| 228 _fullpath( in_path, buf, _MAX_PATH ); | |
| 229 api_result = XDW_MergeXdwFiles( blk_path_addr, last_page % BLOCKSZ, in_path, NULL ); | |
| 230 if ( api_result < 0 ) { | |
| 231 print_error( api_result ); | |
| 232 exit( 1 ); | |
| 233 } | |
| 234 } | |
| 235 | |
| 236 // ブロックをまとめる | |
| 237 for ( int b = 0; b < bn; b++ ) { | |
| 238 sprintf( buf, "tempXXXX/b%04d.xdw", b + 1 ); | |
| 239 _fullpath( in_path, buf, _MAX_PATH ); | |
| 240 strncpy( &blk_path[ b * _MAX_PATH ], buf, _MAX_PATH ); | |
| 241 blk_path_addr[b] = &blk_path[ b * _MAX_PATH ]; | |
| 242 } | |
| 243 _fullpath( in_path, "tempXXXX/temp.xdw", _MAX_PATH ); | |
| 244 remove( in_path ); | |
| 245 api_result = XDW_MergeXdwFiles( blk_path_addr, bn, in_path, NULL ); | |
| 246 if ( api_result < 0 ) { | |
| 247 print_error( api_result ); | |
| 248 exit( 1 ); | |
| 249 } | |
| 250 | |
| 251 free( blk_path ); | |
| 252 free( blk_path_addr ); | |
| 253 | |
| 254 /* ふるぺーじばん ok | |
| 255 char *buf_path = (char*)malloc( last_page * sizeof( char ) * _MAX_PATH ); | |
| 256 const char **buf_path_addr = (const char**)malloc( last_page * sizeof( char* ) * _MAX_PATH ); | |
| 257 if ( buf_path == NULL || buf_path_addr == NULL ) { | |
| 258 fprintf( stderr, "can't allocate memory\n" ); | |
| 259 exit( 1 ); | |
| 260 } | |
| 261 | |
| 262 for ( int p = 0; p < last_page; p++ ) { | |
| 263 sprintf( buf, "tempXXXX/%04d.xdw", p + 1 ); | |
| 264 _fullpath( buf, buf, _MAX_PATH ); | |
| 265 strncpy( &buf_path[ p * _MAX_PATH ], buf, _MAX_PATH ); | |
| 266 //printf( "%d %x %s\n", p, &buf_path[ p * _MAX_PATH ], &buf_path[ p * _MAX_PATH ] ); | |
| 267 buf_path_addr[p] = &buf_path[ p * _MAX_PATH ]; | |
| 268 } | |
| 269 | |
| 270 _fullpath( in_path, "tempXXXX/temp.xdw", _MAX_PATH ); | |
| 271 remove( in_path ); | |
| 272 api_result = XDW_MergeXdwFiles( buf_path_addr, last_page, in_path, NULL ); | |
| 273 if ( api_result < 0 ) { | |
| 274 print_error( api_result ); | |
| 275 exit( 1 ); | |
| 276 } | |
| 277 | |
| 278 free( buf_path ); | |
| 279 free( buf_path_addr ); | |
| 280 */ | |
| 281 | |
| 282 /* 最適化 */ | |
| 283 if ( pnow ) print_now( "optimizing." ); | |
| 284 char out_path[ _MAX_PATH ]; | |
| 285 _fullpath( out_path, "out.xdw", _MAX_PATH ); | |
| 286 remove( out_path ); | |
| 287 api_result = XDW_OptimizeDocument( in_path, out_path, NULL ); | |
| 288 if ( api_result < 0 ) { | |
| 289 print_error( api_result ); | |
| 290 exit( 1 ); | |
| 291 } | |
| 292 | |
| 293 /* 後処理 */ | |
| 294 if ( pnow ) print_now( "cleaning." ); | |
| 295 glob_t globbuf; | |
| 296 glob( "tempXXXX/*.*", 0, NULL, &globbuf ); | |
| 297 for ( int i = 0; i < globbuf.gl_pathc; i++ ) { | |
| 298 _fullpath( in_path, globbuf.gl_pathv[i], _MAX_PATH ); | |
| 299 remove( in_path ); | |
| 300 } | |
| 301 globfree( &globbuf ); | |
| 302 rmdir( "tempXXXX" ); | |
| 303 | |
| 304 if ( pnow ) print_now( "done." ); | |
| 305 return 0; | |
| 306 } | |
| 307 |
