Belle II Software  release-05-01-25
RFConf.cc
1 //+
2 // File : RFConf.cc
3 // Description : Access to configuration file
4 //
5 // Author : Ryosuke Itoh, KEK
6 // Date : 13- June - 2013
7 // Note : Code recycled from Nakao's d2 library
8 //-
9 
10 #include "daq/rfarm/manager/RFConf.h"
11 
12 #include <cctype>
13 #include <cstdlib>
14 #include <cstring>
15 
16 using namespace std;
17 using namespace Belle2;
18 
19 RFConf::RFConf(const char* filename)
20 {
21  m_fd = fopen(filename, "r");
22  if (m_fd == NULL) {
23  fprintf(stderr, "RFConf : config file not found %s\n", filename);
24  exit(-1);
25  }
26 }
27 
28 RFConf::~RFConf()
29 {
30  fclose(m_fd);
31 }
32 
33 // Get Configuration
34 
35 char* RFConf::getconf(const char* key1, const char* key2, const char* key3)
36 {
37  char buf[1024], keybuf[256];
38  char* p, *q, *keyp, *delp, *valp;
39  int line;
40 
41  struct RFConf_t top, *cur;
42  top.key = NULL;
43 
44  // Encode key2 and key3 in the first key if exist
45  // if (! key2) { key2 = key3; key3 = 0; }
46  // if (! key1) { key1 = key2; key2 = key3; key3 = 0; }
47  if (key3)
48  sprintf(keybuf, "%s.%s.%s", key1, key2, key3);
49  else if (key2)
50  sprintf(keybuf, "%s.%s", key1, key2);
51  else if (key1)
52  strcpy(keybuf, key1);
53  else
54  return NULL;
55 
56  // Search for the record in the configuration file
57  rewind(m_fd);
58  if (! top.key) { /* the first invokation */
59  line = 0;
60  cur = &top;
61  while (fgets(buf, sizeof(buf), m_fd)) {
62  line++;
63  /* remove '\n' and skip too long line */
64  p = strchr(buf, '\n');
65  if (! p) {
66  fprintf(stderr, "RFConf : line %d too long\n", line);
67  while (fgets(buf, sizeof(buf), m_fd) && !strchr(buf, '\n'))
68  ;
69  continue;
70  }
71  *p = 0;
72  // printf ( "buf = %s\n", buf );
73 
74  /* sorry for this very tricky code... */
75  keyp = valp = delp = 0;
76  for (p = buf; *p && *p != '#'; p++) {
77  if (! keyp) {
78  if (! isspace(*p)) keyp = p;
79  } else if (! delp) {
80  if (isspace(*p)) {
81  if (!isspace(*(p + 1)) && *(p + 1) != ':') {
82  fprintf(stderr, "RFConf : invalid key at line %d\n", line);
83  break;
84  }
85  *p = 0;
86  } else if (*p == ':') {
87  *(delp = p) = 0;
88  }
89  } else if (! valp) {
90  if (! isspace(*p)) {
91  valp = q = p;
92  q++;
93  }
94  } else if (! isspace(*p)) {
95  *q++ = *p;
96  } else if (! isspace(*(p + 1))) {
97  *q++ = ' ';
98  }
99  }
100  if (valp) {
101  for (*q-- = 0; isspace(*q); *q-- = 0)
102  ;
103  }
104  if (delp) {
105  cur->next = (RFConf_t*)malloc(sizeof(*cur));
106  cur = cur->next;
107  cur->next = 0;
108  cur->key = (char*)malloc(strlen(keyp) + 1);
109  strcpy(cur->key, keyp);
110  if (valp) {
111  cur->val = (char*)malloc(strlen(valp) + 1);
112  strcpy(cur->val, valp);
113  } else {
114  cur->val = 0;
115  }
116  }
117  }
118  } else if (! top.val) { /* the first invokation must have failed */
119  return NULL;
120  }
121 
122  int nitem = 0;
123  for (cur = &top; cur; cur = cur->next) {
124  if (cur->key == NULL) continue;
125  if (strcmp(cur->key, keybuf) == 0) return cur->val;
126  nitem++;
127  }
128  printf("RFConf: Key %s not found\n", keybuf);
129  printf("nitem = %d, keybuf = %s\n", nitem, keybuf);
130  return NULL;
131 }
132 
133 int RFConf::getconfi(const char* key1, const char* key2, const char* key3)
134 {
135  return atoi(getconf(key1, key2, key3));
136 }
137 
138 
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::RFConf_t
Definition: RFConf.h:19