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