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