source: git/Singular/links/asciiLink.cc @ 117e00

spielwiese
Last change on this file since 117e00 was 117e00, checked in by Hans Schoenemann <hannes@…>, 6 years ago
removed unused system includes, math.h ->cmath for .cc files
  • Property mode set to 100644
File size: 12.9 KB
Line 
1/****************************************
2 * *  Computer Algebra System SINGULAR     *
3 * ****************************************/
4
5/*
6 * ABSTRACT: ascii links (standard)
7 */
8
9#include "kernel/mod2.h"
10#include "misc/options.h"
11#include "omalloc/omalloc.h"
12
13#include "Singular/tok.h"
14#include "Singular/subexpr.h"
15#include "Singular/ipshell.h"
16#include "Singular/ipid.h"
17#include "Singular/fevoices.h"
18#include "kernel/oswrapper/feread.h"
19#include "Singular/ipshell.h"
20#include "Singular/links/silink.h"
21
22/* declarations */
23static BOOLEAN DumpAscii(FILE *fd, idhdl h,char ***list_of_libs);
24static BOOLEAN DumpAsciiIdhdl(FILE *fd, idhdl h,char ***list_of_libs);
25static const char* GetIdString(idhdl h);
26static int DumpRhs(FILE *fd, idhdl h);
27static BOOLEAN DumpQring(FILE *fd, idhdl h, const char *type_str);
28static BOOLEAN DumpAsciiMaps(FILE *fd, idhdl h, idhdl rhdl);
29static BOOLEAN CollectLibs(char *name, char ***list_of_libs);
30//static BOOLEAN DumpLibs(FILE *fd, char ***list_of_libs);
31
32extern si_link_extension si_link_root;
33
34/* =============== ASCII ============================================= */
35BOOLEAN slOpenAscii(si_link l, short flag, leftv /*h*/)
36{
37  const char *mode;
38  if (flag & SI_LINK_OPEN)
39  {
40    if (l->mode[0] != '\0' && (strcmp(l->mode, "r") == 0))
41      flag = SI_LINK_READ;
42    else flag = SI_LINK_WRITE;
43  }
44
45  if (flag == SI_LINK_READ) mode = "r";
46  else if (strcmp(l->mode, "w") == 0) mode = "w";
47  else mode = "a";
48
49
50  if (l->name[0] == '\0')
51  {
52    // stdin or stdout
53    if (flag == SI_LINK_READ)
54    {
55      l->data = (void *) stdin;
56      mode = "r";
57    }
58    else
59    {
60      l->data = (void *) stdout;
61      mode = "a";
62    }
63  }
64  else
65  {
66    // normal ascii link to a file
67    FILE *outfile;
68    char *filename=l->name;
69
70    if(filename[0]=='>')
71    {
72      if (filename[1]=='>')
73      {
74        filename+=2;
75        mode = "a";
76      }
77      else
78      {
79        filename++;
80        mode="w";
81      }
82    }
83    outfile=myfopen(filename,mode);
84    if (outfile!=NULL)
85      l->data = (void *) outfile;
86    else
87      return TRUE;
88  }
89
90  omFree(l->mode);
91  l->mode = omStrDup(mode);
92  SI_LINK_SET_OPEN_P(l, flag);
93  return FALSE;
94}
95
96BOOLEAN slCloseAscii(si_link l)
97{
98  SI_LINK_SET_CLOSE_P(l);
99  if (l->name[0] != '\0')
100  {
101    return (fclose((FILE *)l->data)!=0);
102  }
103  return FALSE;
104}
105
106leftv slReadAscii2(si_link l, leftv pr)
107{
108  FILE * fp=(FILE *)l->data;
109  char * buf=NULL;
110  if (fp!=NULL && l->name[0] != '\0')
111  {
112    fseek(fp,0L,SEEK_END);
113    long len=ftell(fp);
114    if (len<0) len=0;
115    fseek(fp,0L,SEEK_SET);
116    buf=(char *)omAlloc((int)len+1);
117    if (BVERBOSE(V_READING))
118      Print("//Reading %ld chars\n",len);
119    if (len>0) myfread( buf, len, 1, fp);
120    buf[len]='\0';
121  }
122  else
123  {
124    if (pr->Typ()==STRING_CMD)
125    {
126      buf=(char *)omAlloc(80);
127      fe_fgets_stdin((char *)pr->Data(),buf,80);
128    }
129    else
130    {
131      WerrorS("read(<link>,<string>) expected");
132      buf=omStrDup("");
133    }
134  }
135  leftv v=(leftv)omAlloc0Bin(sleftv_bin);
136  v->rtyp=STRING_CMD;
137  v->data=buf;
138  return v;
139}
140
141leftv slReadAscii(si_link l)
142{
143  sleftv tmp;
144  memset(&tmp,0,sizeof(sleftv));
145  tmp.rtyp=STRING_CMD;
146  tmp.data=(void*) "? ";
147  return slReadAscii2(l,&tmp);
148}
149
150BOOLEAN slWriteAscii(si_link l, leftv v)
151{
152  FILE *outfile=(FILE *)l->data;
153  BOOLEAN err=FALSE;
154  char *s;
155  while (v!=NULL)
156  {
157    switch(v->Typ())
158    {
159    case IDEAL_CMD:
160    case MODUL_CMD:
161    case MATRIX_CMD:
162      {
163        ideal I=(ideal)v->Data();
164        for(int i=0;i<IDELEMS(I);i++)
165        {
166          char *s=pString(I->m[i]);
167          fwrite(s,strlen(s),1,outfile);
168          omFree(s);
169          if (i<IDELEMS(I)-1) fwrite(",",1,1,outfile);
170        }
171        break;
172      }
173    default:
174      s = v->String();
175      // free v ??
176      if (s!=NULL)
177      {
178        fputs(s,outfile);
179        fputc('\n',outfile);
180        omFree((ADDRESS)s);
181      }
182      else
183      {
184        WerrorS("cannot convert to string");
185        err=TRUE;
186      }
187    }
188    v = v->next;
189  }
190  fflush(outfile);
191  return err;
192}
193
194const char* slStatusAscii(si_link l, const char* request)
195{
196  if (strcmp(request, "read") == 0)
197  {
198    if (SI_LINK_R_OPEN_P(l)) return "ready";
199    else return "not ready";
200  }
201  else if (strcmp(request, "write") == 0)
202  {
203    if (SI_LINK_W_OPEN_P(l)) return "ready";
204    else return "not ready";
205  }
206  else return "unknown status request";
207}
208
209/*------------------ Dumping in Ascii format -----------------------*/
210
211BOOLEAN slDumpAscii(si_link l)
212{
213  FILE *fd = (FILE *) l->data;
214  idhdl h = IDROOT, rh = currRingHdl;
215  char **list_of_libs=NULL;
216  BOOLEAN status = DumpAscii(fd, h, &list_of_libs);
217
218  if (! status ) status = DumpAsciiMaps(fd, h, NULL);
219
220  if (currRingHdl != rh) rSetHdl(rh);
221  fprintf(fd, "option(set, intvec(%d, %d));\n", si_opt_1, si_opt_2);
222  char **p=list_of_libs;
223  if (p!=NULL)
224  {
225    while((*p!=NULL) && (*p!=(char*)1))
226    {
227      fprintf(fd,"load(\"%s\",\"try\");\n",*p);
228      p++;
229    }
230    omFree(list_of_libs);
231  }
232  fputs("RETURN();\n",fd);
233  fflush(fd);
234
235  return status;
236}
237
238// we do that recursively, to dump ids in the the order in which they
239// were actually defined
240static BOOLEAN DumpAscii(FILE *fd, idhdl h, char ***list_of_libs)
241{
242  if (h == NULL) return FALSE;
243
244  if (DumpAscii(fd, IDNEXT(h),list_of_libs)) return TRUE;
245
246  // need to set the ring before writing it, otherwise we get in
247  // trouble with minpoly
248  if (IDTYP(h) == RING_CMD)
249    rSetHdl(h);
250
251  if (DumpAsciiIdhdl(fd, h,list_of_libs)) return TRUE;
252
253  if (IDTYP(h) == RING_CMD)
254    return DumpAscii(fd, IDRING(h)->idroot,list_of_libs);
255  else
256    return FALSE;
257}
258
259static BOOLEAN DumpAsciiMaps(FILE *fd, idhdl h, idhdl rhdl)
260{
261  if (h == NULL) return FALSE;
262  if (DumpAsciiMaps(fd, IDNEXT(h), rhdl)) return TRUE;
263
264  if (IDTYP(h) == RING_CMD)
265    return DumpAsciiMaps(fd, IDRING(h)->idroot, h);
266  else if (IDTYP(h) == MAP_CMD)
267  {
268    char *rhs;
269    rSetHdl(rhdl);
270    rhs = h->String();
271
272    if (fprintf(fd, "setring %s;\n", IDID(rhdl)) == EOF) return TRUE;
273    if (fprintf(fd, "%s %s = %s, %s;\n", Tok2Cmdname(MAP_CMD), IDID(h),
274                IDMAP(h)->preimage, rhs) == EOF)
275    {
276      omFree(rhs);
277      return TRUE;
278    }
279    else
280    {
281      omFree(rhs);
282      return FALSE;
283    }
284  }
285  else return FALSE;
286}
287
288static BOOLEAN DumpAsciiIdhdl(FILE *fd, idhdl h, char ***list_of_libs)
289{
290  const char *type_str = GetIdString(h);
291  int type_id = IDTYP(h);
292
293  if (type_id == PACKAGE_CMD)
294  {
295    if (strcmp(IDID(h),"Top")==0) return FALSE; // do not dump "Top"
296    if (IDPACKAGE(h)->language==LANG_SINGULAR) return FALSE;
297  }
298  if (type_id == CRING_CMD)
299  {
300    // do not dump the default CRINGs:
301    if (strcmp(IDID(h),"QQ")==0) return FALSE;
302    if (strcmp(IDID(h),"ZZ")==0) return FALSE;
303    if (strcmp(IDID(h),"AE")==0) return FALSE;
304    if (strcmp(IDID(h),"QAE")==0) return FALSE;
305    if (strcmp(IDID(h),"flint_poly_Q")==0) return FALSE;
306  }
307
308  // we do not throw an error if a wrong type was attempted to be dumped
309  if (type_str == NULL)
310    return FALSE;
311
312  // handle qrings separately
313  if ((type_id == RING_CMD)&&(IDRING(h)->qideal!=NULL))
314    return DumpQring(fd, h, type_str);
315
316  // C-proc not to be dumped
317  if ((type_id == PROC_CMD) && (IDPROC(h)->language == LANG_C))
318    return FALSE;
319
320  // handle libraries
321  if ((type_id == PROC_CMD)
322  && (IDPROC(h)->language == LANG_SINGULAR)
323  && (IDPROC(h)->libname!=NULL))
324    return CollectLibs(IDPROC(h)->libname,list_of_libs);
325
326  // put type and name
327  if (fprintf(fd, "%s %s", type_str, IDID(h)) == EOF)
328    return TRUE;
329  // for matricies, append the dimension
330  if (type_id == MATRIX_CMD)
331  {
332    ideal id = IDIDEAL(h);
333    if (fprintf(fd, "[%d][%d]", id->nrows, id->ncols)== EOF) return TRUE;
334  }
335  else if (type_id == INTMAT_CMD)
336  {
337    if (fprintf(fd, "[%d][%d]", IDINTVEC(h)->rows(), IDINTVEC(h)->cols())
338        == EOF) return TRUE;
339  }
340
341  if (type_id == PACKAGE_CMD)
342  {
343    return (fputs(";\n",fd) == EOF);
344  }
345
346  // write the equal sign
347  if (fputs(" = ",fd) == EOF) return TRUE;
348
349  // and the right hand side
350  if (DumpRhs(fd, h) == EOF) return TRUE;
351
352  // semicolon und tschuess
353  if (fputs(";\n",fd) == EOF) return TRUE;
354
355  return FALSE;
356}
357
358static const char* GetIdString(idhdl h)
359{
360  int type = IDTYP(h);
361
362  switch(type)
363  {
364    case LIST_CMD:
365    {
366      lists l = IDLIST(h);
367      int i, nl = l->nr + 1;
368
369      for (i=0; i<nl; i++)
370        if (GetIdString((idhdl) &(l->m[i])) == NULL) return NULL;
371    }
372    case CRING_CMD:
373    #ifdef SINGULAR_4_2
374    case CNUMBER_CMD:
375    case CMATRIX_CMD:
376    #endif
377    case BIGINT_CMD:
378    case PACKAGE_CMD:
379    case INT_CMD:
380    case INTVEC_CMD:
381    case INTMAT_CMD:
382    case STRING_CMD:
383    case RING_CMD:
384    case QRING_CMD:
385    case PROC_CMD:
386    case NUMBER_CMD:
387    case POLY_CMD:
388    case IDEAL_CMD:
389    case VECTOR_CMD:
390    case MODUL_CMD:
391    case MATRIX_CMD:
392      return Tok2Cmdname(type);
393
394    case MAP_CMD:
395    case LINK_CMD:
396      return NULL;
397
398    default:
399      Warn("Error dump data of type %s", Tok2Cmdname(IDTYP(h)));
400       return NULL;
401  }
402}
403
404static BOOLEAN DumpQring(FILE *fd, idhdl h, const char *type_str)
405{
406  char *ring_str = h->String();
407  if (fprintf(fd, "%s temp_ring = %s;\n", Tok2Cmdname(RING_CMD), ring_str)
408              == EOF) return TRUE;
409  if (fprintf(fd, "%s temp_ideal = %s;\n", Tok2Cmdname(IDEAL_CMD),
410              iiStringMatrix((matrix) IDRING(h)->qideal, 1, currRing, n_GetChar(currRing->cf)))
411      == EOF) return TRUE;
412  if (fputs("attrib(temp_ideal, \"isSB\", 1);\n",fd) == EOF) return TRUE;
413  if (fprintf(fd, "%s %s = temp_ideal;\n", type_str, IDID(h)) == EOF)
414    return TRUE;
415  if (fputs("kill temp_ring;\n",fd) == EOF) return TRUE;
416  else
417  {
418    omFree(ring_str);
419    return FALSE;
420  }
421}
422
423static BOOLEAN CollectLibs(char *name, char *** list_of_libs)
424{
425  if (*list_of_libs==NULL)
426  {
427    #define MAX_LIBS 256
428    (*list_of_libs)=(char**)omalloc0(MAX_LIBS*sizeof(char**));
429    (*list_of_libs)[0]=name;
430    (*list_of_libs)[MAX_LIBS-1]=(char*)1;
431    return FALSE;
432  }
433  else
434  {
435    char **p=*list_of_libs;
436    while (((*p)!=NULL)&&((*p!=(char*)1)))
437    {
438      if (strcmp((*p),name)==0) return FALSE;
439      p++;
440    }
441    if (*p==(char*)1)
442    {
443      WerrorS("too many libs");
444      return TRUE;
445    }
446    else
447    {
448      *p=name;
449    }
450  }
451  return FALSE;
452}
453
454
455static int DumpRhs(FILE *fd, idhdl h)
456{
457  int type_id = IDTYP(h);
458
459  if (type_id == LIST_CMD)
460  {
461    lists l = IDLIST(h);
462    int i, nl = l->nr;
463
464    fputs("list(",fd);
465
466    for (i=0; i<nl; i++)
467    {
468      if (DumpRhs(fd, (idhdl) &(l->m[i])) == EOF) return EOF;
469      fputs(",",fd);
470    }
471    if (nl > 0)
472    {
473      if (DumpRhs(fd, (idhdl) &(l->m[nl])) == EOF) return EOF;
474    }
475    fputs(")",fd);
476  }
477  else  if (type_id == STRING_CMD)
478  {
479    char *pstr = IDSTRING(h);
480    fputc('"', fd);
481    while (*pstr != '\0')
482    {
483      if (*pstr == '"' || *pstr == '\\')  fputc('\\', fd);
484      fputc(*pstr, fd);
485      pstr++;
486    }
487    fputc('"', fd);
488  }
489  else  if (type_id == PROC_CMD)
490  {
491    procinfov pi = IDPROC(h);
492    if (pi->language == LANG_SINGULAR)
493    {
494      /* pi-Libname==NULL */
495      char *pstr = pi->data.s.body;
496      fputc('"', fd);
497      while (*pstr != '\0')
498      {
499        if (*pstr == '"' || *pstr == '\\') fputc('\\', fd);
500        fputc(*pstr, fd);
501        pstr++;
502      }
503      fputc('"', fd);
504    }
505    else fputs("(null)", fd);
506  }
507  else
508  {
509    char *rhs = h->String();
510
511    if (rhs == NULL) return EOF;
512
513    BOOLEAN need_klammer=FALSE;
514    if (type_id == INTVEC_CMD) { fputs("intvec(",fd);need_klammer=TRUE; }
515    else if (type_id == IDEAL_CMD) { fputs("ideal(",fd);need_klammer=TRUE; }
516    else if (type_id == MODUL_CMD) { fputs("module(",fd);need_klammer=TRUE; }
517    else if (type_id == BIGINT_CMD) { fputs("bigint(",fd);need_klammer=TRUE; }
518
519    if (fputs(rhs,fd) == EOF) return EOF;
520    omFree(rhs);
521
522    if ((type_id == RING_CMD) &&
523        IDRING(h)->cf->type==n_algExt)
524    {
525      StringSetS("");
526      p_Write(IDRING(h)->cf->extRing->qideal->m[0],IDRING(h)->cf->extRing);
527      rhs = StringEndS();
528      if (fprintf(fd, "; minpoly = %s", rhs) == EOF) { omFree(rhs); return EOF;}
529      omFree(rhs);
530    }
531    else if (need_klammer) fputc(')',fd);
532  }
533  return 1;
534}
535
536BOOLEAN slGetDumpAscii(si_link l)
537{
538  if (l->name[0] == '\0')
539  {
540    WerrorS("getdump: Can not get dump from stdin");
541    return TRUE;
542  }
543  else
544  {
545    BOOLEAN status = newFile(l->name);
546    if (status)
547      return TRUE;
548
549    int old_echo=si_echo;
550    si_echo=0;
551
552    status=yyparse();
553
554    si_echo=old_echo;
555
556    if (status)
557      return TRUE;
558    else
559    {
560      // lets reset the file pointer to the end to reflect that
561      // we are finished with reading
562      FILE *f = (FILE *) l->data;
563      fseek(f, 0L, SEEK_END);
564      return FALSE;
565    }
566  }
567}
568
569
570void slStandardInit()
571{
572  si_link_extension s;
573  si_link_root=(si_link_extension)omAlloc0Bin(s_si_link_extension_bin);
574  si_link_root->Open=slOpenAscii;
575  si_link_root->Close=slCloseAscii;
576  si_link_root->Kill=NULL;
577  si_link_root->Read=slReadAscii;
578  si_link_root->Read2=slReadAscii2;
579  si_link_root->Write=slWriteAscii;
580  si_link_root->Dump=slDumpAscii;
581  si_link_root->GetDump=slGetDumpAscii;
582  si_link_root->Status=slStatusAscii;
583  si_link_root->type="ASCII";
584  s = si_link_root;
585  s->next = NULL;
586}
Note: See TracBrowser for help on using the repository browser.