21 sprintf(buf,
"%d", value);
46 std::string
Path(
const char* varname)
72 std::string path =
Path(subdir);
74 int status = db_find_key(
fDB, 0, path.c_str(), &hkey);
75 if (status == DB_SUCCESS) {
77 status = db_get_key(
fDB, hkey, &key);
78 if (status != DB_SUCCESS) {
94 status = db_create_key(
fDB, 0, path.c_str(),
TID_KEY);
95 if (status != DB_SUCCESS) {
105 std::string path =
Path(varname);
114 status = db_find_key(
fDB, 0, path.c_str(), &hkey);
115 if (status != DB_SUCCESS)
119 status = db_get_key(
fDB, hkey, &key);
120 if (status != DB_SUCCESS)
124 *num_elements = key.num_values;
127 *element_size = key.item_size;
130 void ReadKey(
const char* varname,
int *tid,
int *num_values,
int *total_size,
int *item_size,
MVOdbError* error)
133 if (num_values) *num_values = 0;
134 if (total_size) *total_size = 0;
135 if (item_size) *item_size = 0;
137 std::string path =
Path(varname);
142 status = db_find_key(
fDB, 0, path.c_str(), &hkey);
143 if (status != DB_SUCCESS) {
149 status = db_get_key(
fDB, hkey, &key);
150 if (status != DB_SUCCESS) {
159 *num_values = key.num_values;
162 *total_size = key.total_size;
165 *item_size = key.item_size;
172 if (last_written) *last_written = 0;
174 std::string path =
Path(varname);
179 status = db_find_key(
fDB, 0, path.c_str(), &hkey);
180 if (status != DB_SUCCESS) {
186 status = db_get_key(
fDB, hkey, &key);
187 if (status != DB_SUCCESS) {
193 *last_written = key.last_written;
198 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)
206 std::string path =
Path(varname);
211 status = db_find_key(
fDB, 0, path.c_str(), &hkey);
213 if (status != DB_SUCCESS) {
218 status = db_set_num_values(
fDB, hkey, new_size);
219 if (status != SUCCESS) {
229 std::string path =
Path(varname);
231 int status = db_resize_string(
fDB, 0, path.c_str(), new_size, new_string_length);
232 if (status != SUCCESS) {
240 bool R(
const char* varname,
int tid,
void *value,
int size,
bool create,
MVOdbError* error)
243 std::string path =
Path(varname);
244 int status = db_get_value(
fDB, 0, path.c_str(), value, &size, tid, create);
245 if (status != DB_SUCCESS) {
253 void RI(
const char* varname,
int *value,
bool create,
MVOdbError* error)
255 R(varname,
TID_INT, value,
sizeof(
int), create, error);
260 R(varname,
TID_WORD, value,
sizeof(uint16_t), create, error);
265 R(varname,
TID_DWORD, value,
sizeof(uint32_t), create, error);
268 void RD(
const char* varname,
double *value,
bool create,
MVOdbError* error)
270 bool ok =
R(varname,
TID_DOUBLE, value,
sizeof(
double), create, error);
272 float fvalue = *value;
273 ok =
R(varname,
TID_FLOAT, &fvalue,
sizeof(
float), create, error);
275 std::string path =
Path(varname);
276 fprintf(stderr,
"MVOdb::RD: Sucessfully read ODB \"%s\" of type FLOAT instead of DOUBLE\n", path.c_str());
282 void RF(
const char* varname,
float *value,
bool create,
MVOdbError* error)
284 R(varname,
TID_FLOAT, value,
sizeof(
float), create, error);
287 void RB(
const char* varname,
bool *value,
bool create,
MVOdbError* error)
291 R(varname,
TID_BOOL, &v,
sizeof(BOOL), create, error);
295 void RS(
const char* varname, std::string* value,
bool create,
int create_string_length,
MVOdbError* error)
298 std::string path =
Path(varname);
300 #ifdef HAVE_DB_GET_VALUE_STRING_CREATE_STRING_LENGTH
301 int status = db_get_value_string(
fDB, 0, path.c_str(), 0, value, create, create_string_length);
303 #warning This MIDAS has an old version of db_get_value_string() and RS() will ignore the create_string_length argument.
304 int status = db_get_value_string(
fDB, 0, path.c_str(), 0, value, create);
307 if (status != DB_SUCCESS) {
315 void RAI(
const char* varname,
int index,
int tid,
void *value,
int size,
MVOdbError* error)
318 std::string path =
Path(varname);
326 int status = db_get_value(
fDB, 0, path.c_str(), value, &size, tid,
FALSE);
327 if (status != DB_SUCCESS) {
336 RAI(varname, index,
TID_INT, value,
sizeof(
int), error);
341 RAI(varname, index,
TID_WORD, value,
sizeof(uint16_t), error);
346 RAI(varname, index,
TID_DWORD, value,
sizeof(uint32_t), error);
351 RAI(varname, index,
TID_DOUBLE, value,
sizeof(
double), error);
356 RAI(varname, index,
TID_FLOAT, value,
sizeof(
float), error);
363 RAI(varname, index,
TID_BOOL, &v,
sizeof(BOOL), error);
370 std::string path =
Path(varname);
377 int status = db_get_value_string(
fDB, 0, path.c_str(), index, value,
FALSE);
379 if (status != DB_SUCCESS) {
387 bool RA(
const std::string& path,
int tid,
void* buf,
int size,
MVOdbError* error)
389 int status = db_get_value(
fDB, 0, path.c_str(), buf, &size, tid,
FALSE);
391 if (status != DB_SUCCESS) {
407 ReadKey(varname, &xtid, pnum_values, &xtotal_size, pitem_size, error);
420 template <
class X>
bool RXA(
const char* varname,
int tid, std::vector<X> *value,
bool create,
int create_size,
MVOdbError* error)
422 std::string path =
Path(varname);
430 if (create && create_size > 0) {
431 if (num_values < 0) {
434 W(varname, tid, &v,
sizeof(X), error);
435 if (error && error->
fError)
438 }
else if (num_values != create_size) {
447 if (num_values > 0) {
448 value->resize(num_values);
449 return RA(path, tid, &((*value)[0]), num_values*
sizeof(X), error);
457 WA(varname, tid, &((*value)[0]), value->size()*
sizeof(X), value->size(), error);
459 if (error && error->
fError)
462 if (create_size > 0) {
463 if (create_size != (
int)value->size()) {
471 void RIA(
const char* varname, std::vector<int> *value,
bool create,
int create_size,
MVOdbError* error)
473 RXA<int>(varname,
TID_INT, value, create, create_size, error);
476 void RFA(
const char* varname, std::vector<float> *value,
bool create,
int create_size,
MVOdbError* error)
478 RXA<float>(varname,
TID_FLOAT, value, create, create_size, error);
481 void RDA(
const char* varname, std::vector<double> *value,
bool create,
int create_size,
MVOdbError* error)
483 bool ok = RXA<double>(varname,
TID_DOUBLE, value, create, create_size, error);
485 std::vector<float> fvalue;
486 std::vector<float> *fvalue_ptr = NULL;
488 fvalue_ptr = &fvalue;
489 for (
size_t i=0; i<value->size(); i++) {
490 fvalue.push_back((*value)[i]);
493 ok = RXA<float>(varname,
TID_FLOAT, fvalue_ptr, create, create_size, error);
494 if (ok && fvalue_ptr) {
496 std::string path =
Path(varname);
497 fprintf(stderr,
"MVOdb::RDA: Sucessfully read ODB \"%s\" of type FLOAT instead of DOUBLE\n", path.c_str());
500 for (
size_t i=0; i<fvalue.size(); i++) {
501 value->push_back(fvalue[i]);
507 void RU16A(
const char* varname, std::vector<uint16_t> *value,
bool create,
int create_size,
MVOdbError* error)
509 RXA<uint16_t>(varname,
TID_WORD, value, create, create_size, error);
512 void RU32A(
const char* varname, std::vector<uint32_t> *value,
bool create,
int create_size,
MVOdbError* error)
514 RXA<uint32_t>(varname,
TID_DWORD, value, create, create_size, error);
517 void RBA(
const char* varname, std::vector<bool> *value,
bool create,
int create_size,
MVOdbError* error)
519 std::vector<BOOL> xvalue;
520 std::vector<BOOL> *xvalue_ptr = NULL;
523 for (std::size_t i=0; i<value->size(); i++) {
525 xvalue.push_back(
TRUE);
527 xvalue.push_back(
FALSE);
529 xvalue_ptr = &xvalue;
532 RXA<BOOL>(varname,
TID_BOOL, xvalue_ptr, create, create_size, error);
536 for (std::size_t i=0; i<xvalue.size(); i++) {
538 value->push_back(
true);
540 value->push_back(
false);
545 void RSA(
const char* varname, std::vector<std::string> *value,
bool create,
int create_size,
int create_string_length,
MVOdbError* error)
547 std::string path =
Path(varname);
555 if (create && (create_size > 0) && (create_string_length > 0)) {
556 if (num_values < 0) {
558 WS(varname,
"", create_string_length, error);
559 if (error && error->
fError)
562 }
else if ((num_values != create_size) || (item_size != create_string_length)) {
573 if (num_values > 0) {
575 int bufsize = num_values*item_size;
576 char* buf = (
char*)malloc(bufsize);
578 memset(buf, 0, bufsize);
580 for (
int i=0; i<num_values; i++) {
581 value->push_back(buf+i*item_size);
598 int string_length = 0;
599 for (
size_t i = 0; i < value->size(); i++) {
600 if (((
int)(*value)[i].length()) > string_length)
601 string_length = (*value)[i].length();
605 if (create_string_length > string_length)
606 string_length = create_string_length;
610 int bufsize = value->size()*string_length;
613 buf = (
char*)malloc(bufsize);
615 memset(buf, 0, bufsize);
617 for (
size_t i=0; i<value->size(); i++) {
618 strlcpy(buf+i*string_length, (*value)[i].c_str(), string_length);
622 WA(varname,
TID_STRING, buf, bufsize, value->size(), error);
629 if (error && error->
fError)
632 if ((create_size > 0) && (create_string_length > 0)) {
633 if ((((
int)value->size()) != create_size) || (string_length != create_string_length)) {
640 void W(
const char* varname,
int tid,
const void* v,
int size,
MVOdbError* error)
642 std::string path =
Path(varname);
644 int status = db_set_value(
fDB, 0, path.c_str(), v, size, 1, tid);
646 if (status == DB_TYPE_MISMATCH) {
648 fprintf(stderr,
"MVOdb::W: Data type mismatch when writing to ODB \"%s\", deleting the old entry\n", path.c_str());
653 status = db_set_value(
fDB, 0, path.c_str(), v, size, 1, tid);
656 if (status != DB_SUCCESS) {
668 W(varname,
TID_BOOL, &vv,
sizeof(BOOL), error);
673 W(varname,
TID_INT, &v,
sizeof(
int), error);
678 W(varname,
TID_WORD, &v,
sizeof(uint16_t), error);
683 W(varname,
TID_DWORD, &v,
sizeof(uint32_t), error);
693 W(varname,
TID_FLOAT, &v,
sizeof(
float), error);
696 void WS(
const char* varname,
const char* v,
int string_length,
MVOdbError* error)
698 if (string_length > 0) {
699 char* buf = (
char*)malloc(string_length);
701 strlcpy(buf, v, string_length);
710 void WAI(
const char* varname,
int index,
int tid,
const void* v,
int size,
MVOdbError* error)
712 std::string path =
Path(varname);
724 status = db_find_key(
fDB, 0, path.c_str(), &hkey);
726 if (status != DB_SUCCESS) {
731 status = db_set_data_index(
fDB, hkey, v, size, index, tid);
733 if (status != DB_SUCCESS) {
744 WAI(varname, index,
TID_BOOL, &vv,
sizeof(BOOL), error);
749 WAI(varname, index,
TID_INT, &v,
sizeof(
int), error);
754 WAI(varname, index,
TID_WORD, &v,
sizeof(uint16_t), error);
759 WAI(varname, index,
TID_DWORD, &v,
sizeof(uint32_t), error);
769 WAI(varname, index,
TID_FLOAT, &v,
sizeof(
float), error);
774 int num_elements = 0;
775 int element_size = 0;
776 RAInfo(varname, &num_elements, &element_size, error);
777 if (error && error->
fError)
779 if (element_size <= 0)
781 char* buf = (
char*)malloc(element_size);
783 strlcpy(buf, v, element_size);
788 void WA(
const char* varname,
int tid,
const void* v,
int size,
int count,
MVOdbError* error)
790 std::string path =
Path(varname);
795 int status = db_create_key(
fDB, 0, path.c_str(), tid);
797 if (status == DB_TYPE_MISMATCH) {
799 fprintf(stderr,
"MVOdb::WA: Data type mismatch when writing to ODB \"%s\", deleting the old entry\n", path.c_str());
804 status = db_create_key(
fDB, 0, path.c_str(), tid);
807 if (status != DB_SUCCESS) {
812 int status = db_set_value(
fDB, 0, path.c_str(), v, size, count, tid);
816 if (status == DB_TYPE_MISMATCH) {
818 fprintf(stderr,
"MVOdb::WA: Data type mismatch when writing to ODB \"%s\", deleting the old entry\n", path.c_str());
823 status = db_set_value(
fDB, 0, path.c_str(), v, size, count, tid);
826 if (status != DB_SUCCESS) {
835 void WBA(
const char* varname,
const std::vector<bool>& v,
MVOdbError* error)
837 unsigned num = v.size();
840 for (
unsigned i=0; i<num; i++) {
844 WA(varname,
TID_BOOL, val, num*
sizeof(BOOL), num, error);
849 WA(varname,
TID_WORD, &v[0], v.size()*
sizeof(uint16_t), v.size(), error);
854 WA(varname,
TID_DWORD, &v[0], v.size()*
sizeof(uint32_t), v.size(), error);
859 WA(varname,
TID_INT, &v[0], v.size()*
sizeof(
int), v.size(), error);
862 void WFA(
const char* varname,
const std::vector<float>& v,
MVOdbError* error)
864 WA(varname,
TID_FLOAT, &v[0], v.size()*
sizeof(
float), v.size(), error);
867 void WDA(
const char* varname,
const std::vector<double>& v,
MVOdbError* error)
869 WA(varname,
TID_DOUBLE, &v[0], v.size()*
sizeof(
double), v.size(), error);
872 void WSA(
const char* varname,
const std::vector<std::string>& v,
int odb_string_size,
MVOdbError* error)
874 unsigned num = v.size();
875 unsigned length = odb_string_size;
878 for (
unsigned i=0; i<v.size(); i++) {
879 if (v[i].length() > length)
880 length = v[i].length();
885 char val[length*num];
886 memset(val, 0, length*num);
888 for (
unsigned i=0; i<num; i++)
889 strlcpy(val+length*i, v[i].c_str(), length);
896 std::string path =
Path(odbname);
901 int status = db_find_key(
fDB, 0, path.c_str(), &hKey);
903 if (status == DB_NO_KEY) {
908 if (status != DB_SUCCESS) {
913 status = db_delete_key(
fDB, hKey,
FALSE);
915 if (status != DB_SUCCESS) {
void RIA(const char *varname, std::vector< int > *value, bool create, int create_size, MVOdbError *error)
void WU16(const char *varname, uint16_t v, MVOdbError *error)
bool GetPrintError() const
void SetPrintError(bool v)
void WU16AI(const char *varname, int index, uint16_t v, MVOdbError *error)
void RD(const char *varname, double *value, bool create, MVOdbError *error)
bool R(const char *varname, int tid, void *value, int size, bool create, MVOdbError *error)
void RF(const char *varname, float *value, bool create, MVOdbError *error)
void WIAI(const char *varname, int index, int v, MVOdbError *error)
MidasOdb(HNDLE hDB, const char *root)
void W(const char *varname, int tid, const void *v, int size, MVOdbError *error)
void RIAI(const char *varname, int index, int *value, MVOdbError *error)
void RFAI(const char *varname, int index, float *value, MVOdbError *error)
void ResizeStringArray(const char *varname, int new_size, int new_string_length, MVOdbError *error)
void RSA(const char *varname, std::vector< std::string > *value, bool create, int create_size, int create_string_length, MVOdbError *error)
void RAInfo(const char *varname, int *num_elements, int *element_size, MVOdbError *error)
void GetArraySize(const char *varname, int *pnum_values, int *pitem_size, MVOdbError *error)
void WU32(const char *varname, uint32_t v, MVOdbError *error)
void RDA(const char *varname, std::vector< double > *value, bool create, int create_size, MVOdbError *error)
void RB(const char *varname, bool *value, bool create, MVOdbError *error)
void WDA(const char *varname, const std::vector< double > &v, MVOdbError *error)
void RSAI(const char *varname, int index, std::string *value, MVOdbError *error)
void WIA(const char *varname, const std::vector< int > &v, MVOdbError *error)
void RU32AI(const char *varname, int index, uint32_t *value, MVOdbError *error)
void RAI(const char *varname, int index, int tid, void *value, int size, MVOdbError *error)
bool RXA(const char *varname, int tid, std::vector< X > *value, bool create, int create_size, MVOdbError *error)
std::string Path(const char *varname)
void WI(const char *varname, int v, MVOdbError *error)
void WS(const char *varname, const char *v, int string_length, MVOdbError *error)
void ReadKeyLastWritten(const char *varname, int *last_written, MVOdbError *error)
void RI(const char *varname, int *value, bool create, MVOdbError *error)
void RU32A(const char *varname, std::vector< uint32_t > *value, bool create, int create_size, MVOdbError *error)
void WSA(const char *varname, const std::vector< std::string > &v, int odb_string_size, MVOdbError *error)
void ResizeArray(const char *varname, int new_size, MVOdbError *error)
void RDAI(const char *varname, int index, double *value, MVOdbError *error)
void WBAI(const char *varname, int index, bool v, MVOdbError *error)
void RBA(const char *varname, std::vector< bool > *value, bool create, int create_size, MVOdbError *error)
void RFA(const char *varname, std::vector< float > *value, bool create, int create_size, MVOdbError *error)
void Delete(const char *odbname, MVOdbError *error)
void WB(const char *varname, bool v, MVOdbError *error)
void WU16A(const char *varname, const std::vector< uint16_t > &v, MVOdbError *error)
void ReadKey(const char *varname, int *tid, int *num_values, int *total_size, int *item_size, MVOdbError *error)
MVOdb * Chdir(const char *subdir, bool create, MVOdbError *error)
void WDAI(const char *varname, int index, double v, MVOdbError *error)
void WBA(const char *varname, const std::vector< bool > &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 RU16A(const char *varname, std::vector< uint16_t > *value, bool create, int create_size, MVOdbError *error)
void RU16(const char *varname, uint16_t *value, bool create, MVOdbError *error)
void WU32A(const char *varname, const std::vector< uint32_t > &v, MVOdbError *error)
void WFA(const char *varname, const std::vector< float > &v, MVOdbError *error)
void WD(const char *varname, double v, MVOdbError *error)
void WSAI(const char *varname, int index, const char *v, MVOdbError *error)
void RS(const char *varname, std::string *value, bool create, int create_string_length, MVOdbError *error)
bool RA(const std::string &path, int tid, void *buf, int size, MVOdbError *error)
void RBAI(const char *varname, int index, bool *value, MVOdbError *error)
void WU32AI(const char *varname, int index, uint32_t v, MVOdbError *error)
void RU16AI(const char *varname, int index, uint16_t *value, MVOdbError *error)
void WF(const char *varname, float v, MVOdbError *error)
void RU32(const char *varname, uint32_t *value, bool create, MVOdbError *error)
void WA(const char *varname, int tid, const void *v, int size, int count, MVOdbError *error)
void WAI(const char *varname, int index, int tid, const void *v, int size, MVOdbError *error)
void WFAI(const char *varname, int index, float v, MVOdbError *error)
void SetMidasStatus(MVOdbError *error, bool print, const std::string &path, const char *midas_func_name, int status)
void SetError(MVOdbError *error, bool print, const std::string &path, const std::string &message)
void SetOk(MVOdbError *error)
MVOdb * MakeMidasOdb(int hDB, MVOdbError *error)
static std::string toString(int value)