Mercurial > mercurial > hgweb_xdwtools.cgi
view xdwaddatn2.cpp @ 9:cc5262d43399
small changes.
author | pyon@macmini |
---|---|
date | Thu, 05 Apr 2018 21:06:13 +0900 |
parents | 61ce4754737e |
children | e421bcd3bce4 |
line wrap: on
line source
/* Makefile debug: xdwaddatn2.cpp #rm -rf tempXXXX #cls gcc -g -O0 -I. xdwaddatn2.cpp xdwapi.lib #date #./a.exe -m -i target.xdw #date release: xdwaddatn2.cpp gcc -I. xdwaddatn2.cpp xdwapi.lib -static -o xdwaddatn2.exe strip xdwaddatn2.exe clean: rm -rf tempXXXX */ #include <stdbool.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> #include <glob.h> #include <io.h> #include <windows.h> #include <xdw_api.h> #include <xdwapian.h> #define MAXCOL 1024 #define MAXLINE 9999 void print_error( int code ) { switch ( code ) { case XDW_E_NOT_INSTALLED: fprintf( stderr, "DocuWorksがインストールされていません。" ); break; case XDW_E_FILE_NOT_FOUND: fprintf( stderr, "指定されたファイルが見つかりません。" ); break; case XDW_E_FILE_EXISTS: fprintf( stderr, "指定されたファイルはすでに存在します。" ); break; case XDW_E_ACCESSDENIED: fprintf( stderr, "指定されたファイルを開くことができません。" ); break; case XDW_E_INVALID_NAME: case XDW_E_BAD_NETPATH: fprintf( stderr, "指定されたファイルを開くことができません。(ネットワーク)" ); break; case XDW_E_BAD_FORMAT: fprintf( stderr, "指定されたファイルは正しいフォーマットではありません。" ); break; case XDW_E_INVALID_ACCESS: fprintf( stderr, "指定された操作をする権利がありません。" ); break; case XDW_E_SHARING_VIOLATION: fprintf( stderr, "ファイルが使用中です。" ); break; default: fprintf( stderr, "エラーが発生しました。" ); break; } fprintf( stderr, "[ Error code : %d ]\n", code ); } void print_help() { char prog[12] = "xdwaddatn2"; fprintf( stderr, "Usage:\n" ); fprintf( stderr, " %s -m mtxt x y txt sz tr infile\n", prog ); fprintf( stderr, " %s -p page x y txt sz tr infile\n", prog ); fprintf( stderr, " %s -m -i infile\n", prog ); fprintf( stderr, " %s -p -i infile\n", prog ); fprintf( stderr, " %s -v -p -i infile\n", prog ); fprintf( stderr, " %s -v -m -i infile\n", prog ); } void print_now( const char *msg ) { time_t now = time( NULL ); struct tm *ts = localtime( &now ); char buf[80]; strftime( buf, sizeof( buf ), "%H:%M:%S %Z", ts ); printf( "%s\t%s\n", buf, msg ); } int add_annotation( XDW_DOCUMENT_HANDLE h, int page, int x, int y, char* string, int* sz, int tr ) { // テキストアノテーションを作成する XDW_ANNOTATION_HANDLE annoation; int api_result = XDW_AddAnnotation( h, XDW_AID_TEXT, page, x, y, NULL, &annoation, NULL ); if ( api_result < 0 ) return api_result; // 作成したアノテーションに文字列を設定する api_result = XDW_SetAnnotationAttribute( h, annoation, XDW_ATN_Text, XDW_ATYPE_STRING, string, 0, NULL ); // 文字テキスト api_result = XDW_SetAnnotationAttribute( h, annoation, XDW_ATN_FontSize, XDW_ATYPE_INT, reinterpret_cast<char*>(sz), 0, NULL ); // フォントサイズ if ( tr ) { int color = XDW_COLOR_NONE; api_result = XDW_SetAnnotationAttribute( h, annoation, XDW_ATN_BackColor, XDW_ATYPE_INT, reinterpret_cast<char*>(&color), 0, NULL ); } return api_result; } int main( int argc, char* argv[] ) { /* オプションの解析 */ char prog[128]; strcpy( prog, argv[0] ); bool pnow = false; bool mmode = false; bool pmode = false; bool csv = false; char c; while ( --argc > 0 && ( *++argv )[0] == '-' ) { while ( c = *++argv[0] ) { switch ( c ) { case 'v': pnow = true; print_now( "start." ); break; case 'm': /* matched page: now writing... */ mmode = true; break; case 'p': /* page number: now writing... */ pmode = true; break; case 'i': /* csv file: now writing... */ csv = true; break; default: fprintf( stderr, "error: illegal option '%c'.\n", c ); print_help(); exit( 1 ); } } } if ( argc < 1 || ( mmode && pmode ) || ( !mmode && !pmode )) { print_help(); exit( 1 ); } /* リストの取り込み */ char *al = (char*)malloc( MAXLINE * sizeof( char ) * MAXCOL ); char buf[ MAXCOL ]; int alN = 0; if ( csv ) { if ( pnow ) print_now( "reading list." ); FILE *fp; if ( al == NULL ) { fprintf( stderr, "can't allocate memory\n" ); exit( 1 ); } if ( ( fp = fopen( "addatn.list", "r" ) ) == NULL ) { fprintf( stderr, "%s: can't open file [addatn.list]\n", argv[0] ); exit ( 1 ); } char *p; while ( fgets( buf, sizeof buf, fp ) ) { if ( !strncmp( buf, "#", 1 ) ) continue; if ( !strncmp( buf, "//", 2 ) ) continue; if ( !strcmp( buf, "\n" ) ) continue; if ( ( p = strchr( buf, '\n' ) ) != NULL ) { *p = '\0'; } strncpy( &al[ alN * MAXCOL ], buf, MAXCOL ); alN++; } fclose( fp ); } /* 本処理:アノテーションを貼付け */ if ( pnow ) print_now( "analizing xdw-file." ); char in_path[ _MAX_PATH ]; sprintf( buf, "copy %s %s", argv[0], "tmpXXXX.xdw" ); system( buf ); _fullpath( in_path, "tmpXXXX.xdw", _MAX_PATH ); int api_result = 0; XDW_DOCUMENT_HANDLE h = NULL; // 文書ハンドルを開く XDW_OPEN_MODE_EX mode = { sizeof( XDW_OPEN_MODE_EX ), XDW_OPEN_UPDATE, XDW_AUTH_NODIALOGUE }; api_result = XDW_OpenDocumentHandle( in_path, &h, (XDW_OPEN_MODE*)&mode ); if ( api_result < 0 ) { print_error( api_result ); return 0; } if ( !csv ) { if ( mmode ) { for ( int i = 0; i < alN; i++ ) { } } else { // pmode のはず } } else { if ( pnow ) print_now( "csv mode." ); XDW_DOCUMENT_INFO info = { sizeof( XDW_DOCUMENT_INFO ), 0, 0, 0 }; // 総ページ数を得る XDW_GetDocumentInformation( h, &info ); int last_page = info.nPages; int x, y, sz, tr; char *str, *m, *s; if ( mmode ) { XDW_FOUND_HANDLE pFoundHandle = NULL; for ( int p = 0; p < last_page; p++ ) { for ( int i = 0; i < alN; i++ ) { strncpy( buf, &al[ i * MAXCOL ], MAXCOL ); m = strtok( buf, "," ); api_result = XDW_FindTextInPage( h, p + 1, m, NULL, &pFoundHandle, NULL ); if ( pFoundHandle ) { x = atoi( strtok( NULL, "," ) ); y = atoi( strtok( NULL, "," ) ); s = strtok( NULL, "," ); sz = atoi( strtok( NULL, "," ) ); tr = atoi( strtok( NULL, "," ) ); api_result = add_annotation( h, p + 1, x, y, s, &sz, tr ); if ( api_result < 0 ) { print_error( api_result ); break; } } } } XDW_CloseFoundHandle( pFoundHandle ); } else { // pmode のはず int p; for ( int i = 0; i < alN; i++ ) { strncpy( buf, &al[ i * MAXCOL ], MAXCOL ); m = strtok( buf, "," ); p = atoi( m ); if ( p > 0 && p <= last_page ) { x = atoi( strtok( NULL, "," ) ); y = atoi( strtok( NULL, "," ) ); s = strtok( NULL, "," ); sz = atoi( strtok( NULL, "," ) ); tr = atoi( strtok( NULL, "," ) ); api_result = add_annotation( h, p, x, y, s, &sz, tr ); if ( api_result < 0 ) { print_error( api_result ); break; } } } } } api_result = XDW_SaveDocument( h, NULL ); // 変更をファイルに反映する XDW_CloseDocumentHandle( h, NULL ); // 文書ハンドルを閉じる /* 最適化 */ if ( pnow ) print_now( "optimizing." ); char out_path[ _MAX_PATH ]; _fullpath( out_path, "out.xdw", _MAX_PATH ); remove( out_path ); api_result = XDW_OptimizeDocument( in_path, out_path, NULL ); if ( api_result < 0 ) { print_error( api_result ); exit( 1 ); } /* 後処理 */ if ( pnow ) print_now( "cleaning." ); remove( in_path ); free( al ); if ( pnow ) print_now( "done." ); return 0; }