/
chasen_lib.c
160 lines (126 loc) · 3.33 KB
/
chasen_lib.c
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
/* Copyright (c) 2000-2006 */
/* Yamashita Lab., Ritsumeikan University */
/* Studio ARC, ASTEM RI/Kyoto */
/* All rights reserved */
/* Modified for library-based implementation by H. Banno */
/* */
/* $Id: chasen_lib.c,v 1.3 2006/10/19 03:27:08 sako Exp $ */
#ifdef USE_SPLIB
#include <sp/spBaseLib.h>
#endif
#ifdef USE_CHASENLIB
#include <chasen.h>
#endif
#include "chaone.h"
struct _CHASEN_FD {
char *result;
};
typedef int (*chasen_getopt_argv_type )( char**, FILE * );
typedef char* (*chasen_sparse_tostr_type )( char* );
chasen_sparse_tostr_type chasen_sparse_func = NULL;
static char *current_result = NULL;
static CHASEN_FD init_chasen_fd(void)
{
CHASEN_FD fd;
fd = (CHASEN_FD)malloc(sizeof(struct _CHASEN_FD));
fd->result = NULL;
return fd;
}
int make_chasen_process( CHASEN_FD *fd_in, CHASEN_FD *fd_out )
{
char *opt[] = { NULL, chasen_rc_option, chasen_rc, NULL };
#if defined(USE_CHASENLIB)
{
chasen_getopt_argv(opt, stdout);
chasen_sparse_func = chasen_sparse_tostr;
*fd_in = init_chasen_fd();
*fd_out = init_chasen_fd();
return 0;
}
#elif defined(USE_SPLIB)
{
void *hlib;
chasen_getopt_argv_type chasen_getopt_func = NULL;
/* libchasen load */
if( ( hlib = spOpenLibrary( chasen_dll ) ) != 0 )
{
if( ( chasen_getopt_func = ( chasen_getopt_argv_type )spGetSymbolAddress( hlib, "chasen_getopt_argv" ) ) != 0 )
{
( *chasen_getopt_func )( opt, stdout );
}
if( ( chasen_sparse_func = ( chasen_sparse_tostr_type )spGetSymbolAddress( hlib, "chasen_sparse_tostr" ) ) != 0 )
{
*fd_in = init_chasen_fd();
*fd_out = init_chasen_fd();
/* no error */
return 0;
}
}
ErrMsg( "DLL open error in make_chasen_process\n" );
}
#else
ErrMsg( "make_chasen_process is not supported\n" );
#endif
return -1;
}
int chasen_write_line( CHASEN_FD fd_out, char *text )
{
char *chasen_result;
if (fd_out == NULL || chasen_sparse_func == NULL) return EOF;
if (current_result != NULL) {
free(current_result);
current_result = NULL;
}
/*TmpMsg( "chasen_write_line: text = %s\n", text );*/
if ((chasen_result = ( *chasen_sparse_func )( text )) == NULL) {
return EOF;
}
/*TmpMsg( "chasen_write_line: chasen_result = %s\n", chasen_result );*/
current_result = make_chaone_process( chasen_result );
fd_out->result = current_result;
return 0;
}
int chasen_read_line( CHASEN_FD fd, char *buf, int len )
{
char *ptr;
int i;
int c, prev_c;
if (fd == NULL || current_result == NULL) return EOF;
if (fd->result == NULL) {
fd->result = current_result;
}
prev_c = '\0';
ptr = fd->result;
for (i = 0; *ptr != '\0'; i++) {
c = *ptr;
if (
#ifdef USE_SPLIB
spIsMBTailCandidate(prev_c, c) == SP_FALSE &&
#endif
(c == '\n' || (i > 0 && c == '<'))) {
*buf = '\0';
if (c == '\n') {
++ptr;
}
if (*ptr == '\0') {
fd->result = NULL;
} else {
fd->result = ptr;
}
return 0;
}
*buf = c;
++buf; --len;
if( len <= 1 ) {
ErrMsg( "Too long line ...\n" );
*buf = '\0';
fd->result = ptr;
return( 0 );
}
prev_c = c;
++ptr;
}
*buf = '\0';
fd->result = NULL;
return EOF;
}