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

Annotation of OpenXM_contrib2/asir2000/io/cio.c, Revision 1.8

1.3       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.4       noro       26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.3       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.8     ! noro       47:  * $OpenXM: OpenXM_contrib2/asir2000/io/cio.c,v 1.7 2001/08/03 08:50:19 noro Exp $
1.3       noro       48: */
1.1       noro       49: #include "ca.h"
                     50: #include "parse.h"
                     51: #include "ox.h"
                     52:
                     53: #define ISIZ sizeof(int)
                     54:
                     55: int write_cmo_zz(FILE *,int,N);
                     56: int read_cmo_zz(FILE *,int *,N *);
                     57:
                     58: int valid_as_cmo(obj)
                     59: Obj obj;
                     60: {
                     61:        NODE m;
                     62:
                     63:        if ( !obj )
                     64:                return 1;
                     65:        switch ( OID(obj) ) {
                     66:                case O_MATHCAP: case O_P: case O_R: case O_DP: case O_STR:
1.6       noro       67:                case O_ERR: case O_USINT: case O_BYTEARRAY: case O_VOID:
1.1       noro       68:                        return 1;
                     69:                case O_N:
1.2       noro       70:                        if ( NID((Num)obj) == N_Q || NID((Num)obj) == N_R )
1.1       noro       71:                                return 1;
                     72:                        else
                     73:                                return 0;
                     74:                case O_LIST:
                     75:                        for ( m = BDY((LIST)obj); m; m = NEXT(m) )
                     76:                                if ( !valid_as_cmo(BDY(m)) )
                     77:                                        return 0;
                     78:                        return 1;
1.7       noro       79:                case O_QUOTE:
                     80:                        return 1;
1.1       noro       81:                default:
                     82:                        return 0;
                     83:        }
                     84: }
                     85:
                     86: write_cmo(s,obj)
                     87: FILE *s;
                     88: Obj obj;
                     89: {
                     90:        int r;
                     91:        char errmsg[BUFSIZ];
                     92:
                     93:        if ( !obj ) {
                     94:                r = CMO_NULL; write_int(s,&r);
                     95:                return;
                     96:        }
                     97:        switch ( OID(obj) ) {
                     98:                case O_N:
1.2       noro       99:                        switch ( NID((Num)obj) ) {
                    100:                                case N_Q:
                    101:                                        write_cmo_q(s,(Q)obj);
                    102:                                        break;
                    103:                                case N_R:
                    104:                                        write_cmo_real(s,(Real)obj);
                    105:                                        break;
                    106:                                default:
                    107:                                        sprintf(errmsg, "write_cmo : number id=%d not implemented.",
                    108:                                                NID((Num)obj));
                    109:                                        error(errmsg);
                    110:                                        break;
                    111:                        }
1.1       noro      112:                        break;
                    113:                case O_P:
                    114:                        write_cmo_p(s,obj);
                    115:                        break;
                    116:                case O_R:
                    117:                        write_cmo_r(s,obj);
                    118:                        break;
                    119:                case O_DP:
                    120:                        write_cmo_dp(s,obj);
                    121:                        break;
                    122:                case O_LIST:
                    123:                        write_cmo_list(s,obj);
                    124:                        break;
                    125:                case O_STR:
                    126:                        write_cmo_string(s,obj);
                    127:                        break;
                    128:                case O_USINT:
                    129:                        write_cmo_uint(s,obj);
                    130:                        break;
                    131:                case O_MATHCAP:
                    132:                        write_cmo_mathcap(s,obj);
                    133:                        break;
                    134:                case O_ERR:
                    135:                        write_cmo_error(s,obj);
                    136:                        break;
1.6       noro      137:                case O_BYTEARRAY:
                    138:                        write_cmo_bytearray(s,obj);
                    139:                        break;
1.1       noro      140:                case O_VOID:
                    141:                        r = ((USINT)obj)->body; write_int(s,&r);
                    142:                        break;
1.7       noro      143:                case O_QUOTE:
                    144:                        write_cmo_tree(s,(FNODE)(((QUOTE)obj)->body));
                    145:                        break;
1.1       noro      146:                default:
                    147:                        sprintf(errmsg, "write_cmo : id=%d not implemented.",OID(obj));
                    148:                        error(errmsg);
                    149:                        break;
                    150:        }
1.5       noro      151: }
                    152:
                    153: int cmo_tag(obj,tag)
                    154: Obj obj;
                    155: int *tag;
                    156: {
                    157:        if ( !valid_as_cmo(obj) )
                    158:                return 0;
                    159:        if ( !obj ) {
                    160:                *tag = CMO_NULL;
                    161:                return 1;
                    162:        }
                    163:        switch ( OID(obj) ) {
                    164:                case O_N:
                    165:                        switch ( NID((Num)obj) ) {
                    166:                                case N_Q:
                    167:                                        *tag = DN((Q)obj) ? CMO_QQ : CMO_ZZ; break;
                    168:                                case N_R:
                    169:                                        *tag = CMO_IEEE_DOUBLE_FLOAT; break;
                    170:                                default:
                    171:                                        return 0;
                    172:                        }
                    173:                        break;
                    174:                case O_P:
                    175:                        *tag = CMO_RECURSIVE_POLYNOMIAL; break;
                    176:                case O_R:
                    177:                        *tag = CMO_RATIONAL; break;
                    178:                case O_DP:
                    179:                        *tag = CMO_DISTRIBUTED_POLYNOMIAL; break;
                    180:                case O_LIST:
                    181:                        *tag = CMO_LIST; break;
                    182:                case O_STR:
                    183:                        *tag = CMO_STRING; break;
                    184:                case O_USINT:
                    185:                        *tag = CMO_INT32; break;
                    186:                case O_MATHCAP:
                    187:                        *tag = CMO_MATHCAP; break;
                    188:                case O_ERR:
                    189:                        *tag = CMO_ERROR2; break;
1.7       noro      190:                case O_QUOTE:
                    191:                        *tag = CMO_TREE; break; break;
1.5       noro      192:                default:
                    193:                        return 0;
                    194:        }
                    195:        return 1;
1.1       noro      196: }
                    197:
                    198: write_cmo_mathcap(s,mc)
                    199: FILE *s;
                    200: MATHCAP mc;
                    201: {
                    202:        unsigned int r;
                    203:
                    204:        r = CMO_MATHCAP; write_int(s,&r);
                    205:        write_cmo(s,BDY(mc));
                    206: }
                    207:
                    208: write_cmo_uint(s,ui)
                    209: FILE *s;
                    210: USINT ui;
                    211: {
                    212:        unsigned int r;
                    213:
                    214:        r = CMO_INT32; write_int(s,&r);
                    215:        r = ui->body; write_int(s,&r);
                    216: }
                    217:
                    218: write_cmo_q(s,q)
                    219: FILE *s;
                    220: Q q;
                    221: {
                    222:        int r;
                    223:
                    224:        if ( q && DN(q) ) {
                    225:                r = CMO_QQ; write_int(s,&r);
                    226:                write_cmo_zz(s,SGN(q),NM(q));
                    227:                write_cmo_zz(s,1,DN(q));
                    228:        } else {
                    229:                r = CMO_ZZ; write_int(s,&r);
                    230:                write_cmo_zz(s,SGN(q),NM(q));
                    231:        }
                    232: }
                    233:
1.2       noro      234: write_cmo_real(s,real)
                    235: FILE *s;
                    236: Real real;
                    237: {
                    238:        unsigned int r;
                    239:        double dbl;
                    240:
                    241:        r = CMO_IEEE_DOUBLE_FLOAT; write_int(s,&r);
                    242:        dbl = real->body; write_double(s,&dbl);
                    243: }
                    244:
1.1       noro      245: write_cmo_zz(s,sgn,n)
                    246: FILE *s;
                    247: int sgn;
                    248: N n;
                    249: {
                    250:        int i,l,bytes;
                    251:        unsigned int t;
                    252:        unsigned int *b;
                    253:        unsigned char c;
                    254:
                    255: #if 1
                    256:        l = PL(n);
                    257:        bytes = sgn*l;
                    258:        write_int(s,&bytes);
                    259:        write_intarray(s,BD(n),l);
                    260: #else
                    261:        l = PL(n); b = (unsigned int *)BD(n);
                    262:        bytes = sgn*4*l;
                    263:        write_int(s,&bytes);
                    264:        for ( i = l-1; i >= 0; i-- ) {
                    265:                t = b[i];
                    266:                c = t>>24; write_char(s,&c);
                    267:                c = (t>>16)&0xff; write_char(s,&c);
                    268:                c = (t>>8)&0xff; write_char(s,&c);
                    269:                c = t&0xff; write_char(s,&c);
                    270:        }
                    271: #endif
                    272: }
                    273:
                    274: write_cmo_p(s,p)
                    275: FILE *s;
                    276: P p;
                    277: {
                    278:        int r,i;
                    279:        VL t,vl;
                    280:        char *namestr;
                    281:        STRING name;
                    282:        NODE n0,n;
                    283:
                    284:        r = CMO_RECURSIVE_POLYNOMIAL; write_int(s,&r);
                    285:        get_vars((Obj)p,&vl);
                    286:
                    287:        /* indeterminate list */
                    288:        r = CMO_LIST; write_int(s,&r);
                    289:        for ( t = vl, i = 0; t; t = NEXT(t), i++ );
                    290:        write_int(s,&i);
                    291:        r = CMO_INDETERMINATE;
                    292:        for ( t = vl; t; t = NEXT(t) ) {
                    293:                write_int(s,&r);
                    294: /*             localname_to_cmoname(NAME(t->v),&namestr); */
                    295:                namestr = NAME(t->v);
                    296:                MKSTR(name,namestr);
                    297:                write_cmo(s,name);
                    298:        }
                    299:
                    300:        /* body */
                    301:        write_cmo_upoly(s,vl,p);
                    302: }
                    303:
                    304: write_cmo_upoly(s,vl,p)
                    305: FILE *s;
                    306: VL vl;
                    307: P p;
                    308: {
                    309:        int r,i;
                    310:        V v;
                    311:        DCP dc,dct;
                    312:        VL vlt;
                    313:
                    314:        if ( NUM(p) )
                    315:                write_cmo(s,p);
                    316:        else {
                    317:                r = CMO_UNIVARIATE_POLYNOMIAL; write_int(s,&r);
                    318:                v = VR(p);
                    319:                dc = DC(p);
                    320:                for ( i = 0, dct = dc; dct; dct = NEXT(dct), i++ );
                    321:                write_int(s,&i);
                    322:                for ( i = 0, vlt = vl; vlt->v != v; vlt = NEXT(vlt), i++ );
                    323:                write_int(s,&i);
                    324:                for ( dct = dc; dct; dct = NEXT(dct) ) {
                    325:                        i = QTOS(DEG(dct)); write_int(s,&i);
                    326:                        write_cmo_upoly(s,vl,COEF(dct));
                    327:                }
                    328:        }
                    329: }
                    330:
                    331: write_cmo_r(s,f)
                    332: FILE *s;
                    333: R f;
                    334: {
                    335:        int r;
                    336:
                    337:        r = CMO_RATIONAL; write_int(s,&r);
                    338:        write_cmo(s,NM(f));
                    339:        write_cmo(s,DN(f));
                    340: }
                    341:
                    342: write_cmo_dp(s,dp)
                    343: FILE *s;
                    344: DP dp;
                    345: {
                    346:        int i,n,nv,r;
                    347:        MP m;
                    348:
                    349:        for ( n = 0, m = BDY(dp); m; m = NEXT(m), n++ );
                    350:        r = CMO_DISTRIBUTED_POLYNOMIAL; write_int(s,&r);
                    351:        r = n; write_int(s,&r);
                    352:        r = CMO_DMS_GENERIC; write_int(s,&r);
                    353:        nv = dp->nv;
                    354:        for ( i = 0, m = BDY(dp); i < n; i++, m = NEXT(m) )
                    355:                write_cmo_monomial(s,m,nv);
                    356: }
                    357:
                    358: write_cmo_monomial(s,m,n)
                    359: FILE *s;
                    360: MP m;
                    361: int n;
                    362: {
                    363:        int i,r;
                    364:        int *p;
                    365:
                    366:        r = CMO_MONOMIAL32; write_int(s,&r);
                    367:        write_int(s,&n);
                    368:        for ( i = 0, p = m->dl->d; i < n; i++ ) {
                    369:                write_int(s,p++);
                    370:        }
                    371:        write_cmo_q(s,m->c);
                    372: }
                    373:
                    374: write_cmo_list(s,list)
                    375: FILE *s;
                    376: LIST list;
                    377: {
                    378:        NODE m;
                    379:        int i,n,r;
                    380:
                    381:        for ( n = 0, m = BDY(list); m; m = NEXT(m), n++ );
                    382:        r = CMO_LIST; write_int(s,&r);
                    383:        write_int(s,&n);
                    384:        for ( i = 0, m = BDY(list); i < n; i++, m = NEXT(m) )
                    385:                write_cmo(s,BDY(m));
                    386: }
                    387:
                    388: write_cmo_string(s,str)
                    389: FILE *s;
                    390: STRING str;
                    391: {
                    392:        int r;
                    393:
                    394:        r = CMO_STRING; write_int(s,&r);
                    395:        savestr(s,BDY(str));
                    396: }
                    397:
1.6       noro      398: write_cmo_bytearray(s,array)
                    399: FILE *s;
                    400: BYTEARRAY array;
                    401: {
                    402:        int r;
                    403:
                    404:        r = CMO_DATUM; write_int(s,&r);
                    405:        write_int(s,&array->len);
                    406:        write_string(s,array->body,array->len);
                    407: }
                    408:
1.1       noro      409: write_cmo_error(s,e)
                    410: FILE *s;
                    411: ERR e;
                    412: {
                    413:        int r;
                    414:
                    415:        r = CMO_ERROR2; write_int(s,&r);
                    416:        write_cmo(s,BDY(e));
                    417: }
                    418:
1.7       noro      419: /* XXX */
                    420:
                    421: write_cmo_tree(s,f)
                    422: FILE *s;
                    423: FNODE f;
                    424: {
                    425:        int r,i,n;
                    426:        char opname[BUFSIZ];
                    427:        NODE args,t;
                    428:        FUNC fspec;
                    429:        struct oSTRING str,nullstr;
                    430:
1.8     ! noro      431:        /* zero */
        !           432:        if ( !f ) {
        !           433:                r = CMO_NULL; write_int(s,&r);
        !           434:                return;
        !           435:        }
        !           436:
1.7       noro      437:        /* null string */
                    438:        nullstr.id = O_STR;
                    439:        nullstr.body = 0;
                    440:
                    441:        /* name string */
                    442:        str.id = O_STR;
                    443:        str.body = opname;
                    444:
                    445:        switch ( f->id ) {
                    446:                case I_BOP: case I_COP:
                    447:                case I_AND: case I_OR:
                    448:                        /*
                    449:                         * (CMO_TREE (CMO_STRING,op),(CMO_STRING,0)(CMO_LIST,2,arg1,arg2))
                    450:                         */
                    451:                        switch ( f->id ) {
                    452:                                case I_BOP:
                    453:                                        strcpy(opname,((ARF)FA0(f))->name); break;
                    454:                                case I_COP:
                    455:                                        switch( (cid)FA0(f) ) {
                    456:                                                case C_EQ: strcpy(opname,"=="); break;
                    457:                                                case C_NE: strcpy(opname,"!="); break;
                    458:                                                case C_GT: strcpy(opname,">"); break;
                    459:                                                case C_LT: strcpy(opname,"<"); break;
                    460:                                                case C_GE: strcpy(opname,">="); break;
                    461:                                                case C_LE: strcpy(opname,"<="); break;
                    462:                                        }
                    463:                                        break;
                    464:                                case I_AND:
                    465:                                        strcpy(opname,"&&"); break;
                    466:                                case I_OR:
                    467:                                        strcpy(opname,"||"); break;
                    468:                        }
                    469:                        r = CMO_TREE; write_int(s,&r);
                    470:                        write_cmo_string(s,&str);
                    471:                        write_cmo_string(s,&nullstr);
                    472:                        r = CMO_LIST; write_int(s,&r);
                    473:                        n = 2; write_int(s,&n);
                    474:                        write_cmo_tree(s,(FNODE)FA1(f));
                    475:                        write_cmo_tree(s,(FNODE)FA2(f));
                    476:                        break;
                    477:                case I_NOT:
                    478:                        r = CMO_TREE; write_int(s,&r);
                    479:                        strcpy(opname,"!");
                    480:                        write_cmo_string(s,&str);;
                    481:                        write_cmo_string(s,&nullstr);
                    482:                        r = CMO_LIST; write_int(s,&r);
                    483:                        n = 1; write_int(s,&n);
                    484:                        write_cmo_tree(s,(FNODE)FA1(f));
                    485:                        break;
                    486:                case I_FUNC:
1.8     ! noro      487:                        r = CMO_TREE; write_int(s,&r);
1.7       noro      488:                        fspec = (FUNC)FA0(f);
                    489:                        strcpy(opname,fspec->name);
                    490:                        write_cmo_string(s,&str);
                    491:                        write_cmo_string(s,&nullstr);
                    492:                        args = (NODE)FA0((FNODE)FA1(f));
                    493:                        r = CMO_LIST; write_int(s,&r);
                    494:                        for ( n = 0, t = args; t; t = NEXT(t), n++ );
                    495:                        write_int(s,&n);
                    496:                        for ( i = 0, t = args; i < n; t = NEXT(t), i++ )
                    497:                                write_cmo_tree(s,BDY(t));
                    498:                        break;
                    499:                case I_FORMULA:
                    500:                        write_cmo(s,FA0(f));
                    501:                        break;
                    502:                default:
                    503:                        break; /* XXX */
                    504:        }
                    505: }
                    506:
1.1       noro      507: read_cmo(s,rp)
                    508: FILE *s;
                    509: Obj *rp;
                    510: {
                    511:        int id;
                    512:        int n,sgn,dummy;
                    513:        Q q,qnm,qdn;
                    514:        N nm,dn;
                    515:        P p,pnm,pdn;
                    516:        R r;
1.2       noro      517:        Real real;
                    518:        double dbl;
1.1       noro      519:        STRING str;
                    520:        USINT t;
                    521:        DP dp;
                    522:        char *b;
                    523:        Obj obj;
                    524:        ERR e;
                    525:        MATHCAP mc;
1.6       noro      526:        BYTEARRAY array;
1.8     ! noro      527:        QUOTE quote;
        !           528:        FNODE fn;
1.1       noro      529:
                    530:        read_int(s,&id);
                    531:        switch ( id ) {
                    532:        /* level 0 objects */
                    533:                case CMO_NULL:
                    534:                        *rp = 0;
                    535:                        break;
                    536:                case CMO_INT32:
                    537:                        read_cmo_uint(s,rp);
                    538:                        break;
                    539:                case CMO_DATUM:
1.6       noro      540:                        loadbytearray(s,&array); *rp = (Obj)array;
                    541:                        break;
1.1       noro      542:                case CMO_STRING:
                    543:                        loadstring(s,&str); *rp = (Obj)str;
                    544:                        break;
                    545:                case CMO_MATHCAP:
                    546:                        read_cmo(s,&obj); MKMATHCAP(mc,(LIST)obj);
                    547:                        *rp = (Obj)mc;
                    548:                        break;
                    549:                case CMO_ERROR:
                    550:                        MKERR(e,0); *rp = (Obj)e;
                    551:                        break;
                    552:                case CMO_ERROR2:
                    553:                        read_cmo(s,&obj); MKERR(e,obj); *rp = (Obj)e;
                    554:                        break;
                    555:        /* level 1 objects */
                    556:                case CMO_LIST:
                    557:                        read_cmo_list(s,rp);
                    558:                        break;
                    559:                case CMO_MONOMIAL32:
                    560:                        read_cmo_monomial(s,rp);
                    561:                        break;
                    562:                case CMO_ZZ:
                    563:                        read_cmo_zz(s,&sgn,&nm);
                    564:                        NTOQ(nm,sgn,q); *rp = (Obj)q;
                    565:                        break;
                    566:                case CMO_QQ:
                    567:                        read_cmo_zz(s,&sgn,&nm);
                    568:                        read_cmo_zz(s,&dummy,&dn);
                    569:                        NDTOQ(nm,dn,sgn,q); *rp = (Obj)q;
1.2       noro      570:                        break;
                    571:                case CMO_IEEE_DOUBLE_FLOAT:
                    572:                        read_double(s,&dbl); MKReal(dbl,real); *rp = (Obj)real;
1.1       noro      573:                        break;
                    574:                case CMO_DISTRIBUTED_POLYNOMIAL:
                    575:                        read_cmo_dp(s,&dp); *rp = (Obj)dp;
                    576:                        break;
                    577:                case CMO_RECURSIVE_POLYNOMIAL:
                    578:                        read_cmo_p(s,&p); *rp = (Obj)p;
                    579:                        break;
                    580:                case CMO_UNIVARIATE_POLYNOMIAL:
                    581:                        read_cmo_upoly(s,&p); *rp = (Obj)p;
                    582:                        break;
                    583:                case CMO_INDETERMINATE:
                    584:                        read_cmo(s,&str); *rp = (Obj)str;
                    585:                        break;
                    586:                case CMO_RATIONAL:
                    587:                        read_cmo(s,&pnm); read_cmo(s,&pdn);
                    588:                        divr(CO,(Obj)pnm,(Obj)pdn,rp);
                    589:                        break;
                    590:                case CMO_ZERO:
                    591:                        *rp = 0;
                    592:                        break;
                    593:                case CMO_DMS_OF_N_VARIABLES:
                    594:                        read_cmo(s,rp);
                    595:                        break;
                    596:                case CMO_RING_BY_NAME:
                    597:                        read_cmo(s,rp);
                    598:                        break;
1.7       noro      599:                case CMO_TREE:
1.8     ! noro      600:                        read_cmo_tree(s,&fn);
        !           601:                        MKQUOTE(quote,fn);
        !           602:                        *rp = (Obj)quote;
1.7       noro      603:                        break;
1.1       noro      604:                default:
                    605:                        MKUSINT(t,id);
                    606:                        t->id = O_VOID;
                    607:                        *rp = (Obj)t;
                    608:                        break;
                    609:        }
                    610: }
                    611:
                    612: read_cmo_uint(s,rp)
                    613: FILE *s;
                    614: USINT *rp;
                    615: {
                    616:        unsigned int body;
                    617:
                    618:        read_int(s,&body);
                    619:        MKUSINT(*rp,body);
                    620: }
                    621:
                    622: read_cmo_zz(s,sgn,rp)
                    623: FILE *s;
                    624: int *sgn;
                    625: N *rp;
                    626: {
                    627:        int l,i,words;
                    628:        N n;
                    629:        unsigned int *b;
                    630:        unsigned int h;
                    631:        unsigned char c;
                    632:
                    633:        read_int(s,&l);
                    634:        if ( l == 0 ) {
                    635:                *sgn = 0;
                    636:                *rp = 0;
                    637:                return;
                    638:        }
                    639:        if ( l < 0 ) {
                    640:                *sgn = -1; l = -l;
                    641:        } else
                    642:                *sgn = 1;
                    643: #if 1
                    644:        *rp = n = NALLOC(l); PL(n) = l;
                    645:        read_intarray(s,BD(n),l);
                    646: #else
                    647:        words = (l+3)/4;
                    648:        *rp = n = NALLOC(words); PL(n) = words; b = BD(n);
                    649:        h = 0;
                    650:        switch ( l % 4 ) {
                    651:                case 0:
                    652:                        read_char(s,&c); h = c;
                    653:                case 3:
                    654:                        read_char(s,&c); h = (h<<8)|c;
                    655:                case 2:
                    656:                        read_char(s,&c); h = (h<<8)|c;
                    657:                case 1:
                    658:                        read_char(s,&c); h = (h<<8)|c;
                    659:        }
                    660:        b[words-1] = h;
                    661:        for ( i = words-2; i >= 0; i-- ) {
                    662:                read_char(s,&c); h = c;
                    663:                read_char(s,&c); h = (h<<8)|c;
                    664:                read_char(s,&c); h = (h<<8)|c;
                    665:                read_char(s,&c); h = (h<<8)|c;
                    666:                b[i] = h;
                    667:        }
                    668: #endif
                    669: }
                    670:
                    671: read_cmo_list(s,rp)
                    672: FILE *s;
                    673: Obj *rp;
                    674: {
                    675:        int len;
                    676:        Obj *w;
                    677:        int i;
                    678:        Obj r,r1;
                    679:        NODE n0,n1;
                    680:        LIST list;
                    681:
                    682:        read_int(s,&len);
                    683:        w = (Obj *)ALLOCA(len*sizeof(Obj));
                    684:        for ( i = 0; i < len; i++ )
                    685:                read_cmo(s,&w[i]);
                    686:        for ( i = len-1, n0 = 0; i >= 0; i-- ) {
                    687:                MKNODE(n1,w[i],n0); n0 = n1;
                    688:        }
                    689:        MKLIST(list,n0);
                    690:        *rp = (Obj)list;
                    691: }
                    692:
                    693: read_cmo_dp(s,rp)
                    694: FILE *s;
                    695: DP *rp;
                    696: {
                    697:        int len;
                    698:        int i;
                    699:        NODE n0,n1;
                    700:        MP mp0,mp;
                    701:        int nv,d;
                    702:        DP dp;
                    703:        Obj obj;
                    704:
                    705:        read_int(s,&len);
                    706:        /* skip the ring definition */
                    707:        read_cmo(s,&obj);
                    708:        for ( mp0 = 0, i = 0, d = 0; i < len; i++ ) {
                    709:                read_cmo(s,&dp);
                    710:                if ( !mp0 ) {
                    711:                        nv = dp->nv;
                    712:                        mp0 = dp->body;
                    713:                        mp = mp0;
                    714:                } else {
                    715:                        NEXT(mp) = dp->body;
                    716:                        mp = NEXT(mp);
                    717:                }
                    718:                d = MAX(d,dp->sugar);
                    719:        }
                    720:        MKDP(nv,mp0,dp);
                    721:        dp->sugar = d; *rp = dp;
                    722: }
                    723:
                    724: read_cmo_monomial(s,rp)
                    725: FILE *s;
                    726: DP *rp;
                    727: {
                    728:        MP m;
                    729:        DP dp;
                    730:        int i,sugar,n;
                    731:        DL dl;
                    732:
                    733:        read_int(s,&n);
                    734:        NEWMP(m); NEWDL(dl,n); m->dl = dl;
                    735:        read_intarray(s,dl->d,n);
                    736:        for ( sugar = 0, i = 0; i < n; i++ )
                    737:                sugar += dl->d[i];
                    738:        dl->td = sugar;
                    739:        read_cmo(s,&m->c);
                    740:        NEXT(m) = 0; MKDP(n,m,dp); dp->sugar = sugar; *rp = dp;
                    741: }
                    742:
                    743: static V *remote_vtab;
                    744:
                    745: read_cmo_p(s,rp)
                    746: FILE *s;
                    747: P *rp;
                    748: {
                    749:        LIST vlist;
                    750:        int nv,i;
                    751:        V *vtab;
                    752:        V v1,v2;
                    753:        NODE t;
                    754:        P v,p;
                    755:        VL tvl,rvl;
                    756:        char *name;
                    757:
                    758:        read_cmo(s,&vlist);
                    759:        nv = length(BDY(vlist));
                    760:        vtab = (V *)ALLOCA(nv*sizeof(V));
                    761:        for ( i = 0, t = BDY(vlist); i < nv; t = NEXT(t), i++ ) {
                    762: /*             cmoname_to_localname(BDY((STRING)BDY(t)),&name); */
                    763:                name = BDY((STRING)BDY(t));
                    764:                makevar(name,&v); vtab[i] = VR(v);
                    765:        }
                    766:        remote_vtab = vtab;
                    767:        read_cmo(s,&p);
                    768:        for ( i = 0; i < nv-1; i++ ) {
                    769:                v1 = vtab[i]; v2 = vtab[i+1];
                    770:                for ( tvl = CO; tvl->v != v1 && tvl->v != v2; tvl = NEXT(tvl) );
                    771:                if ( tvl->v == v2 )
                    772:                        break;
                    773:        }
                    774:        if ( i < nv-1 ) {
                    775:                 for ( i = nv-1, rvl = 0; i >= 0; i-- ) {
                    776:                        NEWVL(tvl); tvl->v = vtab[i]; NEXT(tvl) = rvl; rvl = tvl;
                    777:                 }
                    778:                 reorderp(CO,rvl,p,rp);
                    779:        } else
                    780:                *rp = p;
                    781: }
                    782:
                    783: read_cmo_upoly(s,rp)
                    784: FILE *s;
                    785: P *rp;
                    786: {
                    787:        int n,ind,i,d;
                    788:        P c;
                    789:        Q q;
                    790:        DCP dc0,dc;
                    791:
                    792:        read_int(s,&n);
                    793:        read_int(s,&ind);
                    794:        for ( i = 0, dc0 = 0; i < n; i++ ) {
                    795:                read_int(s,&d);
                    796:                read_cmo(s,&c);
                    797:                if ( c ) {
                    798:                        if ( OID(c) == O_USINT ) {
                    799:                                UTOQ(((USINT)c)->body,q); c = (P)q;
                    800:                        }
                    801:                        NEXTDC(dc0,dc);
                    802:                        STOQ(d,q);
                    803:                        dc->c = c; dc->d = q;
                    804:                }
                    805:        }
                    806:        if ( dc0 )
                    807:                NEXT(dc) = 0;
                    808:        MKP(remote_vtab[ind],dc0,*rp);
1.7       noro      809: }
                    810:
                    811: /* XXX */
                    812:
                    813: extern struct oARF arf[];
                    814:
                    815: struct operator_tab {
                    816:        char *name;
                    817:        fid id;
                    818:        ARF arf;
                    819:        cid cid;
                    820: };
                    821:
                    822: static struct operator_tab optab[] = {
                    823:        {"+",I_BOP,&arf[0],0}, /* XXX */
                    824:        {"-",I_BOP,&arf[1],0},
                    825:        {"*",I_BOP,&arf[2],0},
                    826:        {"/",I_BOP,&arf[3],0},
                    827:        {"%",I_BOP,&arf[4],0},
                    828:        {"^",I_BOP,&arf[5],0},
                    829:        {"==",I_COP,0,C_EQ},
                    830:        {"!=",I_COP,0,C_NE},
                    831:        {"<",I_COP,0,C_LT},
                    832:        {"<=",I_COP,0,C_LE},
                    833:        {">",I_COP,0,C_GT},
                    834:        {">=",I_COP,0,C_GE},
                    835:        {"&&",I_AND,0,0},
                    836:        {"||",I_OR,0,0},
                    837:        {"!",I_NOT,0,0},
                    838: };
                    839:
                    840: static int optab_len = sizeof(optab)/sizeof(struct operator_tab);
                    841:
                    842: read_cmo_tree(s,rp)
                    843: FILE *s;
1.8     ! noro      844: FNODE *rp;
1.7       noro      845: {
                    846:        int r,i,n;
                    847:        char *opname;
                    848:        STRING name,cd;
                    849:        int op;
1.8     ! noro      850:        pointer *arg;
1.7       noro      851:        QUOTE quote;
                    852:        FNODE fn;
1.8     ! noro      853:        NODE t,t1;
1.7       noro      854:        fid id;
1.8     ! noro      855:        Obj expr;
        !           856:        FUNC func;
1.7       noro      857:
                    858:        read_cmo(s,&name);
                    859:        read_cmo(s,&cd); /* XXX: currently not used */
                    860:        for ( i = 0; i < optab_len; i++ )
                    861:                if ( !strcmp(optab[i].name,BDY(name)) )
                    862:                        break;
                    863:        if ( i == optab_len ) {
                    864:                /* may be a function name */
1.8     ! noro      865:                n = read_cmo_tree_arg(s,&arg);
        !           866:                for ( i = n-1, t = 0; i >= 0; i-- ) {
        !           867:                        MKNODE(t1,arg[i],t); t = t1;
        !           868:                }
        !           869:                searchf(sysf,BDY(name),&func);
        !           870:                if ( !func )
        !           871:                        searchf(ubinf,BDY(name),&func);
        !           872:                if ( !func )
        !           873:                        searchpf(BDY(name),&func);
        !           874:                if ( !func )
        !           875:                        searchf(usrf,BDY(name),&func);
        !           876:                if ( !func )
        !           877:                        appenduf(BDY(name),&func);
        !           878:                *rp = mkfnode(2,I_FUNC,func,mkfnode(1,I_LIST,t));
1.7       noro      879:        } else {
                    880:                opname = optab[i].name;
                    881:                id = optab[i].id;
                    882:                switch ( id ) {
                    883:                        case I_BOP:
1.8     ! noro      884:                                read_cmo_tree_arg(s,&arg);
        !           885:                                *rp = mkfnode(3,I_BOP,optab[i].arf,arg[0],arg[1]);
        !           886:                                return;
1.7       noro      887:                        case I_COP:
1.8     ! noro      888:                                read_cmo_tree_arg(s,&arg);
        !           889:                                *rp = mkfnode(3,I_COP,optab[i].cid,arg[0],arg[0]);
        !           890:                                return;
1.7       noro      891:                        case I_AND:
1.8     ! noro      892:                                read_cmo_tree_arg(s,&arg);
        !           893:                                *rp = mkfnode(2,I_AND,arg[0],arg[1]);
        !           894:                                return;
1.7       noro      895:                        case I_OR:
1.8     ! noro      896:                                read_cmo_tree_arg(s,&arg);
        !           897:                                *rp = mkfnode(2,I_OR,arg[0],arg[1]);
        !           898:                                return;
1.7       noro      899:                        case I_NOT:
1.8     ! noro      900:                                read_cmo_tree_arg(s,&arg);
        !           901:                                *rp = mkfnode(1,I_OR,arg[0]);
        !           902:                                return;
1.7       noro      903:                }
                    904:        }
1.8     ! noro      905: }
        !           906:
        !           907: int read_cmo_tree_arg(s,argp)
        !           908: FILE *s;
        !           909: pointer **argp;
        !           910: {
        !           911:        int id,n,i;
        !           912:        pointer *ap;
        !           913:        Obj t;
        !           914:
        !           915:        read_int(s,&id); /* id = CMO_LIST */
        !           916:        read_int(s,&n); /* n = the number of args */
        !           917:        *argp = ap = (pointer *) MALLOC(n*sizeof(pointer));
        !           918:        for ( i = 0; i < n; i++ ) {
        !           919:                read_cmo(s,&t);
        !           920:                if ( !t || (OID(t) != O_QUOTE) )
        !           921:                        ap[i] = mkfnode(1,I_FORMULA,t);
        !           922:                else
        !           923:                        ap[i] = BDY((QUOTE)t);
        !           924:        }
        !           925:        return n;
1.1       noro      926: }
                    927:
                    928: localname_to_cmoname(a,b)
                    929: char *a;
                    930: char **b;
                    931: {
                    932:        int l;
                    933:        char *t;
                    934:
                    935:        l = strlen(a);
                    936:        if ( l >= 2 && a[0] == '@' && isupper(a[1]) ) {
                    937:                t = *b = (char *)MALLOC_ATOMIC(l);
                    938:                strcpy(t,a+1);
                    939:        } else {
                    940:                t = *b = (char *)MALLOC_ATOMIC(l+1);
                    941:                strcpy(t,a);
                    942:        }
                    943: }
                    944:
                    945: cmoname_to_localname(a,b)
                    946: char *a;
                    947: char **b;
                    948: {
                    949:        int l;
                    950:        char *t;
                    951:
                    952:        l = strlen(a);
                    953:        if ( isupper(a[0]) ) {
                    954:                t = *b = (char *)MALLOC_ATOMIC(l+2);
                    955:                strcpy(t+1,a);
                    956:                t[0] = '@';
                    957:        } else {
                    958:                t = *b = (char *)MALLOC_ATOMIC(l+1);
                    959:                strcpy(t,a);
                    960:        }
                    961: }

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