22 snprintf(buf,
sizeof(buf),
"%d", i);
69 msg +=
"Cannot find ";
92 msg +=
"JSON node type mismatch: cannot convert node type ";
94 msg +=
" to c++ type ";
106 template <
typename T>
127 if (*path==0 || *path==
'/')
138 assert(s->size() == n->size());
140 for (
unsigned i=0; i<s->size(); i++) {
141 if (strcasecmp(elem.c_str(), (*s)[i].c_str()) == 0) {
175 msg +=
" JSON node is ";
179 msg +=
" instead of subdirectory";
197 void ReadKey(
const char* varname,
int *tid,
int *num_values,
int *total_size,
int *item_size,
MVOdbError* error)
200 if (num_values) *num_values = 0;
201 if (total_size) *total_size = 0;
202 if (item_size) *item_size = 0;
209 if (last_written) *last_written = 0;
214 void ReadDir(std::vector<std::string>* varname, std::vector<int> *tid, std::vector<int> *num_values, std::vector<int> *total_size, std::vector<int> *item_size,
MVOdbError* error)
220 void RB(
const char* varname,
bool *value,
bool create,
MVOdbError* error)
222 RBAI(varname, 0, value, error);
225 void RI(
const char* varname,
int *value,
bool create,
MVOdbError* error)
227 RIAI(varname, 0, value, error);
230 void RD(
const char* varname,
double *value,
bool create,
MVOdbError* error)
232 RDAI(varname, 0, value, error);
235 void RF(
const char* varname,
float *value,
bool create,
MVOdbError* error)
237 RFAI(varname, 0, value, error);
240 void RS(
const char* varname, std::string *value,
bool create,
int create_string_length,
MVOdbError* error)
242 RSAI(varname, 0, value, error);
247 RU16AI(varname, 0, value, error);
252 RU32AI(varname, 0, value, error);
255 template <
typename T>
272 SetVarError(error, varname,
"JSON node is a subdirectory");
278 int num_values = a->size();
282 for (
int i=0; i<num_values; i++) {
285 bool ok = GetJsonValue<T>(varname, elem, &v, error);
292 bool ok = GetJsonValue<T>(varname, node, &v, error);
300 void RBA(
const char* varname, std::vector<bool> *value,
bool create,
int create_size,
MVOdbError* error)
302 RXA(varname, value, error);
305 void RIA(
const char* varname, std::vector<int> *value,
bool create,
int create_size,
MVOdbError* error)
307 RXA(varname, value, error);
310 void RDA(
const char* varname, std::vector<double> *value,
bool create,
int create_size,
MVOdbError* error)
312 RXA(varname, value, error);
315 void RFA(
const char* varname, std::vector<float> *value,
bool create,
int create_size,
MVOdbError* error)
317 RXA(varname, value, error);
320 void RSA(
const char* varname, std::vector<std::string> *value,
bool create,
int create_size,
int create_string_length,
MVOdbError* error)
322 RXA(varname, value, error);
325 void RU16A(
const char* varname, std::vector<uint16_t> *value,
bool create,
int create_size,
MVOdbError* error)
327 RXA(varname, value, error);
330 void RU32A(
const char* varname, std::vector<uint32_t> *value,
bool create,
int create_size,
MVOdbError* error)
332 RXA(varname, value, error);
335 template <
typename T>
353 SetVarError(error, varname,
"JSON node is a subdirectory");
360 int num_values = a->size();
366 msg +=
" for array of size ";
372 if (index >= num_values) {
376 msg +=
" for array of size ";
384 GetJsonValue<T>(varname, elem, value, error);
389 msg +=
"non-zero index ";
391 msg +=
" for non-array";
396 GetJsonValue<T>(varname, node, value, error);
403 RXAI(varname, index, value, error);
408 RXAI(varname, index, value, error);
413 RXAI(varname, index, value, error);
418 RXAI(varname, index, value, error);
423 RXAI(varname, index, value, error);
428 RXAI(varname, index, value, error);
433 RXAI(varname, index, value, error);
442 void WS(
const char* varname,
const char* v,
int string_length,
MVOdbError* error) {
SetOk(error); };
450 void WSA(
const char* varname,
const std::vector<std::string>& data,
int odb_string_length,
MVOdbError* error) {
SetOk(error); };
468 bool JsonOdb::GetJsonValue<int>(
const char* varname,
const MJsonNode* node,
int* value,
MVOdbError* error)
472 default:
SetWrongType(error, varname, node,
"int");
return false;
477 bool JsonOdb::GetJsonValue<double>(
const char* varname,
const MJsonNode* node,
double* value,
MVOdbError* error)
482 default:
SetWrongType(error, varname, node,
"double");
return false;
487 bool JsonOdb::GetJsonValue<float>(
const char* varname,
const MJsonNode* node,
float* value,
MVOdbError* error)
492 default:
SetWrongType(error, varname, node,
"float");
return false;
497 bool JsonOdb::GetJsonValue<bool>(
const char* varname,
const MJsonNode* node,
bool* value,
MVOdbError* error)
503 default:
SetWrongType(error, varname, node,
"bool");
return false;
508 bool JsonOdb::GetJsonValue<uint16_t>(
const char* varname,
const MJsonNode* node, uint16_t* value,
MVOdbError* error)
513 default:
SetWrongType(error, varname, node,
"uint16_t");
return false;
518 bool JsonOdb::GetJsonValue<uint32_t>(
const char* varname,
const MJsonNode* node, uint32_t* value,
MVOdbError* error)
523 default:
SetWrongType(error, varname, node,
"uint32_t");
return false;
528 bool JsonOdb::GetJsonValue<std::string>(
const char* varname,
const MJsonNode* node, std::string* value,
MVOdbError* error)
532 default: SetWrongType(error, varname, node,
"std::string");
return false;
541 FILE *fp = fopen(filename,
"r");
544 msg +=
"Cannot open file ";
548 msg +=
" fopen() errno: ";
551 msg += strerror(errno);
553 SetError(error,
true, filename, msg);
559 const char* s = fgets(buf,
sizeof(buf), fp);
571 return new JsonOdb(root, root, error);
578 return new JsonOdb(root, root, error);
Access to ODB saved in JSON format inside midas .mid files.
void WU32AI(const char *varname, int index, uint32_t v, MVOdbError *error)
void WB(const char *varname, bool v, MVOdbError *error)
void WF(const char *varname, float v, MVOdbError *error)
void RDA(const char *varname, std::vector< double > *value, bool create, int create_size, MVOdbError *error)
JsonOdb(MJsonNode *root, MJsonNode *dir, MVOdbError *error)
MVOdb * Chdir(const char *subdir, bool create, MVOdbError *error)
void WU16(const char *varname, uint16_t v, MVOdbError *error)
void WI(const char *varname, int v, MVOdbError *error)
void WD(const char *varname, double v, MVOdbError *error)
void RU16AI(const char *varname, int index, uint16_t *value, MVOdbError *error)
void SetNotFound(MVOdbError *error, const char *varname)
void WBAI(const char *varname, int index, bool v, MVOdbError *error)
void WIAI(const char *varname, int index, int v, MVOdbError *error)
void ReadKeyLastWritten(const char *varname, int *last_written, MVOdbError *error)
void WS(const char *varname, const char *v, int string_length, MVOdbError *error)
void RU16A(const char *varname, std::vector< uint16_t > *value, bool create, int create_size, MVOdbError *error)
void RSAI(const char *varname, int index, std::string *value, MVOdbError *error)
void RBA(const char *varname, std::vector< bool > *value, bool create, int create_size, MVOdbError *error)
void RXA(const char *varname, std::vector< T > *value, MVOdbError *error)
void RB(const char *varname, bool *value, bool create, MVOdbError *error)
void WSA(const char *varname, const std::vector< std::string > &data, int odb_string_length, MVOdbError *error)
void RFA(const char *varname, std::vector< float > *value, bool create, int create_size, MVOdbError *error)
void RS(const char *varname, std::string *value, bool create, int create_string_length, MVOdbError *error)
void WU16A(const char *varname, const std::vector< uint16_t > &v, MVOdbError *error)
void SetPrintError(bool v)
void Delete(const char *odbname, MVOdbError *error)
void RU32A(const char *varname, std::vector< uint32_t > *value, bool create, int create_size, MVOdbError *error)
void ReadKey(const char *varname, int *tid, int *num_values, int *total_size, int *item_size, MVOdbError *error)
void RXAI(const char *varname, int index, T *value, MVOdbError *error)
bool GetPrintError() const
void WBA(const char *varname, const std::vector< bool > &v, MVOdbError *error)
void RD(const char *varname, double *value, bool create, MVOdbError *error)
void WFA(const char *varname, const std::vector< float > &v, MVOdbError *error)
void ReadDir(std::vector< std::string > *varname, std::vector< int > *tid, std::vector< int > *num_values, std::vector< int > *total_size, std::vector< int > *item_size, MVOdbError *error)
void RU16(const char *varname, uint16_t *value, bool create, MVOdbError *error)
void RU32(const char *varname, uint32_t *value, bool create, MVOdbError *error)
void WU16AI(const char *varname, int index, uint16_t v, MVOdbError *error)
void RF(const char *varname, float *value, bool create, MVOdbError *error)
void WDAI(const char *varname, int index, double v, MVOdbError *error)
void RIAI(const char *varname, int index, int *value, MVOdbError *error)
void WDA(const char *varname, const std::vector< double > &v, MVOdbError *error)
void WU32(const char *varname, uint32_t v, MVOdbError *error)
void RSA(const char *varname, std::vector< std::string > *value, bool create, int create_size, int create_string_length, MVOdbError *error)
static MJsonNode * FindPath(MJsonNode *dir, const char *path)
Follow the ODB path through the JSON tree.
void RI(const char *varname, int *value, bool create, MVOdbError *error)
void WSAI(const char *varname, int index, const char *v, MVOdbError *error)
void RFAI(const char *varname, int index, float *value, MVOdbError *error)
void SetVarError(MVOdbError *error, const char *varname, std::string msg)
void WIA(const char *varname, const std::vector< int > &v, MVOdbError *error)
void WU32A(const char *varname, const std::vector< uint32_t > &v, MVOdbError *error)
void RBAI(const char *varname, int index, bool *value, MVOdbError *error)
void RDAI(const char *varname, int index, double *value, MVOdbError *error)
void RIA(const char *varname, std::vector< int > *value, bool create, int create_size, MVOdbError *error)
void WFAI(const char *varname, int index, float v, MVOdbError *error)
void SetWrongType(MVOdbError *error, const char *varname, const MJsonNode *node, const char *wanted_type)
bool GetJsonValue(const char *varname, const MJsonNode *node, T *value, MVOdbError *error)
void RU32AI(const char *varname, int index, uint32_t *value, MVOdbError *error)
std::string GetString() const
find subnode with given name, NULL if not object, NULL is name not found
const MJsonNodeVector * GetArray() const
get node type: MJSON_xxx
int GetType() const
delete a node from an object
std::vector< MJsonNode * > MJsonNodeVector
std::vector< std::string > MJsonStringVector
long long GetInt() const
get string value, "" if not string or value is JSON "null"
double GetDouble() const
get 64-bit long long value, 0 if not an integer or value is JSON "null"
const MJsonNodeVector * GetObjectNodes() const
get array of object names, NULL if not object, empty array if value is JSON "null"
bool GetBool() const
get number or integer value, 0 if not a number or value is JSON "null"
const MJsonStringVector * GetObjectNames() const
get array value, NULL if not array, empty array if value is JSON "null"
static MJsonNode * Parse(const char *jsonstring)
static const char * TypeToString(int type)
get error message from MJSON_ERROR nodes
void SetError(MVOdbError *error, bool print, const std::string &path, const std::string &message)
void SetOk(MVOdbError *error)
MVOdb * MakeJsonFileOdb(const char *filename, MVOdbError *error)
MVOdb * MakeJsonBufferOdb(const char *buf, int bufsize, MVOdbError *error)
static std::string toString(int i)