Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages   Examples  

CTBattrList.cxx

Go to the documentation of this file.
00001 
00006 /*----------------------------------------------------------------------------*/
00007 /* C Tool Box: Designed and implemented by:                                   */
00008 /*    Walter F.J. Mueller   Gesellschaft fuer Schwerionenforschung (GSI)      */
00009 /*                          Planckstrasse 1, D-64291 Darmstadt, Germany       */
00010 /*                  Email:  W.F.J.Mueller@gsi.de                              */
00011 /*                  WWW:    http://www-kp3.gsi.de/www/kp3/people/mueller.html */
00012 /*------------------------------------------+---------------------------------*/
00013 
00014 #include "CTB.hxx"
00015 #include "CTBattrList.hxx"
00016 
00024 //------------------------------------------+-----------------------------------
00026 
00027 CTBattrList::CTBattrList()
00028 {}
00029 
00030 //------------------------------------------+-----------------------------------
00032 
00033 CTBattrList::~CTBattrList()
00034 {}
00035 
00036 //------------------------------------------+-----------------------------------
00038 
00039 void CTBattrList::Set(const char* c_key, const char* c_value)
00040 {
00041   m_attrlist[FindOrCreate(c_key)].Value() = c_value;
00042   return;
00043 }
00044 
00045 //------------------------------------------+-----------------------------------
00047 
00048 void CTBattrList::Set(const char* c_key, const CTBstring& value)
00049 {
00050   m_attrlist[FindOrCreate(c_key)].Value() = value;
00051   return;
00052 }
00053 
00054 //------------------------------------------+-----------------------------------
00056 
00057 void CTBattrList::Clear()
00058 {
00059   m_attrlist.Resize(0);
00060   return;
00061 }
00062 
00063 //------------------------------------------+-----------------------------------
00065 
00066 void CTBattrList::ToStream(ostream& os) const
00067 {
00068   for (CTBint i = 0; i < m_attrlist.Size(); i++) {
00069     os << m_attrlist[i].Key() << " : " << m_attrlist[i].Value() << endl;
00070   }
00071   return;
00072 }
00073 
00074 //------------------------------------------+-----------------------------------
00076 
00077 void CTBattrList::ToXStream(CTBxmlOStream& xos, const char* c_name) const
00078 {
00079   CTBstring tagname;
00080   
00081   xos.PutStartTag(c_name);
00082 
00083   for (CTBint i = 0; i < m_attrlist.Size(); i++) {
00084     if (m_attrlist[i].Value().Length() > 0) {
00085       tagname  = "a:";
00086       tagname += m_attrlist[i].Key();
00087       xos.PutTaggedValue(tagname,m_attrlist[i].Value());
00088     }
00089   }
00090 
00091   xos.PutEndTag();
00092 
00093   return;
00094 }
00095 
00096 //------------------------------------------+-----------------------------------
00098 
00099 void CTBattrList::FromXStream(CTBxmlIStream& xis, const char* c_name)
00100 {
00101   xis.OpenElement(c_name);
00102 
00103   Clear();
00104 
00105   while(xis.NextNotEnd()) {
00106     const CTBxmlToken& token = xis.PeekTagOrNonWhiteData();
00107 
00108     if (token.Type() == CTBxmlToken::starttag) {
00109       CTBstring tagname = token.Name();
00110       if (tagname(0,2) == "a:" && tagname.Length() > 2) {
00111         CTBstring key   = tagname(2,tagname.Length()-2);
00112         CTBint    i_ind = FindOrCreate(key);
00113         xis.GetTaggedValue(tagname, m_attrlist[i_ind].Value());
00114       } else {
00115         xis.DropElement();
00116         cerr << "%CTBattrList::FromXStream-E bad key ignored " << endl;
00117       }
00118     }
00119   }
00120 
00121   xis.CloseElement();
00122   
00123   return;
00124 }
00125 
00126 //------------------------------------------+-----------------------------------
00128 
00129 const CTBstring& CTBattrList::operator[](CTBint i_ind) const
00130 {
00131   return (i_ind >= 0 && i_ind < m_attrlist.Size()) ?
00132     m_attrlist[i_ind].Value() : CTBstring::NullString();
00133 }
00134 
00135 //------------------------------------------+-----------------------------------
00137 
00138 const CTBstring& CTBattrList::operator[](const char* c_key) const
00139 {
00140   CTBint i_ind = Find(c_key);
00141   return (i_ind >= 0) ? m_attrlist[i_ind].Value() : CTBstring::NullString();
00142 }
00143 
00144 //------------------------------------------+-----------------------------------
00146 
00147 const CTBstring& CTBattrList::operator[](const CTBstring& key) const
00148 {
00149   CTBint i_ind = Find(key);
00150   return (i_ind >= 0) ? m_attrlist[i_ind].Value() : CTBstring::NullString();
00151 }
00152 
00153 //------------------------------------------+-----------------------------------
00155 
00156 CTBint CTBattrList::Find(const char* c_key) const
00157 {
00158   for (CTBint i = 0; i < m_attrlist.Size(); i++) {
00159     if (m_attrlist[i].Key() == c_key) return i;
00160   }
00161   return -1;
00162 }
00163 
00164 //------------------------------------------+-----------------------------------
00166 
00167 CTBint CTBattrList::Find(const CTBstring& key) const
00168 {
00169   return Find((const char*) key);
00170 }
00171 
00172 //------------------------------------------+-----------------------------------
00174 
00175 CTBint CTBattrList::FindOrCreate(const char* c_key)
00176 {
00177   CTBint i_ind = Find(c_key);
00178 
00179   if (i_ind >= 0) return i_ind;
00180 
00181   m_attrlist << CTBpair<CTBstring,CTBstring>(c_key);
00182   return m_attrlist.Size()-1;
00183 }
00184 
00185 //------------------------------------------+-----------------------------------
00187 
00188 CTBint CTBattrList::FindOrCreate(const CTBstring& key)
00189 {
00190   return FindOrCreate((const char*) key);
00191 }
00192 

Generated at Fri Oct 24 18:11:27 2003 for CTBbase by doxygen1.2.9-20010812 written by Dimitri van Heesch, © 1997-2001