comparison xdwsort.cpp @ 8:61ce4754737e

bug fix.
author pyon@macmini
date Tue, 03 Apr 2018 00:11:01 +0900
parents edfa39292d78
children
comparison
equal deleted inserted replaced
7:8de5b1bd9506 8:61ce4754737e
1 /* Makefile 1 /* Makefile
2 debug: xdwsort.c 2 debug: xdwsort.cpp
3 #rm -rf tempXXXX 3 #rm -rf tempXXXX
4 #cls 4 #cls
5 gcc -g -O0 -I. xdwsort.c xdwapi.lib 5 gcc -g -O0 -I. xdwsort.cpp xdwapi.lib
6 #date 6 #date
7 #./a.exe target.xdw 7 #./a.exe target.xdw
8 #date 8 #date
9 9
10 release: xdwsort.c 10 release: xdwsort.cpp
11 gcc -I. xdwsort.c xdwapi.lib -static -o xdwsort.exe 11 gcc -I. xdwsort.cpp xdwapi.lib -static -o xdwsort.exe
12 strip xdwsort.exe 12 strip xdwsort.exe
13 13
14 clean: 14 clean:
15 rm -rf tempXXXX 15 rm -rf tempXXXX
16 */ 16 */
17 17
18 #include <stdbool.h>
18 #include <stdio.h> 19 #include <stdio.h>
19 #include <string.h> 20 #include <string.h>
20 #include <stdlib.h> 21 #include <stdlib.h>
21 #include <time.h> 22 #include <time.h>
22 #include <glob.h> 23 #include <glob.h>
55 fprintf( stderr, "エラーが発生しました。" ); 56 fprintf( stderr, "エラーが発生しました。" );
56 break; 57 break;
57 } 58 }
58 } 59 }
59 60
60 void print_now( char *msg ) { 61 void print_now( const char *msg ) {
61 time_t now = time( NULL ); 62 time_t now = time( NULL );
62 struct tm *ts = localtime( &now ); 63 struct tm *ts = localtime( &now );
63 64
64 char buf[80]; 65 char buf[80];
65 strftime( buf, sizeof( buf ), "%H:%M:%S %Z", ts ); 66 strftime( buf, sizeof( buf ), "%H:%M:%S %Z", ts );
70 71
71 /* オプションの解析 */ 72 /* オプションの解析 */
72 char prog[128]; 73 char prog[128];
73 strcpy( prog, argv[0] ); 74 strcpy( prog, argv[0] );
74 75
75 int pnow = 0; 76 bool pnow = false;
77 bool debug = false;
76 char c; 78 char c;
77 while ( --argc > 0 && ( *++argv )[0] == '-' ) { 79 while ( --argc > 0 && ( *++argv )[0] == '-' ) {
78 while ( c = *++argv[0] ) { 80 while ( c = *++argv[0] ) {
79 switch ( c ) { 81 switch ( c ) {
80 case 'v': 82 case 'v':
81 pnow = 1; 83 pnow = true;
82 print_now( "start." ); 84 print_now( "start." );
83 break; 85 break;
84 case 'l': /* now writing... */ 86 case 'l': /* now writing... */
87 break;
88 case 'd':
89 pnow = true;
90 debug = true;
91 print_now( "debug-mode start." );
85 break; 92 break;
86 default: 93 default:
87 fprintf( stderr, "error: illegal option '%c'.\n", c ); 94 fprintf( stderr, "error: illegal option '%c'.\n", c );
88 exit( 1 ); 95 exit( 1 );
89 } 96 }
91 } 98 }
92 99
93 if ( argc < 1 ) { 100 if ( argc < 1 ) {
94 fprintf( stderr, "%s infile\n", prog ); 101 fprintf( stderr, "%s infile\n", prog );
95 fprintf( stderr, "%s -v infile\n", prog ); 102 fprintf( stderr, "%s -v infile\n", prog );
103 fprintf( stderr, "%s -d infile\n", prog );
96 exit( 1 ); 104 exit( 1 );
97 } 105 }
98 106
99 /* ソートリストの取り込み */ 107 /* ソートリストの取り込み */
100 if ( pnow ) print_now( "reading sort-list." ); 108 if ( pnow ) print_now( "reading sort-list." );
107 fprintf( stderr, "can't allocate memory\n" ); 115 fprintf( stderr, "can't allocate memory\n" );
108 exit( 1 ); 116 exit( 1 );
109 } 117 }
110 118
111 if ( ( fp = fopen( "sort.list", "r" ) ) == NULL ) { 119 if ( ( fp = fopen( "sort.list", "r" ) ) == NULL ) {
112 fprintf( stderr, "%s: can't open file [sort.list]\n", argv[0] ); 120 fprintf( stderr, "%s: can't open file [sort.list]\n", prog );
113 exit ( 1 ); 121 exit ( 1 );
114 } 122 }
115 123
116 char *p; 124 char *p;
117 int slN = 0; 125 int slN = 0;
221 sprintf( buf, "tempXXXX/%04d.xdw", p + 1 ); 229 sprintf( buf, "tempXXXX/%04d.xdw", p + 1 );
222 _fullpath( in_path, buf, _MAX_PATH ); 230 _fullpath( in_path, buf, _MAX_PATH );
223 strncpy( &blk_path[ m * _MAX_PATH ], in_path, _MAX_PATH ); 231 strncpy( &blk_path[ m * _MAX_PATH ], in_path, _MAX_PATH );
224 blk_path_addr[m] = &blk_path[ m * _MAX_PATH ]; 232 blk_path_addr[m] = &blk_path[ m * _MAX_PATH ];
225 } 233 }
226 if ( last_page % BLOCKSZ != 0 ) { 234
227 sprintf( buf, "tempXXXX/b%04d.xdw", ++bn ); 235 sprintf( buf, "tempXXXX/b%04d.xdw", ++bn );
228 _fullpath( in_path, buf, _MAX_PATH ); 236 _fullpath( in_path, buf, _MAX_PATH );
229 api_result = XDW_MergeXdwFiles( blk_path_addr, last_page % BLOCKSZ, in_path, NULL ); 237 int mod = last_page % BLOCKSZ;
230 if ( api_result < 0 ) { 238 if ( mod == 0 ) mod = BLOCKSZ;
231 print_error( api_result ); 239 api_result = XDW_MergeXdwFiles( blk_path_addr, mod, in_path, NULL );
232 exit( 1 ); 240 if ( api_result < 0 ) {
233 } 241 print_error( api_result );
242 exit( 1 );
234 } 243 }
235 244
236 // ブロックをまとめる 245 // ブロックをまとめる
237 for ( int b = 0; b < bn; b++ ) { 246 for ( int b = 0; b < bn; b++ ) {
238 sprintf( buf, "tempXXXX/b%04d.xdw", b + 1 ); 247 sprintf( buf, "tempXXXX/b%04d.xdw", b + 1 );
289 print_error( api_result ); 298 print_error( api_result );
290 exit( 1 ); 299 exit( 1 );
291 } 300 }
292 301
293 /* 後処理 */ 302 /* 後処理 */
294 if ( pnow ) print_now( "cleaning." ); 303 if ( !debug ) {
295 glob_t globbuf; 304 if ( pnow ) print_now( "cleaning." );
296 glob( "tempXXXX/*.*", 0, NULL, &globbuf ); 305 glob_t globbuf;
297 for ( int i = 0; i < globbuf.gl_pathc; i++ ) { 306 glob( "tempXXXX/*.*", 0, NULL, &globbuf );
298 _fullpath( in_path, globbuf.gl_pathv[i], _MAX_PATH ); 307 for ( int i = 0; i < globbuf.gl_pathc; i++ ) {
299 remove( in_path ); 308 _fullpath( in_path, globbuf.gl_pathv[i], _MAX_PATH );
300 } 309 remove( in_path );
301 globfree( &globbuf ); 310 }
302 rmdir( "tempXXXX" ); 311 globfree( &globbuf );
312 rmdir( "tempXXXX" );
313 }
303 314
304 if ( pnow ) print_now( "done." ); 315 if ( pnow ) print_now( "done." );
305 return 0; 316 return 0;
306 } 317 }
307 318