ROOTANA
mvodb.cxx
Go to the documentation of this file.
1 /********************************************************************\
2 
3  Name: mvodb.cxx
4  Created by: K.Olchanski
5 
6  Contents: common functions of MVOdb ODB interface
7 
8 \********************************************************************/
9 
10 #include <stdio.h> // sprintf(), fprintf()
11 
12 #include "mvodb.h"
13 
14 MVOdb::~MVOdb() // dtor
15 {
16  // empty
17 }
18 
19 static std::string toString(int value)
20 {
21  char buf[256];
22  sprintf(buf, "%d", value);
23  return buf;
24 }
25 
27 {
28  SetOk(this);
29 }
30 
31 void SetOk(MVOdbError* error)
32 {
33  if (error) {
34  error->fError = false;
35  error->fErrorString = "";
36  error->fPath = "";
37  error->fStatus = 0;
38  }
39 }
40 
41 void SetMidasStatus(MVOdbError* error, bool print, const std::string& path, const char* midas_func_name, int status)
42 {
43  if (error) {
44  error->fStatus = status;
45  error->fPath = path;
46  if (status == 1) {
47  error->fError = false;
48  error->fErrorString = "";
49  } else {
50  error->fError = true;
51  error->fErrorString = "";
52  error->fErrorString += "MIDAS ";
53  error->fErrorString += midas_func_name;
54  error->fErrorString += "()";
55  error->fErrorString += " at ODB path \"";
56  error->fErrorString += path;
57  error->fErrorString += "\" returned status ";
58  error->fErrorString += toString(status);
59  if (print) {
60  fprintf(stderr, "MVOdb::SetMidasStatus: %s\n", error->fErrorString.c_str());
61  }
62  }
63  } else {
64  if (print) {
65  fprintf(stderr, "MVOdb::SetMidasStatus: Error: MIDAS %s() at ODB path \"%s\" returned status %d\n", midas_func_name, path.c_str(), status);
66  }
67  }
68 }
69 
70 void SetError(MVOdbError* error, bool print, const std::string& path, const std::string& message)
71 {
72  if (error) {
73  error->fStatus = 0;
74  error->fPath = path;
75  error->fError = true;
76  error->fErrorString = "";
77  error->fErrorString += message;
78  error->fErrorString += " at ODB path \"";
79  error->fErrorString += path;
80  error->fErrorString += "\"";
81  if (print) {
82  fprintf(stderr, "MVOdb::SetError: %s\n", error->fErrorString.c_str());
83  }
84  } else {
85  if (print) {
86  fprintf(stderr, "MVOdb::SetError: Error: %s at ODB path \"%s\"\n", message.c_str(), path.c_str());
87  }
88  }
89 }
90 
91 MVOdb* MakeFileDumpOdb(const char* buf, int bufsize, MVOdbError* error)
92 {
93  //printf("MakeFileDumpOdb: odb dump size %d, first char \'%c\'\n", bufsize, buf[0]);
94  if (buf[0] == '[') {
95  // ODB format
96  char str[256];
97  sprintf(str, "MakeFileDumpOdb: old ODB dump format is not supported, sorry");
98  SetError(error, false, "buffer", str);
99  return MakeNullOdb();
100  } else if (buf[0] == '<') {
101  // XML format
102  return MakeXmlBufferOdb(buf, bufsize, error);
103  } else if (buf[0] == '{') {
104  // JSON format
105  return MakeJsonBufferOdb(buf, bufsize, error);
106  } else {
107  // unknown format
108  char str[256];
109  sprintf(str, "MakeFileDumpOdb: unknown ODB dump format, first char is \'%c\' (%d), dump size %d", buf[0], buf[0], bufsize);
110  SetError(error, false, "buffer", str);
111  return MakeNullOdb();
112  }
113 }
114 
115 /* emacs
116  * Local Variables:
117  * tab-width: 8
118  * c-basic-offset: 3
119  * indent-tabs-mode: nil
120  * End:
121  */
std::string fPath
Definition: mvodb.h:189
int fStatus
Definition: mvodb.h:190
MVOdbError()
Definition: mvodb.cxx:26
bool fError
Definition: mvodb.h:187
std::string fErrorString
Definition: mvodb.h:188
Definition: mvodb.h:21
virtual ~MVOdb()=0
Definition: mvodb.cxx:14
MVOdb * MakeNullOdb()
Definition: nullodb.cxx:123
MVOdb * MakeXmlBufferOdb(const char *buf, int bufsize, MVOdbError *error=NULL)
Definition: mxmlodb.cxx:713
MVOdb * MakeJsonBufferOdb(const char *buf, int bufsize, MVOdbError *error=NULL)
Definition: mjsonodb.cxx:567
void SetMidasStatus(MVOdbError *error, bool print, const std::string &path, const char *midas_func_name, int status)
Definition: mvodb.cxx:41
void SetError(MVOdbError *error, bool print, const std::string &path, const std::string &message)
Definition: mvodb.cxx:70
MVOdb * MakeFileDumpOdb(const char *buf, int bufsize, MVOdbError *error)
Access ODB from a midas file dump. FOrmat could be .xml, .json or .odb.
Definition: mvodb.cxx:91
void SetOk(MVOdbError *error)
Definition: mvodb.cxx:31
static std::string toString(int value)
Definition: mvodb.cxx:19