source: git/Singular/newstruct.cc @ 5d84a4

spielwiese
Last change on this file since 5d84a4 was 5d84a4, checked in by Hans Schoenemann <hannes@…>, 10 years ago
fix: allow printing of bb-members, if they have a short representation
  • Property mode set to 100644
File size: 18.7 KB
RevLine 
[5c3bc3]1#include <ctype.h>
2
[16f511]3#ifdef HAVE_CONFIG_H
[ba5e9e]4#include "singularconfig.h"
[16f511]5#endif /* HAVE_CONFIG_H */
[f5b40a]6#include <kernel/mod2.h>
[5c3bc3]7#include <Singular/ipid.h>
8#include <Singular/blackbox.h>
9#include <Singular/lists.h>
10#include <Singular/ipid.h>
11#include <Singular/ipshell.h>
12#include <Singular/newstruct.h>
13
14struct newstruct_member_s;
15typedef struct newstruct_member_s *newstruct_member;
16struct  newstruct_member_s
17{
18  newstruct_member next;
19  char *         name;
20  int            typ;
21  int            pos;
22};
23
[06c0b3]24struct newstruct_proc_s;
25typedef struct newstruct_proc_a *newstruct_proc;
26struct  newstruct_proc_a
27{
28  newstruct_proc next;
29  int            t; /*tok id */
30  int            args; /* number of args */
31  procinfov      p;
32};
33
[5c3bc3]34struct newstruct_desc_s
35{
36  newstruct_member member;
[64cf44]37  newstruct_desc   parent;
[06c0b3]38  newstruct_proc   procs;
[5c3bc3]39  int            size; // number of mebers +1
[64cf44]40  int            id;   // the type id assigned to this bb
[5c3bc3]41};
42
[8357e21]43int newstruct_desc_size()
[b2aa08]44{
45  return sizeof(newstruct_desc_s);
46}
[5c3bc3]47
48char * newstruct_String(blackbox *b, void *d)
49{
50  if (d==NULL) return omStrDup("oo");
51  else
52  {
[7f581e]53    newstruct_desc ad=(newstruct_desc)(b->data);
[3877fe]54
55    newstruct_proc p=ad->procs;
56    while((p!=NULL)&&(p->t!=STRING_CMD))
57      p=p->next;
58
59    if (p!=NULL)
60    {
[5edb77]61      BOOLEAN sl;
[3877fe]62      sleftv tmp;
63      memset(&tmp,0,sizeof(tmp));
64      tmp.rtyp=ad->id;
65      void * newstruct_Copy(blackbox*, void *); //forward declaration
66      tmp.data=(void*)newstruct_Copy(b,d);
67      idrec hh;
68      memset(&hh,0,sizeof(hh));
69      hh.id=Tok2Cmdname(p->t);
70      hh.typ=PROC_CMD;
71      hh.data.pinf=p->p;
72      sl=iiMake_proc(&hh,NULL,&tmp);
73
[e3b071]74      if ((!sl)&& (iiRETURNEXPR.Typ() == STRING_CMD))
[3877fe]75      {
[e3b071]76        char *res = omStrDup((char*)iiRETURNEXPR.CopyD());
[5edb77]77        iiRETURNEXPR.CleanUp();
[f92a39]78        iiRETURNEXPR.Init();
[3877fe]79        return res;
80      }
[5edb77]81      iiRETURNEXPR.CleanUp();
[f92a39]82      iiRETURNEXPR.Init();
[3877fe]83    }
84
[5c3bc3]85    lists l=(lists)d;
86    newstruct_member a=ad->member;
87    StringSetS("");
88    loop
89    {
90      StringAppendS(a->name);
[538512]91      StringAppendS("=");
[5c3bc3]92      if ((!RingDependend(a->typ))
93      || ((l->m[a->pos-1].data==(void *)currRing)
94         && (currRing!=NULL)))
95      {
[64cf44]96        if (l->m[a->pos].rtyp==LIST_CMD)
97        {
98          StringAppendS("<list>");
99        }
[5d84a4]100        else
[64cf44]101        {
102          char *tmp2=omStrDup(l->m[a->pos].String());
[a8f29f]103          if ((strlen(tmp2)>80)||(strchr(tmp2,'\n')!=NULL))
104          {
[73c359d]105            StringAppendS("<");
106            StringAppendS(Tok2Cmdname(l->m[a->pos].rtyp));
107            StringAppendS(">");
[a8f29f]108          }
109          else StringAppendS(tmp2);
[64cf44]110          omFree(tmp2);
111        }
[5c3bc3]112      }
113      else StringAppendS("??");
114      if (a->next==NULL) break;
115      StringAppendS("\n");
116      if(errorreported) break;
117      a=a->next;
118    }
[538512]119    return StringEndS();
[5c3bc3]120  }
121}
[a2faa3]122lists lCopy_newstruct(lists L)
123{
124  lists N=(lists)omAlloc0Bin(slists_bin);
125  int n=L->nr;
126  ring save_ring=currRing;
127  N->Init(n+1);
128  for(;n>=0;n--)
129  {
[5e147a]130    if (RingDependend(L->m[n].rtyp))
[a2faa3]131    {
[5e147a]132      assume((L->m[n-1].rtyp==RING_CMD) || (L->m[n-1].data==NULL));
133      if(L->m[n-1].data!=NULL)
134      {
135        if (L->m[n-1].data!=(void*)currRing)
136          rChangeCurrRing((ring)(L->m[n-1].data));
137        N->m[n].Copy(&L->m[n]);
138      }
139      else
140      {
141        N->m[n].rtyp=L->m[n].rtyp;
142        N->m[n].data=idrecDataInit(L->m[n].rtyp);
143      }
144    }
[06c0b3]145    else if(L->m[n].rtyp==LIST_CMD)
[5e147a]146    {
147      N->m[n].rtyp=L->m[n].rtyp;
[06c0b3]148      N->m[n].data=(void *)lCopy((lists)(L->m[n].data));
[a2faa3]149    }
[06c0b3]150    else if(L->m[n].rtyp>MAX_TOK)
[46eef0]151    {
152      N->m[n].rtyp=L->m[n].rtyp;
[06c0b3]153      blackbox *b=getBlackboxStuff(N->m[n].rtyp);
154      N->m[n].data=(void *)b->blackbox_Copy(b,L->m[n].data);
[46eef0]155    }
[a2faa3]156    else
157      N->m[n].Copy(&L->m[n]);
158  }
159  if (currRing!=save_ring) rChangeCurrRing(save_ring);
160  return N;
161}
[06c0b3]162void * newstruct_Copy(blackbox*, void *d)
[5c3bc3]163{
164  lists n1=(lists)d;
[a2faa3]165  return (void*)lCopy_newstruct(n1);
[5c3bc3]166}
167
[a04a05]168// Used by newstruct_Assign for overloaded '='
169BOOLEAN newstruct_equal(int op, leftv l, leftv r)
170{
171  blackbox *ll=getBlackboxStuff(op);
172  assume(ll->data != NULL);
173  newstruct_desc nt=(newstruct_desc)ll->data;
174  newstruct_proc p=nt->procs;
[f92a39]175
[a04a05]176  while( (p!=NULL) && ((p->t!='=')||(p->args!=1)) ) p=p->next;
177
178  if (p!=NULL)
179  {
[5edb77]180    BOOLEAN sl;
[a04a05]181    idrec hh;
182    memset(&hh,0,sizeof(hh));
183    hh.id=Tok2Cmdname(p->t);
184    hh.typ=PROC_CMD;
185    hh.data.pinf=p->p;
186    sleftv tmp;
187    memset(&tmp,0,sizeof(sleftv));
188    tmp.Copy(r);
[5edb77]189    sl = iiMake_proc(&hh, NULL, &tmp);
190    if (!sl)
[a04a05]191    {
[5edb77]192      if (iiRETURNEXPR.Typ() == op)
[f92a39]193      {
[5edb77]194        l->Copy(&iiRETURNEXPR);
[f92a39]195        iiRETURNEXPR.Init();
196        return FALSE;
197      }
[5edb77]198      iiRETURNEXPR.CleanUp();
[f92a39]199      iiRETURNEXPR.Init();
[a04a05]200    }
201  }
202  return TRUE;
203}
204
[bf7dfc]205void lClean_newstruct(lists l)
206{
207  if (l->nr>=0)
208  {
209    int i;
210    ring r=NULL;
211    for(i=l->nr;i>=0;i--)
212    {
213      if ((i>0) && (l->m[i-1].rtyp==RING_CMD))
214        r=(ring)(l->m[i-1].data);
215      else
216        r=NULL;
217      l->m[i].CleanUp(r);
218    }
219    omFreeSize((ADDRESS)l->m, (l->nr+1)*sizeof(sleftv));
220    l->nr=-1;
221  }
222  omFreeBin((ADDRESS)l,slists_bin);
223}
224
[5c3bc3]225BOOLEAN newstruct_Assign(leftv l, leftv r)
226{
227  if (r->Typ()>MAX_TOK)
228  {
229    blackbox *rr=getBlackboxStuff(r->Typ());
[64cf44]230    if (l->Typ()!=r->Typ())
231    {
232      newstruct_desc rrn=(newstruct_desc)rr->data;
[90707f]233
234      if (!rrn)
235      {
[3d69257]236        Werror("custom type %s(%d) cannot be assigned to newstruct %s(%d)",
[90707f]237               Tok2Cmdname(r->Typ()), r->Typ(), Tok2Cmdname(l->Typ()), l->Typ());
238        return TRUE;
239      }
240
[64cf44]241      newstruct_desc rrp=rrn->parent;
242      while ((rrp!=NULL)&&(rrp->id!=l->Typ())) rrp=rrp->parent;
243      if (rrp!=NULL)
244      {
245        if (l->rtyp==IDHDL)
246        {
247          IDTYP((idhdl)l->data)=r->Typ();
248        }
249        else
250        {
251          l->rtyp=r->Typ();
252        }
253      }
[ed47aab]254      else                      // unrelated types - look for custom conversion
255      {
256        sleftv tmp;
257        BOOLEAN newstruct_Op1(int, leftv, leftv);  // forward declaration
258        if (! newstruct_Op1(l->Typ(), &tmp, r))  return newstruct_Assign(l, &tmp);
259      }
[64cf44]260    }
[5c3bc3]261    if (l->Typ()==r->Typ())
262    {
263      if (l->Data()!=NULL)
264      {
265        lists n1=(lists)l->Data();
[bf7dfc]266        lClean_newstruct(n1);
[5c3bc3]267      }
[a2faa3]268      lists n2=(lists)r->Data();
269      n2=lCopy_newstruct(n2);
[5c3bc3]270      if (l->rtyp==IDHDL)
271      {
272        IDDATA((idhdl)l->data)=(char *)n2;
273      }
274      else
275      {
276        l->data=(void *)n2;
277      }
278      return FALSE;
279    }
280  }
[7b156fb]281
[5edb77]282  else
[7b156fb]283  {
[a04a05]284    assume(l->Typ() > MAX_TOK);
285    sleftv tmp;
286    if(!newstruct_equal(l->Typ(), &tmp, r)) return newstruct_Assign(l, &tmp);
[7b156fb]287  }
[5c3bc3]288  Werror("assign %s(%d) = %s(%d)",
289        Tok2Cmdname(l->Typ()),l->Typ(),Tok2Cmdname(r->Typ()),r->Typ());
290  return TRUE;
291}
292
[0a64d50]293BOOLEAN newstruct_Op1(int op, leftv res, leftv arg)
294{
295  // interpreter: arg is newstruct
296  blackbox *a=getBlackboxStuff(arg->Typ());
297  newstruct_desc nt=(newstruct_desc)a->data;
298  newstruct_proc p=nt->procs;
299
300  while((p!=NULL) &&( (p->t!=op) || (p->args!=1) )) p=p->next;
301
302  if (p!=NULL)
303  {
[5edb77]304    BOOLEAN sl;
[0a64d50]305    sleftv tmp;
306    memset(&tmp,0,sizeof(sleftv));
307    tmp.Copy(arg);
308    idrec hh;
309    memset(&hh,0,sizeof(hh));
310    hh.id=Tok2Cmdname(p->t);
311    hh.typ=PROC_CMD;
312    hh.data.pinf=p->p;
313    sl=iiMake_proc(&hh,NULL,&tmp);
[5edb77]314    if (sl) return TRUE;
[0a64d50]315    else
316    {
[5edb77]317      res->Copy(&iiRETURNEXPR);
[f92a39]318      iiRETURNEXPR.Init();
[0a64d50]319      return FALSE;
320    }
321  }
322  return blackboxDefaultOp1(op,res,arg);
323}
324
325
326
[5c3bc3]327BOOLEAN newstruct_Op2(int op, leftv res, leftv a1, leftv a2)
328{
[06c0b3]329  // interpreter: a1 or a2 is newstruct
[5c3bc3]330  blackbox *a=getBlackboxStuff(a1->Typ());
[06c0b3]331  newstruct_desc nt;
[5c3bc3]332  lists al=(lists)a1->Data();
[06c0b3]333  if (a!=NULL)
[5c3bc3]334  {
[06c0b3]335    nt=(newstruct_desc)a->data;
336    switch(op)
[5c3bc3]337    {
[06c0b3]338      case '.':
[5c3bc3]339      {
[06c0b3]340        if (a2->name!=NULL)
[5c3bc3]341        {
[06c0b3]342          BOOLEAN search_ring=FALSE;
343          newstruct_member nm=nt->member;
344          while ((nm!=NULL)&&(strcmp(nm->name,a2->name)!=0)) nm=nm->next;
345          if ((nm==NULL) && (strncmp(a2->name,"r_",2)==0))
[5e147a]346          {
[06c0b3]347            nm=nt->member;
348            while ((nm!=NULL)&&(strcmp(nm->name,a2->name+2)!=0)) nm=nm->next;
349            if ((nm!=NULL)&&(RingDependend(nm->typ)))
350              search_ring=TRUE;
351            else
352              nm=NULL;
[5e147a]353          }
[06c0b3]354          if (nm==NULL)
355          {
356            Werror("member %s nor found", a2->name);
357            return TRUE;
358          }
359          if (search_ring)
[5c3bc3]360          {
[06c0b3]361            ring r;
362            res->rtyp=RING_CMD;
363            res->data=al->m[nm->pos-1].data;
364            r=(ring)res->data;
365            if (r==NULL) { res->data=(void *)currRing; r=currRing; }
366            if (r!=NULL) r->ref++;
367            else Werror("ring of this member is not set and no basering found");
368            return r==NULL;
369          }
370          else if (RingDependend(nm->typ))
371          {
372            if (al->m[nm->pos].data==NULL)
373            {
374              // NULL belongs to any ring
375              ring r=(ring)al->m[nm->pos-1].data;
376              if (r!=NULL)
377              {
378                r->ref--;
379                al->m[nm->pos-1].data=NULL;
380                al->m[nm->pos-1].rtyp=DEF_CMD;
381              }
382            }
383            else
384            {
385              //Print("checking ring at pos %d for dat at pos %d\n",nm->pos-1,nm->pos);
386              if ((al->m[nm->pos-1].data!=(void *)currRing)
387              &&(al->m[nm->pos-1].data!=(void*)0L))
388              {
389                Werror("different ring %lx(data) - %lx(basering)",
390                  (long unsigned)(al->m[nm->pos-1].data),(long unsigned)currRing);
391                return TRUE;
392              }
393            }
394            if ((currRing!=NULL)&&(al->m[nm->pos-1].data==NULL))
[5c3bc3]395            {
[06c0b3]396              // remember the ring, if not already set
397              al->m[nm->pos-1].data=(void *)currRing;
398              al->m[nm->pos-1].rtyp=RING_CMD;
399              currRing->ref++;
[5c3bc3]400            }
401          }
[06c0b3]402          Subexpr r=(Subexpr)omAlloc0Bin(sSubexpr_bin);
403          r->start = nm->pos+1;
404          memcpy(res,a1,sizeof(sleftv));
405          memset(a1,0,sizeof(sleftv));
406          if (res->e==NULL) res->e=r;
407          else
[5e147a]408          {
[06c0b3]409            Subexpr sh=res->e;
410            while (sh->next != NULL) sh=sh->next;
411            sh->next=r;
[5e147a]412          }
[06c0b3]413          return FALSE;
[5c3bc3]414        }
415        else
416        {
[06c0b3]417          WerrorS("name expected");
418          return TRUE;
[5c3bc3]419        }
420      }
421    }
422  }
[06c0b3]423  else
424  {
425    a=getBlackboxStuff(a2->Typ());
426    nt=(newstruct_desc)a->data;
427    al=(lists)a2->Data();
428  }
429  newstruct_proc p=nt->procs;
[2262ab]430  while((p!=NULL) && ( (p->t!=op) || (p->args!=2) )) p=p->next;
[06c0b3]431  if (p!=NULL)
432  {
[5edb77]433    BOOLEAN sl;
[06c0b3]434    sleftv tmp;
435    memset(&tmp,0,sizeof(sleftv));
436    tmp.Copy(a1);
437    tmp.next=(leftv)omAlloc0(sizeof(sleftv));
438    tmp.next->Copy(a2);
439    idrec hh;
440    memset(&hh,0,sizeof(hh));
441    hh.id=Tok2Cmdname(p->t);
442    hh.typ=PROC_CMD;
443    hh.data.pinf=p->p;
444    sl=iiMake_proc(&hh,NULL,&tmp);
[5edb77]445    if (sl) return TRUE;
[06c0b3]446    else
447    {
[5edb77]448      res->Copy(&iiRETURNEXPR);
[f92a39]449      iiRETURNEXPR.Init();
[06c0b3]450      return FALSE;
451    }
452  }
[5c3bc3]453  return blackboxDefaultOp2(op,res,a1,a2);
454}
[a2faa3]455
[5c3bc3]456// BOOLEAN opM(int op, leftv res, leftv args)
457BOOLEAN newstruct_OpM(int op, leftv res, leftv args)
458{
459  // interpreter: args->1. arg is newstruct
460  blackbox *a=getBlackboxStuff(args->Typ());
[06c0b3]461  newstruct_desc nt=(newstruct_desc)a->data;
[5c3bc3]462  switch(op)
463  {
464    case STRING_CMD:
465    {
466      res->data=(void *)a->blackbox_String(a,args->Data());
467      res->rtyp=STRING_CMD;
468      return FALSE;
469    }
470    default:
471      break;
472  }
[06c0b3]473  newstruct_proc p=nt->procs;
[8357e21]474
[2262ab]475  while((p!=NULL) &&( (p->t!=op) || (p->args!=4) )) p=p->next;
476
[06c0b3]477  if (p!=NULL)
478  {
[5edb77]479    BOOLEAN sl;
[06c0b3]480    sleftv tmp;
481    memset(&tmp,0,sizeof(sleftv));
482    tmp.Copy(args);
483    idrec hh;
484    memset(&hh,0,sizeof(hh));
485    hh.id=Tok2Cmdname(p->t);
486    hh.typ=PROC_CMD;
487    hh.data.pinf=p->p;
488    sl=iiMake_proc(&hh,NULL,&tmp);
[5edb77]489    if (sl) return TRUE;
[06c0b3]490    else
491    {
[5edb77]492      res->Copy(&iiRETURNEXPR);
[f92a39]493      iiRETURNEXPR.Init();
[06c0b3]494      return FALSE;
495    }
496  }
497  return blackbox_default_OpM(op,res,args);
498}
499
[2e4ec14]500void newstruct_destroy(blackbox */*b*/, void *d)
[5c3bc3]501{
502  if (d!=NULL)
503  {
504    lists n=(lists)d;
[06c0b3]505    lClean_newstruct(n);
[5c3bc3]506  }
507}
508
509void *newstruct_Init(blackbox *b)
510{
511  newstruct_desc n=(newstruct_desc)b->data;
512  lists l=(lists)omAlloc0Bin(slists_bin);
513  l->Init(n->size);
514  newstruct_member nm=n->member;
515  while (nm!=NULL)
516  {
517    l->m[nm->pos].rtyp=nm->typ;
518    l->m[nm->pos].data=idrecDataInit(nm->typ);
519    nm=nm->next;
520  }
521  return l;
522}
523
[2e4ec14]524BOOLEAN newstruct_CheckAssign(blackbox */*b*/, leftv L, leftv R)
[8357e21]525{
526  int lt=L->Typ();
527  int rt=R->Typ();
528  if ((lt!=DEF_CMD)&&(lt!=rt))
529  {
530    Werror("can not assign %s(%d) to member of type %s(%d)",
531            Tok2Cmdname(rt),rt,
532            Tok2Cmdname(lt),lt);
533    return TRUE;
534  }
535  return FALSE;
536}
537
538/* check internal structure:
539* BOOLEAN newstruct_Check(blackbox *b, void *d)
[5c3bc3]540{
541  newstruct_desc n=(newstruct_desc)b->data;
542  lists l=(lists)d;
543  newstruct_member nm=n->member;
544  while (nm!=NULL)
545  {
546    if ((l->m[nm->pos].rtyp!=nm->typ)
547    &&( nm->typ!=DEF_CMD))
548    {
549      Werror("type change in member %s (%s(%d) -> %s(%d))",nm->name,
550          Tok2Cmdname(nm->typ),nm->typ,
[5e147a]551          Tok2Cmdname(l->m[nm->pos].rtyp),l->m[nm->pos].rtyp);
[5c3bc3]552      return TRUE;
553    }
554    nm=nm->next;
555  }
556  return FALSE;
557}
[8357e21]558*/
[a2faa3]559
[f84c3b]560BOOLEAN newstruct_serialize(blackbox *b, void *d, si_link f)
561{
562  newstruct_desc dd=(newstruct_desc)b->data;
[a088a12]563  sleftv l;
564  memset(&l,0,sizeof(l));
565  l.rtyp=STRING_CMD;
566  l.data=(void*)getBlackboxName(dd->id);
567  f->m->Write(f, &l);
[f84c3b]568  lists ll=(lists)d;
[a088a12]569  memset(&l,0,sizeof(l));
570  l.rtyp=LIST_CMD;
571  l.data=ll;
572  f->m->Write(f, &l);
[f84c3b]573  return FALSE;
574}
575
[2e4ec14]576BOOLEAN newstruct_deserialize(blackbox **/*b*/, void **d, si_link f)
[f84c3b]577{
[a088a12]578  // newstruct is serialiazed as a list,
579  // just read a list and take data,
580  // rtyp must be set correctly (to the blackbox id) by routine calling
581  // newstruct_deserialize
582  leftv l=f->m->Read(f);
583  //newstruct_desc n=(newstruct_desc)b->data;
584  //TODO: check compatibility of list l->data with description in n
585  *d=l->data;
586  return FALSE;
[f84c3b]587}
588
[06c0b3]589void newstruct_Print(blackbox *b,void *d)
590{
591  newstruct_desc dd=(newstruct_desc)b->data;
592  newstruct_proc p=dd->procs;
593  while((p!=NULL)&&(p->t!=PRINT_CMD))
594    p=p->next;
595  if (p!=NULL)
596  {
[5edb77]597    BOOLEAN sl;
[06c0b3]598    sleftv tmp;
599    memset(&tmp,0,sizeof(tmp));
600    tmp.rtyp=dd->id;
601    tmp.data=(void*)newstruct_Copy(b,d);
602    idrec hh;
603    memset(&hh,0,sizeof(hh));
604    hh.id=Tok2Cmdname(p->t);
605    hh.typ=PROC_CMD;
606    hh.data.pinf=p->p;
607    sl=iiMake_proc(&hh,NULL,&tmp);
[5edb77]608    if (!sl) iiRETURNEXPR.CleanUp();
[f92a39]609    iiRETURNEXPR.Init();
[06c0b3]610  }
611  else
612    blackbox_default_Print(b,d);
613}
[5c3bc3]614void newstruct_setup(const char *n, newstruct_desc d )
615{
616  blackbox *b=(blackbox*)omAlloc0(sizeof(blackbox));
617  // all undefined entries will be set to default in setBlackboxStuff
[06c0b3]618  // the default Print is quite useful,
[5c3bc3]619  // all other are simply error messages
620  b->blackbox_destroy=newstruct_destroy;
621  b->blackbox_String=newstruct_String;
[06c0b3]622  b->blackbox_Print=newstruct_Print;//blackbox_default_Print;
[5c3bc3]623  b->blackbox_Init=newstruct_Init;
624  b->blackbox_Copy=newstruct_Copy;
625  b->blackbox_Assign=newstruct_Assign;
[0a64d50]626  b->blackbox_Op1=newstruct_Op1;
[5c3bc3]627  b->blackbox_Op2=newstruct_Op2;
628  //b->blackbox_Op3=blackbox_default_Op3;
629  b->blackbox_OpM=newstruct_OpM;
[8357e21]630  b->blackbox_CheckAssign=newstruct_CheckAssign;
[f84c3b]631  b->blackbox_serialize=newstruct_serialize;
632  b->blackbox_deserialize=newstruct_deserialize;
[5c3bc3]633  b->data=d;
634  b->properties=1; // list_like
635  int rt=setBlackboxStuff(b,n);
[64cf44]636  d->id=rt;
[114346]637  //Print("create type %d (%s)\n",rt,n);
[5c3bc3]638}
639
[64cf44]640static newstruct_desc scanNewstructFromString(const char *s, newstruct_desc res)
[5c3bc3]641{
642  char *ss=omStrDup(s);
643  char *p=ss;
644  char *start;
645  int t;
646  char c;
647  newstruct_member elem;
[64cf44]648
[5c3bc3]649  idhdl save_ring=currRingHdl;
650  currRingHdl=(idhdl)1; // fake ring detection
651  loop
652  {
653    // read type:
654    while (*p==' ') p++;
655    start=p;
656    while (isalpha(*p)) p++;
657    *p='\0';
658    IsCmd(start,t);
659    if (t==0)
660    {
661      Werror("unknown type `%s`",start);
662      omFree(ss);
663      omFree(res);
664      currRingHdl=save_ring;
665      return NULL;
666    }
667    if (RingDependend(t))
668      res->size++;    // one additional field for the ring (before the data)
669    //Print("found type %s at real-pos %d",start,res->size);
670    elem=(newstruct_member)omAlloc0(sizeof(*elem));
671    // read name:
672    p++;
673    while (*p==' ') p++;
674    start=p;
675    while (isalpha(*p)) p++;
676    c=*p;
677    *p='\0';
678    elem->typ=t;
679    elem->pos=res->size;
680    if (*start=='\0') /*empty name*/
681    {
682      WerrorS("empty name for element");
[4878eb]683      goto error_in_newstruct_def;
[5c3bc3]684    }
685    elem->name=omStrDup(start);
686    //Print(" name:%s\n",start);
687    elem->next=res->member;
688    res->member=elem;
689    res->size++;
690
691    // next ?
692    *p=c;
693    while (*p==' ') p++;
[eb5526e]694    if (*p!=',')
[4878eb]695    {
[eb5526e]696      if (*p!='\0')
697      {
698        Werror("unknown character in newstruct:>>%s<<",p);
699        goto error_in_newstruct_def;
700      }
701      break; // end-of-list
[4878eb]702    }
[5c3bc3]703    p++;
704  }
705  omFree(ss);
706  currRingHdl=save_ring;
[64cf44]707  //Print("new type with %d elements\n",res->size);
[5c3bc3]708  return res;
[4878eb]709error_in_newstruct_def:
710   omFree(elem);
711   omFree(ss);
712   omFree(res);
713   currRingHdl=save_ring;
714   return NULL;
[5c3bc3]715}
[eff324]716newstruct_desc newstructFromString(const char *s)
[64cf44]717{
718  newstruct_desc res=(newstruct_desc)omAlloc0(sizeof(*res));
719  res->size=0;
720
[eff324]721  return scanNewstructFromString(s,res);
[64cf44]722}
723newstruct_desc newstructChildFromString(const char *parent, const char *s)
[a2faa3]724{
[64cf44]725  // find parent:
726  int parent_id=0;
727  blackboxIsCmd(parent,parent_id);
728  if (parent_id<MAX_TOK)
729  {
730    Werror(">>%s< not found",parent);
731    return NULL;
732  }
733  blackbox *parent_bb=getBlackboxStuff(parent_id);
734  // check for the correct type:
735  if (parent_bb->blackbox_destroy!=newstruct_destroy)
736  {
737    Werror(">>%s< is not a user defined type",parent);
738    return NULL;
739  }
740  // setup for scanNewstructFromString:
741  newstruct_desc res=(newstruct_desc)omAlloc0(sizeof(*res));
742  newstruct_desc parent_desc=(newstruct_desc)parent_bb->data;
743  res->size=parent_desc->size;
744  res->member=parent_desc->member;
745  res->parent=parent_desc;
746
747  return scanNewstructFromString(s,res);
[a2faa3]748}
[06c0b3]749void newstructShow(newstruct_desc d)
750{
751  newstruct_member elem;
752  Print("id: %d\n",d->id);
753  elem=d->member;
754  while (elem!=NULL)
755  {
756    Print(">>%s<< at pos %d, type %d\n",elem->name,elem->pos,elem->typ);
757    elem=elem->next;
758  }
759}
760
761BOOLEAN newstruct_set_proc(const char *bbname,const char *func, int args,procinfov pr)
762{
763  int id=0;
764  blackboxIsCmd(bbname,id);
765  blackbox *bb=getBlackboxStuff(id);
766  newstruct_desc desc=(newstruct_desc)bb->data;
767  newstruct_proc p=(newstruct_proc)omAlloc(sizeof(*p));
768  p->next=desc->procs; desc->procs=p;
[632c3a]769
770  idhdl save_ring=currRingHdl;
771  currRingHdl=(idhdl)1; // fake ring detection
772
[06c0b3]773  if(!IsCmd(func,p->t))
774  {
775    int t=0;
776    if (func[1]=='\0') p->t=func[0];
777    else if((t=iiOpsTwoChar(func))!=0)
778    {
779      p->t=t;
780    }
781    else
782    {
783      Werror(">>%s<< is not a kernel command",func);
[632c3a]784      currRingHdl = save_ring;
[06c0b3]785      return TRUE;
786    }
787  }
788  p->args=args;
789  p->p=pr; pr->ref++;
[632c3a]790  currRingHdl = save_ring;
[06c0b3]791  return FALSE;
792}
Note: See TracBrowser for help on using the repository browser.