[BACK]Return to tree2polymake.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kan96xx / trans

Annotation of OpenXM/src/kan96xx/trans/tree2polymake.c, Revision 1.3

1.3     ! takayama    1: /* $OpenXM: OpenXM/src/kan96xx/trans/tree2polymake.c,v 1.2 2005/07/03 11:08:54 ohara Exp $ */
1.1       takayama    2: #include <stdio.h>
1.2       ohara       3: #include <string.h>
1.1       takayama    4: #include "../Kan/datatype.h"
                      5: #include "../Kan/stackm.h"
                      6: #include "../Kan/extern.h"
                      7: #include "../Kan/gradedset.h"
                      8: #include "../Kan/extern2.h"
                      9: #include "../Kan/kclass.h"
                     10: #include "../plugin/file2.h"
                     11: #include "trans.h"
                     12:
                     13: int rationalToPolymakeS(struct object ob,FILE2 *fp) {
                     14:   struct object ob1;
                     15:   struct object ob2;
                     16:   int r;
                     17:   r = 0;
                     18:   if (ob.tag == SrationalFunction) {
                     19:     ob1 = *(Knumerator(ob));
                     20:     ob2 = *(Kdenominator(ob));
                     21:     r=integerToPolymakeS(ob1,fp);
                     22:     r=(fp2fputs("/",fp)<0?-1:r);
                     23:     r=(integerToPolymakeS(ob2,fp)<0?-1:r);
                     24:   }else if ((ob.tag == Sinteger) || (ob.tag == SuniversalNumber)) {
                     25:     r=integerToPolymakeS(ob,fp);
                     26:   }else{
                     27:     return(-1);
                     28:   }
                     29:   return(r);
                     30: }
                     31:
                     32: int integerToPolymakeS(struct object ob,FILE2 *fp) {
                     33:   int r;
                     34:   char *s;
                     35:   char s2[254];
                     36:   r = 0;
                     37:   if (ob.tag == Sinteger) {
                     38:     sprintf(s2,"%d",ob.lc.ival);
                     39:     r=(fp2fputs(s2,fp)<0?-1:r);
                     40:   }else if (ob.tag == SuniversalNumber) {
                     41:     s=coeffToString(ob.lc.universalNumber);
                     42:     r=(fp2fputs(s,fp)<0?-1:r);
                     43:   }else{
                     44:     return(-1);
                     45:   }
                     46:   return(r);
                     47: }
                     48:
                     49: int arrayToPolymakeS(struct object ob,FILE2 *fp,int format) {
                     50:   int r;
                     51:   int n,i;
                     52:   struct object ob1;
                     53:   r = 0;
                     54:   if (ob.tag != Sarray) {
                     55:     return(-1);
                     56:   }
                     57:   if (format == 0) {
                     58:     n = getoaSize(ob);
                     59:     for (i=0; i<n; i++) {
                     60:       ob1 = getoa(ob,i);
                     61:       if (ob1.tag == Sarray) {
                     62:         r=(arrayToPolymakeS(ob1,fp,format)<0?-1:r);
                     63:       }else{
1.3     ! takayama   64:         r=(rationalToPolymakeS(ob1,fp)<0?-1:r);
1.1       takayama   65:         if (i != n-1) fp2fputc(' ',fp);
                     66:       }
                     67:     }
                     68:     fp2fputc('\n',fp);
                     69:   }else{
                     70:     n = getoaSize(ob);
                     71:     for (i=0; i<n; i++) {
                     72:       ob1 = getoa(ob,i);
                     73:       if (ob1.tag == Sarray) {
                     74:         if (i==0) fp2fputc('{',fp);
                     75:         r=(arrayToPolymakeS(ob1,fp,format)<0?-1:r);
                     76:         if (i==n-1) fp2fputc('}',fp);
                     77:       }else{
1.3     ! takayama   78:         r=(rationalToPolymakeS(ob1,fp)<0?-1:r);
1.1       takayama   79:         if (i != n-1) fp2fputc(' ',fp);
                     80:       }
                     81:     }
                     82:     fp2fputc('\n',fp);
                     83:   }
                     84:   return(0);
                     85: }
                     86:
                     87: int treeToPolymakeS(struct object ob,FILE2 *fp) {
                     88:   struct object obe;
                     89:   struct object obc;
                     90:   int r;
                     91:   int i,n;
                     92:   char *key;
                     93:   r = 0;
                     94:   if (ob.tag != Sclass) return(-1);
                     95:   if (ectag(ob) != CLASSNAME_tree) return(-1);
                     96:   obe = KtreeGetDocumentElement(ob);
                     97:   obc = KtreeGetChildNodes(ob);
                     98:   if (obe.tag != Sdollar) return(-1);
                     99:   if (obc.tag != Sarray) return(-1);
                    100:   key = KopString(obe);
                    101:   /* printf("key=%s\n",key); */
                    102:   if (strcmp(key,"data")==0) {
                    103:     /* Top node */
                    104:        n = getoaSize(obc);
                    105:     for (i=0; i<n; i++) {
                    106:       r=(treeToPolymakeS(getoa(obc,i),fp)<0?-1:r);
                    107:     }
                    108:   }
                    109:   else if (strcmp(key,"POINTS")==0) {
                    110:     /* array of array is expected. */
                    111:     fp2fputs("POINTS\n",fp);
                    112:     r=(arrayToPolymakeS(obc,fp,0)<0?-1:r);
                    113:     fp2fputc('\n',fp);
                    114:   }
                    115:   else if (strcmp(key,"FACETS") == 0) {
                    116:     fp2fputs("FACETS\n",fp);
                    117:     r=(arrayToPolymakeS(obc,fp,0)<0?-1:r);
                    118:   }
                    119:   /* Add more */
                    120:   else {
                    121:     /* Generic */
                    122:     fp2fputs(key,fp);
                    123:     fp2fputc('\n',fp);
                    124:     if (getoaSize(obc) > 0) {
                    125:       r=(arrayToPolymakeS(obc,fp,0)<0?-1:r);
                    126:     }
                    127:     fp2fputc('\n',fp);
                    128:   }
                    129:   fp2fputc('\n',fp);
                    130:   return(r);
                    131: }
                    132:
                    133:
                    134: struct object KtreeToPolymakeS(struct object ob) {
                    135:   FILE2 *fp;
                    136:   int r;
                    137:   int size;
                    138:   char *s,*s2;
                    139:   if (ob.tag != Sclass) errorKan1("%s\n","KtreeToPolymakeS: argument is not a tree.");
                    140:   if (ectag(ob) != CLASSNAME_tree) errorKan1("%s\n","KtreeToPolymakeS: argument is not a tree.");
                    141:   fp = fp2open(-1);
                    142:   if (fp == NULL) errorKan1("%s\n","KtreeToPolymakeS: fails to open a stream.");
                    143:   r = treeToPolymakeS(ob,fp);
                    144:   if (r < 0) errorKan1("%s\n","KtreeToPolymakeS: the tree may contain an object which cannot be translated.\n");
                    145:   s = fp2fcloseInString(fp,&size);
                    146:   if (s == NULL) errorKan1("%s\n","KtreeToPolymakeS:");
                    147:   s2 = (char *)sGC_malloc(size+2);
                    148:   if (s2 == NULL) errorKan1("%s\n","No more memory.");
                    149:   strcpy(s2,s);
                    150:   return KpoString(s2);
                    151: }

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