source: git/Singular/attrib.cc @ bcb8f3

spielwiese
Last change on this file since bcb8f3 was bcb8f3, checked in by Hans Schoenemann <hannes@…>, 10 years ago
avoid assume failure (attr_free -> s_internaldelete)
  • Property mode set to 100644
File size: 9.2 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4
5/*
6* ABSTRACT: attributes to leftv and idhdl
7*/
8
9#include <stdlib.h>
10#include <stdio.h>
11#include <string.h>
12#include <ctype.h>
13#include <unistd.h>
14
15#ifdef HAVE_CONFIG_H
16#include "singularconfig.h"
17#endif /* HAVE_CONFIG_H */
18#include <kernel/mod2.h>
19#include <omalloc/omalloc.h>
20#include <misc/options.h>
21#include <Singular/tok.h>
22#include <Singular/ipid.h>
23#include <misc/intvec.h>
24#include <kernel/polys.h>
25#include <kernel/ideals.h>
26#include <polys/matpol.h>
27#include <Singular/ipshell.h>
28#include <Singular/attrib.h>
29
30static omBin sattr_bin = omGetSpecBin(sizeof(sattr));
31
32void sattr::Print()
33{
34  omCheckAddrSize(this,sizeof(sattr));
35  ::Print("attr:%s, type %s \n",name,Tok2Cmdname(atyp));
36  if (next!=NULL) next->Print();
37}
38
39attr sattr::Copy()
40{
41  if (this!=NULL)
42  {
43    omCheckAddrSize(this,sizeof(sattr));
44    attr n=(attr)omAlloc0Bin(sattr_bin);
45    n->atyp=atyp;
46    if (name!=NULL) n->name=omStrDup(name);
47    n->data=CopyA();
48    if (next!=NULL)
49    {
50      n->next=next->Copy();
51    }
52    return n;
53  }
54  else
55    return NULL;
56}
57
58// in subexr.cc:
59//void * sattr::CopyA()
60//{
61//  omCheckAddrSize(this,sizeof(sattr));
62//  return s_internalCopy(atyp,data);
63//}
64
65static void attr_free(attr h, const ring r=currRing)
66{
67  if (h->data!=NULL) /*avoid assume failure */
68  {
69    s_internalDelete(h->atyp,h->data,r);
70    h->data=NULL;
71  }
72}
73
74attr sattr::set(const char * s, void * d, int t)
75{
76  attr h = get(s);
77  attr result=this;
78  if (h!=NULL)
79  {
80    attr_free(h);
81  }
82  else
83  {
84    h = (attr)omAlloc0Bin(sattr_bin);
85    h->next = this;
86    result=h;
87  }
88  h->name = s;
89  h->data = d;
90  h->atyp = t;
91#ifdef TEST
92  //::Print("set attr >>%s<< of type %s\n",h->name, Tok2Cmdname(t));
93#endif
94  return  result;
95}
96
97attr sattr::get(const char * s)
98{
99  attr h = this;
100  while (h!=NULL)
101  {
102    if (0 == strcmp(s,h->name))
103    {
104#ifdef TEST
105      //::Print("get attr >>%s<< of type %s\n",h->name, Tok2Cmdname(h->atyp));
106#endif
107      return h;
108    }
109    h = h->next;
110  }
111  return NULL;
112}
113
114#if 0
115void * atGet(idhdl root,const char * name)
116{
117  attr temp = root->attribute->get(name);
118  if (temp!=NULL)
119    return temp->data;
120  else
121    return NULL;
122}
123
124void * atGet(leftv root,const char * name)
125{
126  attr temp;
127  attr a=*(root->Attribute());
128  temp = a->get(name);
129  if (temp!=NULL)
130    return temp->data;
131  else
132    return NULL;
133}
134#endif
135
136void * atGet(idhdl root,const char * name, int t, void *defaultReturnValue)
137{
138  attr temp = root->attribute->get(name);
139  if ((temp!=NULL) && (temp->atyp==t))
140    return temp->data;
141  else
142    return defaultReturnValue;
143}
144
145void * atGet(leftv root,const char * name, int t)
146{
147  attr *a=(root->Attribute());
148  if (a!=NULL)
149  {
150    attr temp = (*a)->get(name);
151    if ((temp!=NULL) && (temp->atyp==t))
152      return temp->data;
153  }
154  return NULL;
155}
156
157void atSet(idhdl root,const char * name,void * data,int typ)
158{
159  if (root!=NULL)
160  {
161    if ((IDTYP(root)!=RING_CMD)
162    && (IDTYP(root)!=QRING_CMD)
163    && (!RingDependend(IDTYP(root)))&&(RingDependend(typ)))
164      WerrorS("cannot set ring-dependend objects at this type");
165    else
166      root->attribute=root->attribute->set(name,data,typ);
167  }
168}
169
170void atSet(leftv root,const char * name,void * data,int typ)
171{
172  if (root!=NULL)
173  {
174    attr *a=root->Attribute();
175    int rt=root->Typ();
176    if (a==NULL)
177      WerrorS("cannot set attributes of this object");
178    else if ((rt!=RING_CMD)
179    && (rt!=QRING_CMD)
180    && (!RingDependend(rt))&&(RingDependend(typ)))
181      WerrorS("cannot set ring-dependend objects at this type");
182    else
183    {
184      *a=(*a)->set(name,data,typ);
185    }
186  }
187}
188
189void sattr::kill(const ring r)
190{
191  attr_free(this,r);
192  omFree((ADDRESS)name);
193  name=NULL;
194  omFreeBin((ADDRESS)this, sattr_bin);
195}
196
197void sattr::killAll(const ring r)
198{
199  attr temp = this,temp1;
200
201  while (temp!=NULL)
202  {
203    temp1 = temp->next;
204    omCheckAddr(temp);
205    temp->kill(r);
206    temp = temp1;
207  }
208}
209
210void at_Kill(idhdl root,const char * name, const ring r)
211{
212  attr temp = root->attribute->get(name);
213  if (temp!=NULL)
214  {
215    attr N = temp->next;
216    attr temp1 = root->attribute;
217    if (temp1==temp)
218    {
219      root->attribute = N;
220    }
221    else
222    {
223      while (temp1->next!=temp) temp1 = temp1->next;
224      temp1->next = N;
225    }
226    temp->kill(r);
227  }
228}
229
230void at_KillAll(idhdl root, const ring r)
231{
232  root->attribute->killAll(r);
233  root->attribute = NULL;
234}
235
236void at_KillAll(leftv root, const ring r)
237{
238  root->attribute->killAll(r);
239  root->attribute = NULL;
240}
241
242BOOLEAN atATTRIB1(leftv res,leftv v)
243{
244  int t;
245  attr *aa=(v->Attribute());
246  if (aa==NULL)
247  {
248    WerrorS("this object cannot have attributes");
249    return TRUE;
250  }
251  attr a=*aa;
252  BOOLEAN haveNoAttribute=TRUE;
253  if (v->e==NULL)
254  {
255    if (hasFlag(v,FLAG_STD))
256    {
257      PrintS("attr:isSB, type int\n");
258      haveNoAttribute=FALSE;
259    }
260    if (hasFlag(v,FLAG_QRING))
261    {
262      PrintS("attr:qringNF, type int\n");
263      haveNoAttribute=FALSE;
264    }
265    if (((t=v->Typ())==RING_CMD)||(t==QRING_CMD))
266    {
267      PrintS("attr:global, type int\n");
268      haveNoAttribute=FALSE;
269    }
270  }
271  else
272  {
273    leftv at=v->LData();
274    return atATTRIB1(res,at);
275  }
276  if (a!=NULL)                    a->Print();
277  else  if(haveNoAttribute)       PrintS("no attributes\n");
278  return FALSE;
279}
280BOOLEAN atATTRIB2(leftv res,leftv v,leftv b)
281{
282  char *name=(char *)b->Data();
283  int t;
284  leftv at=NULL;
285  if (v->e!=NULL)
286    at=v->LData();
287  if (strcmp(name,"isSB")==0)
288  {
289    res->rtyp=INT_CMD;
290    res->data=(void *)(long)hasFlag(v,FLAG_STD);
291    if (at!=NULL) res->data=(void *)(long)(hasFlag(v,FLAG_STD)||(hasFlag(at,FLAG_STD)));
292  }
293  else if ((strcmp(name,"rank")==0)&&(v->Typ()==MODUL_CMD))
294  {
295    res->rtyp=INT_CMD;
296    res->data=(void *)(((ideal)v->Data())->rank);
297  }
298  else if ((strcmp(name,"global")==0)
299  &&(((t=v->Typ())==RING_CMD)||(t==QRING_CMD)))
300  {
301    res->rtyp=INT_CMD;
302    res->data=(void *)(((ring)v->Data())->OrdSgn==1);
303  }
304  else if (strcmp(name,"qringNF")==0)
305  {
306    res->rtyp=INT_CMD;
307    res->data=(void *)(long)hasFlag(v,FLAG_QRING);
308    if (at!=NULL) res->data=(void *)(long)(hasFlag(v,FLAG_QRING)||(hasFlag(at,FLAG_QRING)));
309  }
310#ifdef HAVE_SHIFTBBA
311  else if ((strcmp(name,"isLPring")==0)
312  &&(((t=v->Typ())==RING_CMD)||(t==QRING_CMD)))
313  {
314    res->rtyp=INT_CMD;
315    res->data=(void *)(long)(((ring)v->Data())->isLPring);
316  }
317#endif
318  else
319  {
320    attr *aa=v->Attribute();
321    if (aa==NULL)
322    {
323      WerrorS("this object cannot have attributes");
324      return TRUE;
325    }
326    attr a=*aa;
327    a=a->get(name);
328    if (a!=NULL)
329    {
330      res->rtyp=a->atyp;
331      res->data=a->CopyA();
332    }
333    else
334    {
335      res->rtyp=STRING_CMD;
336      res->data=omStrDup("");
337    }
338  }
339  return FALSE;
340}
341BOOLEAN atATTRIB3(leftv /*res*/,leftv v,leftv b,leftv c)
342{
343  idhdl h=(idhdl)v->data;
344  int t;
345  if (v->e!=NULL)
346  {
347    v=v->LData();
348    if (v==NULL) return TRUE;
349    h=NULL;
350  }
351  else if (v->rtyp!=IDHDL) h=NULL;
352
353  char *name=(char *)b->Data();
354  if (strcmp(name,"isSB")==0)
355  {
356    if (c->Typ()!=INT_CMD)
357    {
358      WerrorS("attribute isSB must be int");
359      return TRUE;
360    }
361    if (((long)c->Data())!=0L)
362    {
363      if (h!=NULL) setFlag(h,FLAG_STD);
364      setFlag(v,FLAG_STD);
365    }
366    else
367    {
368      if (h!=NULL) resetFlag(h,FLAG_STD);
369      resetFlag(v,FLAG_STD);
370    }
371  }
372  else if (strcmp(name,"qringNF")==0)
373  {
374    if (c->Typ()!=INT_CMD)
375    {
376      WerrorS("attribute qringNF must be int");
377      return TRUE;
378    }
379    if (((long)c->Data())!=0L)
380    {
381      if (h!=NULL) setFlag(h,FLAG_QRING);
382      setFlag(v,FLAG_QRING);
383    }
384    else
385    {
386      if (h!=NULL) resetFlag(h,FLAG_QRING);
387      resetFlag(v,FLAG_QRING);
388    }
389  }
390  else if ((strcmp(name,"rank")==0)&&(v->Typ()==MODUL_CMD))
391  {
392    if (c->Typ()!=INT_CMD)
393    {
394      WerrorS("attribute `rank` must be int");
395      return TRUE;
396    }
397    ideal I=(ideal)v->Data();
398    I->rank=si_max((int)I->rank,(int)((long)c->Data()));
399  }
400  else if ((strcmp(name,"global")==0)
401  &&(((t=v->Typ())==RING_CMD)||(t==QRING_CMD)))
402  {
403    WerrorS("can not set attribute `global`");
404    return TRUE;
405  }
406#ifdef HAVE_SHIFTBBA
407  else if ((strcmp(name,"isLPring")==0)
408  &&(((t=v->Typ())==RING_CMD)||(t==QRING_CMD)))
409  {
410    if (c->Typ()==INT_CMD)
411      ((ring)v->Data())->isLPring=(int)(long)c->Data();
412    else
413    {
414      WerrorS("attribute `isLPring` must be int");
415      return TRUE;
416    }
417  }
418#endif
419  else
420  {
421    int typ=c->Typ();
422    if (h!=NULL) atSet(h,omStrDup(name),c->CopyD(typ),typ/*c->T(yp()*/);
423    else         atSet(v,omStrDup(name),c->CopyD(typ),typ/*c->T(yp()*/);
424  }
425  return FALSE;
426}
427
428BOOLEAN atKILLATTR1(leftv /*res*/,leftv a)
429{
430  idhdl h=NULL;
431  if ((a->rtyp==IDHDL)&&(a->e==NULL))
432  {
433    h=(idhdl)a->data;
434    resetFlag((idhdl)a->data,FLAG_STD);
435  }
436  resetFlag(a,FLAG_STD);
437  if (h->attribute!=NULL)
438  {
439    atKillAll(h);
440    a->attribute=NULL;
441  }
442  else atKillAll(a);
443  return FALSE;
444}
445BOOLEAN atKILLATTR2(leftv /*res*/,leftv a,leftv b)
446{
447  if ((a->rtyp!=IDHDL)||(a->e!=NULL))
448  {
449    WerrorS("object must have a name");
450    return TRUE;
451  }
452  char *name=(char *)b->Data();
453  if (strcmp(name,"isSB")==0)
454  {
455    resetFlag(a,FLAG_STD);
456    resetFlag((idhdl)a->data,FLAG_STD);
457  }
458  else if (strcmp(name,"global")==0)
459  {
460    WerrorS("can not set attribut `global`");
461    return TRUE;
462  }
463  else
464  {
465    atKill((idhdl)a->data,name);
466  }
467  return FALSE;
468}
469
Note: See TracBrowser for help on using the repository browser.