diff options
author | Peng Wu <alexepico@gmail.com> | 2013-04-01 13:53:23 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2013-04-01 13:53:23 +0800 |
commit | 796012ff32e0b39539ddd5d328403147d71a47ff (patch) | |
tree | 5bdda4175cc87698e5f4449705a2fe7beee91282 /utils/segment/spseg.cpp | |
parent | bd294b8971242bfa3602dbba85faca1643792366 (diff) | |
download | libpinyin-796012ff32e0b39539ddd5d328403147d71a47ff.tar.gz libpinyin-796012ff32e0b39539ddd5d328403147d71a47ff.tar.xz libpinyin-796012ff32e0b39539ddd5d328403147d71a47ff.zip |
update spseg.cpp
Diffstat (limited to 'utils/segment/spseg.cpp')
-rw-r--r-- | utils/segment/spseg.cpp | 74 |
1 files changed, 41 insertions, 33 deletions
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'; |