CGEM BOSS 6.6.5.i
BESIII Offline Software System
Loading...
Searching...
No Matches
Event/RootCnvSvc/RootCnvSvc-04-01-18/RootCnvSvc/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
8/** @file Util.h
9 @author J. Bogart
10
11 This file declares the class Util for basic static utilities
12 and an associated exception class.
13
14*/
15namespace facilities {
16 /// This class provides a home for utility functions with no need for
17 /// any context (hence static)
18
19 /// Exception class used by expandEnvVar
21 public:
22 Untranslatable(const std::string& toTrans) : m_badVar(toTrans) {}
23 std::string m_badVar;
24 };
25
26 /// Exception class used when converting from string to numeric type
27
28 class WrongType {
29 public:
30 WrongType(const std::string& toConvert, const std::string& typeName) :
31 m_toConvert(toConvert), m_typeName(typeName) {}
32 std::string getMsg() {
33 std::string msg =
34 "facilities::WrongType. Cannot convert '" + m_toConvert + "' to type "
35 + m_typeName;
36 return msg;
37 }
38 private:
39 std::string m_toConvert;
40 std::string m_typeName;
41 };
42
43 class Util {
44 public:
45 /** Given input string @a toExpand expand references to environment
46 variables, by default of the form $(varname) and put the
47 expanded version back into the original string. Alternate
48 delimiters for the @a varname may optionally be specified
49 @param toExpand string for which expansion is to be done
50 @param openDel opening delimiter (defaults to "$(")
51 @param closeDel closing delimiter (defaults to ")")
52
53 @return -1 if attempt at expansion failed at least once,
54 else number of successful expansions.
55
56 TODO: Perhaps add optional arguments to specify alternate
57 delimiters.
58 */
59 static int expandEnvVar(std::string* toExpand,
60 const std::string& openDel = std::string("$("),
61 const std::string& closeDel = std::string(")"));
62
63 /** Given input string @a toCatch catch val from string opt, by default of
64 the form #(opt) and remove "#(opt)" from the original string.
65 @param toCatch string for which expansion is to be done
66 @param openDel opening delimiter (defaults to "#(")
67 @param closeDel closing delimiter (defaults to ")")
68 @return -1 if attempt at expansion failed,
69 else atoi(opt).
70 TODO: Perhaps add optional arguments to specify alternate delimiters.
71 */
72 static int catchOptionVal(std::string* toCatch,
73 const int ops = 0,
74 const std::string& openDel = std::string("#("),
75 const std::string& closeDel = std::string(")"));
76
77 /** Given an input integer @a val to convert and an output string @a outStr
78 converts val into a std::string.
79 This method duplicates the stdlib.h method itoa, except that it returns
80 std::string rather than char*.
81 @param val
82 @param outStr will be modified by this method
83
84 @return const char* based on the contents of outStr.c_str()
85 */
86 static const char* itoa(int val, std::string &outStr);
87
88 /// converts an std::string to an integer
89 static int atoi(const std::string& InStr);
90
91
92 /// converts a std::string to a double. If string contents are not
93 /// of proper form, throws facilities::WrongType
94 static double stringToDouble(const std::string& InStr);
95
96 /// converts a std::string to an int. If string contents are not
97 /// of proper form, throws facilities::WrongType
98 static int stringToInt(const std::string& InStr);
99
100
101
102 /** This routine breaksdown a string into tokens, based on the
103 characters appearing in the string @a delimiters.
104 @param input string to be tokenized
105 @param delimiters string containing one or more delimiter characters
106 @param tokens vector of strings to hold resulting tokens
107 @param clear if true (default) @a tokens will be cleared
108 at the start of processing
109 */
110 static void stringTokenize(std::string input, const std::string &delimiters,
111 std::vector<std::string> &tokens,
112 bool clear = true);
113
114 /** return the "non-directory" part of a (supposed) file identifier, @a path.
115 Environment variable translation should be done before calling @a basename.
116 @sa { Util::expandEnvVar }
117 @param path string assumed to be a file identifier.
118 */
119 static std::string basename(const std::string &path);
120 };
121}
122
123#endif
static int stringToInt(const std::string &InStr)
static const char * itoa(int val, std::string &outStr)
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 catchOptionVal(std::string *toCatch, const int ops=0, const std::string &openDel=std::string("#("), const std::string &closeDel=std::string(")"))
static int atoi(const std::string &InStr)
converts an std::string to an integer
static double stringToDouble(const std::string &InStr)
static std::string basename(const std::string &path)
Exception class used when converting from string to numeric type.
WrongType(const std::string &toConvert, const std::string &typeName)