1 /* Modified by Paul Goins:
2 * This file contains modifications so it will compile without GTK
3 * dependencies and to make it better work with wxKanjipad.
5 /* KanjiPad - Japanese handwriting recognition front end
6 * Copyright (C) 1997 Owen Taylor
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program 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
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define MAX_STROKES 32
28 #ifndef PATH_MAX //FIXME: this is not found if debug mode is used (-pedantic ?)
29 # define PATH_MAX BUFLEN
32 static StrokeDic stroke_dicts
[MAX_STROKES
];
33 static char *progname
;
34 static char *data_dir
;
47 int len
= strlen(data_dir
)+1;
48 dir
= malloc(sizeof(char)*(len
));
49 strncpy(dir
, data_dir
, len
);
53 /* Default to current directory if no data dir is specified. */
54 dir
= malloc(sizeof(char)*9);
55 strcpy(dir
, "kanjipad");
59 for(at_db
=0;at_db
<MAX_STROKES
;++at_db
)
60 stroke_dicts
[at_db
]=0;
62 for(at_db
=0;at_db
<MAX_STROKES
;++at_db
)
64 int len
= strlen(dir
) + 12; /* dir len + ##.unistrok (11 chars)
65 + sep char = dir len + 12 */
66 if(len
<1 || len
>=PATH_MAX
)
68 fprintf(stderr
,"Failed to format path\n");
74 char separator
= '\\';
78 sprintf(fname
,"%s%c%02d.unistrok",dir
,separator
,at_db
);
80 fd
= fopen(fname
,"r");
83 StrokeDicEntryList
**ppnext
=&stroke_dicts
[at_db
];
85 while( fgets(buf
,PATH_MAX
,fd
) )
87 unsigned long unicode_point
;
91 StrokeDicEntryList
*plist
=0;
96 unicode_point
= strtol(pchr
,&next_pchr
,16);
100 fprintf(stderr
,"Failed to parse line:\n\"%s\"\n",pchr
);
105 if(unicode_point
<0x3300 ||
106 unicode_point
>0x2FA1F)
108 fprintf(stderr
,"Unicode out of range for Kanji: 0x%08lX\n"
109 "file \"%s\"\n\"%s\" ",
117 plist
= malloc(sizeof(StrokeDicEntryList
));
122 fprintf(stderr
,"out of memory\n");
128 plist
->m_entry
.m_character
=malloc(20);
129 if(!plist
->m_entry
.m_character
)
131 //plist->m_entry.m_character[ucs4toutf8(unicode_point,plist->m_entry.m_character)]=0;
132 sprintf(plist
->m_entry
.m_character
,"%lu",unicode_point
);
137 if( !*pchr
|| *pchr
=='\n')
140 fprintf(stderr
,"Failed to parse line: No Strokes\n\"%s\"\n",buf
);
141 free(plist
->m_entry
.m_character
);
149 while(*pchr
==' '||*pchr
=='\t')
152 while(*pchr
&& *pchr
!='#' && *pchr
!='|' && *pchr
!='\n')
155 goto fail_no_strokes
;
156 plist
->m_entry
.m_strokes
=malloc(pchr
-begin
+1);
157 if(!plist
->m_entry
.m_strokes
)
159 strncpy(plist
->m_entry
.m_strokes
,begin
,pchr
-begin
);
160 plist
->m_entry
.m_strokes
[pchr
-begin
]=0;
163 plist
->m_entry
.m_filters
=0;
166 if( !*pchr
|| *pchr
=='\n')
174 while(*pchr
==' '||*pchr
=='\t')
177 while(*pchr
&& *pchr
!='#' && *pchr
!='\n')
181 plist
->m_entry
.m_filters
=malloc(pchr
-begin
+1);
182 if(!plist
->m_entry
.m_filters
)
184 strncpy(plist
->m_entry
.m_filters
, begin
,pchr
-begin
);
185 plist
->m_entry
.m_filters
[pchr
-begin
]=0;
191 ppnext
=&plist
->m_next
;
193 fprintf(stderr,"Loaded %s\n",plist->m_entry.m_character);
194 fprintf(stderr,"Strokes:--%s--\n",plist->m_entry.m_strokes);
195 if(plist->m_entry.m_filters)
197 fprintf(stderr,"Filters:--%s--\n",plist->m_entry.m_filters);
211 process_strokes (FILE *file
)
213 RawStroke strokes
[MAX_STROKES
];
214 char *buffer
= malloc(BUFLEN
);
218 /* Read in strokes from standard in, all points for each stroke
219 * strung together on one line, until we get a blank line
227 if (!fgets(buffer
, buflen
, file
))
231 while( (buffer
[len
-1] != '\n') )
233 //fprintf(stderr,"READ PARTIAL (%d so far)\n",buflen-1);
238 buffer
= realloc(buffer
, buflen
);
240 if (!fgets(buffer
+len
, buflen
-len
, file
))
245 //fprintf(stderr,"parsing %d bytes\n",strlen(buffer));
251 while (isspace ((unsigned char) *p
)) p
++;
254 strokes
[nstrokes
].m_x
[len
] = strtol (p
, &q
, 0);
259 while (isspace ((unsigned char) *p
)) p
++;
262 strokes
[nstrokes
].m_y
[len
] = strtol (p
, &q
, 0);
273 strokes
[nstrokes
].m_len
= len
;
275 if (nstrokes
== MAX_STROKES
)
277 //fprintf(stderr,"added strok (%d)\n",nstrokes);
280 if (nstrokes
!= 0 && nstrokes
< MAX_STROKES
&& stroke_dicts
[nstrokes
])
282 //fprintf(stderr,"attempting %d stroke lookup\n",nstrokes);
285 StrokeScorer
*scorer
= StrokeScorerCreate (stroke_dicts
[nstrokes
],
289 StrokeScorerProcess(scorer
, -1);
290 top_picks
= StrokeScorerTopPicks(scorer
);
291 StrokeScorerDestroy(scorer
);
294 for (i
=0;i
<(int)top_picks
->m_argc
;i
++)
298 printf("%s",top_picks
->m_argv
[i
]);
309 fprintf(stderr
,"Invalid number of strokes: %d\n",nstrokes
);
317 fprintf(stderr
, "Usage: %s [-f/--data-dir DIR]\n", progname
);
322 real_main(int argc
, char **argv
)
325 char *p
= progname
= argv
[0];
328 if (*p
== '/') progname
= p
+1;
332 for (i
=1; i
<argc
; i
++)
334 if (!strcmp(argv
[i
], "--data-dir") ||
335 !strcmp(argv
[i
], "-d"))
351 while (process_strokes (stdin
))
360 /* To avoid a console window coming up while running our
361 * program, on Win32, we act as a GUI app... a WinMain()
366 # define _stdcall __attribute__((stdcall))
371 WinMain (struct HINSTANCE__
*hInstance
,
372 struct HINSTANCE__
*hPrevInstance
,
376 return real_main (__argc
, __argv
);
380 main(int argc
, char **argv
)
382 return real_main (argc
, argv
);