Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

Omniifr::PersistNode Class Reference

#include <PersistNode.h>

List of all members.

Public Member Functions

 PersistNode ()
 Create an empty node.
 PersistNode (istream &is)
 Create root node and read tree from stream.
 ~PersistNode ()
 Free node and all its children.
void output (ostream &os, string name) const
bool readnode (istream &is)
bool readtoken (istream &is, string &tok)
PersistNodeaddnode (const string &name)
void delnode (const string &name)
void addattr (const string &keyvalue)
void addattr (const string &key, long value)
bool hasAttr (const string &key) const
string attrString (const string &key, const string &fallback="") const
long attrLong (const string &key, long fallback=0) const
cdrMemoryStream attrCdrStream (const string &key) const
PersistNodechild (const string &key) const

Static Public Member Functions

static void outputCdrMemoryStream (ostream &os, cdrMemoryStream &memstr, const char *prefix=NULL)
 Writes an encoded version of the buffer to the output stream.
static void outputIOR (ostream &os, CORBA::Object_ptr obj, const char *prefix=NULL)
 Writes an IOR to the output stream.

Public Attributes

map< string, PersistNode * > _child
map< string, string > _attr

Static Public Attributes

static const char * _separator = "::"
 Separator for node names ("::").


Constructor & Destructor Documentation

Omniifr::PersistNode::PersistNode  )  [inline]
 

Create an empty node.

Definition at line 53 of file PersistNode.h.

Referenced by addnode().

Omniifr::PersistNode::PersistNode istream &  is  ) 
 

Create root node and read tree from stream.

Definition at line 35 of file PersistNode.cc.

References readnode().

00036 {
00037   while( readnode(is) ){}
00038 }

Omniifr::PersistNode::~PersistNode  ) 
 

Free node and all its children.

Definition at line 40 of file PersistNode.cc.

References _child.

00041 {
00042   for(map<string,PersistNode*>::iterator i=_child.begin(); i!=_child.end(); ++i)
00043       delete i->second;
00044 }


Member Function Documentation

void Omniifr::PersistNode::addattr const string &  key,
long  value
 

Definition at line 146 of file PersistNode.cc.

References _attr.

00147 {
00148   char buf[64];
00149   sprintf(buf,"%i",value);
00150   _attr[key]=string(buf);
00151 }

void Omniifr::PersistNode::addattr const string &  keyvalue  ) 
 

Definition at line 140 of file PersistNode.cc.

References _attr.

Referenced by readnode().

00141 {
00142   string::size_type pos =keyvalue.find('=');
00143   _attr[keyvalue.substr(0,pos)]=(pos==string::npos?"":keyvalue.substr(pos+1));
00144 }

PersistNode * Omniifr::PersistNode::addnode const string &  name  ) 
 

Definition at line 100 of file PersistNode.cc.

References _child, _separator, and PersistNode().

Referenced by readnode().

00101 {
00102   string::size_type pos =name.find(_separator);
00103   // get reference to Next node in the path.
00104   PersistNode*& newchild =_child[name.substr(0,pos)];
00105 
00106   if(pos==string::npos) // leaf: add new leaf.
00107   {
00108     if(newchild)
00109         delete newchild; // overwrite old leaf (and its children)
00110     newchild=new PersistNode();
00111     return newchild;
00112   }
00113   else // branch: just add the branch if it's missing, and then recurse.
00114   {
00115     if(!newchild)
00116         newchild=new PersistNode();
00117     return newchild->addnode(name.substr(pos+2));
00118   }
00119 }

cdrMemoryStream Omniifr::PersistNode::attrCdrStream const string &  key  )  const
 

Definition at line 173 of file PersistNode.cc.

References _attr, and DB.

00174 {
00175   map<string,string>::const_iterator pos=_attr.find(key);
00176   if(pos==_attr.end())
00177   {
00178     DB(1,"ERROR, missing cdrStream attribute: "<<key.c_str())
00179     return cdrMemoryStream(); // eek! bad input data.
00180   }
00181   if(1==pos->second.size()%2)
00182   {
00183     DB(1,"ERROR, cdrStream attribute should have even-number of chars: "
00184          <<key.c_str())
00185     return cdrMemoryStream(); // eek! bad input data.
00186   }
00187   // OK
00188   const int len =pos->second.size()/2;
00189   CORBA::Octet* buf =new CORBA::Octet[len]; // Make a buffer of the right size
00190   char str[3];
00191   str[2]='\0';
00192   for(int i=0; i<len; ++i)                  // Fill it
00193   {
00194     str[0]=pos->second[2*i  ];
00195     str[1]=pos->second[2*i+1];
00196     long byte=::strtol(str,NULL,16);
00197     assert(byte>=0 && byte<256);
00198     buf[i]=(CORBA::Octet)byte;
00199   }
00200   cdrMemoryStream memstr;                   // don't bother to clear memory.
00201   memstr.put_octet_array(buf,len);          // Copy it into a cdrMemoryStream
00202   delete[] buf; // ?? use auto_ptr
00203   return memstr;
00204 }

long Omniifr::PersistNode::attrLong const string &  key,
long  fallback = 0
const
 

Definition at line 165 of file PersistNode.cc.

References _attr, and DB.

00166 {
00167   map<string,string>::const_iterator pos=_attr.find(key);
00168   if(pos!=_attr.end())
00169       return ::atol(pos->second.c_str());
00170   DB(20,"PersistNode failed to find key: "<<key.c_str()<<" (long)")
00171   return fallback;
00172 }

string Omniifr::PersistNode::attrString const string &  key,
const string &  fallback = ""
const
 

Definition at line 157 of file PersistNode.cc.

References _attr, and DB.

00158 {
00159   map<string,string>::const_iterator pos=_attr.find(key);
00160   if(pos!=_attr.end())
00161       return pos->second;
00162   DB(20,"PersistNode failed to find key: "<<key.c_str()<<" (string)")
00163   return fallback;
00164 }

PersistNode * Omniifr::PersistNode::child const string &  key  )  const
 

Definition at line 205 of file PersistNode.cc.

References _child.

Referenced by Omniifr::Repository_impl::reincarnate().

00206 {
00207   map<string,PersistNode*>::const_iterator pos=_child.find(key);
00208   if(pos==_child.end())
00209       return NULL;
00210   else
00211       return pos->second;
00212 }

void Omniifr::PersistNode::delnode const string &  name  ) 
 

Definition at line 121 of file PersistNode.cc.

References _child, and _separator.

Referenced by readnode().

00122 {
00123   string::size_type pos =name.find(_separator);
00124   // get reference to Next node in the path.
00125   map<string,PersistNode*>::iterator childpos =_child.find(name.substr(0,pos));
00126   if(childpos!=_child.end())
00127   {
00128     if(pos==string::npos) // leaf: delete leaf.
00129     {
00130       delete childpos->second;
00131       _child.erase(childpos);
00132     }
00133     else // branch: recurse
00134     {
00135       childpos->second->delnode(name.substr(pos+2));
00136     }
00137   }
00138 }

bool Omniifr::PersistNode::hasAttr const string &  key  )  const
 

Definition at line 153 of file PersistNode.cc.

References _attr.

00154 {
00155   return( _attr.find(key)!=_attr.end() );
00156 }

void Omniifr::PersistNode::output ostream &  os,
string  name
const
 

Definition at line 46 of file PersistNode.cc.

References _attr, _child, and _separator.

00047 {
00048   if(!name.empty()) // Don't output root node.
00049   {
00050     os<<name<<'\n';
00051     for(map<string,string>::const_iterator i=_attr.begin();
00052         i!=_attr.end();
00053         ++i)
00054     {
00055       os<<" "<<i->first<<"="<<i->second<<'\n';
00056     }
00057     os<<" ;;\n";
00058     name+=_separator;
00059   }
00060   for(map<string,PersistNode*>::const_iterator i=_child.begin();
00061       i!=_child.end();
00062      ++i)
00063   {
00064     i->second->output(os,name+i->first);
00065   }
00066 }

void Omniifr::PersistNode::outputCdrMemoryStream ostream &  os,
cdrMemoryStream &  memstr,
const char *  prefix = NULL
[static]
 

Writes an encoded version of the buffer to the output stream.

Definition at line 214 of file PersistNode.cc.

00219 {
00220   if(prefix)
00221       os<<prefix;
00222   char buf[3];
00223   CORBA::ULong len(memstr.bufSize());
00224   CORBA::Octet* ptr((CORBA::Octet*)memstr.bufPtr());
00225   for(CORBA::ULong i=0; i<len; ++i)
00226   {
00227     sprintf(buf,"%02x",ptr[i]);
00228     os.write(buf,2);
00229   }
00230 }

void Omniifr::PersistNode::outputIOR ostream &  os,
CORBA::Object_ptr  obj,
const char *  prefix = NULL
[static]
 

Writes an IOR to the output stream.

Definition at line 232 of file PersistNode.cc.

00237 {
00238   if(prefix)
00239       os<<prefix;
00240   CORBA::String_var iorstr =
00241     Repository_impl::inst()._orb->object_to_string(obj);
00242   os<<iorstr.in();
00243 }

bool Omniifr::PersistNode::readnode istream &  is  )  [inline]
 

Definition at line 69 of file PersistNode.cc.

References addattr(), addnode(), delnode(), and readtoken().

Referenced by PersistNode().

00070 {
00071   PersistNode* node =NULL;
00072   string tok;
00073   while(true)
00074   {
00075     if(!readtoken(is,tok) || tok==";;")
00076         return bool(node);
00077     else if(node)
00078         node->addattr(tok);
00079     else if(tok[0]=='-')
00080         delnode(tok.substr(1));
00081     else
00082         node=addnode(tok);
00083   }
00084 }

bool Omniifr::PersistNode::readtoken istream &  is,
string &  tok
[inline]
 

Definition at line 86 of file PersistNode.cc.

Referenced by readnode().

00087 {
00088   while(is)
00089   {
00090     is>>tok;
00091     if(tok.empty())
00092         break;
00093     if(tok[0]!='#')
00094         return true;
00095     is.ignore(INT_MAX,'\n');
00096   }
00097   return false;
00098 }


Member Data Documentation

map<string,string> Omniifr::PersistNode::_attr
 

Definition at line 76 of file PersistNode.h.

Referenced by addattr(), attrCdrStream(), attrLong(), attrString(), hasAttr(), and output().

map<string,PersistNode*> Omniifr::PersistNode::_child
 

Definition at line 75 of file PersistNode.h.

Referenced by addnode(), child(), delnode(), output(), Omniifr::Repository_impl::reincarnate(), and ~PersistNode().

const char * Omniifr::PersistNode::_separator = "::" [static]
 

Separator for node names ("::").

Definition at line 33 of file PersistNode.cc.

Referenced by addnode(), delnode(), and output().


The documentation for this class was generated from the following files:
Generated on Fri Mar 4 13:04:06 2005 for OmniIFR by  doxygen 1.4.1