Annotation of OpenXM_contrib2/asir2000/io/bsave.c, Revision 1.3
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.3 ! noro 47: * $OpenXM: OpenXM_contrib2/asir2000/io/bsave.c,v 1.2 2000/08/21 08:31:38 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: #if INET
54: #include "com.h"
55: #endif
56:
57: #if PARI
58: #include "genpari.h"
59: int get_lg(GEN);
60: #endif
61:
62: void saveerror(FILE *,ERR);
63: void saveui(FILE *,USINT);
64: void savedp(FILE *,DP);
65: void savestr(FILE *,char *);
66: void savestring(FILE *,STRING);
67: void savemat(FILE *,MAT);
68: void savevect(FILE *,VECT);
69: void savelist(FILE *,LIST);
70: void saver(FILE *,R);
71: void savep(FILE *,P);
72: void savegf2n(FILE *,GF2N);
73: void savegfpn(FILE *,GFPN);
74: void savelm(FILE *,LM);
75: void savemi(FILE *,MQ);
76: void savecplx(FILE *,C);
77: void savebf(FILE *,BF);
78: void savereal(FILE *,Real);
79: void saveq(FILE *,Q);
80: void savenum(FILE *,Num);
81: void savepfins(FILE *,V);
82: void savegfmmat(FILE *,GFMMAT);
83:
84: #define O_GF2MAT 12
85: #define O_MATHCAP 13
86: #define O_F 14
87: #define O_GFMMAT 15
88:
89: void (*savef[])() = { 0, savenum, savep, saver, savelist, savevect,
90: savemat, savestring, 0, savedp, saveui, saveerror,0,0,0,savegfmmat };
91: void (*nsavef[])() = { saveq, savereal, 0, savebf, savecplx ,savemi, savelm, savegf2n, savegfpn};
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: }
176:
177: void savecplx(s,p)
178: FILE *s;
179: C p;
180: { saveobj(s,(Obj)p->r); saveobj(s,(Obj)p->i); }
181:
182: void savemi(s,p)
183: FILE *s;
184: MQ p;
185: { write_int(s,&CONT(p)); }
186:
187: void savelm(s,p)
188: FILE *s;
189: LM p;
190: {
191: int size;
192:
193: size = PL(BDY(p));
194: write_int(s,&size);
195: write_intarray(s,BD(BDY(p)),size);
196: }
197:
198: void savegf2n(s,p)
199: FILE *s;
200: GF2N p;
201: {
202: int len;
203:
204: len = p->body->w;
205: write_int(s,&len);
206: write_intarray(s,p->body->b,len);
207: }
208:
209: void savegfpn(s,p)
210: FILE *s;
211: GFPN p;
212: {
213: int d,i;
214:
215: d = p->body->d;
216: write_int(s,&d);
217: for ( i = 0; i <= d; i++ )
218: saveobj(s,(Obj)p->body->c[i]);
219: }
220:
221: void savep(s,p)
222: FILE *s;
223: P p;
224: {
225: DCP dc;
226: int n;
227: int vindex;
228:
229: if ( NUM(p) )
230: savenum(s,(Num)p);
231: else {
232: vindex = save_convv(VR(p));
233: for ( dc = DC(p), n = 0; dc; dc = NEXT(dc), n++ );
234: write_short(s,&OID(p));
235: write_int(s,&vindex);
236: if ( vindex < 0 )
237: savepfins(s,VR(p));
238: write_int(s,&n);
239: for ( dc = DC(p); dc; dc = NEXT(dc) ) {
240: saveobj(s,(Obj)DEG(dc)); saveobj(s,(Obj)COEF(dc));
241: }
242: }
243: }
244:
245: /* save a pure function (v->attr = V_PF) */
246: /* |name(str)|argc(int)|darray(intarray)|args| */
247:
248: void savepfins(s,v)
249: FILE *s;
250: V v;
251: {
252: PFINS ins;
253: PF pf;
254: int argc,i;
255: int *darray;
256:
257: ins = (PFINS)v->priv;
258: pf = ins->pf;
259: savestr(s,NAME(pf));
260: argc = pf->argc;
261: write_int(s,&argc);
262: darray = (int *)ALLOCA(argc*sizeof(int));
263: for ( i = 0; i < argc; i++ )
264: darray[i] = ins->ad[i].d;
265: write_intarray(s,darray,argc);
266: for ( i = 0; i < argc; i++ )
267: saveobj(s,ins->ad[i].arg);
268: }
269:
270: void saver(s,p)
271: FILE *s;
272: R p;
273: {
274: if ( !RAT(p) )
275: savep(s,(P)p);
276: else {
277: write_short(s,&OID(p)); write_short(s,&p->reduced);
278: savep(s,NM(p)); savep(s,DN(p));
279: }
280: }
281:
282: void savelist(s,p)
283: FILE *s;
284: LIST p;
285: {
286: int n;
287: NODE tn;
288:
289: for ( tn = BDY(p), n = 0; tn; tn = NEXT(tn), n++ );
290: write_short(s,&OID(p)); write_int(s,&n);
291: for ( tn = BDY(p); tn; tn = NEXT(tn) )
292: saveobj(s,(Obj)BDY(tn));
293: }
294:
295: void savevect(s,p)
296: FILE *s;
297: VECT p;
298: {
299: int i,len = 2;
300:
301: write_short(s,&OID(p)); write_int(s,&p->len);
302: for ( i = 0, len = p->len; i < len; i++ )
303: saveobj(s,(Obj)BDY(p)[i]);
304: }
305:
306: void savemat(s,p)
307: FILE *s;
308: MAT p;
309: {
310: int i,j,row,col;
311: int len = 3;
312:
313: write_short(s,&OID(p)); write_int(s,&p->row); write_int(s,&p->col);
314: for ( i = 0, row = p->row, col = p->col; i < row; i++ )
315: for ( j = 0; j < col; j++ )
316: saveobj(s,(Obj)BDY(p)[i][j]);
317: }
318:
319: void savestring(s,p)
320: FILE *s;
321: STRING p;
322: {
323: write_short(s,&OID(p)); savestr(s,BDY(p));
324: }
325:
326: void savestr(s,p)
327: FILE *s;
328: char *p;
329: {
330: int size;
331:
332: size = p ? strlen(p) : 0; write_int(s,&size);
333: if ( size )
334: write_string(s,p,size);
335: }
336:
337: void savedp(s,p)
338: FILE *s;
339: DP p;
340: {
341: int nv,n,i,sugar;
342: MP m,t;
343:
344: nv = p->nv; m = p->body; sugar = p->sugar;
345: for ( n = 0, t = m; t; t = NEXT(t), n++ );
346: write_short(s,&OID(p)); write_int(s,&nv); write_int(s,&sugar); write_int(s,&n);
347: for ( i = 0, t = m; i < n; i++, t = NEXT(t) ) {
348: saveobj(s,(Obj)t->c);
349: write_int(s,&t->dl->td); write_intarray(s,&(t->dl->d[0]),nv);
350: }
351: }
352:
353: void saveui(s,u)
354: FILE *s;
355: USINT u;
356: {
357: write_short(s,&OID(u)); write_int(s,&BDY(u));
358: }
359:
360: void saveerror(s,e)
361: FILE *s;
362: ERR e;
363: {
364: write_short(s,&OID(e)); saveobj(s,(Obj)BDY(e));
365: }
366:
367: void savegfmmat(s,p)
368: FILE *s;
369: GFMMAT p;
370: {
371: int i,j,row,col;
372:
373: write_short(s,&OID(p)); write_int(s,&p->row); write_int(s,&p->col);
374: for ( i = 0, row = p->row, col = p->col; i < row; i++ )
375: write_intarray(s,p->body[i],col);
376: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>