StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StGetConfigValue.hh
1 /***************************************************************************
2  *
3  * $Id: StGetConfigValue.hh,v 1.3 2003/09/02 17:59:34 perev Exp $
4  *
5  * Author: Thomas Ullrich, Nov 4, 1997
6  ***************************************************************************
7  *
8  * Description:
9  * Returns resource value read from a configuration file.
10  * There are two version of StGetConfigValue(). One for
11  * reading a scalar resource and one for a multi-value
12  * resources (e.g arrays).
13  *
14  * Example:
15  * EtInt foo = 10;
16  * StGetConfigValue("my.conf", "foo", foo);
17  * // foo == 10 in case foo is not defined in my.conf.
18  *
19  * The second version has different arguments.
20  *
21  * Example:
22  * EtInt afoo[3];
23  * StGetConfigValue("my.conf", "afoo", afoo, 3);
24  *
25  * Syntax of the resource file:
26  * The resource (configuration) file has to be in ascii format.
27  * Each resource line must be defined as:
28  * resource: value
29  * as in:
30  * foo: 17
31  * afoo: 0.1 0.2 0.3
32  *
33  * The following comment characters are supported: '//' and '#'
34  * Example:
35  * // foo: 10
36  * # afoo: 10 20 30
37  * foo: 20 # was 10
38  *
39  * Note that no warning is submitted in case the file doesn't exist
40  * or isn't readable.
41  ***************************************************************************
42  *
43  * $Log: StGetConfigValue.hh,v $
44  * Revision 1.3 2003/09/02 17:59:34 perev
45  * gcc 3.2 updates + WarnOff
46  *
47  * Revision 1.2 1999/12/21 15:14:00 ullrich
48  * Modified to cope with new compiler version on Sun (CC5.0).
49  *
50  * Revision 1.1 1999/01/30 03:59:01 fisyak
51  * Root Version of StarClassLibrary
52  *
53  * Revision 1.1 1999/01/23 00:27:47 ullrich
54  * Initial Revision
55  *
56  **************************************************************************/
57 #ifndef ST_GET_CONFIG_VALUE_HH
58 #define ST_GET_CONFIG_VALUE_HH
59 
60 #include "Stiostream.h"
61 #include <Stsstream.h>
62 #include <string>
63 #include "StGlobals.hh"
64 #if !defined(ST_NO_NAMESPACES)
65 using std::string;
66 #endif
67 
68 template<class T>
69 void StGetConfigValue(const char* filename, const char* name, T& value)
70 {
71  ifstream ifs(filename);
72  if (!ifs) return;
73 
74  string line;
75  StSizeType pos;
76  while (ifs.good() && !ifs.eof()) {
77 #if defined(__SUNPRO_CC)
78  char c; line.erase();
79  while ((c = ifs.get()) && c != '\n' && !ifs.eof()) line += c;
80 #else
81  getline(ifs,line,'\n');
82 #endif
83  if ((pos = line.find('#')) != StNPOS) // remove text after '#'
84  line.erase(pos,line.length());
85  if ((pos = line.find("//")) != StNPOS) // remove text after '//'
86  line.erase(pos,line.length());
87  if ((pos = line.find(name)) != StNPOS) { // search for resource name
88  if ((pos = line.find(':')) != StNPOS) { // search for separator
89  line.erase(0,pos+1);
90  istrstream ist(line.c_str());
91  ist >> value; // type conversion
92  return; // found and assign resource value
93  }
94  }
95  }
96  return;
97 }
98 
99 
100 template<class T>
101 void StGetConfigValue(const char* filename, const char* name, T& value, int nitems)
102 {
103  ifstream ifs(filename);
104  if (!ifs) return;
105 
106  string line;
107  StSizeType pos;
108  while (ifs.good() && !ifs.eof()) {
109 #if defined(__SUNPRO_CC)
110  char c; line.erase();
111  while ((c = ifs.get()) && c != '\n' && !ifs.eof()) line += c;
112 #else
113  getline(ifs,line,'\n');
114 #endif
115  if ((pos = line.find('#')) != StNPOS) // remove text after '#'
116  line.erase(pos,line.length());
117  if ((pos = line.find("//")) != StNPOS) // remove text after '//'
118  line.erase(pos,line.length());
119  if ((pos = line.find(name)) != StNPOS) { // search for resource name
120  if ((pos = line.find(':')) != StNPOS) { // search for separator
121  line.erase(0,pos+1);
122  istrstream ist(line.c_str());
123  for (int i=0; i<nitems; i++)
124  ist >> value[i]; // type conversion
125  }
126  }
127  }
128 }
129 
130 #endif