File: main.c

package info (click to toggle)
anthy 6300d-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 7,400 kB
  • ctags: 2,270
  • sloc: ansic: 17,009; sh: 13,554; lisp: 1,039; makefile: 252; ruby: 212; perl: 10
file content (228 lines) | stat: -rwxr-xr-x 4,452 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* 饤֥δؿƤӽФΥƥ */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#include <anthy.h>
#include <config.h>
#ifdef USE_UCS4
#include <langinfo.h>
#endif

/* Makefile  $(srcdir) (Ūǡեδǥ쥯ȥ) */
#ifndef SRCDIR
# define SRCDIR "."
#endif
/* ӥɻΥȥǥ쥯ȥ ( .anthy ) */
#ifndef TEST_HOME
# define TEST_HOME "."		/* FIXME: ºݤХѥȸư */
#endif

#define TESTDATA "test.txt"
const char *testdata = SRCDIR "/" TESTDATA;

struct input {
  char *str;
  char *kw;
  int serial;
} cur_input;

struct condition {
  int serial;
  int from;
  int to;
  char *kw;
  int loop;
  int silent;
} cond;

static int read_file(FILE *fp)
{
  char buf[256];
  while(fgets(buf, 256, fp)) {
    switch(buf[0]){
    case '#':
      break;
    case '*':
      if (cur_input.str) {
	free(cur_input.str);
	cur_input.str = 0;
      }
      buf[strlen(buf)-1] = 0;
      cur_input.str = strdup(&buf[1]);
      break;
    case ':':
      break;
    case '-':
      cur_input.serial ++;
      if (cur_input.kw) {
	free(cur_input.kw);
	cur_input.kw = 0;
      }
      return 0;
      break;
    }
  }
  return -1;
}

static int
check_cond(void)
{
  if (cur_input.serial == cond.serial) {
    return 1;
  }
  if (cur_input.kw && cond.kw && strstr(cur_input.kw, cond.kw)) {
    return 1;
  }
  if (cur_input.serial <= cond.to && cur_input.serial >= cond.from) {
    return 1;
  }
  return 0;
}

static void
init_lib(void)
{
  /* ˥󥹥ȡ뤵Ƥեαƶʤ褦ˤ */
  anthy_conf_override("CONFFILE", "../anthy-conf");
  anthy_conf_override("HOME", TEST_HOME);
  anthy_conf_override("DEPWORD", "master.depword");
  anthy_conf_override("DIC_FILE", "../mkanthydic/anthy.dic");
  anthy_conf_override("ANTHYDIR", SRCDIR "/../depgraph");
  if (anthy_init()) {
    printf("failed to init anthy\n");
    exit(0);
  }
  anthy_set_personality("");
#ifdef USE_UCS4
  /* ɽ뤿Υ󥳡ǥ󥰤ꤹ */
  anthy_context_set_encoding(NULL, ANTHY_UTF8_ENCODING);
#endif
}

static void
print_usage(void)
{
  printf("Anthy "VERSION"\n"
	 "./anthy [test-id]\n"
	 " For example.\n"
	 "  ./anthy 1\n"
	 "  ./anthy --to 100\n"
	 "  ./anthy --from 10 --to 100\n"
	 "  ./anthy --loop\n"
	 "  ./anthy --silent\n"
	 "  ./anthy --all\n"
	 "  ./anthy --ll 1\n\n");
  exit(0);
}

static void
parse_args(int argc, char **argv)
{
  int i;
  char *arg;
  for (i = 1; i < argc; i++) {
    arg = argv[i];
    if (!strncmp(arg, "--", 2)) {
      arg = &arg[2];
      if (!strcmp(arg, "help") || !strcmp(arg, "version")) {
	print_usage();
      }
      if (!strcmp(arg, "all")) {
	cond.from = 0;
	cond.to = 100000000;
      } else if (!strcmp(arg, "loop")) {
	cond.loop = 1;
      } else if (!strcmp(arg, "silent")) {
	cond.silent = 1;
      }
      if (i + 1 < argc) {
	if (!strcmp(arg, "from")){
	  cond.from = atoi(argv[i+1]);
	  i++;
	}else if (!strcmp(arg, "to")){
	  cond.to = atoi(argv[i+1]);
	  i++;
	}else if (!strcmp(arg, "ll")) {
	  anthy_set_logger(NULL, atoi(argv[i+1]));
	  i++;
	}
      }
    } else {
      int num = atoi(arg);
      if (num) {
	cond.serial = num;
      } else {
	char *buf = alloca(strlen(SRCDIR)+strlen(arg) + 10);
	sprintf(buf, SRCDIR "/%s.txt", arg);
	testdata = strdup(buf);
      }
    }
  }
}

static void
print_run_env(void)
{
  time_t t;
  const char *env;
  env = getenv("ANTHY_ENABLE_DEBUG_PRINT");
  if (!env) {
    env = "";
  }
  printf("ANTHY_ENABLE_DEBUG_PRINT=(%s)\n", env);
  env = getenv("ANTHY_SPLITTER_PRINT");
  if (!env) {
    env = "";
  }
  printf("ANTHY_SPLITTER_PRINT=(%s)\n", env);
  t = time(&t);
  printf(PACKAGE "-" VERSION " %s", ctime(&t));
}

int
main(int argc,char **argv)
{
  anthy_context_t ac;
  FILE *fp;

  parse_args(argc,argv);
  printf("./test_anthy --help to print usage.\n");

  print_run_env();

  init_lib();

 begin:;
  fp = fopen(testdata, "r");
  if (!fp) {
    printf("failed to open %s.\n", testdata);
    return 0;
  }
  
  ac = anthy_create_context();
  anthy_context_set_encoding(ac, ANTHY_EUC_JP_ENCODING);

  while (!read_file(fp)) {
    if (check_cond()) {
      anthy_set_string(ac,cur_input.str);
      if (!cond.silent) {
	printf("%d:(%s)\n", cur_input.serial, cur_input.str);
	anthy_print_context(ac);
      }
      anthy_reset_context(ac);
    }
  }
  anthy_release_context(ac);

  if (cond.loop) {
    fclose(fp);
    goto begin;
  }
  anthy_quit();

  return 0;
}