-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyalibnkf.c
195 lines (166 loc) · 4.5 KB
/
yalibnkf.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
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
/*
yalibnkf
Based on Python Interface to NKF
Licensed under MIT (New BSD) License
2014-2015, snipsnipsnip <[email protected]>
*/
/*
Changes.
2009.6.2 Remove WISH_TRUE, use get_guessed_code() for nkf-2.0.9
by SATOH Fumiyasu (fumiyas @ osstech co jp)
2008.7.17 Change the type of strlen from long to int, by SATOH Fumiyasu.
2007.2.1 Add guess() function.
2007.1.13 Remove yalibnkf_parseopts(), by SATOH Fumiyasu.
*/
/** Python Interface to NKF
***************************************************************************
** Copyright (c) 2005 Matsumoto, Tadashi <[email protected]>
** All Rights Reserved.
**
** Everyone is permitted to do anything on this program
** including copying, modifying, improving,
** as long as you don't try to pretend that you wrote it.
** i.e., the above copyright notice has to appear in all copies.
** Binary distribution requires original version messages.
** You don't have to ask before copying, redistribution or publishing.
** THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE.
***************************************************************************/
#include <setjmp.h>
#include <stdlib.h>
#include <stdio.h>
#include "yalibnkf.h"
#undef getc
#define getc(f) yalibnkf_getc(f)
#undef putchar
#undef TRUE
#undef FALSE
#define putchar(c) yalibnkf_putchar(c)
static size_t yalibnkf_ibufsize, yalibnkf_obufsize;
static const char *yalibnkf_inbuf;
static char *yalibnkf_outbuf;
static size_t yalibnkf_icount,yalibnkf_ocount;
static char *yalibnkf_optr;
static jmp_buf env;
static int yalibnkf_guess_flag;
static size_t yalibnkf_writecount;
static int
yalibnkf_getc(FILE *f)
{
unsigned char c;
if (yalibnkf_icount >= yalibnkf_ibufsize) return EOF;
c = yalibnkf_inbuf[yalibnkf_icount];
yalibnkf_icount++;
return (int)c;
}
static void
yalibnkf_putchar(int c)
{
if (yalibnkf_guess_flag) {
return;
}
if (yalibnkf_ocount--){
*yalibnkf_optr++ = (unsigned char)c;
}else{
size_t size = yalibnkf_obufsize + yalibnkf_obufsize;
char *p = (char *)realloc(yalibnkf_outbuf, size + 1);
if (p == NULL){ longjmp(env, 1); }
yalibnkf_outbuf = p;
yalibnkf_optr = yalibnkf_outbuf + yalibnkf_obufsize;
yalibnkf_ocount = yalibnkf_obufsize;
yalibnkf_obufsize = size;
*yalibnkf_optr++ = (unsigned char)c;
yalibnkf_ocount--;
}
yalibnkf_writecount++;
}
#define PERL_XS 1
#define DEFAULT_CODE_JIS
#include "nkf/utf8tbl.c"
#include "nkf/nkf.c"
/**
* Split opts with space character and feed to nkf's option().
* We need to handle space-delimited option one by one since nkf's handling
* seems buggy.
*/
static int load_nkf_options(const char *opts)
{
size_t len = strlen(opts);
unsigned char *optchunk = (unsigned char *)malloc(len + 1);
size_t i = 0;
if (optchunk == NULL) {
return -1;
}
while (i < len) {
size_t skip = strcspn(&opts[i], " ");
if (skip > 0) {
memcpy(optchunk, &opts[i], skip);
optchunk[skip] = '\0';
if (options(optchunk) != 0) {
free(optchunk);
return -1;
}
}
i += skip + strspn(&opts[i + skip], " ");
}
free(optchunk);
return 0;
}
struct yalibnkf_str
yalibnkf_convert(const char *opts, const char *str, size_t strlen)
{
struct yalibnkf_str ret;
ret.len = 0;
ret.str = NULL;
if (yalibnkf_outbuf != NULL) {
return ret;
}
yalibnkf_ibufsize = strlen;
yalibnkf_obufsize = yalibnkf_ibufsize * 3 / 2 + 256;
yalibnkf_outbuf = (char *)malloc(yalibnkf_obufsize);
if (yalibnkf_outbuf == NULL){
return ret;
}
yalibnkf_outbuf[0] = '\0';
yalibnkf_ocount = yalibnkf_obufsize;
yalibnkf_optr = yalibnkf_outbuf;
yalibnkf_icount = 0;
yalibnkf_writecount = 0;
yalibnkf_inbuf = str;
yalibnkf_guess_flag = 0;
if (setjmp(env) == 0){
reinit();
if (load_nkf_options(opts) != 0 || kanji_convert(NULL) != 0) {
return ret;
}
}else{
free(yalibnkf_outbuf);
return ret;
}
ret.len = yalibnkf_writecount;
ret.str = yalibnkf_outbuf;
yalibnkf_outbuf = NULL;
return ret;
}
const char *
yalibnkf_guess(const char *str, size_t strlen)
{
yalibnkf_ibufsize = strlen;
yalibnkf_icount = 0;
yalibnkf_inbuf = str;
yalibnkf_guess_flag = 1;
reinit();
guess_f = 1;
kanji_convert(NULL);
return get_guessed_code();
}
void
yalibnkf_free(struct yalibnkf_str result)
{
free((void *)result.str);
}
const char *
yalibnkf_version(void)
{
return "yalibnkf 0.0.0 based on Network Kanji Filter Version "
NKF_VERSION " (" NKF_RELEASE_DATE ") \n" COPY_RIGHT;
}