Belle II Software development
RFConf Class Reference
Inheritance diagram for RFConf:
RFNodeManager

Public Member Functions

 RFConf (const char *file)
 
 RFConf (const RFConf &)=delete
 Copy constructor (disabled).
 
RFConf & operator= (const RFConf &)=delete
 Operator = (disabled).
 
char * getconf (const char *key1, const char *key2=NULL, const char *key3=NULL)
 
int getconfi (const char *key1, const char *key2=NULL, const char *key3=NULL)
 

Private Attributes

FILE * m_fd
 

Detailed Description

Definition at line 24 of file RFConf.h.

Constructor & Destructor Documentation

◆ RFConf()

RFConf ( const char * file)
explicit

Definition at line 18 of file RFConf.cc.

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}

◆ ~RFConf()

~RFConf ( )

Definition at line 27 of file RFConf.cc.

28{
29 fclose(m_fd);
30}

Member Function Documentation

◆ getconf()

char * getconf ( const char * key1,
const char * key2 = NULL,
const char * key3 = NULL )

Definition at line 34 of file RFConf.cc.

35{
36 char buf[1024], keybuf[256];
37 char* p, *q;
38
39 struct RFConf_t top, *cur;
40 top.key = NULL;
41
42 // Encode key2 and key3 in the first key if exist
43 // if (! key2) { key2 = key3; key3 = 0; }
44 // if (! key1) { key1 = key2; key2 = key3; key3 = 0; }
45 if (key3)
46 sprintf(keybuf, "%s.%s.%s", key1, key2, key3);
47 else if (key2)
48 sprintf(keybuf, "%s.%s", key1, key2);
49 else if (key1)
50 strcpy(keybuf, key1);
51 else
52 return NULL;
53
54 // Search for the record in the configuration file
55 rewind(m_fd);
56 // cppcheck-suppress knownConditionTrueFalse ; top.key is filled while parsing the file
57 if (! top.key) { /* the first invokation */
58 int line = 0;
59 cur = ⊤
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 const char* keyp = nullptr;
75 // cppcheck-suppress constVariablePointer ; the delimiter is overwritten below
76 char* delp = nullptr;
77 const char* valp = nullptr;
78 for (p = buf; *p && *p != '#'; p++) {
79 if (! keyp) {
80 if (! isspace(*p)) keyp = p;
81 } else if (! delp) {
82 if (isspace(*p)) {
83 if (!isspace(*(p + 1)) && *(p + 1) != ':') {
84 fprintf(stderr, "RFConf : invalid key at line %d\n", line);
85 break;
86 }
87 *p = 0;
88 } else if (*p == ':') {
89 *(delp = p) = 0;
90 }
91 } else if (! valp) {
92 if (! isspace(*p)) {
93 valp = q = p;
94 q++;
95 }
96 } else if (! isspace(*p)) {
97 *q++ = *p;
98 } else if (! isspace(*(p + 1))) {
99 *q++ = ' ';
100 }
101 }
102 if (valp) {
103 for (*q-- = 0; isspace(*q); *q-- = 0)
104 ;
105 }
106 if (delp) {
107 cur->next = static_cast<RFConf_t*>(malloc(sizeof(*cur)));
108 if (cur->next == 0) return NULL;
109 cur = cur->next;
110 cur->next = 0;
111 cur->key = static_cast<char*>(malloc(strlen(keyp) + 1));
112 strcpy(cur->key, keyp);
113 if (valp) {
114 cur->val = static_cast<char*>(malloc(strlen(valp) + 1));
115 strcpy(cur->val, valp);
116 } else {
117 cur->val = 0;
118 }
119 }
120 }
121 } else if (! top.val) { /* the first invokation must have failed */
122 return NULL;
123 }
124
125 int nitem = 0;
126 for (cur = &top; cur; cur = cur->next) {
127 if (cur->key == NULL) continue;
128 if (strcmp(cur->key, keybuf) == 0) return cur->val;
129 nitem++;
130 }
131 printf("RFConf: Key %s not found\n", keybuf);
132 printf("nitem = %d, keybuf = %s\n", nitem, keybuf);
133 return NULL;
134}

◆ getconfi()

int getconfi ( const char * key1,
const char * key2 = NULL,
const char * key3 = NULL )

Definition at line 136 of file RFConf.cc.

137{
138 return atoi(getconf(key1, key2, key3));
139}

Member Data Documentation

◆ m_fd

FILE* m_fd
private

Definition at line 43 of file RFConf.h.


The documentation for this class was generated from the following files: