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