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