Update license in the spec file
[platform/core/uifw/anthy.git] / src-util / morph-main.c
1 /* ¥³¡¼¥Ñ¥¹¤«¤éÁ«°Ü¹ÔÎó¤òºî¤ë¤¿¤á¤Î¥³¡¼¥É 
2  *
3  * Copyright (C) 2005-2006 TABATA Yusuke
4  * Copyright (C) 2005-2006 YOSHIDA Yuichi
5  *
6  */
7 /*
8   This library is free software; you can redistribute it and/or
9   modify it under the terms of the GNU Lesser General Public
10   License as published by the Free Software Foundation; either
11   version 2 of the License, or (at your option) any later version.
12
13   This library is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public
19   License along with this library; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
21  */
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <anthy/anthy.h>
26 /* for print_context_info() */
27 #include <anthy/convdb.h>
28
29 struct test_context {
30   anthy_context_t ac;
31 };
32
33 static void read_file(struct test_context *tc, const char *fn);
34 extern void anthy_reload_record(void);
35
36 int verbose;
37 int use_utf8;
38
39 /**/
40 static void
41 init_test_context(struct test_context *tc)
42 {
43   tc->ac = anthy_create_context();
44   anthy_set_reconversion_mode(tc->ac, ANTHY_RECONVERT_ALWAYS);
45   if (use_utf8) {
46     anthy_context_set_encoding(tc->ac, ANTHY_UTF8_ENCODING);
47   }
48   anthy_reload_record();
49 }
50
51 static void
52 conv_sentence(struct test_context *tc, const char *str)
53 {
54   anthy_set_string(tc->ac, str);
55   if (verbose) {
56     anthy_print_context(tc->ac);
57   }
58   /**/
59   print_context_info(tc->ac, NULL);
60 }
61
62 /* ¹ÔËö¤Î²þ¹Ô¤òºï½ü */
63 static void
64 chomp(char *buf)
65 {
66   int len = strlen(buf);
67   while (len > 0) {
68     char c = buf[len - 1];
69     if (c == '\n' || c == '\r' || c == ' ' || c == '\t') {
70       buf[len - 1] = 0;
71       len --;
72     } else {
73       return ;
74     }
75   }
76 }
77
78 static void
79 read_fp(struct test_context *tc, FILE *fp)
80 {
81   char buf[1024];
82   while (fgets(buf, 1024, fp)) {
83     if (buf[0] == '#') {
84       continue;
85     }
86
87     if (!strncmp(buf, "\\include ", 9)) {
88       read_file(tc, &buf[9]);
89       continue;
90     }
91     chomp(buf);
92     conv_sentence(tc, buf);
93   }
94 }
95
96 static void
97 read_file(struct test_context *tc, const char *fn)
98 {
99   FILE *fp;
100   fp = fopen(fn, "r");
101   if (!fp) {
102     printf("failed to open (%s)\n", fn);
103     return ;
104   }
105   read_fp(tc, fp);
106   fclose(fp);
107 }
108
109 static void
110 print_usage(void)
111 {
112   printf("morphological analyzer\n");
113   printf(" $ ./morphological analyzer < [text-file]\n or");
114   printf(" $ ./morphological analyzer [text-file]\n");
115   exit(0);
116 }
117
118 static void
119 parse_args(int argc, char **argv)
120 {
121   int i;
122   for (i = 1; i < argc; i++) {
123     char *arg = argv[i];
124     if (!strcmp(arg, "--utf8")) {
125       use_utf8 = 1;
126     } else if (arg[i] == '-') {
127       print_usage();
128     }
129   }
130 }
131
132 int
133 main(int argc, char **argv)
134 {
135   struct test_context tc;
136   int i, nr;
137   anthy_init();
138   anthy_set_personality("");
139   init_test_context(&tc);
140
141   /*read_file(&tc, "index.txt");*/
142   parse_args(argc, argv);
143
144   nr = 0;
145   for (i = 1; i < argc; i++) {
146     read_file(&tc, argv[i]);
147     nr ++;
148   }
149   if (nr == 0) {
150     read_fp(&tc, stdin);
151   }
152
153   return 0;
154 }