source: git/Singular/silink.cc @ 1fc83c0

spielwiese
Last change on this file since 1fc83c0 was 1fc83c0, checked in by Hans Schönemann <hannes@…>, 27 years ago
* hannes: added nExactDiv to numbers (numbers.*, longrat.*) corrected open bug in sing_dbm.cc (could only "r") simplified sing_dbm.cc (many tests already in silink.cc) (sing_dbm.cc, silink.h) added "mod" (as an alias to "%") (iparith.cc) updated singular.doc: div/mod, DBM: links ANSI-conversion in ndbm.cc (added return types, include files) git-svn-id: file:///usr/local/Singular/svn/trunk@614 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 15.9 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: silink.cc,v 1.12 1997-08-12 17:14:43 Singular Exp $ */
5
6/*
7* ABSTRACT: general interface to links
8*/
9
10#include <stdio.h>
11#include <string.h>
12#include "mod2.h"
13#include "tok.h"
14#include "mmemory.h"
15#include "febase.h"
16#include "subexpr.h"
17#include "ipid.h"
18#include "silink.h"
19#include "ipshell.h"
20#include "ring.h"
21#include "lists.h"
22#include "ideals.h"
23#include "numbers.h"
24#include "intvec.h"
25
26/* declarations */
27static BOOLEAN DumpAscii(FILE *fd, idhdl h);
28static BOOLEAN DumpAsciiIdhdl(FILE *fd, idhdl h);
29static char* GetIdString(idhdl h);
30static int DumpRhs(FILE *fd, idhdl h);
31static BOOLEAN DumpQring(FILE *fd, idhdl h, char *type_str);
32static BOOLEAN DumpAsciiMaps(FILE *fd, idhdl h, idhdl rhdl);
33
34/* ====================================================================== */
35si_link_extension si_link_root=NULL;
36
37BOOLEAN slInit(si_link l, char *istr)
38{
39  char *type = NULL, *mode = NULL, *name = NULL;
40  int i = 0, j;
41 
42  // set mode and type
43  if (istr != NULL)
44  {
45    // find the first colon char in istr
46    i = 0;
47    while (istr[i] != ':' && istr[i] != '\0') i++;
48    if (istr[i] == ':')
49    {
50      // if found, set type
51      if (i > 0)
52      {
53        istr[i] = '\0';
54        type = mstrdup(istr);
55        istr[i] = ':';
56      }
57      // and check for mode
58      j = ++i;
59      while (istr[j] != ' ' && istr[j] != '\0') j++;
60      if (j > i)
61      {
62        mode = mstrdup(&(istr[i]));
63        mode[j - i] = '\0';
64      }
65      // and for the name
66      while (istr[j] == ' '&& istr[j] != '\0') j++;
67      if (istr[j] != '\0') name = mstrdup(&(istr[j]));
68    }
69    else // no colon find -- string is entire name
70    {
71      j=0;
72      while (istr[j] == ' '&& istr[j] != '\0') j++;
73      if (istr[j] != '\0') name = mstrdup(&(istr[j]));
74    }
75  }
76
77  // set the link extension
78  if (type != NULL)
79  {
80    si_link_extension s = si_link_root;
81   
82    while (s != NULL && (strcmp(s->type, type) != 0)) s = s->next;
83
84    if (s != NULL)
85      l->m = s;
86    else
87    {
88      Warn("Found unknown link type: %s", type);
89      Warn("Use default link type: %s", si_link_root->type);
90      l->m = si_link_root;
91    }
92    FreeL(type);
93  }
94  else
95    l->m = si_link_root;
96
97  l->name = (name != NULL ? name : mstrdup(""));
98  l->mode = (mode != NULL ? mode : mstrdup(""));
99  l->ref = 1;
100  return FALSE;
101}
102
103void slCleanUp(si_link l)
104{
105  (l->ref)--;
106  if (l->ref == 0)
107  {
108    if (SI_LINK_OPEN_P(l)) slClose(l);
109    FreeL((ADDRESS)l->name);
110    FreeL((ADDRESS)l->mode);
111    memset((void *) l, 0, sizeof(ip_link));
112  }
113}
114
115void slKill(si_link l)
116{
117  slCleanUp(l);
118  if (l->ref == 0)
119    Free((ADDRESS)l, sizeof(ip_link));
120}
121
122char* slStatus(si_link l, char *request)
123{
124  if (l == NULL) return "empty link";
125  else if (l->m == NULL) return "unknown link type";
126  else if (strcmp(request, "type") == 0) return l->m->type;
127  else if (strcmp(request, "mode") == 0) return l->mode;
128  else if (strcmp(request, "name") == 0) return l->name;
129  else if (strcmp(request, "open") == 0)
130  {
131    if (SI_LINK_OPEN_P(l)) return "yes";
132    else return "no";
133  }
134  else if (strcmp(request, "openread") == 0)
135  {
136    if (SI_LINK_R_OPEN_P(l)) return "yes";
137    else return "no";
138  }
139  else if (strcmp(request, "openwrite") == 0)
140  {
141    if (SI_LINK_W_OPEN_P(l)) return "yes";
142    else return "no";
143  }
144  else if (l->m->Status == NULL) return "unknown status request";
145  else return l->m->Status(l, request);
146}
147
148//--------------------------------------------------------------------------
149BOOLEAN slOpen(si_link l, short flag)
150{
151  BOOLEAN res;
152 
153  if (l->m == NULL) slInit(l, "");
154
155  if (SI_LINK_OPEN_P(l))
156  {
157    Warn("open: link of type: %s, mode: %s, name: %s is already open",
158         l->m->type, l->mode, l->name);
159    return FALSE;
160  }
161  else if (l->m->Open != NULL)
162    res = l->m->Open(l, flag);
163  else
164    res = TRUE;
165
166  if (res)
167    Werror("open: Error for link of type: %s, mode: %s, name: %s",
168           l->m->type, l->mode, l->name);
169  return res;
170}
171
172BOOLEAN slClose(si_link l)
173{
174  BOOLEAN res;
175
176  if(! SI_LINK_OPEN_P(l))
177    return FALSE;
178  else if (l->m->Close != NULL)
179    res = l->m->Close(l);
180  else
181    res = TRUE;
182 
183  if (res)
184    Werror("close: Error for link of type: %s, mode: %s, name: %s",
185           l->m->type, l->mode, l->name);
186  return res;
187}
188
189leftv slRead(si_link l, leftv a)
190{
191  leftv v = NULL;
192  if( ! SI_LINK_R_OPEN_P(l)) // open r ?
193  {
194    if (slOpen(l, SI_LINK_READ)) return NULL;
195  }
196
197  if (SI_LINK_R_OPEN_P(l))
198  { // open r
199    if (a==NULL)
200    {
201      if (l->m->Read != NULL) v = l->m->Read(l);
202    }
203    else
204    {
205      if (l->m->Read2 != NULL) v = l->m->Read2(l,a);
206    }
207  }
208  else
209  {
210    Werror("read: Error to open link of type %s, mode: %s, name: %s for reading",
211           l->m->type, l->mode, l->name);
212    return NULL;
213  }
214 
215  // here comes the eval:
216  if (v != NULL)
217    v->Eval();
218  else
219    Werror("read: Error for link of type %s, mode: %s, name: %s",
220           l->m->type, l->mode, l->name);
221  return v;
222}
223
224BOOLEAN slWrite(si_link l, leftv v)
225{
226  BOOLEAN res;
227 
228  if(! SI_LINK_W_OPEN_P(l)) // open w ?
229  {
230    if (slOpen(l, SI_LINK_WRITE)) return TRUE;
231  }
232
233  if(SI_LINK_W_OPEN_P(l))
234  { // now open w
235    if (l->m->Write != NULL)
236      res = l->m->Write(l,v);
237    else
238      res = TRUE;
239
240    if (res)
241      Werror("write: Error for link of type %s, mode: %s, name: %s",
242             l->m->type, l->mode, l->name);
243    return res;
244  }
245  else
246  {
247    Werror("write: Error to open link of type %s, mode: %s, name: %s for writing",
248           l->m->type, l->mode, l->name);
249    return TRUE;
250  }
251}
252
253BOOLEAN slDump(si_link l)
254{
255  BOOLEAN res;
256
257  if(! SI_LINK_W_OPEN_P(l)) // open w ?
258  {
259    if (slOpen(l, SI_LINK_WRITE)) return TRUE;
260  }
261
262  if(SI_LINK_W_OPEN_P(l))
263  { // now open w
264    if (l->m->Dump != NULL)
265      res = l->m->Dump(l);
266    else
267      res = TRUE;
268
269    if (res)
270      Werror("dump: Error for link of type %s, mode: %s, name: %s",
271             l->m->type, l->mode, l->name);
272    return res;
273  }
274  else
275  {
276    Werror("dump: Error to open link of type %s, mode: %s, name: %s for writing",
277           l->m->type, l->mode, l->name);
278    return TRUE;
279  }
280}
281
282BOOLEAN slGetDump(si_link l)
283{
284  BOOLEAN res;
285
286  if(! SI_LINK_R_OPEN_P(l)) // open r ?
287  {
288    if (slOpen(l, SI_LINK_READ)) return TRUE;
289  }
290
291  if(SI_LINK_R_OPEN_P(l))
292  { // now open r
293    if (l->m->GetDump != NULL)
294      res = l->m->GetDump(l);
295    else
296      res = TRUE;
297
298    if (res)
299      Werror("getdump: Error for link of type %s, mode: %s, name: %s",
300             l->m->type, l->mode, l->name);
301    return res;
302  }
303  else
304  {
305    Werror("dump: Error open link of type %s, mode: %s, name: %s for reading",
306           l->m->type, l->mode, l->name);
307    return TRUE;
308  }
309}
310
311
312/* =============== ASCII ============================================= */
313BOOLEAN slOpenAscii(si_link l, short flag)
314{
315  char *mode;
316  if (flag & SI_LINK_OPEN)
317  {
318    if (l->mode[0] != '\0' && (strcmp(l->mode, "r") == 0))
319      flag = SI_LINK_READ;
320    else flag = SI_LINK_WRITE;
321  }
322
323  if (flag == SI_LINK_READ) mode = "r";
324  else if (strcmp(l->mode, "w") == 0) mode = "w";
325  else mode = "a";
326   
327       
328  if (l->name[0] == '\0')
329  {
330    // stdin or stdout
331    if (flag == SI_LINK_READ)
332    {
333      l->data = (void *) stdin;
334      mode = "r";
335    }
336    else
337    {
338      l->data = (void *) stdout;
339      mode = "a";
340    }
341  }
342  else
343  {
344    // normal ascii link to a file
345    FILE *outfile;
346    char *filename=l->name;
347
348    if(filename[0]=='>')
349    {
350      if (filename[1]=='>')
351      {
352        filename+=2;
353        mode = "a";
354      }
355      else
356      {
357        filename++;
358        mode="w";
359      }
360    }
361    outfile=feFopen(filename,mode);
362    if (outfile!=NULL)
363      l->data = (void *) outfile;
364    else
365      return TRUE;
366  }
367
368  FreeL(l->mode);
369  l->mode = mstrdup(mode);
370  SI_LINK_SET_OPEN_P(l, flag);
371  return FALSE;
372}
373
374BOOLEAN slCloseAscii(si_link l)
375{
376  SI_LINK_SET_CLOSE_P(l);
377  if (l->name[0] != '\0')
378  {
379    return (fclose((FILE *)l->data)!=0);
380  }
381  return FALSE;
382}
383
384leftv slReadAscii(si_link l)
385{
386  FILE * fp=(FILE *)l->data;
387  char * buf=NULL;
388  if (fp!=NULL && l->name[0] != '\0')
389  {
390    fseek(fp,0L,SEEK_END);
391    long len=ftell(fp);
392    fseek(fp,0L,SEEK_SET);
393    buf=(char *)AllocL((int)len+1);
394    if (BVERBOSE(V_READING))
395      Print("//Reading %d chars\n",len);
396    fread( buf, len, 1, fp);
397    buf[len]='\0';
398  }
399  else
400  {
401    PrintS("? "); mflush();
402    buf=(char *)AllocL(80);
403    fe_fgets_stdin(buf,80);
404  }
405  leftv v=(leftv)Alloc0(sizeof(sleftv));
406  v->rtyp=STRING_CMD;
407  v->data=buf;
408  return v;
409}
410BOOLEAN slWriteAscii(si_link l, leftv v)
411{
412  FILE *outfile=(FILE *)l->data;
413  BOOLEAN err=FALSE;
414  char *s;
415  while (v!=NULL)
416  {
417    s = v->String();
418    // free v ??
419    if (s!=NULL)
420    {
421      fprintf(outfile,"%s\n",s);
422      FreeL((ADDRESS)s);
423    }
424    else
425    {
426      Werror("cannot convert to string");
427      err=TRUE;
428    }
429    v = v->next;
430  }
431  fflush(outfile);
432  return err;
433}
434
435char* slStatusAscii(si_link l, char* request)
436{
437  if (strcmp(request, "read") == 0)
438  {
439    if (SI_LINK_R_OPEN_P(l)) return "ready";
440    else return "not ready";
441  }
442  else if (strcmp(request, "write") == 0)
443  {
444    if (SI_LINK_W_OPEN_P(l)) return "ready";
445    else return "not ready";
446  }
447  else return "unknown status request";
448}
449
450/*------------------ Dumping in Ascii format -----------------------*/
451
452BOOLEAN slDumpAscii(si_link l)
453{
454  FILE *fd = (FILE *) l->data;
455  idhdl h = idroot, rh = currRingHdl;
456  BOOLEAN status = DumpAscii(fd, h);
457
458  if (! status ) status = DumpAsciiMaps(fd, h, NULL);
459
460  if (currRingHdl != rh) rSetHdl(rh, TRUE);
461  fflush(fd);
462 
463  return status;
464}
465
466// we do that recursively, to dump ids in the the order in which they
467// were actually defined
468static BOOLEAN DumpAscii(FILE *fd, idhdl h)
469{
470  if (h == NULL) return FALSE;
471
472  if (DumpAscii(fd, IDNEXT(h))) return TRUE;
473
474  // need to set the ring before writing it, otherwise we get in
475  // trouble with minpoly
476  if (IDTYP(h) == RING_CMD || IDTYP(h) == QRING_CMD)
477    rSetHdl(h, TRUE);
478
479  if (DumpAsciiIdhdl(fd, h)) return TRUE;
480
481  if (IDTYP(h) == RING_CMD || IDTYP(h) == QRING_CMD)
482    return DumpAscii(fd, IDRING(h)->idroot);
483  else
484    return FALSE;
485}
486
487static BOOLEAN DumpAsciiMaps(FILE *fd, idhdl h, idhdl rhdl)
488{
489  if (h == NULL) return FALSE;
490  if (DumpAsciiMaps(fd, IDNEXT(h), rhdl)) return TRUE;
491
492  if (IDTYP(h) == RING_CMD || IDTYP(h) == QRING_CMD)
493    return DumpAsciiMaps(fd, IDRING(h)->idroot, h);
494  else if (IDTYP(h) == MAP_CMD)
495  {
496    char *rhs;
497    rSetHdl(rhdl, TRUE);
498    rhs = ((leftv) h)->String();
499   
500    if (fprintf(fd, "setring %s;\n", IDID(rhdl)) == EOF) return TRUE;
501    if (fprintf(fd, "%s %s = %s, %s;\n", Tok2Cmdname(MAP_CMD), IDID(h),
502                IDMAP(h)->preimage, rhs) == EOF)
503    {
504      FreeL(rhs);
505      return TRUE;
506    }
507    else
508    {
509      FreeL(rhs);
510      return FALSE;
511    }
512  }
513  else return FALSE;
514}
515 
516static BOOLEAN DumpAsciiIdhdl(FILE *fd, idhdl h)
517{
518  char *type_str = GetIdString(h);
519  idtyp type_id = IDTYP(h);
520
521  // we do not throw an error if a wrong type was attempted to be dumped
522  if (type_str == NULL) return FALSE;
523
524  // handle qrings separately
525  if (type_id == QRING_CMD) return DumpQring(fd, h, type_str);
526 
527  if (type_id == STRING_CMD && strcmp("LIB", IDID(h)) == 0)
528  {
529    if (fprintf(fd, "LIB \"%s\";\n", IDSTRING(h)) == EOF) return TRUE;
530    else return FALSE;
531  }
532                                     
533  // put type and name
534  if (fprintf(fd, "%s %s", type_str, IDID(h)) == EOF) return TRUE;
535
536  // for matricies, append the dimension
537  if (type_id == MATRIX_CMD)
538  {
539    ideal id = IDIDEAL(h);
540    if (fprintf(fd, "[%d][%d]", id->nrows, id->ncols)== EOF) return TRUE;
541  }
542  else if (type_id == INTMAT_CMD)
543  {
544    if (fprintf(fd, "[%d][%d]", IDINTVEC(h)->rows(), IDINTVEC(h)->cols())
545        == EOF) return TRUE;
546  }
547  // write the equal sign
548  if (fprintf(fd, " = ") == EOF) return TRUE;
549
550  // and the right hand side
551  if (DumpRhs(fd, h) == EOF) return TRUE;
552 
553  // semicolon und tschuess
554  if (fprintf(fd, ";\n") == EOF) return TRUE;
555
556  return FALSE;
557}
558
559static char* GetIdString(idhdl h)
560{
561  idtyp type = IDTYP(h);
562 
563  switch(type)
564  {
565      case LIST_CMD:
566      {
567        lists l = IDLIST(h);
568        int i, nl = l->nr + 1;
569        char *name;
570
571        for (i=0; i<nl; i++)
572          if (GetIdString((idhdl) &(l->m[i])) == NULL) return NULL;
573      }
574      case INT_CMD:
575      case INTVEC_CMD:
576      case INTMAT_CMD:
577      case STRING_CMD:
578      case RING_CMD:
579      case QRING_CMD:
580      case PROC_CMD:
581      case NUMBER_CMD:
582      case POLY_CMD:
583      case IDEAL_CMD:
584      case VECTOR_CMD:
585      case MODUL_CMD:
586      case MATRIX_CMD:
587        return Tok2Cmdname(type);
588       
589      case MAP_CMD:
590      case LINK_CMD:
591        return NULL;
592
593      default:
594       Warn("Error dump data of type %s", Tok2Cmdname(IDTYP(h)));
595       return NULL;
596  }
597}
598
599static BOOLEAN DumpQring(FILE *fd, idhdl h, char *type_str)
600{
601  char *ring_str = ((leftv) h)->String();
602  if (fprintf(fd, "%s temp_ring = %s;\n", Tok2Cmdname(RING_CMD), ring_str)
603              == EOF) return TRUE;
604  if (fprintf(fd, "%s temp_ideal = %s;\n", Tok2Cmdname(IDEAL_CMD),
605              iiStringMatrix((matrix) IDRING(h)->qideal, 1))
606      == EOF) return TRUE;
607  if (fprintf(fd, "attrib(temp_ideal, \"isSB\", 1);\n") == EOF) return TRUE;
608  if (fprintf(fd, "%s %s = temp_ideal;\n", type_str, IDID(h)) == EOF)
609    return TRUE;
610  if (fprintf(fd, "kill temp_ring;\n") == EOF) return TRUE;
611  else
612  {
613    FreeL(ring_str);
614    return FALSE;
615  }
616}
617
618 
619static int DumpRhs(FILE *fd, idhdl h)
620{
621  idtyp type_id = IDTYP(h);
622
623  if (type_id == LIST_CMD)
624  {
625    lists l = IDLIST(h);
626    int i, nl = l->nr;
627
628    fprintf(fd, "list(");
629   
630    for (i=0; i<nl; i++)
631    {
632      if (DumpRhs(fd, (idhdl) &(l->m[i])) == EOF) return EOF;
633      fprintf(fd, ",");
634    }
635    if (nl > 0)
636    {
637      if (DumpRhs(fd, (idhdl) &(l->m[nl])) == EOF) return EOF;
638    }
639    fprintf(fd, ")");
640  }
641  else  if (type_id == PROC_CMD || type_id == STRING_CMD)
642  {
643    char *pstr = IDSTRING(h), c;
644    fputc('"', fd);
645    while (*pstr != '\0')
646    {
647      if (*pstr == '"') fputc('\\', fd);
648      fputc(*pstr, fd);
649      pstr++;
650    }
651    fputc('"', fd);
652  }
653  else
654  {
655    char *rhs = ((leftv) h)->String();
656
657    if (rhs == NULL) return EOF;
658
659    if (type_id == INTVEC_CMD) fprintf(fd, "intvec(");
660
661    if (fprintf(fd, "%s", rhs) == EOF) return EOF;
662    FreeL(rhs);
663
664    if ((type_id == RING_CMD || type_id == QRING_CMD) &&
665        IDRING(h)->minpoly != NULL)
666    {
667      StringSetS("");
668      nWrite(IDRING(h)->minpoly);
669      rhs = StringAppend("");
670      if (fprintf(fd, "; minpoly = %s", rhs) == EOF) return EOF;
671    }
672    else if (type_id == INTVEC_CMD) fprintf(fd, ")");
673  }
674  return 1;
675}
676   
677BOOLEAN slGetDumpAscii(si_link l)
678{
679  if (l->name[0] == '\0')
680  {
681    Werror("getdump: Can not get dump from stdin");
682    return TRUE;
683  }
684  else
685  {
686    BOOLEAN status = iiPStart(NULL, l->name, NULL);
687
688    if (status)
689      return TRUE;
690    else
691    {
692      // lets reset the file pointer to the end to reflect that
693      // we are finished with reading
694      FILE *f = (FILE *) l->data;
695      fseek(f, 0L, SEEK_END);
696      return FALSE;
697    }
698  }
699}
700     
701 
702/*------------Initialization at Start-up time------------------------*/
703
704#ifdef HAVE_DBM
705#include "sing_dbm.h"
706#endif
707
708#ifdef HAVE_MPSR
709#include "sing_mp.h"
710#endif 
711
712void slStandardInit()
713{
714  si_link_extension s;
715  si_link_root=(si_link_extension)Alloc0(sizeof(*si_link_root));
716  si_link_root->Open=slOpenAscii;
717  si_link_root->Close=slCloseAscii;
718  si_link_root->Read=slReadAscii;
719  si_link_root->Write=slWriteAscii;
720  si_link_root->Dump=slDumpAscii;
721  si_link_root->GetDump=slGetDumpAscii;
722  si_link_root->Status=slStatusAscii;
723  si_link_root->type="Ascii";
724  s = si_link_root;
725#ifdef HAVE_DBM
726#ifndef HAVE_MODULE_DBM
727  s->next = (si_link_extension)Alloc0(sizeof(*si_link_root));
728  s = s->next;
729  slInitDBMExtension(s);
730#endif
731#endif
732#ifdef HAVE_MPSR
733  s->next = (si_link_extension)Alloc0(sizeof(*si_link_root));
734  s = s->next;
735  slInitMPFileExtension(s);
736  s->next = (si_link_extension)Alloc0(sizeof(*si_link_root));
737  s = s->next;
738  slInitMPTcpExtension(s);
739#endif 
740}
Note: See TracBrowser for help on using the repository browser.