File: PersistNode.cc

package info (click to toggle)
omnievents 1%3A2.6.2-5.1%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,004 kB
  • sloc: cpp: 7,676; python: 3,138; sh: 2,575; xml: 2,057; java: 1,409; javascript: 551; makefile: 317; ansic: 9
file content (180 lines) | stat: -rw-r--r-- 4,489 bytes parent folder | download | duplicates (6)
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
//                            Package   : omniEvents
// PersistNode.cc             Created   : 2004/04/29
//                            Author    : Alex Tingle
//
//    Copyright (C) 2004 Alex Tingle.
//
//    This file is part of the omniEvents application.
//
//    omniEvents is free software; you can redistribute it and/or
//    modify it under the terms of the GNU Lesser General Public
//    License as published by the Free Software Foundation; either
//    version 2.1 of the License, or (at your option) any later version.
//
//    omniEvents is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//    Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public
//    License along with this library; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

#include "PersistNode.h"

#include <stdlib.h>
#include <stdio.h>
#include <climits>

namespace OmniEvents {


PersistNode::PersistNode(istream& is)
{
  while( readnode(is) ){}
}

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

void PersistNode::output(ostream& os,string name) const
{
  if(!name.empty()) // Don't output root node.
  {
    os<<name<<'\n';
    for(map<string,string>::const_iterator i=_attr.begin();
        i!=_attr.end();
        ++i)
    {
      os<<" "<<i->first<<"="<<i->second<<'\n';
    }
    os<<" ;;\n";
    name+="/";
  }
  for(map<string,PersistNode*>::const_iterator i=_child.begin();
      i!=_child.end();
     ++i)
  {
    i->second->output(os,name+i->first);
  }
}


inline bool PersistNode::readnode(istream& is)
{
  PersistNode* node =NULL;
  string tok;
  while(true)
  {
    if(!readtoken(is,tok) || tok==";;")
        return bool(node);
    else if(node)
        node->addattr(tok);
    else if(tok[0]=='-')
        delnode(tok.substr(1));
    else
        node=addnode(tok);
  }
}

inline bool PersistNode::readtoken(istream& is, string& tok)
{
  while(is)
  {
    is>>tok;
    if(tok.empty())
        break;
    if(tok[0]!='#')
        return true;
    is.ignore(INT_MAX,'\n');
  }
  return false;
}

PersistNode* PersistNode::addnode(const string& name)
{
  string::size_type pos =name.find('/');
  // get reference to Next node in the path.
  PersistNode*& newchild =_child[name.substr(0,pos)];

  if(pos==string::npos) // leaf: add new leaf.
  {
    if(newchild)
        delete newchild; // overwrite old leaf (and its children)
    newchild=new PersistNode();
    return newchild;
  }
  else // branch: just add the branch if it's missing, and then recurse.
  {
    if(!newchild)
        newchild=new PersistNode();
    return newchild->addnode(name.substr(pos+1));
  }
}

void PersistNode::delnode(const string& name)
{
  string::size_type pos =name.find('/');
  // get reference to Next node in the path.
  map<string,PersistNode*>::iterator childpos =_child.find(name.substr(0,pos));
  if(childpos!=_child.end())
  {
    if(pos==string::npos) // leaf: delete leaf.
    {
      delete childpos->second;
      _child.erase(childpos);
    }
    else // branch: recurse
    {
      childpos->second->delnode(name.substr(pos+1));
    }
  }
}

void PersistNode::addattr(const string& keyvalue)
{
  string::size_type pos =keyvalue.find('=');
  _attr[keyvalue.substr(0,pos)]=(pos==string::npos?"":keyvalue.substr(pos+1));
}

void PersistNode::addattr(const string& key, long value)
{
  char buf[64];
  sprintf(buf,"%i",value);
  _attr[key]=string(buf);
}

bool PersistNode::hasAttr(const string& key) const
{
  return( _attr.find(key)!=_attr.end() );
}
string PersistNode::attrString(const string& key, const string& fallback) const
{
  map<string,string>::const_iterator pos=_attr.find(key);
  if(pos==_attr.end())
      return fallback;
  else
      return pos->second;
}
long PersistNode::attrLong(const string& key, long fallback) const
{
  map<string,string>::const_iterator pos=_attr.find(key);
  if(pos==_attr.end())
      return fallback;
  else
      return atol(pos->second.c_str());
}
PersistNode* PersistNode::child(const string& key) const
{
  map<string,PersistNode*>::const_iterator pos=_child.find(key);
  if(pos==_child.end())
      return NULL;
  else
      return pos->second;
}

}; // end namespace OmniEvents