version 0.1.14
[fms.git] / include / stringfunctions.h
1 #ifndef _string_functions_\r
2 #define _string_functions_\r
3 \r
4 #include <string>\r
5 #include <vector>\r
6 #include <sstream>\r
7 \r
8 namespace StringFunctions\r
9 {\r
10 \r
11 inline const bool Convert(const std::string &input, std::string &output)\r
12 {\r
13         output=input;\r
14         return true;\r
15 }\r
16 \r
17 /**\r
18         \brief Converts a string into any other type\r
19         \param input[in] string to convert\r
20         \param output[out] output type\r
21         \return true if conversion was successful, false if it was not\r
22 */\r
23 template <class T>\r
24 inline const bool Convert(const std::string &input, T &output)\r
25 {\r
26         std::istringstream i(input);\r
27         if(i >> output)\r
28         {\r
29                 return true;\r
30         }\r
31         else\r
32         {\r
33                 return false;\r
34         }\r
35 }\r
36 \r
37 /**\r
38         \brief Converter from any type into a string\r
39         \param input[in] data to convert\r
40         \param output[out] string output\r
41         \return true if conversion was successful, false if it was not\r
42 */\r
43 template <class T>\r
44 inline const bool Convert(const T &input, std::string &output)\r
45 {\r
46         std::ostringstream o;\r
47         o << input;\r
48         output=o.str();\r
49         return true;    \r
50 }\r
51 \r
52 /**\r
53         \brief Replaces occurences of a string with another string\r
54 \r
55         \param input string to search in\r
56         \param find string to find in input string\r
57         \param replace string to replace find string with\r
58 */\r
59 std::string Replace(const std::string &input, const std::string &find, const std::string &replace);\r
60 \r
61 /**\r
62         \brief Splits a string into pieces separated by a delimeter\r
63         \r
64         If the delimiter is not found within the string, the output vector will contain 1 element with the entire input string\r
65         \param str string to split\r
66         \param delim delimeter to split at\r
67         \param[out] vector containing parts\r
68 */\r
69 void Split(const std::string &str, const std::string &delim, std::vector<std::string> &output);\r
70 \r
71 /**\r
72         \brief Splits a string into pieces separated by one or more delimeters\r
73         \r
74         The delimiter string contains individual character delimieters\r
75         \param str string to split\r
76         \param delim string containing individual characters to split at\r
77         \param[out] vector containing parts\r
78 */\r
79 void SplitMultiple(const std::string &str, const std::string &delim, std::vector<std::string> &output);\r
80 \r
81 /**\r
82         \brief Trims whitespace from beginning and end of string\r
83 */\r
84 std::string TrimWhitespace(const std::string &str);\r
85 \r
86 /**\r
87         \brief Converts a string to upper case\r
88         \param str string to convert to upper case\r
89         \param[out] string converted to upper case\r
90 */\r
91 void UpperCase(const std::string &str, std::string &output);\r
92 \r
93 /**\r
94         \brief Converts a string to lower case\r
95         \param str string to convert to lower case\r
96         \param[out] string converted to lower case\r
97 */\r
98 void LowerCase(const std::string &str, std::string &output);\r
99 \r
100 /**\r
101         \brief Decodes a URI encoded string\r
102         \param aSrc string that is URI encoded\r
103         \return URI decoded input string\r
104 */\r
105 std::string UriDecode(const std::string & sSrc);\r
106 \r
107 /**\r
108         \brief URI Encodes a string\r
109         \param aSrc string to encode\r
110         \return input string URI encoded\r
111 */\r
112 std::string UriEncode(const std::string & sSrc);\r
113 \r
114 };\r
115 \r
116 #endif  // _string_functions_\r