1 #ifndef _ots_ConfigurationView_h_
2 #define _ots_ConfigurationView_h_
4 #include "otsdaq-core/ConfigurationDataFormats/ViewColumnInfo.h"
5 #include "otsdaq-core/MessageFacility/MessageFacility.h"
6 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
7 #include "otsdaq-core/ConfigurationDataFormats/ConfigurationVersion.h"
18 std::string ots_demangle(
const char* name);
28 static const unsigned int INVALID;
29 typedef std::vector<std::vector<std::string> > DataView;
30 typedef DataView::iterator iterator;
31 typedef DataView::const_iterator const_iterator;
44 unsigned int findRow(
unsigned int col,
const T& value,
45 unsigned int offsetRow=0)
const
47 std::istringstream s(value);
48 return findRow(col,s.str(),offsetRow);
50 unsigned int findRow (
unsigned int col,
const std::string& value,
unsigned int offsetRow=0)
const;
53 unsigned int findRowInGroup(
unsigned int col,
const T& value,
54 const std::string &groupId,
const std::string &childLinkIndex,
unsigned int offsetRow=0)
const
56 std::istringstream s(value);
57 return findRowInGroup(col,s.str(),groupId,childLinkIndex,offsetRow);
59 unsigned int findRowInGroup (
unsigned int col,
const std::string &value,
const std::string &groupId,
const std::string &childLinkIndex,
unsigned int offsetRow=0)
const;
60 unsigned int findCol (
const std::string &name)
const;
61 unsigned int findColByType (
const std::string &type,
int startingCol = 0)
const;
64 const std::string& getUniqueStorageIdentifier (
void)
const;
65 const std::string& getTableName (
void)
const;
67 const std::string& getComment (
void)
const;
68 const std::string& getAuthor (
void)
const;
69 const time_t& getCreationTime (
void)
const;
70 const time_t& getLastAccessTime (
void)
const;
71 const bool& getLooseColumnMatching (
void)
const;
72 const unsigned int getSourceColumnSize (
void)
const;
73 const unsigned int& getSourceColumnMismatch (
void)
const;
74 const unsigned int& getSourceColumnMissing (
void)
const;
75 const std::set<std::string>& getSourceColumnNames (
void)
const;
76 std::set<std::string> getColumnNames (
void)
const;
77 std::set<std::string> getColumnStorageNames (
void)
const;
78 std::vector<std::string> getDefaultRowValues (
void)
const;
80 unsigned int getNumberOfRows (
void)
const;
81 unsigned int getNumberOfColumns (
void)
const;
82 const unsigned int getColUID (
void)
const;
88 bool isEntryInGroupCol (
const unsigned int &row,
const unsigned int &groupCol,
const std::string &groupNeedle, std::set<std::string> *groupIDList = 0)
const;
91 std::set<std::string> getSetOfGroupIDs (
const std::string &childLinkIndex,
unsigned int row = -1)
const;
92 bool isEntryInGroup (
const unsigned int &row,
const std::string &childLinkIndex,
const std::string &groupNeedle)
const;
93 const bool getChildLink (
const unsigned int &col,
bool *isGroup, std::pair<unsigned int /*link col*/, unsigned int /*link id col*/> *linkPair)
const;
94 const unsigned int getColLinkGroupID (
const std::string &childLinkIndex)
const;
95 void addRowToGroup (
const unsigned int &row,
const unsigned int &col,
const std::string &groupID,
const std::string &colDefault);
96 void removeRowFromGroup (
const unsigned int &row,
const unsigned int &col,
const std::string &groupID);
103 void getValue(T& value,
unsigned int row,
unsigned int col,
bool convertEnvironmentVariables=
true)
const
105 if(!(col < columnsInfo_.size() && row < getNumberOfRows()))
106 throw std::runtime_error(
"Invalid row col requested");
108 if(columnsInfo_[col].getDataType() == ViewColumnInfo::DATATYPE_NUMBER)
110 std::string data = convertEnvironmentVariables?convertEnvVariables(theDataView_[row][col]):
111 theDataView_[row][col];
115 __SS__ << (data +
" is not a number!") << std::endl;
116 __MOUT__ <<
"\n" << ss.str();
117 throw std::runtime_error(ss.str());
120 if(
typeid(
double) ==
typeid(value))
121 value = strtod(data.c_str(),0);
122 else if(
typeid(
float) ==
typeid(value))
123 value = strtof(data.c_str(),0);
124 else if(data.size() > 2 && data[1] ==
'x')
125 value = strtol(data.c_str(),0,16);
126 else if(data.size() > 1 && data[0] ==
'b')
127 value = strtol(data.substr(1).c_str(),0,2);
129 value = strtol(data.c_str(),0,10);
131 else if(columnsInfo_[col].getType() == ViewColumnInfo::TYPE_FIXED_CHOICE_DATA &&
132 columnsInfo_[col].getDataType() == ViewColumnInfo::DATATYPE_STRING &&
133 (
typeid(int) ==
typeid(value) ||
134 typeid(
unsigned int) ==
typeid(value)))
141 if(theDataView_[row][col] == ViewColumnInfo::DATATYPE_STRING_DEFAULT)
145 std::vector<std::string> choices = columnsInfo_[col].getDataChoices();
146 for(value=1;value-1<(T)choices.size();++value)
147 if(theDataView_[row][col] == choices[value-1])
150 __SS__ <<
"\tInvalid value for column data type: " << columnsInfo_[col].getDataType()
151 <<
" in configuration " << tableName_
152 <<
" at column=" << columnsInfo_[col].getName()
153 <<
" for getValue with type '" << ots_demangle(
typeid(value).name())
155 <<
"Attempting to get index of '" << theDataView_[row][col]
156 <<
" in fixed choice array, but was not found in array. "
157 <<
"Here are the valid choices:\n";
158 ss <<
"\t" << ViewColumnInfo::DATATYPE_STRING_DEFAULT <<
"\n";
159 for(
const auto &choice:choices)
160 ss <<
"\t" << choice <<
"\n";
161 __MOUT__ <<
"\n" << ss.str();
162 throw std::runtime_error(ss.str());
165 else if(columnsInfo_[col].getDataType() == ViewColumnInfo::DATATYPE_STRING &&
166 typeid(bool) ==
typeid(value))
168 if(columnsInfo_[col].getType() == ViewColumnInfo::TYPE_ON_OFF)
169 value = (theDataView_[row][col] == ViewColumnInfo::TYPE_VALUE_ON) ?
true:
false;
170 else if(columnsInfo_[col].getType() == ViewColumnInfo::TYPE_TRUE_FALSE)
171 value = (theDataView_[row][col] == ViewColumnInfo::TYPE_VALUE_TRUE) ?
true:
false;
172 else if(columnsInfo_[col].getType() == ViewColumnInfo::TYPE_YES_NO)
173 value = (theDataView_[row][col] == ViewColumnInfo::TYPE_VALUE_YES) ?
true:
false;
177 if(columnsInfo_[col].getType() == ViewColumnInfo::TYPE_FIXED_CHOICE_DATA)
178 __MOUT_WARN__ <<
"For column type " << ViewColumnInfo::TYPE_FIXED_CHOICE_DATA
179 <<
" the only valid numeric types are 'int' and 'unsigned int.'";
181 __SS__ <<
"\tUnrecognized column data type: " << columnsInfo_[col].getDataType()
182 <<
" and column type: " << columnsInfo_[col].getType()
183 <<
", in configuration " << tableName_
184 <<
" at column=" << columnsInfo_[col].getName()
185 <<
" for getValue with type '" << ots_demangle(
typeid(value).name())
187 throw std::runtime_error(ss.str());
190 void getValue(std::string &value,
unsigned int row,
unsigned int col,
bool convertEnvironmentVariables=
true)
const;
191 std::string getValueAsString(
unsigned int row,
unsigned int col,
bool convertEnvironmentVariables=
true)
const;
192 std::string getEscapedValueAsString(
unsigned int row,
unsigned int col,
bool convertEnvironmentVariables=
true)
const;
193 bool isURIEncodedCommentTheSame(
const std::string &comment)
const;
195 const DataView& getDataView (
void)
const;
196 const std::vector<ViewColumnInfo>& getColumnsInfo (
void)
const;
197 std::vector<ViewColumnInfo>* getColumnsInfoP(
void);
202 void setUniqueStorageIdentifier (
const std::string &storageUID);
203 void setTableName (
const std::string &name );
204 void setComment (
const std::string &comment );
205 void setURIEncodedComment (
const std::string &uriComment );
206 void setAuthor (
const std::string &author );
207 void setCreationTime (time_t t );
208 void setLastAccessTime (time_t t = time(0) );
209 void setLooseColumnMatching (
bool setValue );
213 void setVersion (
const T &version)
222 void setValue(
const T &value,
unsigned int row,
unsigned int col)
224 if(!(col < columnsInfo_.size() && row < getNumberOfRows()))
226 __SS__ <<
"Invalid row (" << row <<
") col (" << col <<
") requested!" << std::endl;
227 throw std::runtime_error(ss.str());
230 if(columnsInfo_[col].getDataType() == ViewColumnInfo::DATATYPE_NUMBER)
232 std::stringstream ss;
234 theDataView_[row][col] = ss.str();
236 else if(columnsInfo_[col].getDataType() == ViewColumnInfo::DATATYPE_TIME &&
237 typeid(time_t) ==
typeid(value))
240 std::stringstream ss;
242 theDataView_[row][col] = ss.str();
246 __SS__ <<
"\tUnrecognized view data type: " << columnsInfo_[col].getDataType()
247 <<
" in configuration " << tableName_
248 <<
" at column=" << columnsInfo_[col].getName()
249 <<
" for setValue with type '" << ots_demangle(
typeid(value).name())
251 throw std::runtime_error(ss.str());
254 void setValue (
const std::string &value,
unsigned int row,
unsigned int col);
255 void setValue (
const char *value,
unsigned int row,
unsigned int col);
258 void setValueAsString (
const std::string &value,
unsigned int row,
unsigned int col);
261 void resizeDataView (
unsigned int nRows,
unsigned int nCols);
263 void deleteRow (
int r);
268 iterator begin (
void) {
return theDataView_.begin();}
269 iterator end (
void) {
return theDataView_.end();}
270 const_iterator begin (
void)
const {
return theDataView_.begin();}
271 const_iterator end (
void)
const {
return theDataView_.end();}
273 void print (std::ostream &out = std::cout)
const;
274 void printJSON (std::ostream &out = std::cout)
const;
275 int fillFromJSON (
const std::string &json);
276 int fillFromCSV (
const std::string &data,
const int &dataOffset = 0,
const std::string &author =
"")
throw(std::runtime_error);
277 bool setURIEncodedValue (
const std::string &value,
const unsigned int &row,
const unsigned int &col);
279 static std::string decodeURIComponent (
const std::string& data);
282 const unsigned int getOrInitColUID (
void);
287 std::string convertEnvVariables (
const std::string& data)
const;
288 bool isNumber (
const std::string& s)
const;
290 std::string uniqueStorageIdentifier_;
291 std::string tableName_ ;
293 std::string comment_ ;
294 std::string author_ ;
295 time_t creationTime_ ;
296 time_t lastAccessTime_ ;
297 unsigned int colUID_ ;
298 std::map<std::string, unsigned int> colLinkGroupIDs_;
300 bool fillWithLooseColumnMatching_;
301 unsigned int sourceColumnMismatchCount_, sourceColumnMissingCount_;
302 std::set<std::string> sourceColumnNames_;
304 std::vector<ViewColumnInfo> columnsInfo_ ;
305 DataView theDataView_ ;