$treeview $search $mathjax $extrastylesheet
otsdaq
v2_03_00
$projectbrief
|
$projectbrief
|
$searchbox |
00001 #include "otsdaq-core/TableCore/TableGroupKey.h" 00002 00003 #include "otsdaq-core/Macros/CoutMacros.h" 00004 00005 #include <string.h> //for strlen 00006 #include <stdexcept> //for runtime_error 00007 00008 using namespace ots; 00009 00010 const unsigned int TableGroupKey::INVALID = -1; 00011 const unsigned int TableGroupKey::DEFAULT = 0; 00012 00013 //============================================================================== 00014 TableGroupKey::TableGroupKey(unsigned int key) : key_(key) {} 00015 00016 //============================================================================== 00017 // groupString parameter can be the full group name, or just the group key 00018 TableGroupKey::TableGroupKey(char* const& groupString) 00019 { 00020 if(!groupString) 00021 { 00022 key_ = TableGroupKey::INVALID; 00023 return; 00024 } 00025 00026 // find last character that is not part of key 00027 // key consists of numeric, dash, underscore, and period 00028 int i = strlen(groupString) - 1; 00029 for(; i >= 0; --i) 00030 if((groupString[i] < '0' || groupString[i] > '9') && groupString[i] != '-' && 00031 groupString[i] != '_' && groupString[i] != '.') 00032 break; // not part of key,... likely a 'v' if using "_v" syntax for version 00033 00034 if(i == (int)strlen(groupString) - 1) // no key characters found 00035 { 00036 key_ = TableGroupKey::DEFAULT; 00037 return; 00038 } 00039 else if(i < 0) // only key characters found, so assume group key string was given 00040 i = 0; 00041 else 00042 ++i; 00043 00044 // at this point, i is start of key sequence 00045 sscanf(&groupString[i], "%u", &key_); 00046 } 00047 00048 //============================================================================== 00049 TableGroupKey::TableGroupKey(const std::string& groupString) 00050 : TableGroupKey((char*)groupString.c_str()) 00051 { 00052 } 00053 00054 //============================================================================== 00055 TableGroupKey::~TableGroupKey(void) {} 00056 00057 //============================================================================== 00058 unsigned int TableGroupKey::key(void) const { return key_; } 00059 00060 //============================================================================== 00061 // operator== 00062 bool TableGroupKey::operator==(unsigned int key) const { return (key_ == key); } 00063 bool TableGroupKey::operator==(const TableGroupKey& key) const 00064 { 00065 return (key_ == key.key_); 00066 } 00067 00068 //============================================================================== 00069 // toString 00070 std::string TableGroupKey::toString(void) const 00071 { 00072 // represent invalid/temporary versions as negative number strings 00073 return (isInvalid()) ? std::to_string((int)key_) : std::to_string(key_); 00074 } 00075 00076 //============================================================================== 00077 // assignment operator with type int 00078 TableGroupKey& TableGroupKey::operator=(const unsigned int key) 00079 { 00080 key_ = key; 00081 return *this; 00082 } 00083 00084 //============================================================================== 00085 bool TableGroupKey::operator!=(unsigned int key) const { return (key_ != key); } 00086 bool TableGroupKey::operator!=(const TableGroupKey& key) const 00087 { 00088 return (key_ != key.key_); 00089 } 00090 00091 //============================================================================== 00092 // operator< 00093 bool TableGroupKey::operator<(const TableGroupKey& key) const 00094 { 00095 return (key_ < key.key_); 00096 } 00097 00098 //============================================================================== 00099 // operator> 00100 bool TableGroupKey::operator>(const TableGroupKey& key) const 00101 { 00102 return (key_ > key.key_); 00103 } 00104 00105 //============================================================================== 00106 // isInvalid 00107 bool TableGroupKey::isInvalid() const { return (key_ == INVALID); } 00108 00109 //============================================================================== 00110 // getNextKey 00111 // returns next key given the most recent key 00112 // if given nothing returns DEFAULT as first key 00113 // if given 0, returns 1, etc. 00114 // if no available keys left return INVALID 00115 TableGroupKey TableGroupKey::getNextKey(const TableGroupKey& key) 00116 { 00117 TableGroupKey retKey(key.key_ + 1); // DEFAULT := INVALID + 1 00118 return retKey; // if retKey is invalid, then INVALID is returned already 00119 } 00120 00121 //============================================================================== 00122 const unsigned int TableGroupKey::getDefaultKey(void) { return DEFAULT; } 00123 00124 //============================================================================== 00125 const unsigned int TableGroupKey::getInvalidKey(void) { return INVALID; } 00126 00127 //============================================================================== 00128 // getGroupNameWithKey 00129 // returns next key given the most recent key 00130 // if given nothing returns DEFAULT as first key 00131 // if given 0, returns 1, etc. 00132 // if no available keys left return INVALID 00133 std::string TableGroupKey::getFullGroupString(const std::string& groupName, 00134 const TableGroupKey& key) 00135 { 00136 if(groupName.size() == 0) 00137 { 00138 __SS__ 00139 << ("TableGroupKey::getFullGroupString() Illegal Group Name! The Group Name " 00140 "was not provided.\n"); 00141 __COUT_ERR__ << ss.str(); 00142 __SS_THROW__; 00143 } 00144 else if(groupName.size() == 1) 00145 { 00146 __SS__ << ("TableGroupKey::getFullGroupString() Illegal Group Name! The Group " 00147 "Name is too short: \"" + 00148 groupName + "\"") 00149 << std::endl; 00150 __COUT_ERR__ << ss.str(); 00151 __SS_THROW__; 00152 } 00153 else 00154 { 00155 for(unsigned int i = 0; i < groupName.size(); ++i) 00156 { 00157 if(!( // alphaNumeric 00158 (groupName[i] >= 'A' && groupName[i] <= 'Z') || 00159 (groupName[i] >= 'a' && groupName[i] <= 'z') || 00160 (groupName[i] >= '0' && groupName[i] <= '9'))) 00161 { 00162 __SS__ << ("TableGroupKey::getFullGroupString() Illegal Group Name! " 00163 "Group Name must be alpha-numeric: \"" + 00164 groupName + "\"") 00165 << std::endl; 00166 __COUT_ERR__ << ss.str(); 00167 __SS_THROW__; 00168 } 00169 } 00170 } 00171 00172 return groupName + "_v" + std::to_string(key.key_); 00173 } 00174 00175 //============================================================================== 00176 void TableGroupKey::getGroupNameAndKey(const std::string& fullGroupString, 00177 std::string& groupName, 00178 TableGroupKey& key) 00179 { 00180 auto i = fullGroupString.rfind("_v"); 00181 groupName = fullGroupString.substr(0, i); 00182 key = TableGroupKey(fullGroupString.substr(i + 2)); 00183 }