[BACK]Return to bsave.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / io

Annotation of OpenXM_contrib2/asir2000/io/bsave.c, Revision 1.1

1.1     ! noro        1: /* $OpenXM: OpenXM/src/asir99/io/bsave.c,v 1.1.1.1 1999/11/10 08:12:30 noro Exp $ */
        !             2: /* saveXXX must not use GC_malloc(), GC_malloc_atomic(). */
        !             3:
        !             4: #include "ca.h"
        !             5: #include "parse.h"
        !             6: #if INET
        !             7: #include "com.h"
        !             8: #endif
        !             9:
        !            10: #if PARI
        !            11: #include "genpari.h"
        !            12: int get_lg(GEN);
        !            13: #endif
        !            14:
        !            15: void saveerror(FILE *,ERR);
        !            16: void saveui(FILE *,USINT);
        !            17: void savedp(FILE *,DP);
        !            18: void savestr(FILE *,char *);
        !            19: void savestring(FILE *,STRING);
        !            20: void savemat(FILE *,MAT);
        !            21: void savevect(FILE *,VECT);
        !            22: void savelist(FILE *,LIST);
        !            23: void saver(FILE *,R);
        !            24: void savep(FILE *,P);
        !            25: void savegf2n(FILE *,GF2N);
        !            26: void savegfpn(FILE *,GFPN);
        !            27: void savelm(FILE *,LM);
        !            28: void savemi(FILE *,MQ);
        !            29: void savecplx(FILE *,C);
        !            30: void savebf(FILE *,BF);
        !            31: void savereal(FILE *,Real);
        !            32: void saveq(FILE *,Q);
        !            33: void savenum(FILE *,Num);
        !            34: void savepfins(FILE *,V);
        !            35: void savegfmmat(FILE *,GFMMAT);
        !            36:
        !            37: #define O_GF2MAT 12
        !            38: #define O_MATHCAP 13
        !            39: #define O_F 14
        !            40: #define O_GFMMAT 15
        !            41:
        !            42: void (*savef[])() = { 0, savenum, savep, saver, savelist, savevect,
        !            43:        savemat, savestring, 0, savedp, saveui, saveerror,0,0,0,savegfmmat };
        !            44: void (*nsavef[])() = { saveq, savereal, 0, savebf, savecplx ,savemi, savelm, savegf2n, savegfpn};
        !            45:
        !            46: static short zeroval = 0;
        !            47:
        !            48: void saveobj(s,p)
        !            49: FILE *s;
        !            50: Obj p;
        !            51: {
        !            52:        if ( !p )
        !            53:                write_short(s,&zeroval);
        !            54:        else if ( !savef[OID(p)] )
        !            55:                error("saveobj : not implemented");
        !            56:        else
        !            57:                (*savef[OID(p)])(s,p);
        !            58: }
        !            59:
        !            60: void savenum(s,p)
        !            61: FILE *s;
        !            62: Num p;
        !            63: {
        !            64:        if ( !nsavef[NID(p)] )
        !            65:                error("savenum : not implemented");
        !            66:        else {
        !            67:                write_short(s,&OID(p)); write_char(s,&NID(p));
        !            68:                write_char(s,&(p->pad));
        !            69:                (*nsavef[NID(p)])(s,p);
        !            70:        }
        !            71: }
        !            72:
        !            73: void saveq(s,p)
        !            74: FILE *s;
        !            75: Q p;
        !            76: {
        !            77:        int size[2];
        !            78:        int len = 2;
        !            79:
        !            80:        size[0] = PL(NM(p)); size[1] = DN(p) ? PL(DN(p)) : 0;
        !            81:        write_intarray(s,size,len);
        !            82:        write_intarray(s,BD(NM(p)),size[0]);
        !            83:        if ( size[1] )
        !            84:                write_intarray(s,BD(DN(p)),size[1]);
        !            85: }
        !            86:
        !            87: void savereal(s,p)
        !            88: FILE *s;
        !            89: Real p;
        !            90: { write_double(s,&BDY(p)); }
        !            91:
        !            92: /*
        !            93:  * BDY(p) = |  z[0]  |   z[1]  | z[2] | ... | z[lg(z)-1] |
        !            94:  * -> | id(2) | nid(1) | sgn(1) | expo>>32 | expo&0xffffffff | len | ... |
        !            95:  */
        !            96:
        !            97: void savebf(s,p)
        !            98: FILE *s;
        !            99: BF p;
        !           100: {
        !           101: #if PARI
        !           102:        GEN z;
        !           103:        int sign;
        !           104:        unsigned long expo;
        !           105:        unsigned int len,t;
        !           106:
        !           107:        z = (GEN)BDY(p);
        !           108:        sign = signe(z);
        !           109:        len = lg(z)-2;
        !           110:        expo = expo(z);
        !           111:
        !           112:        write_int(s,&sign);
        !           113:
        !           114: #if defined(LONG_IS_32BIT)
        !           115:        write_int(s,(int *)&zeroval); /* expo>>32 is always 0 */
        !           116:        write_int(s,(int *)&expo);
        !           117:        write_int(s,&len);
        !           118:        write_intarray(s,(int *)&z[2],len);
        !           119: #elif defined(LONG_IS_64BIT)
        !           120:        t = expo>>32; write_int(s,(int *)&t);
        !           121:        t = expo&0xffffffff; write_int(s,&t);
        !           122:        t = 2*len; write_int(s,&t);
        !           123:        write_longarray(s,&z[2],len);
        !           124: #endif
        !           125: #else
        !           126:        error("savebf : PARI is not combined");
        !           127: #endif
        !           128: }
        !           129:
        !           130: void savecplx(s,p)
        !           131: FILE *s;
        !           132: C p;
        !           133: { saveobj(s,(Obj)p->r); saveobj(s,(Obj)p->i); }
        !           134:
        !           135: void savemi(s,p)
        !           136: FILE *s;
        !           137: MQ p;
        !           138: { write_int(s,&CONT(p)); }
        !           139:
        !           140: void savelm(s,p)
        !           141: FILE *s;
        !           142: LM p;
        !           143: {
        !           144:        int size;
        !           145:
        !           146:        size = PL(BDY(p));
        !           147:        write_int(s,&size);
        !           148:        write_intarray(s,BD(BDY(p)),size);
        !           149: }
        !           150:
        !           151: void savegf2n(s,p)
        !           152: FILE *s;
        !           153: GF2N p;
        !           154: {
        !           155:        int len;
        !           156:
        !           157:        len = p->body->w;
        !           158:        write_int(s,&len);
        !           159:        write_intarray(s,p->body->b,len);
        !           160: }
        !           161:
        !           162: void savegfpn(s,p)
        !           163: FILE *s;
        !           164: GFPN p;
        !           165: {
        !           166:        int d,i;
        !           167:
        !           168:        d = p->body->d;
        !           169:        write_int(s,&d);
        !           170:        for ( i = 0; i <= d; i++ )
        !           171:                saveobj(s,(Obj)p->body->c[i]);
        !           172: }
        !           173:
        !           174: void savep(s,p)
        !           175: FILE *s;
        !           176: P p;
        !           177: {
        !           178:        DCP dc;
        !           179:        int n;
        !           180:        int vindex;
        !           181:
        !           182:        if ( NUM(p) )
        !           183:                savenum(s,(Num)p);
        !           184:        else {
        !           185:                vindex = save_convv(VR(p));
        !           186:                for ( dc = DC(p), n = 0; dc; dc = NEXT(dc), n++ );
        !           187:                write_short(s,&OID(p));
        !           188:                write_int(s,&vindex);
        !           189:                if ( vindex < 0 )
        !           190:                        savepfins(s,VR(p));
        !           191:                write_int(s,&n);
        !           192:                for ( dc = DC(p); dc; dc = NEXT(dc) ) {
        !           193:                        saveobj(s,(Obj)DEG(dc)); saveobj(s,(Obj)COEF(dc));
        !           194:                }
        !           195:        }
        !           196: }
        !           197:
        !           198: /* save a pure function (v->attr = V_PF) */
        !           199: /* |name(str)|argc(int)|darray(intarray)|args| */
        !           200:
        !           201: void savepfins(s,v)
        !           202: FILE *s;
        !           203: V v;
        !           204: {
        !           205:        PFINS ins;
        !           206:        PF pf;
        !           207:        int argc,i;
        !           208:        int *darray;
        !           209:
        !           210:        ins = (PFINS)v->priv;
        !           211:        pf = ins->pf;
        !           212:        savestr(s,NAME(pf));
        !           213:        argc = pf->argc;
        !           214:        write_int(s,&argc);
        !           215:        darray = (int *)ALLOCA(argc*sizeof(int));
        !           216:        for ( i = 0; i < argc; i++ )
        !           217:                darray[i] = ins->ad[i].d;
        !           218:        write_intarray(s,darray,argc);
        !           219:        for ( i = 0; i < argc; i++ )
        !           220:                saveobj(s,ins->ad[i].arg);
        !           221: }
        !           222:
        !           223: void saver(s,p)
        !           224: FILE *s;
        !           225: R p;
        !           226: {
        !           227:        if ( !RAT(p) )
        !           228:                savep(s,(P)p);
        !           229:        else {
        !           230:                write_short(s,&OID(p)); write_short(s,&p->reduced);
        !           231:                savep(s,NM(p)); savep(s,DN(p));
        !           232:        }
        !           233: }
        !           234:
        !           235: void savelist(s,p)
        !           236: FILE *s;
        !           237: LIST p;
        !           238: {
        !           239:        int n;
        !           240:        NODE tn;
        !           241:
        !           242:        for ( tn = BDY(p), n = 0; tn; tn = NEXT(tn), n++ );
        !           243:        write_short(s,&OID(p)); write_int(s,&n);
        !           244:        for ( tn = BDY(p); tn; tn = NEXT(tn) )
        !           245:                saveobj(s,(Obj)BDY(tn));
        !           246: }
        !           247:
        !           248: void savevect(s,p)
        !           249: FILE *s;
        !           250: VECT p;
        !           251: {
        !           252:        int i,len = 2;
        !           253:
        !           254:        write_short(s,&OID(p)); write_int(s,&p->len);
        !           255:        for ( i = 0, len = p->len; i < len; i++ )
        !           256:                saveobj(s,(Obj)BDY(p)[i]);
        !           257: }
        !           258:
        !           259: void savemat(s,p)
        !           260: FILE *s;
        !           261: MAT p;
        !           262: {
        !           263:        int i,j,row,col;
        !           264:        int len = 3;
        !           265:
        !           266:        write_short(s,&OID(p)); write_int(s,&p->row); write_int(s,&p->col);
        !           267:        for ( i = 0, row = p->row, col = p->col; i < row; i++ )
        !           268:                for ( j = 0; j < col; j++ )
        !           269:                        saveobj(s,(Obj)BDY(p)[i][j]);
        !           270: }
        !           271:
        !           272: void savestring(s,p)
        !           273: FILE *s;
        !           274: STRING p;
        !           275: {
        !           276:        write_short(s,&OID(p)); savestr(s,BDY(p));
        !           277: }
        !           278:
        !           279: void savestr(s,p)
        !           280: FILE *s;
        !           281: char *p;
        !           282: {
        !           283:        int size;
        !           284:
        !           285:        size = p ? strlen(p) : 0; write_int(s,&size);
        !           286:        if ( size )
        !           287:                write_string(s,p,size);
        !           288: }
        !           289:
        !           290: void savedp(s,p)
        !           291: FILE *s;
        !           292: DP p;
        !           293: {
        !           294:        int nv,n,i,sugar;
        !           295:        MP m,t;
        !           296:
        !           297:        nv = p->nv; m = p->body; sugar = p->sugar;
        !           298:        for ( n = 0, t = m; t; t = NEXT(t), n++ );
        !           299:        write_short(s,&OID(p)); write_int(s,&nv); write_int(s,&sugar); write_int(s,&n);
        !           300:        for ( i = 0, t = m; i < n; i++, t = NEXT(t) ) {
        !           301:                saveobj(s,(Obj)t->c);
        !           302:                write_int(s,&t->dl->td); write_intarray(s,&(t->dl->d[0]),nv);
        !           303:        }
        !           304: }
        !           305:
        !           306: void saveui(s,u)
        !           307: FILE *s;
        !           308: USINT u;
        !           309: {
        !           310:        write_short(s,&OID(u)); write_int(s,&BDY(u));
        !           311: }
        !           312:
        !           313: void saveerror(s,e)
        !           314: FILE *s;
        !           315: ERR e;
        !           316: {
        !           317:        write_short(s,&OID(e)); saveobj(s,(Obj)BDY(e));
        !           318: }
        !           319:
        !           320: void savegfmmat(s,p)
        !           321: FILE *s;
        !           322: GFMMAT p;
        !           323: {
        !           324:        int i,j,row,col;
        !           325:
        !           326:        write_short(s,&OID(p)); write_int(s,&p->row); write_int(s,&p->col);
        !           327:        for ( i = 0, row = p->row, col = p->col; i < row; i++ )
        !           328:                write_intarray(s,p->body[i],col);
        !           329: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>