[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.9

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

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