Annotation of OpenXM/src/ox_toolkit/mathcap.c, Revision 1.12
1.1 ohara 1: /* -*- mode: C; coding: euc-japan -*- */
1.12 ! ohara 2: /* $OpenXM: OpenXM/src/ox_toolkit/mathcap.c,v 1.11 2005/03/04 06:29:46 ohara Exp $ */
1.1 ohara 3:
4: /* This module includes functions for handling mathcap databases. */
5:
1.12 ! ohara 6: #include <stdio.h>
1.1 ohara 7: #include <stdlib.h>
1.5 ohara 8: #include <string.h>
1.1 ohara 9: #include "ox_toolkit.h"
10:
1.12 ! ohara 11: #define MATHCAP_1ST_FORMAT "(CMO_LIST (CMO_INT32 %d) (CMO_STRING \"%s\") (CMO_STRING \"%s\") (CMO_STRING \"%s\"))"
1.3 ohara 12:
1.12 ! ohara 13: static int default_cmd[] = {
! 14: SM_popCMO,
! 15: SM_popString,
! 16: SM_mathcap,
! 17: SM_pops,
! 18: SM_executeStringByLocalParser,
! 19: SM_executeFunction,
! 20: SM_control_kill,
! 21: SM_control_reset_connection,
! 22: 0 };
! 23: static int default_cmo[] = {
1.3 ohara 24: CMO_NULL,
25: CMO_INT32,
26: CMO_STRING,
27: CMO_MATHCAP,
28: CMO_LIST,
29: CMO_MONOMIAL32,
30: CMO_ZZ,
1.11 ohara 31: CMO_QQ,
1.3 ohara 32: CMO_ZERO,
33: CMO_DMS_GENERIC,
34: CMO_RING_BY_NAME,
1.12 ! ohara 35: CMO_RECURSIVE_POLYNOMIAL,
! 36: CMO_DISTRIBUTED_POLYNOMIAL,
! 37: CMO_POLYNOMIAL_IN_ONE_VARIABLE,
! 38: CMO_64BIT_MACHINE_DOUBLE,
! 39: CMO_IEEE_DOUBLE_FLOAT,
1.3 ohara 40: CMO_INDETERMINATE,
1.12 ! ohara 41: CMO_TREE,
! 42: CMO_LAMBDA,
1.3 ohara 43: CMO_ERROR2,
1.12 ! ohara 44: 0 };
! 45: static int default_oxtag[] = { OX_DATA, 0 };
1.1 ohara 46:
1.5 ohara 47: static struct {
1.12 ! ohara 48: int ox_version;
1.5 ohara 49: char *sysname;
1.12 ! ohara 50: char *version;
1.5 ohara 51: char *hosttype;
1.12 ! ohara 52: } sysinfo = {OX_PROTOCOL_VERSION, "ox_toolkit", OX_TOOLKIT_VERSION, "generic"};
1.5 ohara 53:
1.12 ! ohara 54: mathcap default_mathcap = {default_cmd, default_cmo};
1.2 ohara 55:
1.12 ! ohara 56: static int ilen(int a[]);
! 57: static int *icopy(int s[]);
! 58: static int *icopyn(int s[], int n);
! 59: static int cmo_int32_to_int(cmo_int32* m);
! 60: static int *imerge(int *base, int *diff);
! 61: static mathcap *mathcap_merge_io(mathcap *this, mathcap *diff);
1.2 ohara 62:
1.12 ! ohara 63: mathcap *new_mathcap()
1.2 ohara 64: {
1.12 ! ohara 65: mathcap *mcap = MALLOC(sizeof(mathcap));
! 66: mcap->cmo = icopy(default_cmo);
! 67: mcap->cmd = icopy(default_cmd);
! 68: return mcap;
! 69: }
! 70:
! 71: mathcap *new_mathcap_set(int *cmd, int *cmo)
! 72: {
! 73: mathcap *mcap = MALLOC(sizeof(mathcap));
! 74: mcap->cmd = (cmd)? cmd: icopy(default_cmd);
! 75: mcap->cmo = (cmo)? cmo: icopy(default_cmo);
! 76: return mcap;
! 77: }
! 78:
! 79: cmo_list *cmo_mathcap_1st()
! 80: {
! 81: char buffer[BUFSIZ];
! 82: static char format[] = MATHCAP_1ST_FORMAT;
! 83: int len = sizeof(format) + 32
! 84: + strlen(sysinfo.sysname)
! 85: + strlen(sysinfo.version)
! 86: + strlen(sysinfo.hosttype);
! 87: if (len < BUFSIZ) {
! 88: sprintf(buffer, format, sysinfo.ox_version, sysinfo.sysname,
! 89: sysinfo.version, sysinfo.hosttype);
! 90: return (cmo_list *)ox_parse_lisp(buffer);
1.2 ohara 91: }
92: return NULL;
93: }
94:
1.12 ! ohara 95: /* 0: terminator of array of integer. */
! 96: static int ilen(int a[])
1.2 ohara 97: {
1.12 ! ohara 98: int i=0;
! 99: if (a != NULL) {
! 100: for( ; a[i] !=0; i++) {
! 101: }
1.2 ohara 102: }
1.12 ! ohara 103: return i;
1.2 ohara 104: }
105:
1.12 ! ohara 106: static int *icopy(int s[])
1.2 ohara 107: {
1.12 ! ohara 108: int n = sizeof(int)*(ilen(s)+1);
! 109: int *d = MALLOC(n);
! 110: memcpy(d,s,n);
! 111: return d;
1.2 ohara 112: }
113:
1.12 ! ohara 114: static int *icopyn(int s[], int n)
1.2 ohara 115: {
1.12 ! ohara 116: int *d = MALLOC((n = sizeof(int)*(n+1)));
! 117: memcpy(d,s,n);
! 118: return d;
1.2 ohara 119: }
120:
1.12 ! ohara 121: cmo_mathcap *new_cmo_mathcap_by_mathcap(mathcap *mcap)
1.5 ohara 122: {
1.12 ! ohara 123: cmo_list *cap1, *cap2, *cap3, *cap4;
! 124: cap1 = cmo_mathcap_1st();
! 125: cap2 = new_cmo_list_map(mcap->cmd, ilen(mcap->cmd), new_cmo_int32);
! 126: cap3 = new_cmo_list_map(default_oxtag,
! 127: ilen(default_oxtag),
! 128: new_cmo_int32);
! 129: cap4 = new_cmo_list_map(mcap->cmo, ilen(mcap->cmo), new_cmo_int32);
! 130: /* new_cmo_mathcap([cap1, cap2, [cap3, cap4]]) */
! 131: return new_cmo_mathcap(
! 132: (cmo *)list_appendl(NULL, cap1, cap2, list_appendl(NULL, cap3, cap4, NULL)));
1.5 ohara 133: }
134:
1.12 ! ohara 135: static int cmo_int32_to_int(cmo_int32* m)
1.5 ohara 136: {
1.12 ! ohara 137: return m->i;
1.5 ohara 138: }
139:
1.12 ! ohara 140: mathcap *new_mathcap_by_cmo_mathcap(cmo_mathcap *cap)
1.5 ohara 141: {
1.12 ! ohara 142: int *cmd = list_to_array_map(list_nth(cap->ob, 1), cmo_int32_to_int);
! 143: int *cmo = list_to_array_map(list_nth(list_nth(cap->ob, 2), 1),
! 144: cmo_int32_to_int);
! 145: return new_mathcap_set(cmd, cmo);
1.5 ohara 146: }
1.1 ohara 147:
1.12 ! ohara 148: /* if base is unsorted. */
! 149: static int *imerge(int *base, int *diff)
1.1 ohara 150: {
1.12 ! ohara 151: int i,j,k;
! 152: int n = ilen(base);
! 153: int m = ilen(diff);
! 154: int *t = ALLOCA(sizeof(int)*(n+1));
! 155: int *ret;
! 156: for(i=0,j=0; i<n; i++) {
! 157: for(k=0; k<m; k++) {
! 158: if (base[i] == diff[k]) {
! 159: t[j++] = base[i];
! 160: break;
1.1 ohara 161: }
162: }
163: }
1.12 ! ohara 164: t[j] = 0;
! 165: ret = icopyn(t,j);
! 166: return ret;
1.1 ohara 167: }
168:
1.12 ! ohara 169: static mathcap *mathcap_merge_io(mathcap *this, mathcap *diff)
1.1 ohara 170: {
1.12 ! ohara 171: int *tmp;
! 172: tmp = imerge(this->cmo, diff->cmo);
! 173: FREE(this->cmo);
! 174: this->cmo = tmp;
! 175: return this;
1.5 ohara 176: }
177:
1.12 ! ohara 178: /* for compatibility */
! 179: void mathcap_init(char *version, char *sysname)
1.1 ohara 180: {
1.12 ! ohara 181: sysinfo.hosttype = getenv("HOSTTYPE");
! 182: sysinfo.version = version;
! 183: sysinfo.sysname = sysname;
1.3 ohara 184: }
185: cmo_mathcap* mathcap_get(mathcap *this)
1.1 ohara 186: {
1.12 ! ohara 187: return new_cmo_mathcap_by_mathcap(this);
1.1 ohara 188: }
1.12 ! ohara 189: mathcap *mathcap_update(mathcap *this, cmo_mathcap *m)
1.5 ohara 190: {
1.12 ! ohara 191: return mathcap_merge_io(this, new_mathcap_by_cmo_mathcap(m));
1.1 ohara 192: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>