BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
Calibration/facilities/include/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 <map>
6#include <string>
7#include <vector>
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 " + m_typeName;
36 return msg;
37 }
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 /// converts a std::string to a double. If string contents are not
79 /// of proper form, throws facilities::WrongType
80 static double stringToDouble( const std::string& InStr );
81
82 /// converts a std::string to an int. If string contents are not
83 /// of proper form, throws facilities::WrongType
84 static int stringToInt( const std::string& InStr );
85
86 /** This routine breaks down a string into tokens, based on the
87 characters appearing in the string @a delimiters.
88 @param input string to be tokenized
89 @param delimiters string containing one or more delimiter characters
90 @param tokens vector of strings to hold resulting tokens
91 @param clear if true (default) @a tokens will be cleared
92 at the start of processing
93 */
94 static void stringTokenize( std::string input, const std::string& delimiters,
95 std::vector<std::string>& tokens, bool clear = true );
96
97 /** This routine breaks down a string into key/value token pairs and
98 stores them in the user-supplied map. , based on the
99 characters appearing in the string @a delimiters and the value
100 of @a pairDelimiter. In a typical example, @a input could be
101 "key1=val1,key2=val2,key3=val3". In this case invoke with
102 delimiters=std::string(",") and pairDelimiter=std::string("=")
103 (or omit pairDelimiter since it has the default value)
104 @param input string to be tokenized
105 @param delimiters string containing one or more delimiter
106 characters
107 @param tokenMap map of strings to hold resulting tokens
108 @param pairDelimiter string separating key and value; defaults
109 to "="
110 @param clear if true (default) @a tokens will be cleared
111 at the start of processing
112 */
113 static void keyValueTokenize( std::string input, const std::string& delimiters,
114 std::map<std::string, std::string>& tokenMap,
115 const std::string& pairDelimiter = std::string( "=" ),
116 bool clear = true );
117
118 /** return the "non-directory" part of a (supposed) file identifier,
119 @a path. Environment variable translation should be done before
120 calling @a basename.
121 @sa { Util::expandEnvVar }
122 @param path string assumed to be a file identifier.
123 */
124 static std::string basename( const std::string& path );
125
126 /**
127 Trim trailing white space characters from the supplied string.
128 White space characters for this purpose are blank, carriage return,
129 line feed and form feed. Return # of characters trimmed.
130 */
131 static unsigned trimTrailing( std::string* toTrim );
132 };
133} // namespace facilities
134
135#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)
WrongType(const std::string &toConvert, const std::string &typeName)