Annotation of OpenXM_contrib2/asir2000/io/bsave.c, Revision 1.5
1.2 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.3 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.2 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.
1.5 ! noro 47: * $OpenXM: OpenXM_contrib2/asir2000/io/bsave.c,v 1.4 2000/11/08 08:02:51 noro Exp $
1.2 noro 48: */
1.1 noro 49: /* saveXXX must not use GC_malloc(), GC_malloc_atomic(). */
50:
51: #include "ca.h"
52: #include "parse.h"
53: #include "com.h"
54:
55: #if PARI
56: #include "genpari.h"
57: int get_lg(GEN);
58: #endif
59:
60: void saveerror(FILE *,ERR);
61: void saveui(FILE *,USINT);
62: void savedp(FILE *,DP);
63: void savestr(FILE *,char *);
64: void savestring(FILE *,STRING);
65: void savemat(FILE *,MAT);
66: void savevect(FILE *,VECT);
67: void savelist(FILE *,LIST);
68: void saver(FILE *,R);
69: void savep(FILE *,P);
70: void savegf2n(FILE *,GF2N);
71: void savegfpn(FILE *,GFPN);
72: void savelm(FILE *,LM);
73: void savemi(FILE *,MQ);
74: void savecplx(FILE *,C);
75: void savebf(FILE *,BF);
76: void savereal(FILE *,Real);
77: void saveq(FILE *,Q);
78: void savenum(FILE *,Num);
79: void savepfins(FILE *,V);
80: void savegfmmat(FILE *,GFMMAT);
1.4 noro 81: void savebytearray(FILE *,BYTEARRAY);
1.1 noro 82:
83: void (*savef[])() = { 0, savenum, savep, saver, savelist, savevect,
1.4 noro 84: savemat, savestring, 0, savedp, saveui, saveerror,0,0,0,savegfmmat, savebytearray };
1.1 noro 85: void (*nsavef[])() = { saveq, savereal, 0, savebf, savecplx ,savemi, savelm, savegf2n, savegfpn};
86:
87: static short zeroval = 0;
88:
89: void saveobj(s,p)
90: FILE *s;
91: Obj p;
92: {
93: if ( !p )
94: write_short(s,&zeroval);
95: else if ( !savef[OID(p)] )
96: error("saveobj : not implemented");
97: else
98: (*savef[OID(p)])(s,p);
99: }
100:
101: void savenum(s,p)
102: FILE *s;
103: Num p;
104: {
105: if ( !nsavef[NID(p)] )
106: error("savenum : not implemented");
107: else {
108: write_short(s,&OID(p)); write_char(s,&NID(p));
109: write_char(s,&(p->pad));
110: (*nsavef[NID(p)])(s,p);
111: }
112: }
113:
114: void saveq(s,p)
115: FILE *s;
116: Q p;
117: {
118: int size[2];
119: int len = 2;
120:
121: size[0] = PL(NM(p)); size[1] = DN(p) ? PL(DN(p)) : 0;
122: write_intarray(s,size,len);
123: write_intarray(s,BD(NM(p)),size[0]);
124: if ( size[1] )
125: write_intarray(s,BD(DN(p)),size[1]);
126: }
127:
128: void savereal(s,p)
129: FILE *s;
130: Real p;
131: { write_double(s,&BDY(p)); }
132:
133: /*
134: * BDY(p) = | z[0] | z[1] | z[2] | ... | z[lg(z)-1] |
135: * -> | id(2) | nid(1) | sgn(1) | expo>>32 | expo&0xffffffff | len | ... |
136: */
137:
138: void savebf(s,p)
139: FILE *s;
140: BF p;
141: {
142: #if PARI
143: GEN z;
144: int sign;
145: unsigned long expo;
146: unsigned int len,t;
147:
148: z = (GEN)BDY(p);
149: sign = signe(z);
150: len = lg(z)-2;
151: expo = expo(z);
152:
153: write_int(s,&sign);
154:
155: #if defined(LONG_IS_32BIT)
156: write_int(s,(int *)&zeroval); /* expo>>32 is always 0 */
157: write_int(s,(int *)&expo);
158: write_int(s,&len);
159: write_intarray(s,(int *)&z[2],len);
160: #elif defined(LONG_IS_64BIT)
161: t = expo>>32; write_int(s,(int *)&t);
162: t = expo&0xffffffff; write_int(s,&t);
163: t = 2*len; write_int(s,&t);
164: write_longarray(s,&z[2],len);
165: #endif
166: #else
167: error("savebf : PARI is not combined");
168: #endif
169: }
170:
171: void savecplx(s,p)
172: FILE *s;
173: C p;
174: { saveobj(s,(Obj)p->r); saveobj(s,(Obj)p->i); }
175:
176: void savemi(s,p)
177: FILE *s;
178: MQ p;
179: { write_int(s,&CONT(p)); }
180:
181: void savelm(s,p)
182: FILE *s;
183: LM p;
184: {
185: int size;
186:
187: size = PL(BDY(p));
188: write_int(s,&size);
189: write_intarray(s,BD(BDY(p)),size);
190: }
191:
192: void savegf2n(s,p)
193: FILE *s;
194: GF2N p;
195: {
196: int len;
197:
198: len = p->body->w;
199: write_int(s,&len);
200: write_intarray(s,p->body->b,len);
201: }
202:
203: void savegfpn(s,p)
204: FILE *s;
205: GFPN p;
206: {
207: int d,i;
208:
209: d = p->body->d;
210: write_int(s,&d);
211: for ( i = 0; i <= d; i++ )
212: saveobj(s,(Obj)p->body->c[i]);
213: }
214:
215: void savep(s,p)
216: FILE *s;
217: P p;
218: {
219: DCP dc;
220: int n;
221: int vindex;
222:
223: if ( NUM(p) )
224: savenum(s,(Num)p);
225: else {
226: vindex = save_convv(VR(p));
227: for ( dc = DC(p), n = 0; dc; dc = NEXT(dc), n++ );
228: write_short(s,&OID(p));
229: write_int(s,&vindex);
230: if ( vindex < 0 )
231: savepfins(s,VR(p));
232: write_int(s,&n);
233: for ( dc = DC(p); dc; dc = NEXT(dc) ) {
234: saveobj(s,(Obj)DEG(dc)); saveobj(s,(Obj)COEF(dc));
235: }
236: }
237: }
238:
239: /* save a pure function (v->attr = V_PF) */
240: /* |name(str)|argc(int)|darray(intarray)|args| */
241:
242: void savepfins(s,v)
243: FILE *s;
244: V v;
245: {
246: PFINS ins;
247: PF pf;
248: int argc,i;
249: int *darray;
250:
251: ins = (PFINS)v->priv;
252: pf = ins->pf;
253: savestr(s,NAME(pf));
254: argc = pf->argc;
255: write_int(s,&argc);
256: darray = (int *)ALLOCA(argc*sizeof(int));
257: for ( i = 0; i < argc; i++ )
258: darray[i] = ins->ad[i].d;
259: write_intarray(s,darray,argc);
260: for ( i = 0; i < argc; i++ )
261: saveobj(s,ins->ad[i].arg);
262: }
263:
264: void saver(s,p)
265: FILE *s;
266: R p;
267: {
268: if ( !RAT(p) )
269: savep(s,(P)p);
270: else {
271: write_short(s,&OID(p)); write_short(s,&p->reduced);
272: savep(s,NM(p)); savep(s,DN(p));
273: }
274: }
275:
276: void savelist(s,p)
277: FILE *s;
278: LIST p;
279: {
280: int n;
281: NODE tn;
282:
283: for ( tn = BDY(p), n = 0; tn; tn = NEXT(tn), n++ );
284: write_short(s,&OID(p)); write_int(s,&n);
285: for ( tn = BDY(p); tn; tn = NEXT(tn) )
286: saveobj(s,(Obj)BDY(tn));
287: }
288:
289: void savevect(s,p)
290: FILE *s;
291: VECT p;
292: {
293: int i,len = 2;
294:
295: write_short(s,&OID(p)); write_int(s,&p->len);
296: for ( i = 0, len = p->len; i < len; i++ )
297: saveobj(s,(Obj)BDY(p)[i]);
298: }
299:
300: void savemat(s,p)
301: FILE *s;
302: MAT p;
303: {
304: int i,j,row,col;
305: int len = 3;
306:
307: write_short(s,&OID(p)); write_int(s,&p->row); write_int(s,&p->col);
308: for ( i = 0, row = p->row, col = p->col; i < row; i++ )
309: for ( j = 0; j < col; j++ )
310: saveobj(s,(Obj)BDY(p)[i][j]);
311: }
312:
313: void savestring(s,p)
314: FILE *s;
315: STRING p;
316: {
317: write_short(s,&OID(p)); savestr(s,BDY(p));
318: }
319:
320: void savestr(s,p)
321: FILE *s;
322: char *p;
323: {
324: int size;
325:
326: size = p ? strlen(p) : 0; write_int(s,&size);
327: if ( size )
328: write_string(s,p,size);
329: }
330:
331: void savedp(s,p)
332: FILE *s;
333: DP p;
334: {
335: int nv,n,i,sugar;
336: MP m,t;
337:
338: nv = p->nv; m = p->body; sugar = p->sugar;
339: for ( n = 0, t = m; t; t = NEXT(t), n++ );
340: write_short(s,&OID(p)); write_int(s,&nv); write_int(s,&sugar); write_int(s,&n);
341: for ( i = 0, t = m; i < n; i++, t = NEXT(t) ) {
342: saveobj(s,(Obj)t->c);
343: write_int(s,&t->dl->td); write_intarray(s,&(t->dl->d[0]),nv);
344: }
345: }
346:
347: void saveui(s,u)
348: FILE *s;
349: USINT u;
350: {
351: write_short(s,&OID(u)); write_int(s,&BDY(u));
352: }
353:
354: void saveerror(s,e)
355: FILE *s;
356: ERR e;
357: {
358: write_short(s,&OID(e)); saveobj(s,(Obj)BDY(e));
359: }
360:
361: void savegfmmat(s,p)
362: FILE *s;
363: GFMMAT p;
364: {
365: int i,j,row,col;
366:
367: write_short(s,&OID(p)); write_int(s,&p->row); write_int(s,&p->col);
368: for ( i = 0, row = p->row, col = p->col; i < row; i++ )
369: write_intarray(s,p->body[i],col);
1.4 noro 370: }
371:
372: void savebytearray(s,p)
373: FILE *s;
374: BYTEARRAY p;
375: {
376: write_short(s,&OID(p)); write_int(s,&p->len);
377: write_string(s,p->body,p->len);
1.1 noro 378: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>