"Fossies" - the Fresh Open Source Software Archive

Member "ivtools-ivtools-2.0.4/src/Attribute/attrlist.c" (9 Oct 2020, 16757 Bytes) of package /linux/misc/ivtools-ivtools-2.0.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "attrlist.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (c) 2001 Scott E. Johnston
    3  * Copyright (c) 1996-1999 Vectaport Inc.
    4  *
    5  * Permission to use, copy, modify, distribute, and sell this software and
    6  * its documentation for any purpose is hereby granted without fee, provided
    7  * that the above copyright notice appear in all copies and that both that
    8  * copyright notice and this permission notice appear in supporting
    9  * documentation, and that the names of the copyright holders not be used in
   10  * advertising or publicity pertaining to distribution of the software
   11  * without specific, written prior permission.  The copyright holders make
   12  * no representations about the suitability of this software for any purpose.
   13  * It is provided "as is" without express or implied warranty.
   14  *
   15  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
   16  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
   17  * IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL,
   18  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
   19  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
   20  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
   21  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   22  * 
   23  */
   24 
   25 /*
   26  * Implementation of AttributeList class.
   27  */
   28 
   29 
   30 #include <Attribute/alist.h>
   31 #include <Attribute/attribute.h>
   32 #include <Attribute/attrlist.h>
   33 #include <Attribute/attrvalue.h>
   34 
   35 #include <Unidraw/globals.h>
   36 #include <Unidraw/iterator.h>
   37 
   38 #include <iostream.h>
   39 #include <string.h>
   40 #include <fstream>
   41 #include <iomanip>
   42 
   43 #include <IV-2_6/_enter.h>
   44 
   45 #ifdef LEAKCHECK
   46 LeakChecker* AttributeValueList::_leakchecker = nil;
   47 #endif
   48 
   49 
   50 /*****************************************************************************/
   51 using std::cerr;
   52 
   53 int AttributeList::_symid = -1;
   54 
   55 AttributeList::AttributeList (AttributeList* s) {
   56     _alist = new AList;
   57     _count = 0;
   58     if (s != nil) {
   59         ALIterator i;
   60 
   61         for (s->First(i); !s->Done(i); s->Next(i)) {
   62         add_attr(new Attribute(*s->GetAttr(i)));
   63     }
   64     }
   65 }
   66 
   67 AttributeList::~AttributeList () { 
   68     if (_alist) {
   69         ALIterator i;
   70     for (First(i); !Done(i); Next(i)) {
   71       Resource::unref(GetAttr(i));
   72     }
   73     delete _alist; 
   74     }
   75 }
   76 
   77 void AttributeList::add_attr(const char* name, AttributeValue& value) {
   78   add_attr(name, new AttributeValue(value));
   79 }
   80 
   81 void AttributeList::add_attr(const char* name, AttributeValue* value) {
   82     Attribute* attr = new Attribute(name, value);
   83     if (add_attr(attr)) {
   84         attr->valueptr = nil;
   85     Resource::unref(attr);
   86     }
   87 }
   88 
   89 void AttributeList::add_attr(int symid, AttributeValue& value) {
   90   add_attr(symid, new AttributeValue(value));
   91 }
   92 
   93 void AttributeList::add_attr(int symid, AttributeValue* value) {
   94     Attribute* attr = new Attribute(symid, value);
   95     if (add_attr(attr)) {
   96         attr->valueptr = nil;
   97     Resource::unref(attr);
   98     }
   99 }
  100 
  101 void AttributeList::add_attribute(Attribute* attr) {
  102     if (add_attr(attr)) {
  103         attr->valueptr = nil;
  104     Resource::unref(attr);
  105     }
  106 }
  107 
  108 int AttributeList::add_attr(Attribute* attr) {
  109     ALIterator i;
  110     for (First(i); !Done(i); Next(i)) {
  111     Attribute* old_attr = GetAttr(i);
  112     if (old_attr && attr->SymbolId() == old_attr->SymbolId()) {
  113         old_attr->Value(attr->Value());
  114         return -1;
  115     }
  116     }
  117     InsertBefore(i, attr);
  118     Resource::ref(attr);
  119     return 0;
  120 }
  121 
  122 Attribute* AttributeList::GetAttr (const char* n) {
  123     ALIterator i;
  124     for (First(i); !Done(i); Next(i)) {
  125     Attribute* attr = GetAttr(i);
  126     if (strcmp(n, attr->Name()) == 0)
  127         return attr;
  128     }
  129     return nil;
  130 }
  131 
  132 Attribute* AttributeList::GetAttr (int symid) {
  133     ALIterator i;
  134     for (First(i); !Done(i); Next(i)) {
  135     Attribute* attr = GetAttr(i);
  136     if (symid == attr->SymbolId())
  137         return attr;
  138     }
  139     return nil;
  140 }
  141 
  142 Attribute* AttributeList::Attr (AList* r) {
  143     return (Attribute*) (*r)();
  144 }
  145 
  146 AList* AttributeList::Elem (ALIterator i) { return (AList*) i.GetValue(); }
  147 
  148 void AttributeList::Append (Attribute* v) {
  149     _alist->Append(new AList(v));
  150     ++_count;
  151 }
  152 
  153 void AttributeList::Prepend (Attribute* v) {
  154     _alist->Prepend(new AList(v));
  155     ++_count;
  156 }
  157 
  158 void AttributeList::InsertAfter (ALIterator i, Attribute* v) {
  159     Elem(i)->Prepend(new AList(v));
  160     ++_count;
  161 }
  162 
  163 void AttributeList::InsertBefore (ALIterator i, Attribute* v) {
  164     Elem(i)->Append(new AList(v));
  165     ++_count;
  166 }
  167 
  168 void AttributeList::Remove (ALIterator& i) {
  169     AList* doomed = Elem(i);
  170 
  171     Next(i);
  172     _alist->Remove(doomed);
  173     delete doomed;
  174     --_count;
  175 }   
  176     
  177 void AttributeList::Remove (Attribute* p) {
  178     AList* temp;
  179 
  180     if ((temp = _alist->Find(p)) != nil) {
  181     _alist->Remove(temp);
  182         delete temp;
  183     --_count;
  184     }
  185 }
  186 
  187 Attribute* AttributeList::GetAttr (ALIterator i) { return Attr(Elem(i)); }
  188 
  189 void AttributeList::SetAttr (Attribute* gv, ALIterator& i) {
  190     i.SetValue(_alist->Find(gv));
  191 }
  192 
  193 void AttributeList::First (ALIterator& i) { i.SetValue(_alist->First()); }
  194 void AttributeList::Last (ALIterator& i) { i.SetValue(_alist->Last()); }
  195 void AttributeList::Next (ALIterator& i) { i.SetValue(Elem(i)->Next()); }
  196 void AttributeList::Prev (ALIterator& i) { i.SetValue(Elem(i)->Prev()); }
  197 boolean AttributeList::Done (ALIterator i) { return Elem(i) == _alist->End(); }
  198 int AttributeList::Number () const { return _count; }
  199 
  200 boolean AttributeList::Includes (Attribute* e) {
  201     return _alist->Find(e) != nil;
  202 }
  203 
  204 boolean AttributeList::IsEmpty () { return _alist->IsEmpty(); }
  205 
  206 void AttributeList::print_attrlist(AttributeList* al) {
  207     print_attrlist(std::cout, al);
  208 }
  209 
  210 void AttributeList::print_attrlist(std::ostream& out, AttributeList* al) {
  211     out << *al;
  212 }
  213 
  214 ostream& operator<< (ostream& out, const AttributeList& al) {
  215 
  216     AttributeList* attrlist = (AttributeList*)&al;
  217     if (al.Number()==0) {
  218         out << "<empty AttributeList>";
  219         return out;
  220     }
  221     ALIterator i;
  222     int init=0;
  223     for (attrlist->First(i); !attrlist->Done(i); attrlist->Next(i)) {
  224     Attribute* attr = attrlist->GetAttr(i);
  225         if(init) out << " "; else init=1;
  226     const char* nm = attr->Name();
  227     int nmsz = strlen(nm);
  228     out << ":";
  229         for(int i=0; i<nmsz; i++) {
  230       if (nm[i]==' ') out << "\\";
  231        out << nm[i];
  232     }
  233     out << " ";
  234 
  235     AttributeValue* attrval = attr->Value();
  236 #if 1
  237     out << *attrval;
  238 #else
  239     char* string;
  240         switch(attr->Value()->type()) {
  241         case AttributeValue::SymbolType:
  242             out << attrval->symbol_ptr();
  243             break;
  244         case AttributeValue::StringType:
  245           string = (char *) attrval->string_ptr();
  246             out << "\"" << string << "\"";
  247             break;
  248         case AttributeValue::CharType:
  249             out << "'" << attrval->char_ref() << "'";
  250             break;
  251         case AttributeValue::UCharType:
  252             out << "'" << attrval->char_ref() << "'";
  253             break;
  254         case AttributeValue::IntType:
  255             out << attrval->int_ref();
  256             break;
  257         case AttributeValue::UIntType:
  258             out << attrval->uint_ref();
  259             break;
  260         case AttributeValue::ShortType:
  261             out << attrval->short_ref();
  262             break;
  263         case AttributeValue::UShortType:
  264             out << attrval->ushort_ref();
  265             break;
  266         case AttributeValue::LongType:
  267             out << attrval->long_ref();
  268             break;
  269         case AttributeValue::ULongType:
  270             out << attrval->ulong_ref();
  271             break;
  272         case AttributeValue::FloatType:
  273             out.form("%.6", attrval->float_val());
  274             break;
  275         case AttributeValue::DoubleType:
  276             out << attrval->double_ref();
  277             break;
  278             default:
  279         out << "Unknown type";
  280             break;
  281     }
  282 #endif
  283     }
  284     return out;
  285 }
  286 
  287 void AttributeList::dump() {
  288   cerr << *this << "\n";
  289 }
  290 
  291 AttributeValue* AttributeList::find(const char* name) {
  292     int id = symbol_find((char *)name);
  293     return find(id);
  294 }
  295 
  296 AttributeValue* AttributeList::find(int symid) {
  297     if (symid==-1)
  298         return nil;
  299     ALIterator i;
  300     for (First(i); !Done(i); Next(i)) {
  301     Attribute* attr = GetAttr(i);
  302     if (attr->SymbolId() == symid) {
  303         return attr->Value();
  304     }
  305     }
  306     return nil;
  307 }
  308 
  309 AttributeList* AttributeList::merge(AttributeList* al) {
  310   if (al) {
  311     ALIterator it;
  312     for( al->First(it); !al->Done(it); al->Next(it)) 
  313       add_attribute(new Attribute(*al->GetAttr(it)));
  314   }
  315   return this;
  316 }
  317 
  318 void AttributeList::clear() {
  319   ALIterator it;
  320   for( First(it); !Done(it); ) {
  321     Attribute* attr = GetAttr(it);
  322     Remove(it);
  323     Resource::unref(attr);
  324   }
  325 }
  326 
  327 /*****************************************************************************/
  328 
  329 AttributeValueList::AttributeValueList (AttributeValueList* s) {
  330 #ifdef LEAKCHECK
  331     if(!_leakchecker) _leakchecker = new LeakChecker("AttributeValueList");
  332     _leakchecker->create();
  333 #endif
  334     _alist = new AList;
  335     _count = 0;
  336     _max_out = -1;
  337     if (s != nil) {
  338         ALIterator i;
  339 
  340         for (s->First(i); !s->Done(i); s->Next(i)) {
  341         Append(new AttributeValue(s->GetAttrVal(i)));
  342     }
  343     }
  344     _nested_insert = false;
  345     _prevget_i = -1;
  346 }
  347 
  348 AttributeValueList::~AttributeValueList () { 
  349 #ifdef LEAKCHECK
  350     _leakchecker->destroy();
  351 #endif
  352     if (_alist) {
  353         ALIterator i;
  354     for (First(i); !Done(i); Next(i)) {
  355         delete GetAttrVal(i);
  356     }
  357     delete _alist; 
  358     }
  359 }
  360 
  361 AttributeValue* AttributeValueList::AttrVal (AList* r) {
  362     return (AttributeValue*) (*r)();
  363 }
  364 
  365 AList* AttributeValueList::Elem (ALIterator i) { return (AList*) i.GetValue(); }
  366 
  367 void AttributeValueList::Append (AttributeValue* v) {
  368     _alist->Append(new AList(v));
  369     ++_count;
  370     _prevget_i = -1;
  371 }
  372 
  373 void AttributeValueList::Prepend (AttributeValue* v) {
  374     _alist->Prepend(new AList(v));
  375     ++_count;
  376     _prevget_i = -1;
  377 }
  378 
  379 void AttributeValueList::InsertAfter (ALIterator i, AttributeValue* v) {
  380     Elem(i)->Prepend(new AList(v));
  381     ++_count;
  382     _prevget_i = -1;
  383 }
  384 
  385 void AttributeValueList::InsertBefore (ALIterator i, AttributeValue* v) {
  386     Elem(i)->Append(new AList(v));
  387     ++_count;
  388     _prevget_i = -1;
  389 }
  390 
  391 void AttributeValueList::Remove (ALIterator& i) {
  392     AList* doomed = Elem(i);
  393 
  394     Next(i);
  395     _alist->Remove(doomed);
  396     delete doomed;
  397     --_count;
  398     _prevget_i = -1;
  399 }   
  400     
  401 void AttributeValueList::Remove (AttributeValue* p) {
  402     AList* temp;
  403 
  404     if ((temp = _alist->Find(p)) != nil) {
  405     _alist->Remove(temp);
  406         delete temp;
  407     --_count;
  408     }
  409     _prevget_i = -1;
  410 }
  411 
  412 AttributeValue* AttributeValueList::GetAttrVal (ALIterator i) { return AttrVal
  413 (Elem(i)); }
  414 
  415 void AttributeValueList::SetAttrVal (AttributeValue* gv, ALIterator& i) {
  416     i.SetValue(_alist->Find(gv));
  417 }
  418 
  419 void AttributeValueList::First (ALIterator& i) { i.SetValue(_alist->First()); }
  420 void AttributeValueList::Last (ALIterator& i) { i.SetValue(_alist->Last()); }
  421 void AttributeValueList::Next (ALIterator& i) { i.SetValue(Elem(i)->Next()); }
  422 void AttributeValueList::Prev (ALIterator& i) { i.SetValue(Elem(i)->Prev()); }
  423 boolean AttributeValueList::Done (ALIterator i) { return Elem(i) == _alist->End(); }
  424 const int AttributeValueList::Number () { return _count; }
  425 
  426 boolean AttributeValueList::Includes (AttributeValue* e) {
  427     return _alist->Find(e) != nil;
  428 }
  429 
  430 boolean AttributeValueList::IsEmpty () { return _alist->IsEmpty(); }
  431 
  432 ostream& operator<< (ostream& out, const AttributeValueList& al) {
  433 
  434     AttributeValueList* attrlist = (AttributeValueList*)&al;
  435     int maxout = attrlist->max_out();
  436     ALIterator i;
  437     for (attrlist->First(i); !attrlist->Done(i) && maxout!=0;) {
  438         maxout--;
  439     AttributeValue* attrval = attrlist->GetAttrVal(i);
  440 
  441     char* string;
  442         switch(attrval->type()) {
  443         case AttributeValue::SymbolType:
  444             out << attrval->symbol_ptr();
  445             break;
  446         case AttributeValue::StringType:
  447           string = (char *) attrval->string_ptr();
  448             out << "\"" << string << "\"";
  449             break;
  450         case AttributeValue::CharType:
  451             out << "`\\" << std::setw(3) << std::setfill('0') << std::oct << (int)attrval->char_ref() << std::dec << "`" << std::resetiosflags(std::ios_base::basefield);   
  452             break;
  453         case AttributeValue::UCharType:
  454             out << "`\\" << std::setw(3) << std::setfill('0') << std::oct << (unsigned int) attrval->uchar_ref() << std::dec << "`" << std::resetiosflags(std::ios_base::basefield);
  455             break;
  456         case AttributeValue::ShortType:
  457             out << attrval->short_ref();
  458             break;
  459         case AttributeValue::UShortType:
  460             out << attrval->ushort_ref();
  461             break;
  462         case AttributeValue::IntType:
  463             out << attrval->int_ref();
  464             break;
  465         case AttributeValue::UIntType:
  466             out << attrval->uint_ref();
  467             break;
  468         case AttributeValue::LongType:
  469             out << attrval->long_ref();
  470             break;
  471         case AttributeValue::ULongType:
  472             out << attrval->ulong_ref();
  473             break;
  474         case AttributeValue::FloatType:
  475             out << attrval->float_ref();
  476             break;
  477         case AttributeValue::DoubleType:
  478             out << attrval->double_ref();
  479             break;
  480         case AttributeValue::BooleanType:
  481             out << attrval->boolean_ref();
  482             break;
  483         case AttributeValue::ArrayType:
  484             out << "{";
  485             out << *attrval->array_ref();
  486             out << "}";
  487             break;
  488         case AttributeValue::StreamType:
  489             out << "stream";
  490             break;
  491         case AttributeValue::ObjectType:
  492             out << "object(class=" << symbol_pntr(attrval->obj_type_val()) << ", ptr= " << attrval->obj_val() << ")";
  493             break;
  494             default:
  495         out << "nil";
  496             break;
  497     }
  498 
  499     attrlist->Next(i);
  500     if (!attrlist->Done(i))  out << ",";
  501     
  502     }
  503     if(maxout==0 && !attrlist->Done(i)) out << "...";
  504     return out;
  505 }
  506 
  507 
  508 void AttributeValueList::clear() {
  509   ALIterator it;
  510   for( First(it); !Done(it); ) {
  511     AttributeValue* av = GetAttrVal(it);
  512     Remove(it);
  513     delete av;
  514   }
  515   _prevget_i = -1;
  516 }
  517 
  518 AttributeValue* AttributeValueList::Get(unsigned int index) {
  519   if (Number()<=index) return nil;
  520   ALIterator it;
  521   int delta = abs(((int)index)-_prevget_i);
  522   if(_prevget_i!=-1 && (delta<<1)<Number()) {
  523     it = _prevget_it;
  524     if(_prevget_i>index) {
  525         for (int i=0; i<delta; i++) Prev(it);
  526     } else {
  527         for (int i=0; i<delta; i++) Next(it);
  528     }
  529   } else {
  530     if(Number()<=index*2) {
  531         Last(it);
  532         for (int i=0; i<Number()-index-1; i++) Prev(it);
  533     } else {
  534         First(it);
  535         for (int i=0; i<index; i++) Next(it);
  536     }
  537   }
  538   _prevget_i = index;
  539   _prevget_it = it;
  540   return GetAttrVal(it);
  541 }
  542 
  543 AttributeValue* AttributeValueList::Set(unsigned int index, AttributeValue* av) {
  544   if (Number()<=index) {
  545     ALIterator it;
  546     Last(it);
  547     int padding = index-Number();
  548     for (int i=0; i<padding; i++) Append(new AttributeValue());
  549     Append(av);
  550     return nil;
  551   }
  552   else {
  553     ALIterator it;
  554     First(it);
  555     for (int i=0; i<index; i++) Next(it);
  556     AttributeValue* oldv = Replace(it, av);
  557     return oldv;
  558   }
  559 }
  560 
  561 void AttributeValueList::Insert(int index, AttributeValue* av) {
  562   ALIterator it;
  563   First(it);
  564   if (index<0) 
  565     InsertBefore(it, av);
  566   else {
  567     if (index>=Number()) index=Number()-1;
  568     int i=0;
  569     while(i++<index) Next(it);
  570     InsertAfter(it, av);
  571   }
  572   _prevget_i = -1;
  573 }
  574 
  575 AttributeValue* AttributeValueList::Replace (ALIterator& i, AttributeValue* av) {
  576     AList* doomed = Elem(i);
  577     AttributeValue* removed = GetAttrVal(i);
  578     Next(i);
  579     _alist->Remove(doomed);
  580     delete doomed;
  581     Elem(i)->Append(new AList(av));
  582     _prevget_i = -1;
  583     return removed;
  584 }   
  585    
  586 boolean AttributeValueList::Equal(AttributeValueList* avl) {
  587   if(avl->Number()!=Number()) return false;
  588   
  589   ALIterator it, jt;
  590   avl->First(it);
  591   First(jt);
  592   while(!Done(jt)) {
  593     AttributeValue* av = avl->GetAttrVal(it);
  594     if (!GetAttrVal(jt)->equal(*av)) return false;
  595     avl->Next(it);
  596     Next(jt);
  597   }
  598   
  599   return true;
  600 }
  601 
  602 boolean AttributeValueList::GreaterThan(AttributeValueList* avl) {
  603 
  604   ALIterator it, jt;
  605   First(it);
  606   avl->First(jt);
  607   while (true) {
  608     if (Done(it) && avl->Done(jt)) { return false; }
  609     if (Done(it)                 ) { return false; }
  610     if (avl->Done(jt)            ) { return true; }
  611     AttributeValue* av = avl->GetAttrVal(jt);
  612     if (GetAttrVal(it)->greaterthan(*av)) { return true; }
  613     if (GetAttrVal(it)->notequal(*av)) { return false; }
  614     Next(it);
  615     avl->Next(jt);
  616   }
  617   
  618 }
  619 
  620 boolean AttributeValueList::LesserThan(AttributeValueList* avl) {
  621   
  622   ALIterator it, jt;
  623   First(it);
  624   avl->First(jt);
  625   while (true) {
  626     if (Done(it) && avl->Done(jt)) { return false; }
  627     if (Done(it)                 ) { return true; }
  628     if (avl->Done(jt)            ) { return false; }
  629     AttributeValue* av = avl->GetAttrVal(jt);
  630     if (GetAttrVal(it)->lesserthan(*av)) { return true; }
  631     if (GetAttrVal(it)->notequal(*av)) { return false; }
  632     Next(it);
  633     avl->Next(jt);
  634   }
  635   
  636 }
  637