BOSS 7.0.3
BESIII Offline Software System
Loading...
Searching...
No Matches
Calibration/facilities/facilities-00-00-04/facilities/Util.h
Go to the documentation of this file.
1// $ Header:$
2#ifndef FACILITIES_UTIL_H
3#define FACILITIES_UTIL_H
4
5#include <string>
6#include <vector>
7#include <map>
8
9/** @file Util.h
10 @author J. Bogart
11
12 This file declares the class Util for basic static utilities
13 and an associated exception class.
14
15*/
16namespace facilities {
17 /// This class provides a home for utility functions with no need for
18 /// any context (hence static)
19
20 /// Exception class used by expandEnvVar
22 public:
23 Untranslatable(const std::string& toTrans) : m_badVar(toTrans) {}
24 std::string m_badVar;
25 };
26
27 /// Exception class used when converting from string to numeric type
28
29 class WrongType {
30 public:
31 WrongType(const std::string& toConvert, const std::string& typeName) :
32 m_toConvert(toConvert), m_typeName(typeName) {}
33 std::string getMsg() {
34 std::string msg =
35 "facilities::WrongType. Cannot convert '" + m_toConvert + "' to type "
36 + m_typeName;
37 return msg;
38 }
39 private:
40 std::string m_toConvert;
41 std::string m_typeName;
42 };
43
44 class Util {
45 public:
46 /** Given input string @a toExpand expand references to environment
47 variables, by default of the form $(varname) and put the
48 expanded version back into the original string. Alternate
49 delimiters for the @a varname may optionally be specified
50 @param toExpand string for which expansion is to be done
51 @param openDel opening delimiter (defaults to "$(")
52 @param closeDel closing delimiter (defaults to ")")
53
54 @return -1 if attempt at expansion failed at least once,
55 else number of successful expansions.
56
57 TODO: Perhaps add optional arguments to specify alternate
58 delimiters.
59 */
60 static int expandEnvVar(std::string* toExpand,
61 const std::string& openDel = std::string("$("),
62 const std::string& closeDel = std::string(")"));
63
64 /** Given an input integer @a val to convert and an output string @a outStr
65 converts val into a std::string.
66 This method duplicates the stdlib.h method itoa, except that it returns
67 std::string rather than char*.
68 @param val
69 @param outStr will be modified by this method
70
71 @return const char* based on the contents of outStr.c_str()
72 */
73 static const char* itoa(int val, std::string &outStr);
74
75 /// converts an std::string to an integer
76 static int atoi(const std::string& InStr);
77
78
79 /// converts a std::string to a double. If string contents are not
80 /// of proper form, throws facilities::WrongType
81 static double stringToDouble(const std::string& InStr);
82
83 /// converts a std::string to an int. If string contents are not
84 /// of proper form, throws facilities::WrongType
85 static int stringToInt(const std::string& InStr);
86
87
88
89 /** This routine breaks down a string into tokens, based on the
90 characters appearing in the string @a delimiters.
91 @param input string to be tokenized
92 @param delimiters string containing one or more delimiter characters
93 @param tokens vector of strings to hold resulting tokens
94 @param clear if true (default) @a tokens will be cleared
95 at the start of processing
96 */
97 static void stringTokenize(std::string input, const std::string &delimiters,
98 std::vector<std::string> &tokens,
99 bool clear = true);
100
101 /** This routine breaks down a string into key/value token pairs and
102 stores them in the user-supplied map. , based on the
103 characters appearing in the string @a delimiters and the value
104 of @a pairDelimiter. In a typical example, @a input could be
105 "key1=val1,key2=val2,key3=val3". In this case invoke with
106 delimiters=std::string(",") and pairDelimiter=std::string("=")
107 (or omit pairDelimiter since it has the default value)
108 @param input string to be tokenized
109 @param delimiters string containing one or more delimiter
110 characters
111 @param tokenMap map of strings to hold resulting tokens
112 @param pairDelimiter string separating key and value; defaults
113 to "="
114 @param clear if true (default) @a tokens will be cleared
115 at the start of processing
116 */
117 static void keyValueTokenize(std::string input,
118 const std::string &delimiters,
119 std::map<std::string,std::string> &tokenMap,
120 const std::string& pairDelimiter =
121 std::string("="),
122 bool clear = true);
123
124 /** return the "non-directory" part of a (supposed) file identifier,
125 @a path. Environment variable translation should be done before
126 calling @a basename.
127 @sa { Util::expandEnvVar }
128 @param path string assumed to be a file identifier.
129 */
130 static std::string basename(const std::string &path);
131
132 /**
133 Trim trailing white space characters from the supplied string.
134 White space characters for this purpose are blank, carriage return,
135 line feed and form feed. Return # of characters trimmed.
136 */
137 static unsigned trimTrailing(std::string* toTrim);
138 };
139}
140
141#endif
static double stringToDouble(const std::string &InStr)
static int expandEnvVar(std::string *toExpand, const std::string &openDel=std::string("$("), const std::string &closeDel=std::string(")"))
static void stringTokenize(std::string input, const std::string &delimiters, std::vector< std::string > &tokens, bool clear=true)
static int atoi(const std::string &InStr)
converts an std::string to an integer
static std::string basename(const std::string &path)
static int stringToInt(const std::string &InStr)
static void keyValueTokenize(std::string input, const std::string &delimiters, std::map< std::string, std::string > &tokenMap, const std::string &pairDelimiter=std::string("="), bool clear=true)
static const char * itoa(int val, std::string &outStr)
static unsigned trimTrailing(std::string *toTrim)
Exception class used when converting from string to numeric type.
WrongType(const std::string &toConvert, const std::string &typeName)