[BACK]Return to cmo.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_toolkit

Annotation of OpenXM/src/ox_toolkit/cmo.c, Revision 1.7

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.7     ! ohara       2: /* $OpenXM: OpenXM/src/ox_toolkit/cmo.c,v 1.6 2003/01/17 06:49:53 ohara Exp $ */
1.1       ohara       3:
                      4: /*
                      5:    This module includes functions for sending/receiveng CMO's.
                      6:    Some commnets is written in Japanese by the EUC-JP coded
                      7:    character set.
                      8: */
                      9:
                     10: #include <stdio.h>
                     11: #include <stdlib.h>
                     12: #include <stdarg.h>
                     13: #include <string.h>
                     14: #include "ox_toolkit.h"
                     15: #include "parse.h"
                     16:
                     17: static cell*        new_cell();
                     18: static char*        new_string_set_cmo_null();
                     19: static char*        new_string_set_cmo_int32(int integer);
                     20: static char*        new_string_set_cmo_list(cmo_list *c);
                     21: static char*        new_string_set_cmo_zz(cmo_zz *c);
1.7     ! ohara      22: static char*        new_string_set_cmo_double(cmo_double *m);
1.1       ohara      23:
                     24: /* functions for a cmo_list */
                     25: static cell* new_cell(cmo *ob)
                     26: {
                     27:     cell* h = malloc(sizeof(cell));
                     28:     h->next = NULL;
                     29:     h->prev = NULL;
                     30:     h->cmo  = ob;
                     31:     return h;
                     32: }
                     33:
                     34: cell* list_next(cell *el)
                     35: {
                     36:     return el->next;
                     37: }
                     38:
                     39: cell* list_first(cmo_list *this)
                     40: {
                     41:     return this->head->next;
                     42: }
                     43:
                     44: cmo* list_first_cmo(cmo_list *this)
                     45: {
                     46:     return list_first(this)->cmo;
                     47: }
                     48:
                     49: int list_endof(cmo_list *this, cell *el)
                     50: {
                     51:     return (this->head == el);
                     52: }
                     53:
                     54: /* (prev, head) => (prev, new, head) */
                     55: static void list_cons(cell *head, cell *new)
                     56: {
                     57:     cell* prev = head->prev;
                     58:     new->prev  = prev;
                     59:     head->prev = new;
                     60:     prev->next = new;
                     61:     new->next  = head;
                     62: }
                     63:
                     64: cmo_list *list_append(cmo_list* this, cmo* ob)
                     65: {
                     66:     list_cons(this->head, new_cell(ob));
                     67:     this->length++;
                     68:     return this;
                     69: }
                     70:
                     71: /* call as list_appendl(List, ob1, ob2, ob3, NULL) */
                     72: cmo_list *list_appendl(cmo_list* this, ...)
                     73: {
                     74:     cmo *ob;
                     75:     va_list ap;
                     76:     va_start(ap, this);
1.3       ohara      77:        if (this == NULL) {
                     78:                this = new_cmo_list();
                     79:        }
1.1       ohara      80:     while((ob = va_arg(ap, cmo *)) != NULL) {
                     81:         list_append(this, ob);
                     82:     }
                     83:     va_end(ap);
                     84:     return this;
                     85: }
                     86:
                     87: int list_length(cmo_list* this)
                     88: {
                     89:     return this->length;
                     90: }
                     91:
                     92: cmo *list_nth(cmo_list* this, int n)
                     93: {
                     94:     cell* el;
                     95:     if(list_length(this) > n) {
                     96:         el = list_first(this);
                     97:         while(n-- > 0) {
                     98:             el = list_next(el);
                     99:         }
                    100:         return el->cmo;
                    101:     }
                    102:     return NULL;
                    103: }
                    104:
                    105: /* for GNU mpz */
                    106: void resize_mpz(mpz_ptr mpz, int size)
                    107: {
                    108:     _mpz_realloc(mpz, abs(size));
                    109:     mpz->_mp_size = size;
                    110: }
                    111:
                    112: /* functions named new_cmo_*. */
                    113: cmo_null* new_cmo_null()
                    114: {
                    115:     cmo_null* m = malloc(sizeof(cmo_null));
                    116:     m->tag = CMO_NULL;
                    117:     return m;
                    118: }
                    119:
                    120: cmo_int32* new_cmo_int32(int i)
                    121: {
                    122:     cmo_int32* c;
                    123:     c = malloc(sizeof(cmo_int32));
                    124:     c->tag     = CMO_INT32;
                    125:     c->i = i;
                    126:     return c;
                    127: }
                    128:
                    129: cmo_string* new_cmo_string(char* s)
                    130: {
                    131:     cmo_string* c = malloc(sizeof(cmo_string));
                    132:     c->tag = CMO_STRING;
                    133:     if (s != NULL) {
                    134:         c->s = malloc(strlen(s)+1);
                    135:         strcpy(c->s, s);
                    136:     }else {
                    137:         c->s = NULL;
                    138:     }
                    139:     return c;
                    140: }
                    141:
                    142: cmo_mathcap* new_cmo_mathcap(cmo* ob)
                    143: {
                    144:     cmo_mathcap* c = malloc(sizeof(cmo_mathcap));
                    145:     c->tag = CMO_MATHCAP;
                    146:     c->ob  = ob;
                    147:     return c;
                    148: }
                    149:
                    150: cmo_list* new_cmo_list()
                    151: {
                    152:     cmo_list* c = malloc(sizeof(cmo_list));
                    153:     c->tag    = CMO_LIST;
                    154:     c->length = 0;
                    155:     c->head->next = c->head;
                    156:     c->head->prev = c->head;
                    157:     return c;
                    158: }
                    159:
                    160: cmo_monomial32* new_cmo_monomial32()
                    161: {
                    162:     cmo_monomial32* c = malloc(sizeof(cmo_monomial32));
                    163:     c->tag  = CMO_MONOMIAL32;
                    164:     return c;
                    165: }
                    166:
                    167: cmo_monomial32* new_cmo_monomial32_size(int size)
                    168: {
                    169:     cmo_monomial32* c = new_cmo_monomial32();
                    170:     if (size>0) {
                    171:         c->length = size;
                    172:         c->exps = malloc(sizeof(int)*size);
                    173:     }
                    174:     return c;
                    175: }
                    176:
                    177: cmo_zz* new_cmo_zz()
                    178: {
                    179:     cmo_zz* c = malloc(sizeof(cmo_zz));
                    180:     c->tag  = CMO_ZZ;
                    181:     mpz_init(c->mpz);
                    182:     return c;
                    183: }
                    184:
                    185: cmo_zz* new_cmo_zz_noinit()
                    186: {
                    187:     cmo_zz* c = malloc(sizeof(cmo_zz));
                    188:     c->tag  = CMO_ZZ;
                    189:     return c;
                    190: }
                    191:
                    192: cmo_zz* new_cmo_zz_set_si(int i)
                    193: {
                    194:     cmo_zz* c = new_cmo_zz();
                    195:     mpz_set_si(c->mpz, i);
                    196:     return c;
                    197: }
                    198:
                    199: cmo_zz* new_cmo_zz_set_mpz(mpz_ptr z)
                    200: {
                    201:     cmo_zz* c = new_cmo_zz();
                    202:     mpz_set(c->mpz, z);
                    203:     return c;
                    204: }
                    205:
                    206: cmo_zz *new_cmo_zz_set_string(char *s)
                    207: {
                    208:     cmo_zz* c = new_cmo_zz_noinit();
                    209:     mpz_init_set_str(c->mpz, s, 10);
                    210:     return c;
                    211: }
                    212:
                    213: cmo_zz* new_cmo_zz_size(int size)
                    214: {
                    215:     cmo_zz* c = new_cmo_zz();
                    216:     resize_mpz(c->mpz, size);
                    217:     return c;
                    218: }
                    219:
                    220: cmo_zero* new_cmo_zero()
                    221: {
                    222:     cmo_zero* m = malloc(sizeof(cmo_zero));
                    223:     m->tag = CMO_ZERO;
1.6       ohara     224:     return m;
                    225: }
                    226:
                    227: cmo_double *new_cmo_double(double d)
                    228: {
                    229:     cmo_double* m = malloc(sizeof(cmo_double));
                    230:     m->tag = CMO_64BIT_MACHINE_DOUBLE;
                    231:     m->d = d;
1.1       ohara     232:     return m;
                    233: }
                    234:
                    235: cmo_dms_generic* new_cmo_dms_generic()
                    236: {
                    237:     cmo_dms_generic* m = malloc(sizeof(cmo_dms_generic));
                    238:     m->tag = CMO_DMS_GENERIC;
                    239:     return m;
                    240: }
                    241:
                    242: cmo_ring_by_name* new_cmo_ring_by_name(cmo* ob)
                    243: {
                    244:     cmo_ring_by_name* c = malloc(sizeof(cmo_ring_by_name));
                    245:     c->tag = CMO_RING_BY_NAME;
                    246:     c->ob  = ob;
                    247:     return c;
                    248: }
                    249:
                    250: cmo_indeterminate* new_cmo_indeterminate(cmo* ob)
                    251: {
                    252:     cmo_indeterminate* c = malloc(sizeof(cmo_indeterminate));
                    253:     c->tag = CMO_INDETERMINATE;
                    254:     c->ob  = ob;
                    255:     return c;
                    256: }
                    257:
                    258: cmo_distributed_polynomial* new_cmo_distributed_polynomial()
                    259: {
                    260:     cmo_distributed_polynomial* c = malloc(sizeof(cmo_distributed_polynomial));
                    261:     c->tag     = CMO_DISTRIBUTED_POLYNOMIAL;
                    262:     c->length  = 0;
                    263:     c->head->next = c->head;
                    264:     c->head->prev = c->head;
                    265:     c->ringdef = NULL;
                    266:     return c;
                    267: }
                    268:
                    269: cmo_error2* new_cmo_error2(cmo* ob)
                    270: {
                    271:     cmo_error2* c = malloc(sizeof(cmo_error2));
                    272:     c->tag = CMO_ERROR2;
                    273:     c->ob  = ob;
                    274:     return c;
                    275: }
                    276:
1.2       ohara     277:
                    278: /* Following functions translate cmo's to (asciiz) strings. */
1.1       ohara     279: static char *new_string_set_cmo_zz(cmo_zz *c)
                    280: {
                    281:     return mpz_get_str(NULL, 10, c->mpz);
                    282: }
                    283:
                    284: static char *new_string_set_cmo_null()
                    285: {
                    286:     static char* null_string = "";
                    287:     return null_string;
                    288: }
                    289:
                    290: static char *new_string_set_cmo_int32(int integer)
                    291: {
                    292:     char buff[1024];
                    293:     char *s;
                    294:
                    295:     sprintf(buff, "%d", integer);
                    296:     s = malloc(strlen(buff)+1);
                    297:     strcpy(s, buff);
                    298:
                    299:     return s;
                    300: }
                    301:
                    302: static char *new_string_set_cmo_list(cmo_list *m)
                    303: {
                    304:     char *s;
                    305:     int i;
                    306:     int size = 0;
                    307:     int len = list_length(m);
                    308:     char **sp = malloc(len*sizeof(cmo *));
                    309:
                    310:     cell* cp = list_first(m);
                    311:     for(i = 0; i < len; i++) {
                    312:         sp[i] = new_string_set_cmo(cp->cmo);
                    313:         size += strlen(sp[i]) + 3;
                    314:         cp = list_next(cp);
                    315:     }
                    316:     s = malloc(size+2);
                    317:     strcpy(s, "[ ");
                    318:     for(i = 0; i < len - 1; i++) {
                    319:         strcat(s, sp[i]);
                    320:         strcat(s, " , ");
                    321:     }
                    322:     strcat(s, sp[len-1]);
                    323:     strcat(s, " ]");
                    324:     free(sp);
                    325:     return s;
                    326: }
                    327:
1.7     ! ohara     328: static char *new_string_set_cmo_double(cmo_double *m)
        !           329: {
        !           330:     char buff[1024];
        !           331:     char *s;
        !           332:
        !           333:     sprintf(buff, "%lf", m->d);
        !           334:     s = malloc(strlen(buff)+1);
        !           335:     strcpy(s, buff);
        !           336:
        !           337:     return s;
        !           338: }
        !           339:
1.1       ohara     340: char *new_string_set_cmo(cmo *m)
                    341: {
                    342:     symbol_t symp;
                    343:     switch(m->tag) {
                    344:     case CMO_ZZ:
                    345:         return new_string_set_cmo_zz((cmo_zz *)m);
                    346:     case CMO_INT32:
                    347:         return new_string_set_cmo_int32(((cmo_int32 *)m)->i);
                    348:     case CMO_STRING:
                    349:         return ((cmo_string *)m)->s;
                    350:     case CMO_NULL:
                    351:         return new_string_set_cmo_null();
                    352:     case CMO_LIST:
                    353:         return new_string_set_cmo_list((cmo_list *)m);
1.7     ! ohara     354:        case CMO_64BIT_MACHINE_DOUBLE:
        !           355:         return new_string_set_cmo_int32(m);
1.1       ohara     356:     default:
                    357: #ifdef DEBUG
                    358:         symp = lookup_by_tag(m->tag);
1.5       ohara     359:         ox_printf("I do not know how to convert %s to a string.\n", symp->key);
1.1       ohara     360: #endif
                    361:         /* yet not implemented. */
                    362:         return NULL;
                    363:     }
                    364: }

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