COPASI API  4.16.103
CCopasiObjectName.cpp
Go to the documentation of this file.
1 /* Begin CVS Header
2  $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/report/CCopasiObjectName.cpp,v $
3  $Revision: 1.14 $
4  $Name: $
5  $Author: shoops $
6  $Date: 2011/03/07 19:32:38 $
7  End CVS Header */
8 
9 // Copyright (C) 2011 - 2010 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., University of Heidelberg, and The University
11 // of Manchester.
12 // All rights reserved.
13 
14 // Copyright (C) 2008 by Pedro Mendes, Virginia Tech Intellectual
15 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
16 // and The University of Manchester.
17 // All rights reserved.
18 
19 // Copyright (C) 2001 - 2007 by Pedro Mendes, Virginia Tech Intellectual
20 // Properties, Inc. and EML Research, gGmbH.
21 // All rights reserved.
22 
23 /**
24  * Class CCopasiObjectName
25  *
26  * This class is the class for handling Copasi object names.
27  *
28  * Copyright Stefan Hoops 2002
29  */
30 
31 //TODO what does getName() vs. getObjectName do?
32 
33 #include <sstream>
34 
35 #include "copasi.h"
36 #include "CCopasiObjectName.h"
37 
38 using std::string;
39 
41  string()
42 {}
43 
44 CCopasiObjectName::CCopasiObjectName(const std::string & name):
45  string(name)
46 {}
47 
49  string(src)
50 {}
51 
53 {}
54 
56 {return substr(0, findEx(","));}
57 
59 {
60  std::string::size_type pos = findEx(",");
61 
62  if (pos == std::string::npos) return CCopasiObjectName();
63 
64  return substr(pos + 1);
65 }
66 
68 {
69  CCopasiObjectName Primary(getPrimary());
70 
71  return CCopasiObjectName::unescape(Primary.substr(0, Primary.findEx("=")));
72 }
73 
75 {
76  CCopasiObjectName Primary = getPrimary();
77  std::string::size_type pos = Primary.findEx("=");
78 
79  if (pos == std::string::npos) return "";
80 
81  CCopasiObjectName tmp = Primary.substr(pos + 1);
82  return CCopasiObjectName::unescape(tmp.substr(0, tmp.findEx("[")));
83 }
84 
85 size_t
86 CCopasiObjectName::getElementIndex(const size_t & pos) const
87 {
88  std::string Index = getElementName(pos);
89  std::stringstream tmp(Index);
90 
91  size_t index = C_INVALID_INDEX;
92 
93  tmp >> index;
94 
95  if (tmp.fail()) return C_INVALID_INDEX;
96 
97  tmp << index;
98 
99  if (Index != tmp.str()) return C_INVALID_INDEX;
100 
101  return index;
102 }
103 
104 std::string CCopasiObjectName::getElementName(const size_t & pos,
105  const bool & unescape) const
106 {
107  CCopasiObjectName Primary = getPrimary();
108 
109  std::string::size_type open = findEx("[");
110  size_t i;
111 
112  for (i = 0; i < pos && open != std::string::npos; i++)
113  open = findEx("[", open + 1);
114 
115  std::string::size_type close = findEx("]", open + 1);
116 
117  if (open == std::string::npos || close == std::string::npos) return "";
118 
119  if (unescape)
120  return CCopasiObjectName::unescape(Primary.substr(open + 1,
121  close - open - 1));
122 
123  return Primary.substr(open + 1, close - open - 1);
124 }
125 
126 std::string CCopasiObjectName::escape(const std::string & name)
127 {
128 #define toBeEscaped "\\[]=,>"
129  std::string Escaped(name);
130  std::string::size_type pos = Escaped.find_first_of(toBeEscaped);
131 
132  while (pos != std::string::npos)
133  {
134  Escaped.insert(pos, "\\");
135  pos += 2;
136  pos = Escaped.find_first_of(toBeEscaped, pos);
137  }
138 
139  return Escaped;
140 #undef toBeEscaped
141 }
142 
143 std::string CCopasiObjectName::unescape(const std::string & name)
144 {
145  std::string Unescaped(name);
146  std::string::size_type pos = Unescaped.find("\\");
147 
148  while (pos != std::string::npos)
149  {
150  Unescaped.erase(pos, 1);
151  pos++;
152  pos = Unescaped.find("\\", pos);
153  }
154 
155  return Unescaped;
156 }
157 
158 std::string::size_type
159 CCopasiObjectName::findEx(const std::string & toFind,
160  const std::string::size_type & pos) const
161 {
162  std::string::size_type where = find_first_of(toFind, pos);
163 
164  std::string::size_type tmp;
165 
166  while (where && where != std::string::npos)
167  {
168  tmp = find_last_not_of("\\", where - 1);
169 
170  if ((where - tmp) % 2)
171  return where;
172 
173  where = find_first_of(toFind, where + 1);
174  }
175 
176  return where;
177 }
178 
179 //********** CRegisteredObjectName ***************
180 
181 std::set<CRegisteredObjectName*> CRegisteredObjectName::mSet;
182 
185 {
186  mSet.insert(this);
187 }
188 
190  CCopasiObjectName(name)
191 {
192  mSet.insert(this);
193 }
194 
196  CCopasiObjectName(src)
197 {
198  mSet.insert(this);
199 }
200 
202 {
203  mSet.erase(this);
204 }
size_t getElementIndex(const size_t &pos=0) const
CCopasiObjectName getRemainder() const
static std::set< CRegisteredObjectName * > mSet
#define C_INVALID_INDEX
Definition: copasi.h:222
std::string getObjectType() const
static std::string unescape(const std::string &name)
static std::string escape(const std::string &name)
CCopasiObjectName getPrimary() const
std::string getElementName(const size_t &pos, const bool &unescape=true) const
std::string::size_type findEx(const std::string &toFind, const std::string::size_type &pos=0) const
std::string getObjectName() const
#define toBeEscaped