From 796012ff32e0b39539ddd5d328403147d71a47ff Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Mon, 1 Apr 2013 13:53:23 +0800 Subject: [PATCH] update spseg.cpp --- tests/storage/test_parser2.cpp | 2 +- utils/segment/spseg.cpp | 74 +++++++++++++++++++--------------- 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/tests/storage/test_parser2.cpp b/tests/storage/test_parser2.cpp index 046c523..5ea3fb8 100644 --- a/tests/storage/test_parser2.cpp +++ b/tests/storage/test_parser2.cpp @@ -66,7 +66,7 @@ int main(int argc, char * argv[]) { g_option_context_add_main_entries(context, entries, NULL); if (!g_option_context_parse(context, &argc, &argv, &error)) { g_print("option parsing failed:%s\n", error->message); - exit(1); + exit(EINVAL); } if (incomplete) diff --git a/utils/segment/spseg.cpp b/utils/segment/spseg.cpp index 85aa14c..212fccc 100644 --- a/utils/segment/spseg.cpp +++ b/utils/segment/spseg.cpp @@ -27,6 +27,17 @@ #include "utils_helper.h" +static gboolean gen_extra_enter = FALSE; +static gchar * outputfile = NULL; + +static GOptionEntry entries[] = +{ + {"outputfile", 'o', 0, G_OPTION_ARG_FILENAME, &outputfile, "output", "filename"}, + {"generate-extra-enter", 0, 0, G_OPTION_ARG_NONE, &gen_extra_enter, "generate ", NULL}, + {NULL} +}; + + /* graph shortest path sentence segment. */ /* Note: @@ -137,42 +148,41 @@ bool backtrace(GArray * steps, glong phrase_len, GArray * strings){ return true; } -void print_help(){ - printf("Usage: spseg [--generate-extra-enter] [-o outputfile] [inputfile]\n"); -} - int main(int argc, char * argv[]){ - int i = 1; - bool gen_extra_enter = false; FILE * input = stdin; FILE * output = stdout; setlocale(LC_ALL, ""); - //deal with options. - 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 if (strcmp("-o", argv[i]) == 0) { - if ( ++i >= argc ){ - print_help(); - exit(EINVAL); - } - output = fopen(argv[i], "w"); - if (NULL == output) { - print_help(); - exit(EINVAL); - } - } else { - input = fopen(argv[i], "r"); - if (NULL == input) { - print_help(); - exit(EINVAL); - } + + GError * error = NULL; + GOptionContext * context; + + context = g_option_context_new("- shortest path segment"); + g_option_context_add_main_entries(context, entries, NULL); + if (!g_option_context_parse(context, &argc, &argv, &error)) { + g_print("option parsing failed:%s\n", error->message); + exit(EINVAL); + } + + if (outputfile) { + output = fopen(outputfile, "w"); + if (NULL == output) { + perror("open file failed"); + exit(EINVAL); + } + } + + if (argc > 2) { + fprintf(stderr, "too many arguments.\n"); + exit(EINVAL); + } + + if (2 == argc) { + input = fopen(argv[1], "r"); + if (NULL == input) { + perror("open file failed"); + exit(EINVAL); } - ++i; } /* init phrase table */ @@ -186,9 +196,7 @@ int main(int argc, char * argv[]){ if (!load_phrase_index(&phrase_index)) exit(ENOENT); - char * linebuf = NULL; - size_t size = 0; - ssize_t read; + char * linebuf = NULL; size_t size = 0; ssize_t read; while( (read = getline(&linebuf, &size, input)) != -1 ){ if ( '\n' == linebuf[strlen(linebuf) - 1] ) { linebuf[strlen(linebuf) - 1] = '\0'; -- 2.34.1