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