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

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

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