ROOTANA
midasodb.cxx
Go to the documentation of this file.
1 /********************************************************************\
2 
3  Name: midasodb.cxx
4  Created by: K.Olchanski
5 
6  Contents: MIDAS implementation of MVOdb ODB interface
7 
8 \********************************************************************/
9 
10 #include <stdio.h>
11 #include <string.h> // strlen()
12 #include <assert.h>
13 #include <stdlib.h> // malloc()
14 
15 #include "mvodb.h"
16 #include "midas.h"
17 
18 static std::string toString(int value)
19 {
20  char buf[256];
21  sprintf(buf, "%d", value);
22  return buf;
23 }
24 
25 class MidasOdb: public MVOdb
26 {
27 public:
28  HNDLE fDB;
29  std::string fRoot;
30  bool fTrace;
32 
33 public:
34  MidasOdb(HNDLE hDB, const char* root)
35  {
36  fDB = hDB;
37  fRoot = root;
38  fTrace = false;
39  fPrintError = true;
40  }
41 
42  ~MidasOdb() // dtor
43  {
44  // poison the data members
45  fDB = 0;
46  fRoot = "deleted";
47  fTrace = false;
48  fPrintError = false;
49  }
50 
51  std::string Path(const char* varname)
52  {
53  std::string path;
54  path += fRoot;
55  path += "/";
56  path += varname;
57  return path;
58  }
59 
60  void SetPrintError(bool v)
61  {
62  fPrintError = v;
63  }
64 
65  bool GetPrintError() const
66  {
67  return fPrintError;
68  }
69 
70  bool IsReadOnly() const
71  {
72  return false;
73  }
74 
75  MVOdb* Chdir(const char* subdir, bool create, MVOdbError* error)
76  {
77  std::string path = Path(subdir);
78  HNDLE hkey;
79  int status = db_find_key(fDB, 0, path.c_str(), &hkey);
80  if (status == DB_SUCCESS) {
81  KEY key;
82  status = db_get_key(fDB, hkey, &key);
83  if (status != DB_SUCCESS) {
84  SetMidasStatus(error, fPrintError, path, "db_get_key", status);
85  return MakeNullOdb();
86  }
87  if (key.type != TID_KEY) {
88  SetError(error, fPrintError, path, "ODB path is not a directory");
89  if (create)
90  return MakeNullOdb();
91  else
92  return NULL;
93  }
94  return new MidasOdb(fDB, path.c_str());
95  } else if (!create) {
96  SetMidasStatus(error, fPrintError, path, "db_find_key", status);
97  return NULL;
98  } else {
99  status = db_create_key(fDB, 0, path.c_str(), TID_KEY);
100  if (status != DB_SUCCESS) {
101  SetMidasStatus(error, fPrintError, path, "db_create_key", status);
102  return MakeNullOdb();
103  }
104  return new MidasOdb(fDB, path.c_str());
105  }
106  }
107 
108  void RAInfo(const char* varname, int* num_elements, int* element_size, MVOdbError* error)
109  {
110  std::string path = Path(varname);
111 
112  if (num_elements)
113  *num_elements = 0;
114  if (element_size)
115  *element_size = 0;
116 
117  int status;
118  HNDLE hkey;
119  status = db_find_key(fDB, 0, path.c_str(), &hkey);
120  if (status != DB_SUCCESS)
121  return;
122 
123  KEY key;
124  status = db_get_key(fDB, hkey, &key);
125  if (status != DB_SUCCESS)
126  return;
127 
128  if (num_elements)
129  *num_elements = key.num_values;
130 
131  if (element_size)
132  *element_size = key.item_size;
133  }
134 
135  void ReadKey(const char* varname, int *tid, int *num_values, int *total_size, int *item_size, MVOdbError* error)
136  {
137  if (tid) *tid = 0;
138  if (num_values) *num_values = 0;
139  if (total_size) *total_size = 0;
140  if (item_size) *item_size = 0;
141 
142  std::string path = Path(varname);
143 
144  int status;
145  HNDLE hkey;
146 
147  status = db_find_key(fDB, 0, path.c_str(), &hkey);
148  if (status != DB_SUCCESS) {
149  SetMidasStatus(error, fPrintError, path, "db_find_key", status);
150  return;
151  }
152 
153  KEY key;
154  status = db_get_key(fDB, hkey, &key);
155  if (status != DB_SUCCESS) {
156  SetMidasStatus(error, fPrintError, path, "db_get_key", status);
157  return;
158  }
159 
160  if (tid)
161  *tid = key.type;
162 
163  if (num_values)
164  *num_values = key.num_values;
165 
166  if (total_size)
167  *total_size = key.total_size;
168 
169  if (item_size)
170  *item_size = key.item_size;
171 
172  SetOk(error);
173  }
174 
175  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)
176  {
177  // FIXME: incomplete!
178  SetOk(error);
179  }
180 
181  void ResizeArray(const char* varname, int new_size, MVOdbError* error)
182  {
183  std::string path = Path(varname);
184 
185  int status;
186  HNDLE hkey;
187 
188  status = db_find_key(fDB, 0, path.c_str(), &hkey);
189 
190  if (status != DB_SUCCESS) {
191  SetMidasStatus(error, fPrintError, path, "db_find_key", status);
192  return;
193  }
194 
195  status = db_set_num_values(fDB, hkey, new_size);
196  if (status != SUCCESS) {
197  SetMidasStatus(error, fPrintError, path, "db_set_num_values", status);
198  return;
199  }
200 
201  SetOk(error);
202  }
203 
204  void ResizeStringArray(const char* varname, int new_size, int new_string_length, MVOdbError* error)
205  {
206  std::string path = Path(varname);
207 
208  int status = db_resize_string(fDB, 0, path.c_str(), new_size, new_string_length);
209  if (status != SUCCESS) {
210  SetMidasStatus(error, fPrintError, path, "db_resize_string", status);
211  return;
212  }
213 
214  SetOk(error);
215  }
216 
217  void R(const char* varname, int tid, void *value, int size, bool create, MVOdbError* error)
218  {
219  assert(value);
220  std::string path = Path(varname);
221  int status = db_get_value(fDB, 0, path.c_str(), value, &size, tid, create);
222  if (status != DB_SUCCESS) {
223  SetMidasStatus(error, fPrintError, path, "db_get_value", status);
224  return;
225  }
226  SetOk(error);
227  }
228 
229  void RI(const char* varname, int *value, bool create, MVOdbError* error)
230  {
231  R(varname, TID_INT, value, sizeof(int), create, error);
232  }
233 
234  void RU16(const char* varname, uint16_t *value, bool create, MVOdbError* error)
235  {
236  R(varname, TID_WORD, value, sizeof(uint16_t), create, error);
237  }
238 
239  void RU32(const char* varname, uint32_t *value, bool create, MVOdbError* error)
240  {
241  R(varname, TID_DWORD, value, sizeof(uint32_t), create, error);
242  }
243 
244  void RD(const char* varname, double *value, bool create, MVOdbError* error)
245  {
246  R(varname, TID_DOUBLE, value, sizeof(double), create, error);
247  }
248 
249  void RF(const char* varname, float *value, bool create, MVOdbError* error)
250  {
251  R(varname, TID_FLOAT, value, sizeof(float), create, error);
252  }
253 
254  void RB(const char* varname, bool *value, bool create, MVOdbError* error)
255  {
256  assert(value);
257  BOOL v = *value;
258  R(varname, TID_BOOL, &v, sizeof(BOOL), create, error);
259  *value = v;
260  }
261 
262  void RS(const char* varname, std::string* value, bool create, int create_string_length, MVOdbError* error)
263  {
264  assert(value);
265  std::string path = Path(varname);
266 
267 #ifdef HAVE_DB_GET_VALUE_STRING_CREATE_STRING_LENGTH
268  int status = db_get_value_string(fDB, 0, path.c_str(), 0, value, create, create_string_length);
269 #else
270 #warning This MIDAS has an old version of db_get_value_string() and RS() will ignore the create_string_length argument.
271  int status = db_get_value_string(fDB, 0, path.c_str(), 0, value, create);
272 #endif
273 
274  if (status != DB_SUCCESS) {
275  SetMidasStatus(error, fPrintError, path, "db_get_value_string", status);
276  return;
277  }
278 
279  SetOk(error);
280  }
281 
282  void RAI(const char* varname, int index, int tid, void *value, int size, MVOdbError* error)
283  {
284  assert(value);
285  std::string path = Path(varname);
286  path += "[";
287  path += toString(index);
288  path += "]";
289  if (index < 0) {
290  SetError(error, fPrintError, path, "RxAI() called with negative array index");
291  return;
292  }
293  int status = db_get_value(fDB, 0, path.c_str(), value, &size, tid, FALSE);
294  if (status != DB_SUCCESS) {
295  SetMidasStatus(error, fPrintError, path, "db_get_value", status);
296  return;
297  }
298  SetOk(error);
299  }
300 
301  void RIAI(const char* varname, int index, int *value, MVOdbError* error)
302  {
303  RAI(varname, index, TID_INT, value, sizeof(int), error);
304  }
305 
306  void RU16AI(const char* varname, int index, uint16_t *value, MVOdbError* error)
307  {
308  RAI(varname, index, TID_WORD, value, sizeof(uint16_t), error);
309  }
310 
311  void RU32AI(const char* varname, int index, uint32_t *value, MVOdbError* error)
312  {
313  RAI(varname, index, TID_DWORD, value, sizeof(uint32_t), error);
314  }
315 
316  void RDAI(const char* varname, int index, double *value, MVOdbError* error)
317  {
318  RAI(varname, index, TID_DOUBLE, value, sizeof(double), error);
319  }
320 
321  void RFAI(const char* varname, int index, float *value, MVOdbError* error)
322  {
323  RAI(varname, index, TID_FLOAT, value, sizeof(float), error);
324  }
325 
326  void RBAI(const char* varname, int index, bool *value, MVOdbError* error)
327  {
328  assert(value);
329  BOOL v = *value;
330  RAI(varname, index, TID_BOOL, &v, sizeof(BOOL), error);
331  *value = v;
332  }
333 
334  void RSAI(const char* varname, int index, std::string* value, MVOdbError* error)
335  {
336  assert(value);
337  std::string path = Path(varname);
338 
339  if (index < 0) {
340  SetError(error, fPrintError, path, "RSAI() called with negative array index");
341  return;
342  }
343 
344  int status = db_get_value_string(fDB, 0, path.c_str(), index, value, FALSE);
345 
346  if (status != DB_SUCCESS) {
347  SetMidasStatus(error, fPrintError, path, "db_get_value_string", status);
348  return;
349  }
350 
351  SetOk(error);
352  }
353 
354  void RA(const std::string& path, int tid, void* buf, int size, MVOdbError* error)
355  {
356  int status = db_get_value(fDB, 0, path.c_str(), buf, &size, tid, FALSE);
357 
358  if (status != DB_SUCCESS) {
359  SetMidasStatus(error, fPrintError, path, "db_get_value", status);
360  return;
361  }
362 
363  SetOk(error);
364  }
365 
366  void GetArraySize(const char* varname, int* pnum_values, int* pitem_size, MVOdbError* error)
367  {
368  int xtid = 0;
369  //int xnum_values = 0;
370  int xtotal_size = 0;
371  //int xitem_size = 0;
372 
373  ReadKey(varname, &xtid, pnum_values, &xtotal_size, pitem_size, error);
374 
375  if (xtid == TID_KEY) {
376  *pnum_values = -1;
377  *pitem_size = -1;
378  }
379 
380  if (xtid == 0) {
381  *pnum_values = -1;
382  *pitem_size = -1;
383  }
384  }
385 
386  template <class X> void RXA(const char* varname, int tid, std::vector<X> *value, bool create, int create_size, MVOdbError* error)
387  {
388  std::string path = Path(varname);
389 
390  int num_values = 0;
391  int item_size = 0;
392 
393  GetArraySize(varname, &num_values, &item_size, error);
394 
395  if (value == NULL) {
396  if (create && create_size > 0) {
397  if (num_values < 0) {
398  // does not exist, create it
399  X v = 0;
400  W(varname, tid, &v, sizeof(X), error);
401  if (error && error->fError)
402  return;
403  ResizeArray(varname, create_size, error);
404  } else if (num_values != create_size) {
405  // wrong size, resize it
406  ResizeArray(varname, create_size, error);
407  return;
408  }
409  }
410  return;
411  }
412 
413  if (num_values > 0) { // array exists
414  value->resize(num_values);
415  RA(path, tid, &((*value)[0]), num_values*sizeof(X), error);
416  return;
417  }
418 
419  // array does not exist
420 
421  if (!create)
422  return;
423 
424  WA(varname, tid, &((*value)[0]), value->size()*sizeof(X), value->size(), error);
425 
426  if (error && error->fError)
427  return;
428 
429  if (create_size > 0) {
430  if (create_size != (int)value->size()) {
431  ResizeArray(varname, create_size, error);
432  }
433  }
434  }
435 
436  void RIA(const char* varname, std::vector<int> *value, bool create, int create_size, MVOdbError* error)
437  {
438  RXA<int>(varname, TID_INT, value, create, create_size, error);
439  }
440 
441  void RFA(const char* varname, std::vector<float> *value, bool create, int create_size, MVOdbError* error)
442  {
443  RXA<float>(varname, TID_FLOAT, value, create, create_size, error);
444  }
445 
446  void RDA(const char* varname, std::vector<double> *value, bool create, int create_size, MVOdbError* error)
447  {
448  RXA<double>(varname, TID_DOUBLE, value, create, create_size, error);
449  }
450 
451  void RU16A(const char* varname, std::vector<uint16_t> *value, bool create, int create_size, MVOdbError* error)
452  {
453  RXA<uint16_t>(varname, TID_WORD, value, create, create_size, error);
454  }
455 
456  void RU32A(const char* varname, std::vector<uint32_t> *value, bool create, int create_size, MVOdbError* error)
457  {
458  RXA<uint32_t>(varname, TID_DWORD, value, create, create_size, error);
459  }
460 
461  void RBA(const char* varname, std::vector<bool> *value, bool create, int create_size, MVOdbError* error)
462  {
463  std::vector<BOOL> xvalue;
464  std::vector<BOOL> *xvalue_ptr = NULL;
465 
466  if (value) {
467  for (std::size_t i=0; i<value->size(); i++) {
468  if ((*value)[i])
469  xvalue.push_back(TRUE);
470  else
471  xvalue.push_back(FALSE);
472  }
473  xvalue_ptr = &xvalue;
474  }
475 
476  RXA<BOOL>(varname, TID_BOOL, xvalue_ptr, create, create_size, error);
477 
478  if (value) {
479  for (std::size_t i=0; i<xvalue.size(); i++) {
480  if (xvalue[i])
481  value->push_back(true);
482  else
483  value->push_back(false);
484  }
485  }
486  }
487 
488  void RSA(const char* varname, std::vector<std::string> *value, bool create, int create_size, int create_string_length, MVOdbError* error)
489  {
490  std::string path = Path(varname);
491 
492  int num_values = 0;
493  int item_size = 0;
494 
495  GetArraySize(varname, &num_values, &item_size, error);
496 
497  if (value == NULL) {
498  if (create && (create_size > 0) && (create_string_length > 0)) {
499  if (num_values < 0) {
500  // does not exist, create it
501  WS(varname, "", create_string_length, error);
502  if (error && error->fError)
503  return;
504  ResizeStringArray(varname, create_size, create_string_length, error);
505  } else if ((num_values != create_size) || (item_size != create_string_length)) {
506  // wrong size, resize it
507  ResizeStringArray(varname, create_size, create_string_length, error);
508  return;
509  }
510  }
511  return;
512  }
513 
514  // array exists, read it
515 
516  if (num_values > 0) {
517  value->clear();
518  int bufsize = num_values*item_size;
519  char* buf = (char*)malloc(bufsize);
520  assert(buf != NULL);
521  memset(buf, 0, bufsize);
522  RA(path, TID_STRING, buf, bufsize, error);
523  for (int i=0; i<num_values; i++) {
524  value->push_back(buf+i*item_size);
525  }
526  free(buf);
527  buf = NULL;
528  return;
529  }
530 
531  // array does not exist
532 
533  if (!create)
534  return;
535 
536  //if (!(create_string_length > 0)) {
537  // SetError(error, fPrintError, path, "RSA() with create==true must have create_string_length>0");
538  // return;
539  //}
540 
541  int string_length = 0;
542  for (size_t i = 0; i < value->size(); i++) {
543  if (((int)(*value)[i].length()) > string_length)
544  string_length = (*value)[i].length();
545  }
546  string_length += 1; // add space for string terminator NUL character '\0'
547 
548  if (create_string_length > string_length)
549  string_length = create_string_length;
550 
551  char* buf = NULL;
552 
553  int bufsize = value->size()*string_length;
554 
555  if (bufsize > 0) {
556  buf = (char*)malloc(bufsize);
557  assert(buf != NULL);
558  memset(buf, 0, bufsize);
559 
560  for (size_t i=0; i<value->size(); i++) {
561  strlcpy(buf+i*string_length, (*value)[i].c_str(), string_length);
562  }
563  }
564 
565  WA(varname, TID_STRING, buf, bufsize, value->size(), error);
566 
567  if (buf) {
568  free(buf);
569  buf = NULL;
570  }
571 
572  if (error && error->fError)
573  return;
574 
575  if ((create_size > 0) && (create_string_length > 0)) {
576  if ((((int)value->size()) != create_size) || (string_length != create_string_length)) {
577  // wrong size, resize it
578  ResizeStringArray(varname, create_size, create_string_length, error);
579  }
580  }
581  }
582 
583  void W(const char* varname, int tid, const void* v, int size, MVOdbError* error)
584  {
585  std::string path = Path(varname);
586 
587  int status = db_set_value(fDB, 0, path.c_str(), v, size, 1, tid);
588 
589  if (status != DB_SUCCESS) {
590  SetMidasStatus(error, fPrintError, path, "db_set_value", status);
591  return;
592  }
593 
594  SetOk(error);
595  }
596 
597  void WB(const char* varname, bool v, MVOdbError* error)
598  {
599  BOOL vv = v;
600  W(varname, TID_BOOL, &vv, sizeof(BOOL), error);
601  }
602 
603  void WI(const char* varname, int v, MVOdbError* error)
604  {
605  W(varname, TID_INT, &v, sizeof(int), error);
606  }
607 
608  void WU16(const char* varname, uint16_t v, MVOdbError* error)
609  {
610  W(varname, TID_WORD, &v, sizeof(uint16_t), error);
611  }
612 
613  void WU32(const char* varname, uint32_t v, MVOdbError* error)
614  {
615  W(varname, TID_DWORD, &v, sizeof(uint32_t), error);
616  }
617 
618  void WD(const char* varname, double v, MVOdbError* error)
619  {
620  W(varname, TID_DOUBLE, &v, sizeof(double), error);
621  }
622 
623  void WF(const char* varname, float v, MVOdbError* error)
624  {
625  W(varname, TID_FLOAT, &v, sizeof(float), error);
626  }
627 
628  void WS(const char* varname, const char* v, int string_length, MVOdbError* error)
629  {
630  if (string_length > 0) {
631  char* buf = (char*)malloc(string_length);
632  assert(buf);
633  strlcpy(buf, v, string_length);
634  W(varname, TID_STRING, buf, string_length, error);
635  free(buf);
636  } else {
637  int len = strlen(v);
638  W(varname, TID_STRING, v, len+1, error);
639  }
640  }
641 
642  void WAI(const char* varname, int index, int tid, const void* v, int size, MVOdbError* error)
643  {
644  std::string path = Path(varname);
645 
646  if (index < 0) {
647  SetError(error, fPrintError, path, "WxAI() called with negative array index");
648  return;
649  }
650 
651  //printf("WAI(\"%s\", [%d], %d) path [%s], size %d\n", varname, index, tid, path.c_str(), size);
652 
653  int status;
654  HNDLE hkey;
655 
656  status = db_find_key(fDB, 0, path.c_str(), &hkey);
657 
658  if (status != DB_SUCCESS) {
659  SetMidasStatus(error, fPrintError, path, "db_find_key", status);
660  return;
661  }
662 
663  status = db_set_data_index(fDB, hkey, v, size, index, tid);
664 
665  if (status != DB_SUCCESS) {
666  SetMidasStatus(error, fPrintError, path, "db_set_value", status);
667  return;
668  }
669 
670  SetOk(error);
671  }
672 
673  void WBAI(const char* varname, int index, bool v, MVOdbError* error)
674  {
675  BOOL vv = v;
676  WAI(varname, index, TID_BOOL, &vv, sizeof(BOOL), error);
677  }
678 
679  void WIAI(const char* varname, int index, int v, MVOdbError* error)
680  {
681  WAI(varname, index, TID_INT, &v, sizeof(int), error);
682  }
683 
684  void WU16AI(const char* varname, int index, uint16_t v, MVOdbError* error)
685  {
686  WAI(varname, index, TID_WORD, &v, sizeof(uint16_t), error);
687  }
688 
689  void WU32AI(const char* varname, int index, uint32_t v, MVOdbError* error)
690  {
691  WAI(varname, index, TID_DWORD, &v, sizeof(uint32_t), error);
692  }
693 
694  void WDAI(const char* varname, int index, double v, MVOdbError* error)
695  {
696  WAI(varname, index, TID_DOUBLE, &v, sizeof(double), error);
697  }
698 
699  void WFAI(const char* varname, int index, float v, MVOdbError* error)
700  {
701  WAI(varname, index, TID_FLOAT, &v, sizeof(float), error);
702  }
703 
704  void WSAI(const char* varname, int index, const char* v, MVOdbError* error)
705  {
706  int num_elements = 0;
707  int element_size = 0;
708  RAInfo(varname, &num_elements, &element_size, error);
709  if (error && error->fError)
710  return;
711  if (element_size <= 0)
712  return;
713  char* buf = (char*)malloc(element_size);
714  assert(buf);
715  strlcpy(buf, v, element_size);
716  WAI(varname, index, TID_STRING, buf, element_size, error);
717  free(buf);
718  }
719 
720  void WA(const char* varname, int tid, const void* v, int size, int count, MVOdbError* error)
721  {
722  std::string path = Path(varname);
723 
724  //printf("WA(tid %d, size %d, count %d)\n", tid, size, count);
725 
726  if (size == 0) {
727  int status = db_create_key(fDB, 0, path.c_str(), tid);
728 
729  if (status != DB_SUCCESS) {
730  SetMidasStatus(error, fPrintError, path, "db_create_key", status);
731  return;
732  }
733  } else {
734  int status = db_set_value(fDB, 0, path.c_str(), v, size, count, tid);
735 
736  //printf("WA db_set_value(tid %d, size %d, count %d) status %d\n", tid, size, count, status);
737 
738  if (status != DB_SUCCESS) {
739  SetMidasStatus(error, fPrintError, path, "db_set_value", status);
740  return;
741  }
742  }
743 
744  SetOk(error);
745  }
746 
747  void WBA(const char* varname, const std::vector<bool>& v, MVOdbError* error)
748  {
749  unsigned num = v.size();
750  BOOL val[num];
751 
752  for (unsigned i=0; i<num; i++) {
753  val[i] = v[i];
754  }
755 
756  WA(varname, TID_BOOL, val, num*sizeof(BOOL), num, error);
757  }
758 
759  void WU16A(const char* varname, const std::vector<uint16_t>& v, MVOdbError* error)
760  {
761  WA(varname, TID_WORD, &v[0], v.size()*sizeof(uint16_t), v.size(), error);
762  }
763 
764  void WU32A(const char* varname, const std::vector<uint32_t>& v, MVOdbError* error)
765  {
766  WA(varname, TID_DWORD, &v[0], v.size()*sizeof(uint32_t), v.size(), error);
767  }
768 
769  void WIA(const char* varname, const std::vector<int>& v, MVOdbError* error)
770  {
771  WA(varname, TID_INT, &v[0], v.size()*sizeof(int), v.size(), error);
772  }
773 
774  void WFA(const char* varname, const std::vector<float>& v, MVOdbError* error)
775  {
776  WA(varname, TID_FLOAT, &v[0], v.size()*sizeof(float), v.size(), error);
777  }
778 
779  void WDA(const char* varname, const std::vector<double>& v, MVOdbError* error)
780  {
781  WA(varname, TID_DOUBLE, &v[0], v.size()*sizeof(double), v.size(), error);
782  }
783 
784  void WSA(const char* varname, const std::vector<std::string>& v, int odb_string_size, MVOdbError* error)
785  {
786  unsigned num = v.size();
787  unsigned length = odb_string_size;
788 
789  if (length == 0) {
790  for (unsigned i=0; i<v.size(); i++) {
791  if (v[i].length() > length)
792  length = v[i].length();
793  }
794  length += 1; // for the string terminator NUL character
795  }
796 
797  char val[length*num];
798  memset(val, 0, length*num);
799 
800  for (unsigned i=0; i<num; i++)
801  strlcpy(val+length*i, v[i].c_str(), length);
802 
803  WA(varname, TID_STRING, val, num*length, num, error);
804  }
805 
806  void Delete(const char* odbname, MVOdbError* error)
807  {
808  std::string path = Path(odbname);
809 
810  //printf("Delete(%s)\n", path.c_str());
811 
812  HNDLE hKey;
813  int status = db_find_key(fDB, 0, path.c_str(), &hKey);
814 
815  if (status == DB_NO_KEY) {
816  SetOk(error);
817  return;
818  }
819 
820  if (status != DB_SUCCESS) {
821  SetMidasStatus(error, fPrintError, path, "db_find_key", status);
822  return;
823  }
824 
825  status = db_delete_key(fDB, hKey, FALSE);
826 
827  if (status != DB_SUCCESS) {
828  SetMidasStatus(error, fPrintError, path, "db_delete_key", status);
829  return;
830  }
831 
832  SetOk(error);
833  };
834 };
835 
836 MVOdb* MakeMidasOdb(int hDB, MVOdbError* error)
837 {
838  SetOk(error);
839  return new MidasOdb(hDB, "");
840 }
841 
842 /* emacs
843  * Local Variables:
844  * tab-width: 8
845  * c-basic-offset: 3
846  * indent-tabs-mode: nil
847  * End:
848  */
bool fError
Definition: mvodb.h:187
Definition: mvodb.h:21
void RIA(const char *varname, std::vector< int > *value, bool create, int create_size, MVOdbError *error)
Definition: midasodb.cxx:436
void WU16(const char *varname, uint16_t v, MVOdbError *error)
Definition: midasodb.cxx:608
bool GetPrintError() const
Definition: midasodb.cxx:65
void SetPrintError(bool v)
Definition: midasodb.cxx:60
void WU16AI(const char *varname, int index, uint16_t v, MVOdbError *error)
Definition: midasodb.cxx:684
void RD(const char *varname, double *value, bool create, MVOdbError *error)
Definition: midasodb.cxx:244
void RF(const char *varname, float *value, bool create, MVOdbError *error)
Definition: midasodb.cxx:249
void RA(const std::string &path, int tid, void *buf, int size, MVOdbError *error)
Definition: midasodb.cxx:354
void WIAI(const char *varname, int index, int v, MVOdbError *error)
Definition: midasodb.cxx:679
bool fPrintError
Definition: midasodb.cxx:31
MidasOdb(HNDLE hDB, const char *root)
Definition: midasodb.cxx:34
void W(const char *varname, int tid, const void *v, int size, MVOdbError *error)
Definition: midasodb.cxx:583
void R(const char *varname, int tid, void *value, int size, bool create, MVOdbError *error)
Definition: midasodb.cxx:217
void RIAI(const char *varname, int index, int *value, MVOdbError *error)
Definition: midasodb.cxx:301
void RFAI(const char *varname, int index, float *value, MVOdbError *error)
Definition: midasodb.cxx:321
void ResizeStringArray(const char *varname, int new_size, int new_string_length, MVOdbError *error)
Definition: midasodb.cxx:204
void RSA(const char *varname, std::vector< std::string > *value, bool create, int create_size, int create_string_length, MVOdbError *error)
Definition: midasodb.cxx:488
void RAInfo(const char *varname, int *num_elements, int *element_size, MVOdbError *error)
Definition: midasodb.cxx:108
void GetArraySize(const char *varname, int *pnum_values, int *pitem_size, MVOdbError *error)
Definition: midasodb.cxx:366
void WU32(const char *varname, uint32_t v, MVOdbError *error)
Definition: midasodb.cxx:613
void RDA(const char *varname, std::vector< double > *value, bool create, int create_size, MVOdbError *error)
Definition: midasodb.cxx:446
void RB(const char *varname, bool *value, bool create, MVOdbError *error)
Definition: midasodb.cxx:254
bool fTrace
Definition: midasodb.cxx:30
void WDA(const char *varname, const std::vector< double > &v, MVOdbError *error)
Definition: midasodb.cxx:779
void RSAI(const char *varname, int index, std::string *value, MVOdbError *error)
Definition: midasodb.cxx:334
void WIA(const char *varname, const std::vector< int > &v, MVOdbError *error)
Definition: midasodb.cxx:769
HNDLE fDB
Definition: midasodb.cxx:28
void RU32AI(const char *varname, int index, uint32_t *value, MVOdbError *error)
Definition: midasodb.cxx:311
void RAI(const char *varname, int index, int tid, void *value, int size, MVOdbError *error)
Definition: midasodb.cxx:282
std::string Path(const char *varname)
Definition: midasodb.cxx:51
void WI(const char *varname, int v, MVOdbError *error)
Definition: midasodb.cxx:603
void WS(const char *varname, const char *v, int string_length, MVOdbError *error)
Definition: midasodb.cxx:628
bool IsReadOnly() const
Definition: midasodb.cxx:70
void RI(const char *varname, int *value, bool create, MVOdbError *error)
Definition: midasodb.cxx:229
void RU32A(const char *varname, std::vector< uint32_t > *value, bool create, int create_size, MVOdbError *error)
Definition: midasodb.cxx:456
void WSA(const char *varname, const std::vector< std::string > &v, int odb_string_size, MVOdbError *error)
Definition: midasodb.cxx:784
void ResizeArray(const char *varname, int new_size, MVOdbError *error)
Definition: midasodb.cxx:181
void RDAI(const char *varname, int index, double *value, MVOdbError *error)
Definition: midasodb.cxx:316
void WBAI(const char *varname, int index, bool v, MVOdbError *error)
Definition: midasodb.cxx:673
void RBA(const char *varname, std::vector< bool > *value, bool create, int create_size, MVOdbError *error)
Definition: midasodb.cxx:461
void RFA(const char *varname, std::vector< float > *value, bool create, int create_size, MVOdbError *error)
Definition: midasodb.cxx:441
void Delete(const char *odbname, MVOdbError *error)
Definition: midasodb.cxx:806
~MidasOdb()
Definition: midasodb.cxx:42
void WB(const char *varname, bool v, MVOdbError *error)
Definition: midasodb.cxx:597
void WU16A(const char *varname, const std::vector< uint16_t > &v, MVOdbError *error)
Definition: midasodb.cxx:759
void ReadKey(const char *varname, int *tid, int *num_values, int *total_size, int *item_size, MVOdbError *error)
Definition: midasodb.cxx:135
MVOdb * Chdir(const char *subdir, bool create, MVOdbError *error)
Definition: midasodb.cxx:75
void WDAI(const char *varname, int index, double v, MVOdbError *error)
Definition: midasodb.cxx:694
void WBA(const char *varname, const std::vector< bool > &v, MVOdbError *error)
Definition: midasodb.cxx:747
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)
Definition: midasodb.cxx:175
void RU16A(const char *varname, std::vector< uint16_t > *value, bool create, int create_size, MVOdbError *error)
Definition: midasodb.cxx:451
void RU16(const char *varname, uint16_t *value, bool create, MVOdbError *error)
Definition: midasodb.cxx:234
void WU32A(const char *varname, const std::vector< uint32_t > &v, MVOdbError *error)
Definition: midasodb.cxx:764
void WFA(const char *varname, const std::vector< float > &v, MVOdbError *error)
Definition: midasodb.cxx:774
void WD(const char *varname, double v, MVOdbError *error)
Definition: midasodb.cxx:618
void WSAI(const char *varname, int index, const char *v, MVOdbError *error)
Definition: midasodb.cxx:704
void RS(const char *varname, std::string *value, bool create, int create_string_length, MVOdbError *error)
Definition: midasodb.cxx:262
std::string fRoot
Definition: midasodb.cxx:29
void RBAI(const char *varname, int index, bool *value, MVOdbError *error)
Definition: midasodb.cxx:326
void WU32AI(const char *varname, int index, uint32_t v, MVOdbError *error)
Definition: midasodb.cxx:689
void RU16AI(const char *varname, int index, uint16_t *value, MVOdbError *error)
Definition: midasodb.cxx:306
void WF(const char *varname, float v, MVOdbError *error)
Definition: midasodb.cxx:623
void RU32(const char *varname, uint32_t *value, bool create, MVOdbError *error)
Definition: midasodb.cxx:239
void WA(const char *varname, int tid, const void *v, int size, int count, MVOdbError *error)
Definition: midasodb.cxx:720
void WAI(const char *varname, int index, int tid, const void *v, int size, MVOdbError *error)
Definition: midasodb.cxx:642
void WFAI(const char *varname, int index, float v, MVOdbError *error)
Definition: midasodb.cxx:699
void RXA(const char *varname, int tid, std::vector< X > *value, bool create, int create_size, MVOdbError *error)
Definition: midasodb.cxx:386
#define TID_DOUBLE
Definition: midasio.h:29
#define TID_KEY
Definition: midasio.h:35
#define TID_BOOL
Definition: midasio.h:26
#define TID_WORD
Definition: midasio.h:18
#define TID_STRING
Definition: midasio.h:32
#define TID_INT
Definition: midasio.h:24
#define TID_FLOAT
Definition: midasio.h:27
#define TID_DWORD
Definition: midasio.h:22
void SetMidasStatus(MVOdbError *error, bool print, const std::string &path, const char *midas_func_name, int status)
Definition: mvodb.cxx:41
MVOdb * MakeNullOdb()
Definition: nullodb.cxx:123
void SetError(MVOdbError *error, bool print, const std::string &path, const std::string &message)
Definition: mvodb.cxx:70
void SetOk(MVOdbError *error)
Definition: mvodb.cxx:31
size_t EXPRT strlcpy(char *dst, const char *src, size_t size)
Definition: strlcpy.cxx:39
MVOdb * MakeMidasOdb(int hDB, MVOdbError *error)
Definition: midasodb.cxx:836
static std::string toString(int value)
Definition: midasodb.cxx:18
#define TRUE
Definition: mxml.cxx:75
#define FALSE
Definition: mxml.cxx:76