Mercurial > mercurial > hgweb_xdwtools.cgi
diff xdwgrep2.cpp @ 7:8de5b1bd9506
update xdwgrep2.
author | pyon@macmini |
---|---|
date | Sat, 24 Mar 2018 08:51:45 +0900 |
parents | edfa39292d78 |
children | 61ce4754737e |
line wrap: on
line diff
--- a/xdwgrep2.cpp Thu Nov 09 18:42:22 2017 +0900 +++ b/xdwgrep2.cpp Sat Mar 24 08:51:45 2018 +0900 @@ -11,6 +11,7 @@ */ +#include <stdbool.h> #include <stdio.h> #include <string.h> #include <stdlib.h> @@ -20,6 +21,7 @@ #include <xdw_api.h> #define MAXKWORD 256 +#define MAXLINE 256 void print_error( int code ) { fprintf( stderr, "Error code : %d\n", code ); @@ -59,6 +61,7 @@ printf( "%s\t%s\n", buf, msg ); } +/* 指定された単語で検索 */ int xdw_grep( char infile[ _MAX_PATH ], char outfile[ _MAX_PATH ], char keyword[ MAXKWORD ], int inv ) { char in_path[ _MAX_PATH ], out_path[ _MAX_PATH ], tmp_path[ _MAX_PATH ]; @@ -114,29 +117,127 @@ return 0; } +/* 検索する単語のリストをファイルから読み込む */ +int xdw_grep_list( char infile[ _MAX_PATH ], char outfile[ _MAX_PATH ], bool inv ) { + + char in_path[ _MAX_PATH ], out_path[ _MAX_PATH ], tmp_path[ _MAX_PATH ]; + _fullpath( in_path, infile, _MAX_PATH ); + _fullpath( out_path, outfile, _MAX_PATH ); + _fullpath( tmp_path, "tempXXXX.xdw", _MAX_PATH ); + + remove( tmp_path ); + remove( out_path ); + + char buf[ _MAX_PATH ]; + sprintf( buf, "copy %s %s", in_path, tmp_path ); + system( buf ); + + /* リストの取り込み */ + char *al = (char*)malloc( MAXLINE * sizeof( char ) * MAXKWORD ); + int alN = 0; + FILE *fp; + + if ( al == NULL ) { + fprintf( stderr, "can't allocate memory\n" ); + exit( 1 ); + } + + if ( ( fp = fopen( "grep.list", "r" ) ) == NULL ) { + fprintf( stderr, "can't open file [grep.list]\n" ); + 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 * MAXKWORD ], buf, MAXKWORD ); + alN++; + } + fclose( fp ); + 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( tmp_path, &h, ( XDW_OPEN_MODE* )&mode ); + if ( api_result < 0 ) { + print_error( api_result ); + return 1; + } + + XDW_DOCUMENT_INFO info = { sizeof( XDW_DOCUMENT_INFO ), 0, 0, 0 }; // 総ページ数を得る + XDW_GetDocumentInformation( h, &info ); + int last_page = info.nPages; + + XDW_FOUND_HANDLE pFoundHandle = NULL; + bool f[9999]; + for ( int p = 0; p < last_page; p++ ) + f[p] = false; + + for ( int p = 0; p < last_page; p++ ) { + for ( int i = 0; i < alN; i++ ) { + api_result = XDW_FindTextInPage( h, p + 1, &al[ i * MAXKWORD ], NULL, &pFoundHandle, NULL ); + if ( pFoundHandle ) f[p] = true; + } + } + XDW_CloseFoundHandle( pFoundHandle ); + + if ( inv ) + for ( int p = 0; p < last_page; p++ ) + f[p] = !f[p]; + + for ( int p = last_page; p >= 1; p-- ) { + if ( !f[ p - 1 ] ) + api_result = XDW_DeletePage( h, p, NULL ); + } + // 文書ハンドルを閉じる + XDW_SaveDocument( h, NULL ); + XDW_CloseDocumentHandle( h, NULL ); + + // 最適化 + api_result = XDW_OptimizeDocument( tmp_path, out_path, NULL ); + if ( api_result < 0 ) { + print_error( api_result ); + exit( 1 ); + } + remove( tmp_path ); + + return 0; +} + + int main( int argc, char* argv[] ) { /* オプションの解析 */ char prog[128]; strcpy( prog, argv[0] ); - int pnow = 0; - int match = 1; - int unmatch = 0; + bool pnow = false; + bool match = true; + bool unmatch = false; + bool list = false; char c; while ( --argc > 0 && ( *++argv )[0] == '-' ) { while ( c = *++argv[0] ) { switch ( c ) { case 'l': - pnow = 1; + pnow = true; print_now( "start." ); break; case 'v': - match = 0; - unmatch = 1; + match = false; + unmatch = true; break; case 'w': - unmatch = 1; + unmatch = true; + break; + case 'i': + list = true; break; default: fprintf( stderr, "error: illegal option '%c'.\n", c ); @@ -145,23 +246,31 @@ } } + + if ( list ) { + xdw_grep_list( argv[0], "mout.xdw", unmatch ); + exit( 0 ); + } + if ( argc < 2 ) { fprintf( stderr, "%s keyword infile\n", prog ); fprintf( stderr, "%s -v keyword infile\n", prog ); fprintf( stderr, "%s -w keyword infile\n", prog ); fprintf( stderr, "%s -l keyword infile\n", prog ); + fprintf( stderr, "%s -i infile\n", prog ); + fprintf( stderr, "%s -i -v infile\n", prog ); exit( 1 ); } /* 検索 */ if ( pnow ) print_now( "searching xdw-file." ); - if ( match == 1 ) // unmatch == 0|1 + if ( match ) // unmatch == 0|1 xdw_grep( argv[1], "mout.xdw", argv[0], 0 ); else // match == 0 && unmatch == 1 xdw_grep( argv[1], "uout.xdw", argv[0], 1 ); - if ( match == 1 && unmatch == 1 ) + if ( match && unmatch ) xdw_grep( argv[1], "uout.xdw", argv[0], 1 ); if ( pnow ) print_now( "done." );