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