EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
misc.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file misc.cxx
1 /* -------------------------------------------------------------------------- */
2 /* misc.cc */
3 /* */
4 /* Some non-HERMES specific source code shared between different HTC */
5 /* programs without particular affiliation. */
6 /* */
7 /* A.Kisselev, PNPI, St.Petersburg, Russia. */
8 /* e-mail: kisselev@hermes.desy.de */
9 /* -------------------------------------------------------------------------- */
10 
11 #include <cassert>
12 #include <cstring>
13 #include <cstdlib>
14 
15 #include <htclib.h>
16 
17 // Hmm, how about offset in ASCII table? :-)
18 char XYZ[3] = {'X', 'Y', 'Z'};
19 
20 /* ========================================================================== */
21 
22 int check_prefix(char *str, char *prefix)
23 {
24  int len = strlen(str), plen = strlen(prefix);
25 
26  if (len >= plen && !memcmp(str, prefix, plen)) return 0;
27 
28  return -1;
29 } /* check_prefix */
30 
31 /* -------------------------------------------------------------------------- */
32 
33 int check_and_remove_suffix(char *ptr, char *suffix)
34 {
35  int len = strlen(ptr), slen = strlen(suffix);
36 
37  // Check suffix match;
38  if (len < slen || memcmp(ptr + len - slen, suffix, slen)) return -1;
39 
40  // Cut suffix out;
41  ptr[len-slen] = 0;
42 
43  return 0;
44 } /* check_and_remove_suffix */
45 
46 /* ----------------------------------------------------------------- */
47 
48 int assign_dimensional_value(char *string, char *suffix,
49  double *value, double scale)
50 {
51  if (!string || !suffix || !value) return -1;
52 
53  {
54  // Don't want to modify "string";
55  char *ptr = strdup(string);
56 
57  if (!ptr || check_and_remove_suffix(ptr, suffix)) return -1;
58 
59  *value = scale*atof(ptr);
60 
61  free(ptr);
62  }
63 
64  return 0;
65 } /* assign_dimensional_value */
66 
67 /* ========================================================================== */
68 
70 {
71  char *qstr;
72  int ip, len;
73 
74  if (!str || !array || array->actual_variable_num <= 0 ||
76  return -1;
77 
78  // Well, don't want to modify original string;
79  qstr = strdup(str); if (!qstr) return -1; len = strlen(qstr);
80 
81  // Calculate actual_variable_num, plen, slen once;
82  for(ip=0; ip<array->actual_variable_num; ip++)
83  {
84  t_cmd_line_variable *var = array->variables + ip;
85 
86  // Yes, reset it right here;
87  var->_assigned = 0;
88 
89  // Yes, something must be wrong with definitions;
90  assert(var->prefix && var->addr);
91 
92  var->plen = strlen(var->prefix);
93  } /*if .. for ip*/
94 
95  // Loop through all known tuning keys;
96  for(ip=0; ip<array->actual_variable_num; ip++)
97  {
98  t_cmd_line_variable *var = array->variables + ip;
99 
100  // Check prefix match;
101  if (len >= var->plen &&
102  !memcmp(qstr, var->prefix, var->plen))
103  {
104  char *ptr = qstr + var->plen;
105 
106  // If suffix present, require suffix match;
107  if (var->suffix && check_and_remove_suffix(ptr, var->suffix))
108  {
109  printf("'%s': syntax error (%s)!\n", var->prefix, qstr);
110  return -1;
111  } /*if*/
112 
113  // Check for double counting;
114  if (var->_assigned)
115  {
116  printf("'%s': key double counting (%s)!\n", var->prefix, qstr);
117  return -1;
118  } /*if*/
119  var->_assigned = 1;
120 
121  // Assign key and check limits;
122  *var->addr = atof(ptr);
123  if (*var->addr < var->min || *var->addr > var->max)
124  {
125  printf("'%s': value out of range (%s)!\n", var->prefix, qstr);
126  return -1;
127  } /*if*/
128 
129  return 0;
130  } /*if*/
131  } /*for ip*/
132 
133  // If failed before, does not matter;
134  free(qstr);
135 
136  // Unknown key?;
137  return -1;
138 } /* cmd_line_variable_parser */
139 
140 /* ========================================================================== */