Annotation of OpenXM_contrib2/asir2000/builtin/strobj.c, Revision 1.8
1.6 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.7 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.6 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.8 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/strobj.c,v 1.7 2000/08/22 05:04:00 noro Exp $
1.6 noro 49: */
1.1 noro 50: #include "ca.h"
51: #include "parse.h"
52: #include "ctype.h"
53: #if PARI
54: #include "genpari.h"
55: extern jmp_buf environnement;
56: #endif
1.5 noro 57: #include <string.h>
58:
1.1 noro 59: extern char *parse_strp;
60:
61: void Prtostr(), Pstrtov(), Peval_str();
1.3 noro 62: void Pstrtoascii(), Pasciitostr();
1.5 noro 63: void Pstr_len(), Pstr_chr(), Psub_str();
1.1 noro 64:
65: struct ftab str_tab[] = {
66: {"rtostr",Prtostr,1},
67: {"strtov",Pstrtov,1},
68: {"eval_str",Peval_str,1},
1.3 noro 69: {"strtoascii",Pstrtoascii,1},
70: {"asciitostr",Pasciitostr,1},
1.5 noro 71: {"str_len",Pstr_len,1},
72: {"str_chr",Pstr_chr,3},
73: {"sub_str",Psub_str,3},
1.1 noro 74: {0,0,0},
75: };
1.5 noro 76:
77: void Pstr_len(arg,rp)
78: NODE arg;
79: Q *rp;
80: {
81: STRING str;
82: int r;
83:
84: str = (STRING)ARG0(arg);
85: asir_assert(str,O_STR,"str_chr");
86: r = strlen(BDY(str));
87: STOQ(r,*rp);
88: }
89:
90: void Pstr_chr(arg,rp)
91: NODE arg;
92: Q *rp;
93: {
94: STRING str,terminator;
95: Q start;
96: char *p,*ind;
97: int chr,spos,r;
98:
99: str = (STRING)ARG0(arg);
100: start = (Q)ARG1(arg);
101: terminator = (STRING)ARG2(arg);
102: asir_assert(str,O_STR,"str_chr");
103: asir_assert(start,O_N,"str_chr");
104: asir_assert(terminator,O_STR,"str_chr");
105: p = BDY(str);
106: spos = QTOS(start);
107: chr = BDY(terminator)[0];
1.8 ! noro 108: if ( spos > (int)strlen(p) )
1.5 noro 109: r = -1;
110: else {
111: ind = strchr(p+spos,chr);
112: if ( ind )
113: r = ind-p;
114: else
115: r = -1;
116: }
117: STOQ(r,*rp);
118: }
119:
120: void Psub_str(arg,rp)
121: NODE arg;
122: STRING *rp;
123: {
124: STRING str;
125: Q head,tail;
126: char *p,*r;
127: int spos,epos,len;
128:
129: str = (STRING)ARG0(arg);
130: head = (Q)ARG1(arg);
131: tail = (Q)ARG2(arg);
132: asir_assert(str,O_STR,"sub_str");
133: asir_assert(head,O_N,"sub_str");
134: asir_assert(tail,O_N,"sub_str");
135: p = BDY(str);
136: spos = QTOS(head);
137: epos = QTOS(tail);
138: len = strlen(p);
139: if ( (spos >= len) || (epos < spos) ) {
140: *rp = 0; return;
141: }
142: if ( epos >= len )
143: epos = len-1;
144: len = epos-spos+1;
145: r = (char *)MALLOC(len+1);
146: strncpy(r,p+spos,len);
147: r[len] = 0;
148: MKSTR(*rp,r);
149: }
1.3 noro 150:
151: void Pstrtoascii(arg,rp)
152: NODE arg;
153: LIST *rp;
154: {
155: STRING str;
156: unsigned char *p;
157: int len,i;
158: NODE n,n1;
159: Q q;
160:
161: str = (STRING)ARG0(arg);
162: asir_assert(str,O_STR,"strtoascii");
163: p = BDY(str);
164: len = strlen(p);
165: for ( i = len-1, n = 0; i >= 0; i-- ) {
166: UTOQ((unsigned int)p[i],q);
167: MKNODE(n1,q,n);
168: n = n1;
169: }
170: MKLIST(*rp,n);
171: }
172:
173: void Pasciitostr(arg,rp)
174: NODE arg;
175: STRING *rp;
176: {
177: LIST list;
178: unsigned char *p;
179: int len,i,j;
180: NODE n;
181: Q q;
182:
183: list = (LIST)ARG0(arg);
184: asir_assert(list,O_LIST,"asciitostr");
185: n = BDY(list);
186: len = length(n);
187: p = MALLOC_ATOMIC(len+1);
188: for ( i = 0; i < len; i++, n = NEXT(n) ) {
189: q = (Q)BDY(n);
190: asir_assert(q,O_N,"asciitostr");
191: j = QTOS(q);
1.4 noro 192: if ( j >= 256 || j <= 0 )
1.3 noro 193: error("asciitostr : argument out of range");
194: p[i] = j;
195: }
196: p[i] = 0;
197: MKSTR(*rp,(char *)p);
198: }
1.1 noro 199:
200: void Peval_str(arg,rp)
201: NODE arg;
202: Obj *rp;
203: {
204: FNODE fnode;
205: char *cmd;
206: #if PARI
1.8 ! noro 207: void recover(int);
! 208:
1.1 noro 209: recover(0);
210: if ( setjmp(environnement) ) {
211: avma = top; recover(1);
212: resetenv("");
213: }
214: #endif
215: cmd = BDY((STRING)ARG0(arg));
216: exprparse(0,cmd,&fnode);
217: *rp = eval(fnode);
218: }
219:
220: void Prtostr(arg,rp)
221: NODE arg;
222: STRING *rp;
223: {
224: char *b;
225: int len;
226:
1.2 noro 227: len = estimate_length(CO,ARG0(arg));
1.1 noro 228: b = (char *)MALLOC(len+1);
229: soutput_init(b);
230: sprintexpr(CO,ARG0(arg));
231: MKSTR(*rp,b);
232: }
233:
234: void Pstrtov(arg,rp)
235: NODE arg;
236: P *rp;
237: {
1.8 ! noro 238: char *p;
1.1 noro 239:
240: p = BDY((STRING)ARG0(arg));
241: #if 0
242: if ( !islower(*p) )
243: *rp = 0;
244: else {
245: for ( t = p+1; t && (isalnum(*t) || *t == '_'); t++ );
246: if ( *t )
247: *rp = 0;
248: else
249: makevar(p,rp);
250: }
251: #else
252: makevar(p,rp);
253: #endif
254: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>