summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2013-04-01 13:53:23 +0800
committerPeng Wu <alexepico@gmail.com>2013-04-01 13:53:23 +0800
commit796012ff32e0b39539ddd5d328403147d71a47ff (patch)
tree5bdda4175cc87698e5f4449705a2fe7beee91282
parentbd294b8971242bfa3602dbba85faca1643792366 (diff)
downloadlibpinyin-796012ff32e0b39539ddd5d328403147d71a47ff.tar.gz
libpinyin-796012ff32e0b39539ddd5d328403147d71a47ff.tar.xz
libpinyin-796012ff32e0b39539ddd5d328403147d71a47ff.zip
update spseg.cpp
-rw-r--r--tests/storage/test_parser2.cpp2
-rw-r--r--utils/segment/spseg.cpp74
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';