otsdaq  v2_01_00
ConfigurationGroupKey.cc
1 #include "otsdaq-core/ConfigurationDataFormats/ConfigurationGroupKey.h"
2 #include "otsdaq-core/Macros/CoutMacros.h"
3 
4 #include <string.h> //for strlen
5 #include <stdexcept> //for runtime_error
6 
7 using namespace ots;
8 
9 const unsigned int ConfigurationGroupKey::INVALID = -1;
10 const unsigned int ConfigurationGroupKey::DEFAULT = 0;
11 
12 //==============================================================================
13 ConfigurationGroupKey::ConfigurationGroupKey(unsigned int key) :
14  key_(key)
15 {}
16 
17 //==============================================================================
18 //groupString parameter can be the full group name, or just the group key
19 ConfigurationGroupKey::ConfigurationGroupKey(char * const &groupString)
20 {
21  if(!groupString) {key_ = ConfigurationGroupKey::INVALID; return; }
22 
23  //find last character that is not part of key
24  // key consists of numeric, dash, underscore, and period
25  int i = strlen(groupString)-1;
26  for(; i >= 0; --i)
27  if((groupString[i] < '0' || groupString[i] > '9') &&
28  groupString[i] != '-' && groupString[i] != '_' && groupString[i] != '.')
29  break; //not part of key,... likely a 'v' if using "_v" syntax for version
30 
31  if(i == (int)strlen(groupString)-1) //no key characters found
32  {
33  key_ = ConfigurationGroupKey::DEFAULT;
34  return;
35  }
36  else if(i < 0) //only key characters found, so assume group key string was given
37  i = 0;
38  else
39  ++i;
40 
41  //at this point, i is start of key sequence
42  sscanf(&groupString[i],"%u",&key_);
43 }
44 
45 //==============================================================================
46 ConfigurationGroupKey::ConfigurationGroupKey(const std::string &groupString)
47 : ConfigurationGroupKey((char *)groupString.c_str())
48 {}
49 
50 //==============================================================================
51 ConfigurationGroupKey::~ConfigurationGroupKey(void)
52 {}
53 
54 //==============================================================================
55 unsigned int ConfigurationGroupKey::key(void) const
56 {
57  return key_;
58 }
59 
60 //==============================================================================
61 //operator==
62 bool ConfigurationGroupKey::operator==(unsigned int key) const
63 {
64  return (key_ == key);
65 }
66 bool ConfigurationGroupKey::operator==(const ConfigurationGroupKey& key) const
67 {
68  return (key_ == key.key_);
69 }
70 
71 //==============================================================================
72 //toString
73 std::string ConfigurationGroupKey::toString(void) const
74 {
75  //represent invalid/temporary versions as negative number strings
76  return (isInvalid())?std::to_string((int)key_):
77  std::to_string(key_);
78 }
79 
80 //==============================================================================
81 //assignment operator with type int
82 ConfigurationGroupKey& ConfigurationGroupKey::operator=(const unsigned int key)
83 {
84  key_ = key;
85  return *this;
86 }
87 
88 //==============================================================================
89 bool ConfigurationGroupKey::operator!=(unsigned int key) const
90 {
91  return (key_ != key);
92 }
93 bool ConfigurationGroupKey::operator!=(const ConfigurationGroupKey& key) const
94 {
95  return (key_ != key.key_);
96 }
97 
98 //==============================================================================
99 //operator<
100 bool ConfigurationGroupKey::operator<(const ConfigurationGroupKey& key) const
101 {
102  return (key_ < key.key_);
103 }
104 
105 //==============================================================================
106 //operator>
107 bool ConfigurationGroupKey::operator>(const ConfigurationGroupKey& key) const
108 {
109  return (key_ > key.key_);
110 }
111 
112 //==============================================================================
113 //isInvalid
114 bool ConfigurationGroupKey::isInvalid() const
115 {
116  return (key_ == INVALID);
117 }
118 
119 //==============================================================================
120 //getNextKey
121 // returns next key given the most recent key
122 // if given nothing returns DEFAULT as first key
123 // if given 0, returns 1, etc.
124 // if no available keys left return INVALID
125 ConfigurationGroupKey ConfigurationGroupKey::getNextKey(const ConfigurationGroupKey& key)
126 {
127  ConfigurationGroupKey retKey(key.key_+1); //DEFAULT := INVALID + 1
128  return retKey; //if retKey is invalid, then INVALID is returned already
129 }
130 
131 //==============================================================================
132 const unsigned int ConfigurationGroupKey::getDefaultKey(void)
133 {
134  return DEFAULT;
135 }
136 
137 //==============================================================================
138 const unsigned int ConfigurationGroupKey::getInvalidKey(void)
139 {
140  return INVALID;
141 }
142 
143 //==============================================================================
144 //getGroupNameWithKey
145 // returns next key given the most recent key
146 // if given nothing returns DEFAULT as first key
147 // if given 0, returns 1, etc.
148 // if no available keys left return INVALID
149 std::string ConfigurationGroupKey::getFullGroupString(const std::string &groupName,
150  const ConfigurationGroupKey& key)
151 {
152  if(groupName.size() == 0)
153  {
154 
155  __SS__ << ("ConfigurationGroupKey::getFullGroupString() Illegal Group Name! The Group Name was not provided.\n");
156  __COUT_ERR__ << ss.str();
157  throw std::runtime_error(ss.str());
158  }
159  else if(groupName.size() == 1)
160  {
161  __SS__ << ("ConfigurationGroupKey::getFullGroupString() Illegal Group Name! The Group Name is too short: \"" +
162  groupName + "\"") << std::endl;
163  __COUT_ERR__ << ss.str();
164  throw std::runtime_error(ss.str());
165  }
166  else
167  {
168  for(unsigned int i=0;i<groupName.size();++i)
169  {
170  if(!( //alphaNumeric
171  (groupName[i] >= 'A' &&groupName[i] <= 'Z') ||
172  (groupName[i] >= 'a' &&groupName[i] <= 'z') ||
173  (groupName[i] >= '0' &&groupName[i] <= '9') ) )
174  {
175  __SS__ << ("ConfigurationGroupKey::getFullGroupString() Illegal Group Name! Group Name must be alpha-numeric: \"" +
176  groupName + "\"") << std::endl;
177  __COUT_ERR__ << ss.str();
178  throw std::runtime_error(ss.str());
179  }
180  }
181  }
182 
183  return groupName + "_v" + std::to_string(key.key_);
184 }
185 
186 //==============================================================================
187 void ConfigurationGroupKey::getGroupNameAndKey(const std::string &fullGroupString,
188  std::string &groupName, ConfigurationGroupKey& key)
189 {
190  auto i = fullGroupString.rfind("_v");
191  groupName = fullGroupString.substr(0,i);
192  key = ConfigurationGroupKey(fullGroupString.substr(i+2));
193 }
194 
195 
196 
197 
198 
199 
200 
201 
202 
203 
204 
205 
206 
207 
208 
209 
210 
211 
212 
213