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