BOSS 7.1.1
BESIII Offline Software System
Loading...
Searching...
No Matches
Event/RootCnvSvc/RootCnvSvc-03-01-03/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
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 input string @a toCatch catch val from string opt, by default of
65 the form #(opt) and remove "#(opt)" from the original string.
66 @param toCatch string for which expansion is to be done
67 @param openDel opening delimiter (defaults to "#(")
68 @param closeDel closing delimiter (defaults to ")")
69 @return -1 if attempt at expansion failed,
70 else atoi(opt).
71 TODO: Perhaps add optional arguments to specify alternate delimiters.
72 */
73 static int catchOptionVal(std::string* toCatch,
74 const int ops = 0,
75 const std::string& openDel = std::string("#("),
76 const std::string& closeDel = std::string(")"));
77
78 /** Given an input integer @a val to convert and an output string @a outStr
79 converts val into a std::string.
80 This method duplicates the stdlib.h method itoa, except that it returns
81 std::string rather than char*.
82 @param val
83 @param outStr will be modified by this method
84
85 @return const char* based on the contents of outStr.c_str()
86 */
87 static const char* itoa(int val, std::string &outStr);
88
89 /// converts an std::string to an integer
90 static int atoi(const std::string& InStr);
91
92
93 /// converts a std::string to a double. If string contents are not
94 /// of proper form, throws facilities::WrongType
95 static double stringToDouble(const std::string& InStr);
96
97 /// converts a std::string to an int. If string contents are not
98 /// of proper form, throws facilities::WrongType
99 static int stringToInt(const std::string& InStr);
100
101
102
103 /** This routine breaksdown a string into tokens, based on the
104 characters appearing in the string @a delimiters.
105 @param input string to be tokenized
106 @param delimiters string containing one or more delimiter characters
107 @param tokens vector of strings to hold resulting tokens
108 @param clear if true (default) @a tokens will be cleared
109 at the start of processing
110 */
111 static void stringTokenize(std::string input, const std::string &delimiters,
112 std::vector<std::string> &tokens,
113 bool clear = true);
114
115 /** return the "non-directory" part of a (supposed) file identifier, @a path.
116 Environment variable translation should be done before calling @a basename.
117 @sa { Util::expandEnvVar }
118 @param path string assumed to be a file identifier.
119 */
120 static std::string basename(const std::string &path);
121 };
122}
123
124#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)