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