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