22 snprintf(buf,
sizeof(buf),
"%d", i);
31 if (strcmp(node->
name, name) == 0)
48 assert(i<node->n_attributes);
56 assert(i<node->n_attributes);
74 static void DumpTree(
PMXML_NODE node,
int level = 0)
78 for (
int k=0; k<level; k++)
80 printf(
"node name: \"%s\"\n", node->
name);
82 for (
int k=0; k<level; k++)
87 for (
int k=0; k<level; k++)
89 printf(
"node text: \"%s\"\n", node->
value);
92 DumpTree(node->
child + i, level + 1);
96 static void DumpDirTree(
PMXML_NODE node,
int level = 0)
100 const char* name = node->
name;
102 if (strcmp(name,
"dir") != 0)
105 for (
int k=0; k<level; k++)
107 printf(
"node name: \"%s\"\n", node->
name);
110 for (
int k=0; k<level; k++)
116 DumpDirTree(node->
child + i, level + 1);
121 template <
typename T>
154 return 0xFFFF & strtoul(text, NULL, 0);
160 return strtoul(text, NULL, 0);
164 std::string GetXmlValue<std::string>(
const char* text)
220 msg +=
"Cannot find ";
234 msg +=
"XML node for ";
265 if (*path==0 || *path==
'/')
277 const char* nodename = node->
name;
278 const char* namevalue =
GetAttr(node,
"name");
282 bool isDir = strcmp(nodename,
"dir") == 0;
283 bool isKey = strcmp(nodename,
"key") == 0;
284 bool isKeyArray = strcmp(nodename,
"keyarray") == 0;
286 if (!isKey && !isDir && !isKeyArray)
293 if (strcasecmp(elem.c_str(), namevalue) == 0) {
298 }
else if (isKey || isKeyArray) {
322 if (strcmp(node->
name,
"dir") != 0) {
327 msg +=
" XML node is ";
331 msg +=
" instead of \"dir\"";
349 void ReadKey(
const char* varname,
int *tid,
int *num_values,
int *total_size,
int *item_size,
MVOdbError* error)
352 if (num_values) *num_values = 0;
353 if (total_size) *total_size = 0;
354 if (item_size) *item_size = 0;
361 if (last_written) *last_written = 0;
366 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)
372 void RB(
const char* varname,
bool *value,
bool create,
MVOdbError* error)
374 RBAI(varname, 0, value, error);
377 void RI(
const char* varname,
int *value,
bool create,
MVOdbError* error)
379 RIAI(varname, 0, value, error);
382 void RD(
const char* varname,
double *value,
bool create,
MVOdbError* error)
384 RDAI(varname, 0, value, error);
387 void RF(
const char* varname,
float *value,
bool create,
MVOdbError* error)
389 RFAI(varname, 0, value, error);
392 void RS(
const char* varname, std::string *value,
bool create,
int create_string_length,
MVOdbError* error)
394 RSAI(varname, 0, value, error);
399 RU16AI(varname, 0, value, error);
404 RU32AI(varname, 0, value, error);
415 const char* attr_type =
GetAttr(node,
"type");
418 fprintf(stderr,
"XmlOdb::FindXmlNode: Error: no type attribute in varname \"%s\"!\n", varname);
423 if (strcmp(attr_type, type1) != 0) {
424 if (strcmp(attr_type, type2) != 0) {
425 fprintf(stderr,
"XmlOdb::FindXmlNode: Error: type mismatch, wanted \"%s\" or \"%s\", got \"%s\"!\n", type1, type2, attr_type);
434 template <
typename T>
435 void RXA(
const char* varname,
const char* type1,
const char* type2, std::vector<T> *value,
MVOdbError* error)
448 if (strcmp(node->
name,
"keyarray") == 0) {
449 const char* num_values_text =
GetAttr(node,
"num_values");
450 if (!num_values_text) {
451 fprintf(stderr,
"no num_values!\n");
456 int num_values = atoi(num_values_text);
459 fprintf(stderr,
"num_values mismatch %d vs %d children!\n", num_values, node->
n_children);
468 const char* text = elem->
value;
473 T v = GetXmlValue<T>(text);
479 }
else if (strcmp(node->
name,
"key") == 0) {
480 const char* text = node->
value;
486 T v = GetXmlValue<T>(text);
491 fprintf(stderr,
"unexpected node %s\n", node->
name);
497 void RBA(
const char* varname, std::vector<bool> *value,
bool create,
int create_size,
MVOdbError* error)
499 RXA(varname,
"BOOL",
"BOOL", value, error);
502 void RIA(
const char* varname, std::vector<int> *value,
bool create,
int create_size,
MVOdbError* error)
504 RXA(varname,
"INT",
"INT32", value, error);
507 void RDA(
const char* varname, std::vector<double> *value,
bool create,
int create_size,
MVOdbError* error)
509 RXA(varname,
"DOUBLE",
"DOUBLE", value, error);
512 void RFA(
const char* varname, std::vector<float> *value,
bool create,
int create_size,
MVOdbError* error)
514 RXA(varname,
"FLOAT",
"FLOAT", value, error);
517 void RSA(
const char* varname, std::vector<std::string> *value,
bool create,
int create_size,
int create_string_length,
MVOdbError* error)
519 RXA(varname,
"STRING",
"STIRNG", value, error);
522 void RU16A(
const char* varname, std::vector<uint16_t> *value,
bool create,
int create_size,
MVOdbError* error)
524 RXA(varname,
"WORD",
"UINT16", value, error);
527 void RU32A(
const char* varname, std::vector<uint32_t> *value,
bool create,
int create_size,
MVOdbError* error)
529 RXA(varname,
"DWORD",
"UINT32", value, error);
532 template <
typename T>
533 void RXAI(
const char* varname,
int index,
const char* type1,
const char* type2, T* value,
MVOdbError* error)
546 if (strcmp(node->
name,
"keyarray") == 0) {
547 const char* num_values_text =
GetAttr(node,
"num_values");
548 if (!num_values_text) {
549 fprintf(stderr,
"no num_values!\n");
554 int num_values = atoi(num_values_text);
557 fprintf(stderr,
"num_values mismatch %d vs %d children!\n", num_values, node->
n_children);
563 fprintf(stderr,
"bad index %d, num_values %d!\n", index, num_values);
568 if (index >= num_values) {
569 fprintf(stderr,
"bad index %d, num_values %d!\n", index, num_values);
575 const char* text = elem->
value;
581 *value = GetXmlValue<T>(text);
585 }
else if (strcmp(node->
name,
"key") == 0) {
588 fprintf(stderr,
"non-zero index %d for non-array!\n", index);
593 const char* text = node->
value;
599 *value = GetXmlValue<T>(text);
604 fprintf(stderr,
"unexpected node %s\n", node->
name);
612 RXAI(varname, index,
"BOOL",
"BOOL", value, error);
617 RXAI(varname, index,
"INT",
"INT32", value, error);
622 RXAI(varname, index,
"DOUBLE",
"DOUBLE", value, error);
627 RXAI(varname, index,
"FLOAT",
"FLOAT", value, error);
632 RXAI(varname, index,
"STRING",
"STRING", value, error);
637 RXAI(varname, index,
"WORD",
"UINT16", value, error);
642 RXAI(varname, index,
"DWORD",
"UINT32", value, error);
651 void WS(
const char* varname,
const char* v,
int string_length,
MVOdbError* error) {
SetOk(error); };
659 void WSA(
const char* varname,
const std::vector<std::string>& data,
int odb_string_length,
MVOdbError* error) {
SetOk(error); };
677 int XmlOdb::odbReadArraySize(
const char*name)
682 const char* num_values =
GetAttr(node,
"num_values");
685 return atoi(num_values);
696 msg +=
"mxml_parse_file() error ";
704 SetError(error,
true, filename, msg);
712 msg +=
"invalid XML tree: no ODB tag";
713 SetError(error,
true, filename, msg);
717 return new XmlOdb(node, odb_node, error);
730 char*buf = (
char*)malloc(bufLength);
731 memcpy(buf, xbuf, bufLength);
732 for (
int i=0; i<bufLength; i++)
733 if (!isascii(buf[i])) {
735 }
else if (buf[i]==
'\n') {
736 }
else if (buf[i]==
'\r') {
737 }
else if (!isprint(buf[i])) {
739 }
else if (buf[i] == 0x1D) {
743 char* xend = strstr(buf,
"odb>");
753 msg +=
"mxml_parse_buffer() error ";
759 SetError(error,
true,
"buffer", msg);
767 msg +=
"invalid XML tree: no ODB tag";
768 SetError(error,
true,
"buffer", msg);
772 return new XmlOdb(node, odb_node, error);
Access to ODB saved in XML format inside midas .mid files.
void WSA(const char *varname, const std::vector< std::string > &data, int odb_string_length, MVOdbError *error)
void RU32A(const char *varname, std::vector< uint32_t > *value, bool create, int create_size, MVOdbError *error)
void WDA(const char *varname, const std::vector< double > &v, MVOdbError *error)
void WSAI(const char *varname, int index, const char *v, MVOdbError *error)
void Delete(const char *odbname, MVOdbError *error)
void WU16(const char *varname, uint16_t v, MVOdbError *error)
void RU16(const char *varname, uint16_t *value, bool create, MVOdbError *error)
void WI(const char *varname, int v, MVOdbError *error)
void RXA(const char *varname, const char *type1, const char *type2, std::vector< T > *value, MVOdbError *error)
void RU16A(const char *varname, std::vector< uint16_t > *value, bool create, int create_size, MVOdbError *error)
void WU16A(const char *varname, const std::vector< uint16_t > &v, MVOdbError *error)
void RU32AI(const char *varname, int index, uint32_t *value, MVOdbError *error)
void WB(const char *varname, bool v, MVOdbError *error)
void RIAI(const char *varname, int index, int *value, MVOdbError *error)
void WF(const char *varname, float v, MVOdbError *error)
void WBAI(const char *varname, int index, bool v, MVOdbError *error)
void WFA(const char *varname, const std::vector< float > &v, MVOdbError *error)
void WIAI(const char *varname, int index, int v, MVOdbError *error)
void WIA(const char *varname, const std::vector< int > &v, MVOdbError *error)
void RFAI(const char *varname, int index, float *value, MVOdbError *error)
void WU32A(const char *varname, const std::vector< uint32_t > &v, MVOdbError *error)
void RS(const char *varname, std::string *value, bool create, int create_string_length, MVOdbError *error)
void ReadKey(const char *varname, int *tid, int *num_values, int *total_size, int *item_size, MVOdbError *error)
void RFA(const char *varname, std::vector< float > *value, bool create, int create_size, MVOdbError *error)
void SetNotFound(MVOdbError *error, const char *varname)
void RDA(const char *varname, std::vector< double > *value, bool create, int create_size, MVOdbError *error)
void WDAI(const char *varname, int index, double v, MVOdbError *error)
void SetPrintError(bool v)
void WU32(const char *varname, uint32_t v, MVOdbError *error)
void SetNullValue(MVOdbError *error, const char *varname)
void WU32AI(const char *varname, int index, uint32_t v, MVOdbError *error)
void ReadKeyLastWritten(const char *varname, int *last_written, MVOdbError *error)
XmlOdb(PMXML_NODE root, PMXML_NODE dir, MVOdbError *error)
void RI(const char *varname, int *value, bool create, MVOdbError *error)
void RB(const char *varname, bool *value, bool create, 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 RIA(const char *varname, std::vector< int > *value, bool create, int create_size, MVOdbError *error)
void RXAI(const char *varname, int index, const char *type1, const char *type2, T *value, MVOdbError *error)
void RDAI(const char *varname, int index, double *value, MVOdbError *error)
PMXML_NODE FindXmlNode(PMXML_NODE dir, const char *varname, const char *type1, const char *type2, MVOdbError *error)
void RU16AI(const char *varname, int index, uint16_t *value, MVOdbError *error)
void RF(const char *varname, float *value, bool create, MVOdbError *error)
void RBA(const char *varname, std::vector< bool > *value, bool create, int create_size, MVOdbError *error)
void RSA(const char *varname, std::vector< std::string > *value, bool create, int create_size, int create_string_length, MVOdbError *error)
void WBA(const char *varname, const std::vector< bool > &v, MVOdbError *error)
void WFAI(const char *varname, int index, float v, MVOdbError *error)
void RD(const char *varname, double *value, bool create, MVOdbError *error)
void RBAI(const char *varname, int index, bool *value, MVOdbError *error)
void RU32(const char *varname, uint32_t *value, bool create, MVOdbError *error)
static PMXML_NODE FindPath(PMXML_NODE dir, const char *path)
Follow the ODB path through the XML DOM tree.
void WD(const char *varname, double v, MVOdbError *error)
void RSAI(const char *varname, int index, std::string *value, MVOdbError *error)
bool GetPrintError() const
void WS(const char *varname, const char *v, int string_length, MVOdbError *error)
void WU16AI(const char *varname, int index, uint16_t v, MVOdbError *error)
MVOdb * Chdir(const char *subdir, bool create, MVOdbError *error)
void SetError(MVOdbError *error, bool print, const std::string &path, const std::string &message)
void SetOk(MVOdbError *error)
void mxml_free_tree(PMXML_NODE tree)
PMXML_NODE mxml_parse_file(const char *file_name, char *error, int error_size, int *error_line)
PMXML_NODE mxml_parse_buffer(const char *buffer, char *error, int error_size, int *error_line)
uint32_t GetXmlValue< uint32_t >(const char *text)
float GetXmlValue< float >(const char *text)
int GetXmlValue< int >(const char *text)
static const char * GetAttr(PMXML_NODE node, const char *attrName)
Return the value of the named attribute.
uint16_t GetXmlValue< uint16_t >(const char *text)
static T GetXmlValue(const char *text)
static const char * GetAttrName(PMXML_NODE node, int i)
Return the name of the indexed attribute.
bool GetXmlValue< bool >(const char *text)
double GetXmlValue< double >(const char *text)
MVOdb * MakeXmlFileOdb(const char *filename, MVOdbError *error)
static std::string toString(int i)
static PMXML_NODE FindNode(PMXML_NODE dir, const char *name)
static const char * GetAttrValue(PMXML_NODE node, int i)
Return the value of the indexed attribute.
MVOdb * MakeXmlBufferOdb(const char *buf, int bufsize, MVOdbError *error)
char name[MXML_NAME_LENGTH]