From 80d2b005249b772e7cd7fdf763e217576d401823 Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Thu, 28 Apr 2011 15:04:46 +0800 Subject: use errno --- utils/segment/ngseg.cpp | 5 ++++- utils/segment/spseg.cpp | 11 +++++++---- utils/storage/gen_binary_files.cpp | 4 ++-- utils/storage/gen_pinyin_table.cpp | 14 +++++++++----- utils/storage/import_interpolation.cpp | 4 ++-- utils/training/estimate_k_mixture_model.cpp | 11 ++++++++--- utils/training/gen_ngram.cpp | 11 +++++++---- 7 files changed, 39 insertions(+), 21 deletions(-) (limited to 'utils') diff --git a/utils/segment/ngseg.cpp b/utils/segment/ngseg.cpp index 504cacc..c1a06a7 100644 --- a/utils/segment/ngseg.cpp +++ b/utils/segment/ngseg.cpp @@ -51,7 +51,6 @@ enum CONTEXT_STATE{ void print_help(){ printf("Usage: ngseg [--generate-extra-enter]\n"); - exit(1); } bool deal_with_segmentable(GArray * current_utf16){ @@ -93,8 +92,12 @@ int main(int argc, char * argv[]){ while ( i < argc ){ if ( strcmp ("--help", argv[i]) == 0 ){ print_help(); + exit(0); } else if ( strcmp("--generate-extra-enter", argv[i]) == 0 ){ gen_extra_enter = true; + } else { + print_help(); + exit(EINVAL); } ++i; } diff --git a/utils/segment/spseg.cpp b/utils/segment/spseg.cpp index 910770a..6a54288 100644 --- a/utils/segment/spseg.cpp +++ b/utils/segment/spseg.cpp @@ -129,7 +129,6 @@ bool backtrace(GArray * steps, glong phrase_len, GArray * strings){ void print_help(){ printf("Usage: spseg [--generate-extra-enter]\n"); - exit(1); } int main(int argc, char * argv[]){ @@ -141,8 +140,12 @@ int main(int argc, char * argv[]){ while ( i < argc ){ if ( strcmp ("--help", argv[i]) == 0) { print_help(); - }else if (strcmp("--generate-extra-enter", argv[i]) == 0) { + exit(0); + } else if (strcmp("--generate-extra-enter", argv[i]) == 0) { gen_extra_enter = true; + } else { + print_help(); + exit(EINVAL); } ++i; } @@ -152,7 +155,7 @@ int main(int argc, char * argv[]){ FILE * gb_file = fopen("../../data/gb_char.table", "r"); if ( gb_file == NULL ){ fprintf(stderr, "can't open gb_char.table!\n"); - exit(1); + exit(ENOENT); } g_phrases->load_text(gb_file); fclose(gb_file); @@ -160,7 +163,7 @@ int main(int argc, char * argv[]){ FILE * gbk_file = fopen("../../data/gbk_char.table", "r"); if ( gbk_file == NULL ){ fprintf(stderr, "can't open gbk_char.table!\n"); - exit(1); + exit(ENOENT); } g_phrases->load_text(gbk_file); fclose(gbk_file); diff --git a/utils/storage/gen_binary_files.cpp b/utils/storage/gen_binary_files.cpp index 757b075..f8d3c70 100644 --- a/utils/storage/gen_binary_files.cpp +++ b/utils/storage/gen_binary_files.cpp @@ -68,7 +68,7 @@ int main(int argc, char * argv[]){ FILE* infile = fopen("../../data/gb_char.table", "r"); if ( NULL == infile ){ printf("open gb_char.table failed!\n"); - exit(1); + exit(ENOENT); } phrase_index.load_text(1, infile); @@ -77,7 +77,7 @@ int main(int argc, char * argv[]){ infile = fopen("../../data/gbk_char.table", "r"); if ( NULL == infile ){ printf("open gbk_char.table failed!\n"); - exit(1); + exit(ENOENT); } phrase_index.load_text(2, infile); diff --git a/utils/storage/gen_pinyin_table.cpp b/utils/storage/gen_pinyin_table.cpp index a753a89..1808162 100644 --- a/utils/storage/gen_pinyin_table.cpp +++ b/utils/storage/gen_pinyin_table.cpp @@ -59,7 +59,6 @@ void print_help(){ printf(" the result output file\n"); printf(" input pinyin files\n"); printf(" phrase index identifier\n"); - exit(1); } gint phrase_item_compare(gconstpointer a, gconstpointer b){ @@ -83,13 +82,18 @@ int main(int argc, char * argv[]){ while ( i < argc ){ if ( strcmp("--help", argv[i] ) == 0) { print_help(); + exit(0); }else if ( strcmp("-t", argv[i] ) == 0){ - if ( ++i >= argc ) + if ( ++i >= argc ) { print_help(); + exit(EINVAL); + } phrase_index = atoi(argv[i]); }else if ( strcmp("-o", argv[i] ) == 0 ){ - if ( ++i >= argc ) + if ( ++i >= argc ) { print_help(); + exit(EINVAL); + } strcpy( outfilename, argv[i]); } else { feed_file(argv[i]); @@ -113,7 +117,7 @@ void feed_file ( const char * filename){ FILE * infile = fopen(filename, "r"); if ( NULL == infile ){ fprintf(stderr, "Can't open file %s.\n", filename); - exit(1); + exit(ENOENT); } while ( !feof(infile)){ fscanf(infile, "%s", phrase); @@ -226,7 +230,7 @@ void gen_phrase_file(const char * outfilename, int phrase_index){ FILE * outfile = fopen(outfilename, "w"); if (NULL == outfile ) { fprintf(stderr, "Can't write file %s.\n", outfilename); - exit(1); + exit(ENOENT); } phrase_token_t token = 1; char pinyin_buffer[4096]; diff --git a/utils/storage/import_interpolation.cpp b/utils/storage/import_interpolation.cpp index 71c4e63..57c87c1 100644 --- a/utils/storage/import_interpolation.cpp +++ b/utils/storage/import_interpolation.cpp @@ -231,7 +231,7 @@ int main(int argc, char * argv[]){ ssize_t result = my_getline(input); if ( result == -1 ) { fprintf(stderr, "empty file input.\n"); - exit(1); + exit(ENODATA); } //read "\data" line @@ -241,7 +241,7 @@ int main(int argc, char * argv[]){ assert(g_hash_table_lookup_extended(required, "model", NULL, (gpointer *)&value)); if ( !( strcmp("interpolation", value) == 0 ) ) { fprintf(stderr, "error: interpolation model expected.\n"); - exit(1); + exit(ENODATA); } result = my_getline(input); diff --git a/utils/training/estimate_k_mixture_model.cpp b/utils/training/estimate_k_mixture_model.cpp index 7a827c9..37a88df 100644 --- a/utils/training/estimate_k_mixture_model.cpp +++ b/utils/training/estimate_k_mixture_model.cpp @@ -26,7 +26,6 @@ void print_help(){ printf("estimate_k_mixture_model [--bigram-file ]\n"); printf(" [--deleted-bigram-file = argc ) + if ( ++i >= argc ) { print_help(); + exit(EINVAL); + } bigram_filename = argv[i]; } else if ( strcmp("--deleted-bigram-file", argv[i]) == 0){ - if ( ++i >= argc ) + if ( ++i >= argc ) { print_help(); + exit(EINVAL); + } deleted_bigram_filename = argv[i]; } else{ print_help(); + exit(EINVAL); } ++i; } diff --git a/utils/training/gen_ngram.cpp b/utils/training/gen_ngram.cpp index 367728a..4ac75e3 100644 --- a/utils/training/gen_ngram.cpp +++ b/utils/training/gen_ngram.cpp @@ -31,7 +31,6 @@ static PhraseLargeTable * g_phrases = NULL; void print_help(){ printf("gen_ngram [--skip-pi-gram-training] [--skip-unigram-training]\n"); printf(" [--bigram-file ]\n"); - exit(1); } int main(int argc, char * argv[]){ @@ -44,16 +43,20 @@ int main(int argc, char * argv[]){ while ( i < argc ){ if ( strcmp("--help", argv[i] ) == 0){ print_help(); + exit(0); }else if ( strcmp("--skip-pi-gram-training", argv[i] ) == 0) { train_pi_gram = false; }else if ( strcmp("--skip-unigram-training", argv[i] ) == 0) { train_unigram = false; }else if ( strcmp("--bigram-file", argv[i] ) == 0){ - if ( ++i >= argc ) + if ( ++i >= argc ) { print_help(); + exit(EINVAL); + } bigram_filename = argv[i]; }else{ print_help(); + exit(EINVAL); } ++i; } @@ -63,14 +66,14 @@ int main(int argc, char * argv[]){ FILE * gb_file = fopen("../../data/gb_char.table", "r"); if ( gb_file == NULL ){ fprintf(stderr, "can't open gb_char.table!\n"); - exit(1); + exit(ENOENT); } g_phrases->load_text(gb_file); fclose(gb_file); FILE * gbk_file = fopen("../../data/gbk_char.table", "r"); if ( gbk_file == NULL ){ fprintf(stderr, "can't open gbk_char.table!\n"); - exit(1); + exit(ENOENT); } g_phrases->load_text(gbk_file); fclose(gbk_file); -- cgit