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

Annotation of OpenXM/src/ox_toolkit/mathcap.c, Revision 1.12.2.1

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

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