EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairDbConf.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairDbConf.cxx
1 #include <map>
2 #include <sstream>
3 #include <cassert>
4 #include <cmath>
5 #include "FairRegistry.h"
6 #include "FairDbConf.h"
7 #include "FairDbString.h"
8 
9 
10 void FairDbConf::RegistryToString(std::string& x, const FairRegistry& r)
11 {
13  const char* s;
14 
15  std::ostringstream ss;
16  while ( (s=rk()) ) {
17  // Try to extract the value for this key
18  char c = 0;
19  const char* cs = 0;
20  int i = 0;
21  double d = 0;
22  FairRegistry reg;
23  bool isChar = r.Get(s,c);
24  bool isCharStar = r.Get(s,cs);
25  bool isInt = r.Get(s,i);
26  bool isDouble = r.Get(s,d);
27  bool isRegistry = r.Get(s,reg);
28 
29  ss << s << "=";
30  if (isChar) { ss << c; }
31  else if (isCharStar) { ss << "'"<<cs<<"'"; }
32  else if (isInt) { ss << i; }
33  else if (isDouble) { ss << d; if (rint(d)==d) { ss << ".0"; } }
34  else if (isRegistry) { ss << reg; } // Don't think this works...
35  else { assert("Unknown type or bad key in registry"==0); }
36  ss << " ";
37  }
38  x = ss.str();
39 }
40 
41 //......................................................................
42 
44 {
45 //======================================================================
46 // Convert the string s to a FairRegistry. Format of s is:
47 //
48 // toggle=on pdq=true a=1 b=2.0 name=Mark longstring='some long text'
49 //
50 //======================================================================
51  // Parse string out into keys and values
52  int len = strlen(s);
53  char* scopy = new char[len+1];
54  strcpy(scopy,s);
55 
56  // Skip ahead until we find an equal sign
57  char* cKey = 0;
58  char* cEqual = 0;
59  char* cValue = 0;
60  char* cEnd = 0;
61  for (int i=0; i<len; ++i) {
62  // Pick off the equal sign...
63  if (scopy[i] == '=') {
64  cEqual = scopy+i;
65  *cEqual = '\0';
66 
67  // Step back to find the start of the key
68  for (cKey=cEqual-1; *cKey==' ' && cKey>scopy; --cKey) { *cKey = '\0'; }
69  for (; *cKey!=' ' && *cKey!=',' && *cKey!='\0' && cKey>scopy; --cKey) { ; }
70  for (; *cKey==' ' || *cKey=='\0'; ++cKey) { ; }
71 
72  // Step forward to get the start of the value
73  for (cValue=cEqual+1; *cValue==' '&&*cValue!='\0'; ++cValue) {
74  *cValue = '\0';
75  }
76  while (*cValue==' ') { ++cValue; }
77 
78  // Handle special case of long strings in single or double quotes
79  bool isString = false;
80  if (*cValue=='\'' || *cValue=='\"') {
81  isString = true;
82  char stringDelim = *cValue;
83  ++cValue;
84  for (cEnd = cValue; *cEnd!='\0' && *cEnd!=stringDelim; ++cEnd) { ; }
85  } else {
86  for (cEnd = cValue; *cEnd!='\0'&& *cEnd!=' '&& *cEnd!=','; ++cEnd) { ; }
87  }
88  *cEnd = '\0';
89 
90  // Convert value to correct data type
91  if ( isString ) {
92  r.Set(cKey,cValue);
93  } else if (FairUtilString::IsInt(cValue)) {
94  int val = atoi(cValue);
95  r.Set(cKey,val);
96  } else if (FairUtilString::IsFloat(cValue)) {
97  double d = atof(cValue);
98  r.Set(cKey,d);
99  } else if (FairUtilString::IsBool(cValue)) {
100  bool b = FairUtilString::atob(cValue);
101  r.Set(cKey,b);
102  } else {
103  // Single character stored at char
104  if (strlen(cValue)==1) {
105  char c = *cValue;
106  r.Set(cKey,c);
107  } else {
108  // Longer string stored as string
109  r.Set(cKey,cValue);
110  }
111  }
112  }
113  cEqual = cEnd+1;
114  }
115 
116  delete [] scopy;
117 }