Changeset 4da485 in git for Singular/links/silink.cc
- Timestamp:
- Feb 21, 2013, 6:16:43 PM (10 years ago)
- Branches:
- (u'jengelh-datetime', 'ceac47cbc86fe4a15902392bdbb9bd2ae0ea02c6')(u'spielwiese', 'a800fe4b3e9d37a38c5a10cc0ae9dfa0c15a4ee6')
- Children:
- 71835b048396c120b4b658858c85c558db662d6a
- Parents:
- c2400cea086b85043ee8187be4765842d83ecac4
- git-author:
- Hans Schoenemann <hannes@mathematik.uni-kl.de>2013-02-21 18:16:43+01:00
- git-committer:
- Hans Schoenemann <hannes@mathematik.uni-kl.de>2013-02-21 18:17:42+01:00
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Singular/links/silink.cc
rc2400ce r4da485 43 43 omBin ip_link_bin = omGetSpecBin(sizeof(ip_link)); 44 44 45 /* declarations */ 46 static BOOLEAN DumpAscii(FILE *fd, idhdl h); 47 static BOOLEAN DumpAsciiIdhdl(FILE *fd, idhdl h); 48 static const char* GetIdString(idhdl h); 49 static int DumpRhs(FILE *fd, idhdl h); 50 static BOOLEAN DumpQring(FILE *fd, idhdl h, const char *type_str); 51 static BOOLEAN DumpAsciiMaps(FILE *fd, idhdl h, idhdl rhdl); 45 /* ====================================================================== */ 52 46 static si_link_extension slTypeInit(si_link_extension s, const char* type); 53 54 /* ====================================================================== */55 47 si_link_extension si_link_root=NULL; 56 48 … … 397 389 } 398 390 399 400 /* =============== ASCII ============================================= */401 BOOLEAN slOpenAscii(si_link l, short flag, leftv h)402 {403 const char *mode;404 if (flag & SI_LINK_OPEN)405 {406 if (l->mode[0] != '\0' && (strcmp(l->mode, "r") == 0))407 flag = SI_LINK_READ;408 else flag = SI_LINK_WRITE;409 }410 411 if (flag == SI_LINK_READ) mode = "r";412 else if (strcmp(l->mode, "w") == 0) mode = "w";413 else mode = "a";414 415 416 if (l->name[0] == '\0')417 {418 // stdin or stdout419 if (flag == SI_LINK_READ)420 {421 l->data = (void *) stdin;422 mode = "r";423 }424 else425 {426 l->data = (void *) stdout;427 mode = "a";428 }429 }430 else431 {432 // normal ascii link to a file433 FILE *outfile;434 char *filename=l->name;435 436 if(filename[0]=='>')437 {438 if (filename[1]=='>')439 {440 filename+=2;441 mode = "a";442 }443 else444 {445 filename++;446 mode="w";447 }448 }449 outfile=myfopen(filename,mode);450 if (outfile!=NULL)451 l->data = (void *) outfile;452 else453 return TRUE;454 }455 456 omFree(l->mode);457 l->mode = omStrDup(mode);458 SI_LINK_SET_OPEN_P(l, flag);459 return FALSE;460 }461 462 BOOLEAN slCloseAscii(si_link l)463 {464 SI_LINK_SET_CLOSE_P(l);465 if (l->name[0] != '\0')466 {467 return (fclose((FILE *)l->data)!=0);468 }469 return FALSE;470 }471 472 leftv slReadAscii2(si_link l, leftv pr)473 {474 FILE * fp=(FILE *)l->data;475 char * buf=NULL;476 if (fp!=NULL && l->name[0] != '\0')477 {478 fseek(fp,0L,SEEK_END);479 long len=ftell(fp);480 fseek(fp,0L,SEEK_SET);481 buf=(char *)omAlloc((int)len+1);482 if (BVERBOSE(V_READING))483 Print("//Reading %ld chars\n",len);484 myfread( buf, len, 1, fp);485 buf[len]='\0';486 }487 else488 {489 if (pr->Typ()==STRING_CMD)490 {491 buf=(char *)omAlloc(80);492 fe_fgets_stdin((char *)pr->Data(),buf,80);493 }494 else495 {496 WerrorS("read(<link>,<string>) expected");497 buf=omStrDup("");498 }499 }500 leftv v=(leftv)omAlloc0Bin(sleftv_bin);501 v->rtyp=STRING_CMD;502 v->data=buf;503 return v;504 }505 506 leftv slReadAscii(si_link l)507 {508 sleftv tmp;509 memset(&tmp,0,sizeof(sleftv));510 tmp.rtyp=STRING_CMD;511 tmp.data=(void*) "? ";512 return slReadAscii2(l,&tmp);513 }514 515 BOOLEAN slWriteAscii(si_link l, leftv v)516 {517 FILE *outfile=(FILE *)l->data;518 BOOLEAN err=FALSE;519 char *s;520 while (v!=NULL)521 {522 s = v->String();523 // free v ??524 if (s!=NULL)525 {526 fprintf(outfile,"%s\n",s);527 omFree((ADDRESS)s);528 }529 else530 {531 Werror("cannot convert to string");532 err=TRUE;533 }534 v = v->next;535 }536 fflush(outfile);537 return err;538 }539 540 const char* slStatusAscii(si_link l, const char* request)541 {542 if (strcmp(request, "read") == 0)543 {544 if (SI_LINK_R_OPEN_P(l)) return "ready";545 else return "not ready";546 }547 else if (strcmp(request, "write") == 0)548 {549 if (SI_LINK_W_OPEN_P(l)) return "ready";550 else return "not ready";551 }552 else return "unknown status request";553 }554 555 /*------------------ Dumping in Ascii format -----------------------*/556 557 BOOLEAN slDumpAscii(si_link l)558 {559 FILE *fd = (FILE *) l->data;560 idhdl h = IDROOT, rh = currRingHdl;561 BOOLEAN status = DumpAscii(fd, h);562 563 if (! status ) status = DumpAsciiMaps(fd, h, NULL);564 565 if (currRingHdl != rh) rSetHdl(rh);566 fprintf(fd, "option(set, intvec(%d, %d));\n", si_opt_1, si_opt_2);567 fprintf(fd, "RETURN();\n");568 fflush(fd);569 570 return status;571 }572 573 // we do that recursively, to dump ids in the the order in which they574 // were actually defined575 static BOOLEAN DumpAscii(FILE *fd, idhdl h)576 {577 if (h == NULL) return FALSE;578 579 if (DumpAscii(fd, IDNEXT(h))) return TRUE;580 581 // need to set the ring before writing it, otherwise we get in582 // trouble with minpoly583 if (IDTYP(h) == RING_CMD || IDTYP(h) == QRING_CMD)584 rSetHdl(h);585 586 if (DumpAsciiIdhdl(fd, h)) return TRUE;587 588 if (IDTYP(h) == RING_CMD || IDTYP(h) == QRING_CMD)589 return DumpAscii(fd, IDRING(h)->idroot);590 else591 return FALSE;592 }593 594 static BOOLEAN DumpAsciiMaps(FILE *fd, idhdl h, idhdl rhdl)595 {596 if (h == NULL) return FALSE;597 if (DumpAsciiMaps(fd, IDNEXT(h), rhdl)) return TRUE;598 599 if (IDTYP(h) == RING_CMD || IDTYP(h) == QRING_CMD)600 return DumpAsciiMaps(fd, IDRING(h)->idroot, h);601 else if (IDTYP(h) == MAP_CMD)602 {603 char *rhs;604 rSetHdl(rhdl);605 rhs = h->String();606 607 if (fprintf(fd, "setring %s;\n", IDID(rhdl)) == EOF) return TRUE;608 if (fprintf(fd, "%s %s = %s, %s;\n", Tok2Cmdname(MAP_CMD), IDID(h),609 IDMAP(h)->preimage, rhs) == EOF)610 {611 omFree(rhs);612 return TRUE;613 }614 else615 {616 omFree(rhs);617 return FALSE;618 }619 }620 else return FALSE;621 }622 623 static BOOLEAN DumpAsciiIdhdl(FILE *fd, idhdl h)624 {625 const char *type_str = GetIdString(h);626 int type_id = IDTYP(h);627 628 if ((type_id == PACKAGE_CMD) &&(strcmp(IDID(h), "Top") == 0))629 return FALSE;630 631 // we do not throw an error if a wrong type was attempted to be dumped632 if (type_str == NULL)633 return FALSE;634 635 // handle qrings separately636 if (type_id == QRING_CMD)637 return DumpQring(fd, h, type_str);638 639 // C-proc not to be dumped640 if ((type_id == PROC_CMD) && (IDPROC(h)->language == LANG_C))641 return FALSE;642 643 // put type and name644 if (fprintf(fd, "%s %s", type_str, IDID(h)) == EOF)645 return TRUE;646 // for matricies, append the dimension647 if (type_id == MATRIX_CMD)648 {649 ideal id = IDIDEAL(h);650 if (fprintf(fd, "[%d][%d]", id->nrows, id->ncols)== EOF) return TRUE;651 }652 else if (type_id == INTMAT_CMD)653 {654 if (fprintf(fd, "[%d][%d]", IDINTVEC(h)->rows(), IDINTVEC(h)->cols())655 == EOF) return TRUE;656 }657 658 if (type_id == PACKAGE_CMD)659 {660 return (fprintf(fd, ";\n") == EOF);661 }662 663 // write the equal sign664 if (fprintf(fd, " = ") == EOF) return TRUE;665 666 // and the right hand side667 if (DumpRhs(fd, h) == EOF) return TRUE;668 669 // semicolon und tschuess670 if (fprintf(fd, ";\n") == EOF) return TRUE;671 672 return FALSE;673 }674 675 static const char* GetIdString(idhdl h)676 {677 int type = IDTYP(h);678 679 switch(type)680 {681 case LIST_CMD:682 {683 lists l = IDLIST(h);684 int i, nl = l->nr + 1;685 686 for (i=0; i<nl; i++)687 if (GetIdString((idhdl) &(l->m[i])) == NULL) return NULL;688 }689 case PACKAGE_CMD:690 case INT_CMD:691 case INTVEC_CMD:692 case INTMAT_CMD:693 case STRING_CMD:694 case RING_CMD:695 case QRING_CMD:696 case PROC_CMD:697 case NUMBER_CMD:698 case POLY_CMD:699 case IDEAL_CMD:700 case VECTOR_CMD:701 case MODUL_CMD:702 case MATRIX_CMD:703 return Tok2Cmdname(type);704 705 case MAP_CMD:706 case LINK_CMD:707 return NULL;708 709 default:710 Warn("Error dump data of type %s", Tok2Cmdname(IDTYP(h)));711 return NULL;712 }713 }714 715 static BOOLEAN DumpQring(FILE *fd, idhdl h, const char *type_str)716 {717 char *ring_str = h->String();718 if (fprintf(fd, "%s temp_ring = %s;\n", Tok2Cmdname(RING_CMD), ring_str)719 == EOF) return TRUE;720 if (fprintf(fd, "%s temp_ideal = %s;\n", Tok2Cmdname(IDEAL_CMD),721 iiStringMatrix((matrix) IDRING(h)->qideal, 1, currRing, n_GetChar(currRing->cf)))722 == EOF) return TRUE;723 if (fprintf(fd, "attrib(temp_ideal, \"isSB\", 1);\n") == EOF) return TRUE;724 if (fprintf(fd, "%s %s = temp_ideal;\n", type_str, IDID(h)) == EOF)725 return TRUE;726 if (fprintf(fd, "kill temp_ring;\n") == EOF) return TRUE;727 else728 {729 omFree(ring_str);730 return FALSE;731 }732 }733 734 735 static int DumpRhs(FILE *fd, idhdl h)736 {737 int type_id = IDTYP(h);738 739 if (type_id == LIST_CMD)740 {741 lists l = IDLIST(h);742 int i, nl = l->nr;743 744 fprintf(fd, "list(");745 746 for (i=0; i<nl; i++)747 {748 if (DumpRhs(fd, (idhdl) &(l->m[i])) == EOF) return EOF;749 fprintf(fd, ",");750 }751 if (nl > 0)752 {753 if (DumpRhs(fd, (idhdl) &(l->m[nl])) == EOF) return EOF;754 }755 fprintf(fd, ")");756 }757 else if (type_id == STRING_CMD)758 {759 char *pstr = IDSTRING(h);760 fputc('"', fd);761 while (*pstr != '\0')762 {763 if (*pstr == '"' || *pstr == '\\') fputc('\\', fd);764 fputc(*pstr, fd);765 pstr++;766 }767 fputc('"', fd);768 }769 else if (type_id == PROC_CMD)770 {771 procinfov pi = IDPROC(h);772 if (pi->language == LANG_SINGULAR)773 {774 if( pi->data.s.body==NULL) iiGetLibProcBuffer(pi);775 char *pstr = pi->data.s.body;776 fputc('"', fd);777 while (*pstr != '\0')778 {779 if (*pstr == '"' || *pstr == '\\') fputc('\\', fd);780 fputc(*pstr, fd);781 pstr++;782 }783 fputc('"', fd);784 }785 else fputs("(null)", fd);786 }787 else788 {789 char *rhs = h->String();790 791 if (rhs == NULL) return EOF;792 793 BOOLEAN need_klammer=FALSE;794 if (type_id == INTVEC_CMD) { fprintf(fd, "intvec(");need_klammer=TRUE; }795 else if (type_id == IDEAL_CMD) { fprintf(fd, "ideal(");need_klammer=TRUE; }796 else if (type_id == MODUL_CMD) { fprintf(fd, "module(");need_klammer=TRUE; }797 798 if (fprintf(fd, "%s", rhs) == EOF) return EOF;799 omFree(rhs);800 801 if ((type_id == RING_CMD || type_id == QRING_CMD) &&802 IDRING(h)->cf->type==n_algExt)803 {804 StringSetS("");805 p_Write(IDRING(h)->cf->extRing->qideal->m[0],IDRING(h)->cf->extRing);806 rhs = StringEndS();807 if (fprintf(fd, "; minpoly = %s", rhs) == EOF) { omFree(rhs); return EOF;}808 omFree(rhs);809 }810 else if (need_klammer) fprintf(fd, ")");811 }812 return 1;813 }814 815 BOOLEAN slGetDumpAscii(si_link l)816 {817 if (l->name[0] == '\0')818 {819 Werror("getdump: Can not get dump from stdin");820 return TRUE;821 }822 else823 {824 BOOLEAN status = newFile(l->name);825 if (status)826 return TRUE;827 828 int old_echo=si_echo;829 si_echo=0;830 831 status=yyparse();832 833 si_echo=old_echo;834 835 if (status)836 return TRUE;837 else838 {839 // lets reset the file pointer to the end to reflect that840 // we are finished with reading841 FILE *f = (FILE *) l->data;842 fseek(f, 0L, SEEK_END);843 return FALSE;844 }845 }846 }847 848 849 391 /*------------Initialization at Start-up time------------------------*/ 850 392 … … 887 429 } 888 430 889 void slStandardInit()890 {891 si_link_extension s;892 si_link_root=(si_link_extension)omAlloc0Bin(s_si_link_extension_bin);893 si_link_root->Open=slOpenAscii;894 si_link_root->Close=slCloseAscii;895 si_link_root->Kill=NULL;896 si_link_root->Read=slReadAscii;897 si_link_root->Read2=slReadAscii2;898 si_link_root->Write=slWriteAscii;899 si_link_root->Dump=slDumpAscii;900 si_link_root->GetDump=slGetDumpAscii;901 si_link_root->Status=slStatusAscii;902 si_link_root->type="ASCII";903 s = si_link_root;904 s->next = NULL;905 }
Note: See TracChangeset
for help on using the changeset viewer.