OSDN Git Service

perl module & chmod +x tests/*.sh
[chasen-legacy/chasen.git] / perl / ChaSen.xs
1 /*
2 Copyright(c) 1998-2000 NOKUBI Takatsugu <knok@daionet.gr.jp>
3 Copyright(c) 1997 Nara Institute of Science and Technorogy.
4 All Rights Reserved.
5
6 $Id: ChaSen.xs,v 1.2 2008/07/22 13:00:45 masayu-a Exp $
7 */
8
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 #include "EXTERN.h"
13 #include "perl.h"
14 #include "XSUB.h"
15 #ifdef __cplusplus
16 }
17 #endif
18
19 /* for old version of perl (< 5.004_04?) */
20 #if !defined(PL_na) && defined(na)
21 #define PL_na na
22 #endif
23 #if !defined(PL_sv_undef) && defined(sv_undef)
24 #define PL_sv_undef sv_undef
25 #endif
26
27 extern int Cha_optind;
28 int chasen_getopt_argv(char **, FILE *);
29
30 /* FILE * could be set NULL */
31 /* return: 0 (1: wrong option) */
32
33 int chasen_fparse(FILE *, FILE *);
34 int chasen_sparse(char *, FILE *);
35 /* return: 0 (1: end) */
36 char *chasen_fparse_tostr(FILE *);
37 char *chasen_sparse_tostr(char *);
38 /* return: pointer to parsed string (NULL: end) */
39
40 #define INNER_BUFSIZE   8192
41 #define CHA_INPUT_SIZE      8192
42
43 static unsigned char *pos;
44
45 /*PROTOTYPES: DISABLE*/
46
47 MODULE = Text::ChaSen           PACKAGE = Text::ChaSen
48
49 int
50 getopt_argv(sv,...)
51         SV* sv
52
53         PREINIT:
54         char **cargs;
55         int i;
56
57         CODE:
58         cargs = (char **) malloc(sizeof(char *) * items + 1);
59         for (i = 0; i < items; i ++) {
60                 cargs[i] = SvPV(ST(i), PL_na);
61         }
62         cargs[items] = NULL;
63         RETVAL = chasen_getopt_argv(cargs, NULL);
64         free(cargs);
65
66         OUTPUT:
67         RETVAL
68
69 SV*
70 sparse_tostr(sv)
71         SV* sv
72
73         PREINIT:
74         char *r, *s;
75
76         CODE:
77         s = SvPV(sv, PL_na);
78         r = chasen_sparse_tostr(s);
79         RETVAL = newSVpv(r, 0);
80
81         OUTPUT:
82         RETVAL
83
84 SV*
85 fparse_tostr(sv)
86         SV* sv
87
88         PREINIT:
89         char *fname, *out;
90         FILE *fp;
91
92         CODE:
93         fname = SvPV(sv, PL_na);
94         fp = fopen(fname, "r");
95         if (fp == NULL) {
96                 RETVAL = &PL_sv_undef;
97         } else {
98                 RETVAL = newSVpv("", 0);
99                 while ((out = chasen_fparse_tostr(fp)) != NULL) {
100                         sv_catpv(RETVAL, out);
101                 }
102                 fclose(fp);
103         }
104
105         OUTPUT:
106         RETVAL
107
108 SV*
109 sparse_tostr_long(sv)
110         SV* sv
111
112         PREINIT:
113         char *r, *s;
114
115         CODE:
116         s = SvPV(sv, PL_na);
117         r = chasen_sparse_tostr(s);
118         RETVAL = newSVpv(r, 0);
119
120         OUTPUT:
121         RETVAL