1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>PersistNode.cc Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.2.15 -->
<center>
<a class="qindex" href="main.html">Main Page</a> <a class="qindex" href="namespaces.html">Namespace List</a> <a class="qindex" href="hierarchy.html">Class Hierarchy</a> <a class="qindex" href="annotated.html">Compound List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="namespacemembers.html">Namespace Members</a> <a class="qindex" href="functions.html">Compound Members</a> <a class="qindex" href="globals.html">File Members</a> </center>
<hr><h1>PersistNode.cc</h1><a href="a00116.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <font class="comment">// Package : omniEvents</font>
00002 <font class="comment">// PersistNode.cc Created : 2004/04/29</font>
00003 <font class="comment">// Author : Alex Tingle</font>
00004 <font class="comment">//</font>
00005 <font class="comment">// Copyright (C) 2004 Alex Tingle.</font>
00006 <font class="comment">//</font>
00007 <font class="comment">// This file is part of the omniEvents application.</font>
00008 <font class="comment">//</font>
00009 <font class="comment">// omniEvents is free software; you can redistribute it and/or</font>
00010 <font class="comment">// modify it under the terms of the GNU Lesser General Public</font>
00011 <font class="comment">// License as published by the Free Software Foundation; either</font>
00012 <font class="comment">// version 2.1 of the License, or (at your option) any later version.</font>
00013 <font class="comment">//</font>
00014 <font class="comment">// omniEvents is distributed in the hope that it will be useful,</font>
00015 <font class="comment">// but WITHOUT ANY WARRANTY; without even the implied warranty of</font>
00016 <font class="comment">// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU</font>
00017 <font class="comment">// Lesser General Public License for more details.</font>
00018 <font class="comment">//</font>
00019 <font class="comment">// You should have received a copy of the GNU Lesser General Public</font>
00020 <font class="comment">// License along with this library; if not, write to the Free Software</font>
00021 <font class="comment">// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</font>
00022 <font class="comment">//</font>
00023
00024 <font class="preprocessor">#include "<a class="code" href="a00117.html">PersistNode.h</a>"</font>
00025
00026 <font class="preprocessor">#include <stdlib.h></font>
00027 <font class="preprocessor">#include <stdio.h></font>
00028
00029 <font class="keyword">namespace </font>OmniEvents {
00030
00031
<a name="l00032"></a><a class="code" href="a00060.html#a1">00032</a> PersistNode::PersistNode(istream& is)
00033 {
00034 <font class="keywordflow">while</font>( <a class="code" href="a00060.html#a4">readnode</a>(is) ){}
00035 }
00036
<a name="l00037"></a><a class="code" href="a00060.html#a2">00037</a> PersistNode::~PersistNode()
00038 {
00039 <font class="keywordflow">for</font>(map<string,PersistNode*>::iterator i=<a class="code" href="a00060.html#m0">_child</a>.begin(); i!=<a class="code" href="a00060.html#m0">_child</a>.end(); ++i)
00040 <font class="keyword">delete</font> i->second;
00041 }
00042
<a name="l00043"></a><a class="code" href="a00060.html#a3">00043</a> <font class="keywordtype">void</font> PersistNode::output(ostream& os,string name)<font class="keyword"> const</font>
00044 <font class="keyword"></font>{
00045 <font class="keywordflow">if</font>(!name.empty()) <font class="comment">// Don't output root node.</font>
00046 {
00047 os<<name<<<font class="charliteral">'\n'</font>;
00048 <font class="keywordflow">for</font>(map<string,string>::const_iterator i=<a class="code" href="a00060.html#m1">_attr</a>.begin();
00049 i!=<a class="code" href="a00060.html#m1">_attr</a>.end();
00050 ++i)
00051 {
00052 os<<<font class="stringliteral">" "</font><<i->first<<<font class="stringliteral">"="</font><<i->second<<<font class="charliteral">'\n'</font>;
00053 }
00054 os<<<font class="stringliteral">" ;;\n"</font>;
00055 name+=<font class="stringliteral">"/"</font>;
00056 }
00057 <font class="keywordflow">for</font>(map<string,PersistNode*>::const_iterator i=<a class="code" href="a00060.html#m0">_child</a>.begin();
00058 i!=<a class="code" href="a00060.html#m0">_child</a>.end();
00059 ++i)
00060 {
00061 i->second->output(os,name+i->first);
00062 }
00063 }
00064
00065
<a name="l00066"></a><a class="code" href="a00060.html#a4">00066</a> <font class="keyword">inline</font> <font class="keywordtype">bool</font> PersistNode::readnode(istream& is)
00067 {
00068 <a class="code" href="a00060.html">PersistNode</a>* node =NULL;
00069 string tok;
00070 <font class="keywordflow">while</font>(true)
00071 {
00072 <font class="keywordflow">if</font>(!<a class="code" href="a00060.html#a5">readtoken</a>(is,tok) || tok==<font class="stringliteral">";;"</font>)
00073 <font class="keywordflow">return</font> bool(node);
00074 <font class="keywordflow">else</font> <font class="keywordflow">if</font>(node)
00075 node-><a class="code" href="a00060.html#a8">addattr</a>(tok);
00076 <font class="keywordflow">else</font> <font class="keywordflow">if</font>(tok[0]==<font class="charliteral">'-'</font>)
00077 <a class="code" href="a00060.html#a7">delnode</a>(tok.substr(1));
00078 <font class="keywordflow">else</font>
00079 node=<a class="code" href="a00060.html#a6">addnode</a>(tok);
00080 }
00081 }
00082
<a name="l00083"></a><a class="code" href="a00060.html#a5">00083</a> <font class="keyword">inline</font> <font class="keywordtype">bool</font> PersistNode::readtoken(istream& is, string& tok)
00084 {
00085 <font class="keywordflow">while</font>(is)
00086 {
00087 is>>tok;
00088 <font class="keywordflow">if</font>(tok.empty())
00089 <font class="keywordflow">break</font>;
00090 <font class="keywordflow">if</font>(tok[0]!=<font class="charliteral">'#'</font>)
00091 <font class="keywordflow">return</font> <font class="keyword">true</font>;
00092 is.ignore(INT_MAX,<font class="charliteral">'\n'</font>);
00093 }
00094 <font class="keywordflow">return</font> <font class="keyword">false</font>;
00095 }
00096
<a name="l00097"></a><a class="code" href="a00060.html#a6">00097</a> <a class="code" href="a00060.html">PersistNode</a>* PersistNode::addnode(<font class="keyword">const</font> string& name)
00098 {
00099 string::size_type pos =name.find(<font class="charliteral">'/'</font>);
00100 <font class="comment">// get reference to Next node in the path.</font>
00101 <a class="code" href="a00060.html">PersistNode</a>*& newchild =<a class="code" href="a00060.html#m0">_child</a>[name.substr(0,pos)];
00102
00103 <font class="keywordflow">if</font>(pos==string::npos) <font class="comment">// leaf: add new leaf.</font>
00104 {
00105 <font class="keywordflow">if</font>(newchild)
00106 <font class="keyword">delete</font> newchild; <font class="comment">// overwrite old leaf (and its children)</font>
00107 newchild=<font class="keyword">new</font> <a class="code" href="a00060.html#a0">PersistNode</a>();
00108 <font class="keywordflow">return</font> newchild;
00109 }
00110 <font class="keywordflow">else</font> <font class="comment">// branch: just add the branch if it's missing, and then recurse.</font>
00111 {
00112 <font class="keywordflow">if</font>(!newchild)
00113 newchild=<font class="keyword">new</font> <a class="code" href="a00060.html#a0">PersistNode</a>();
00114 <font class="keywordflow">return</font> newchild-><a class="code" href="a00060.html#a6">addnode</a>(name.substr(pos+1));
00115 }
00116 }
00117
<a name="l00118"></a><a class="code" href="a00060.html#a7">00118</a> <font class="keywordtype">void</font> PersistNode::delnode(<font class="keyword">const</font> string& name)
00119 {
00120 string::size_type pos =name.find(<font class="charliteral">'/'</font>);
00121 <font class="comment">// get reference to Next node in the path.</font>
00122 map<string,PersistNode*>::iterator childpos =<a class="code" href="a00060.html#m0">_child</a>.find(name.substr(0,pos));
00123 <font class="keywordflow">if</font>(childpos!=<a class="code" href="a00060.html#m0">_child</a>.end())
00124 {
00125 <font class="keywordflow">if</font>(pos==string::npos) <font class="comment">// leaf: delete leaf.</font>
00126 {
00127 <font class="keyword">delete</font> childpos->second;
00128 <a class="code" href="a00060.html#m0">_child</a>.erase(childpos);
00129 }
00130 <font class="keywordflow">else</font> <font class="comment">// branch: recurse</font>
00131 {
00132 childpos->second->delnode(name.substr(pos+1));
00133 }
00134 }
00135 }
00136
<a name="l00137"></a><a class="code" href="a00060.html#a8">00137</a> <font class="keywordtype">void</font> PersistNode::addattr(<font class="keyword">const</font> string& keyvalue)
00138 {
00139 string::size_type pos =keyvalue.find(<font class="charliteral">'='</font>);
00140 <a class="code" href="a00060.html#m1">_attr</a>[keyvalue.substr(0,pos)]=(pos==string::npos?<font class="stringliteral">""</font>:keyvalue.substr(pos+1));
00141 }
00142
<a name="l00143"></a><a class="code" href="a00060.html#a9">00143</a> <font class="keywordtype">void</font> PersistNode::addattr(<font class="keyword">const</font> string& key, <font class="keywordtype">long</font> value)
00144 {
00145 <font class="keywordtype">char</font> buf[64];
00146 sprintf(buf,<font class="stringliteral">"%i"</font>,value);
00147 <a class="code" href="a00060.html#m1">_attr</a>[key]=string(buf);
00148 }
00149
<a name="l00150"></a><a class="code" href="a00060.html#a10">00150</a> <font class="keywordtype">bool</font> PersistNode::hasAttr(<font class="keyword">const</font> string& key)<font class="keyword"> const</font>
00151 <font class="keyword"></font>{
00152 <font class="keywordflow">return</font>( <a class="code" href="a00060.html#m1">_attr</a>.find(key)!=<a class="code" href="a00060.html#m1">_attr</a>.end() );
00153 }
<a name="l00154"></a><a class="code" href="a00060.html#a11">00154</a> string PersistNode::attrString(<font class="keyword">const</font> string& key, <font class="keyword">const</font> string& fallback)<font class="keyword"> const</font>
00155 <font class="keyword"></font>{
00156 map<string,string>::const_iterator pos=<a class="code" href="a00060.html#m1">_attr</a>.find(key);
00157 <font class="keywordflow">if</font>(pos==<a class="code" href="a00060.html#m1">_attr</a>.end())
00158 <font class="keywordflow">return</font> fallback;
00159 <font class="keywordflow">else</font>
00160 <font class="keywordflow">return</font> pos->second;
00161 }
<a name="l00162"></a><a class="code" href="a00060.html#a12">00162</a> <font class="keywordtype">long</font> PersistNode::attrLong(<font class="keyword">const</font> string& key, <font class="keywordtype">long</font> fallback)<font class="keyword"> const</font>
00163 <font class="keyword"></font>{
00164 map<string,string>::const_iterator pos=<a class="code" href="a00060.html#m1">_attr</a>.find(key);
00165 <font class="keywordflow">if</font>(pos==<a class="code" href="a00060.html#m1">_attr</a>.end())
00166 <font class="keywordflow">return</font> fallback;
00167 <font class="keywordflow">else</font>
00168 <font class="keywordflow">return</font> atol(pos->second.c_str());
00169 }
<a name="l00170"></a><a class="code" href="a00060.html#a13">00170</a> <a class="code" href="a00060.html">PersistNode</a>* PersistNode::child(<font class="keyword">const</font> string& key)<font class="keyword"> const</font>
00171 <font class="keyword"></font>{
00172 map<string,PersistNode*>::const_iterator pos=<a class="code" href="a00060.html#m0">_child</a>.find(key);
00173 <font class="keywordflow">if</font>(pos==<a class="code" href="a00060.html#m0">_child</a>.end())
00174 <font class="keywordflow">return</font> NULL;
00175 <font class="keywordflow">else</font>
00176 <font class="keywordflow">return</font> pos->second;
00177 }
00178
00179 }; <font class="comment">// end namespace OmniEvents</font>
</pre></div><hr><address align="right"><small>Generated on Fri Nov 19 17:42:20 2004 for OmniEvents by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0
width=110 height=53></a>1.2.15 </small></address>
</body>
</html>
|