source: git/Singular/attrib.cc @ 72a01e

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