MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
stringtool.h
1 // © Copyright 2010 - 2014 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _stringtool_h
41 #define _stringtool_h
42 
43 #include "Input/metacode.h"
44 #include "vector2.h"
45 #include "vector3.h"
46 #include "quaternion.h"
47 #include "colourvalue.h"
48 #include "countedptr.h"
49 
50 namespace Mezzanine
51 {
52  ///////////////////////////////////////////////////////////////////////////////
53  /// @brief This is a utility to help perform all kinds of string related tasks.
54  /// @details
55  ///////////////////////////////////////
56  namespace StringTools
57  {
58  ///////////////////////////////////////////////////////////////////////////////
59  // Globals, mostly used for comparison and such
60  const String Blank = "";
61 
62  ///////////////////////////////////////////////////////////////////////////////
63  // String Manipulation and checks
64 
65  /// @brief Trims all whitespaces and tabs from a one or both sides of a string.
66  /// @param Source The original string to be trimmed.
67  /// @param Left Whether or not to trim the left side of the string.
68  /// @param Right Whether or not to trim the right side of the string.
69  void MEZZ_LIB Trim(String& Source, bool Left = true, bool Right = true);
70  /// @brief Splits a string into multiple substrings based on the specified delimiters.
71  /// @param Source The string to be split.
72  /// @param Delims The characters to look for and use as split points in the source string.
73  /// @param MaxSplits The maximum number of splits to perform on this string. Value of zero means unlimited splits.
74  /// @return Returns a vector containing all the substrings generated from the source string.
75  CountedPtr<StringVector> MEZZ_LIB Split(const String& Source, const String& Delims = " \t\n", const Whole& MaxSplits = 0);
76  /// @brief Converts all lower case characters in a string to their respective upper case.
77  /// @param Source The string to be converted.
78  void MEZZ_LIB ToUpperCase(String& Source);
79  /// @brief Converts all upper case characters in a string to their respective lower case.
80  /// @param Source The string to be converted.
81  void MEZZ_LIB ToLowerCase(String& Source);
82  /// @brief Checks a string to see if it starts with a specific pattern.
83  /// @param Str The string to check.
84  /// @param Pattern The sequence to check for at the start of the string.
85  /// @param CaseSensitive If false this function will check lower-case copies for the pattern, otherwise the strings will be checked as is.
86  /// @return Returns true if the string starts with the provided pattern, false otherwise.
87  bool MEZZ_LIB StartsWith(const String& Str, const String& Pattern, const bool CaseSensitive);
88  /// @brief Checks a string to see if it ends with a specific pattern.
89  /// @param Str The string to check.
90  /// @param Pattern The sequence to check for at the end of the string.
91  /// @param CaseSensitive If false this function will check lower-case copies for the pattern, otherwise the strings will be checked as is.
92  /// @return Returns true if the string ends with the provided pattern, false otherwise.
93  bool MEZZ_LIB EndsWith(const String& Str, const String& Pattern, const bool CaseSensitive);
94  /// @brief Replaces all instances of multiple consecutive whitespaces with only a single whitespace.
95  /// @param Source The string to be altered.
97 
98  ///////////////////////////////////////////////////////////////////////////////
99  // Data Class Utilities
100 
101  /// @brief Convert two numbers in a string into a Vector2.
102  /// @param ToConvert The string to be converted.
103  /// @remarks The string is expected to have a certain format. The format should be "X Y". If there are not 2 numbers an exception will be thrown.
104  /// @return Returns a Vector2 populated with the values from the string passed in.
105  Vector2 MEZZ_LIB ConvertToVector2(const String& ToConvert);
106  /// @brief Converts a Vector2 into a string.
107  /// @param ToConvert The Vector2 to be converted.
108  /// @return Returns a string containing the values from the Vector2 in "X Y" format.
109  String MEZZ_LIB ConvertToString(const Vector2& ToConvert);
110  /// @brief Convert three numbers in a string into a Vector3.
111  /// @param ToConvert The string to be converted.
112  /// @remarks The string is expected to have a certain format. The format should be "X Y Z". If there are not 3 numbers an exception will be thrown.
113  /// @return Returns a Vector3 populated with the values from the string passed in.
114  Vector3 MEZZ_LIB ConvertToVector3(const String& ToConvert);
115  /// @brief Converts a Vector3 into a string.
116  /// @param ToConvert The Vector3 to be converted.
117  /// @return Returns a string containing the values from the Vector3 in "X Y Z" format.
118  String MEZZ_LIB ConvertToString(const Vector3& ToConvert);
119  /// @brief Convert four numbers in a string into a Quaternion.
120  /// @param ToConvert The string to be converted.
121  /// @remarks The string is expected to have a certain format. The format should be "X Y Z W". If there are not 4 numbers an exception will be thrown.
122  /// @return Returns a Quaternion populated with the values from the string passed in.
123  Quaternion MEZZ_LIB ConvertToQuaternion(const String& ToConvert);
124  /// @brief Converts a Quaternion into a string.
125  /// @param ToConvert The Quaternion to be converted.
126  /// @return Returns a string containing the values from the Quaternion in "X Y Z W" format.
127  String MEZZ_LIB ConvertToString(const Quaternion& ToConvert);
128  /// @brief Convert four numbers in a string into a ColourValue.
129  /// @param ToConvert The string to be converted.
130  /// @remarks The string is expected to have a certain format. The format should be "R G B A". If there are not 4 numbers an exception will be thrown.
131  /// @return Returns a ColourValue populated with the values from the string passed in.
133  /// @brief Converts a ColourValue into a string.
134  /// @param ToConvert The ColourValue to be converted.
135  /// @return Returns a string containing the values from the ColourValue in "R G B A" format.
136  String MEZZ_LIB ConvertToString(const ColourValue& ToConvert);
137 
138  /// @brief Converts a Hex code in a string into a ColourValue.
139  /// @param ToConvert The string to be converted.
140  /// @return Returns a ColourValue populated with the values from the string passed in.
142  /// @brief Converts a ColourValue into a string as a Hex code.
143  /// @param ToConvert The ColourValue to be converted.
144  /// @return Returns a string containing the values from the string passed in.
145  String MEZZ_LIB ConvertToHexString(const ColourValue& ToConvert);
146 
147  ///////////////////////////////////////////////////////////////////////////////
148  // Convert-To-Data functions
149 
150  /// @brief Converts a string into a bool.
151  /// @param ToConvert The string to be converted to a bool.
152  /// @return Returns a bool with the converted value.
153  bool MEZZ_LIB ConvertToBool(const String& ToConvert, const bool Default = false);
154  /// @brief Converts a string into a Real.
155  /// @param ToConvert The string to be converted to a Real.
156  /// @return Returns a Real with the converted value.
157  Real MEZZ_LIB ConvertToReal(const String& ToConvert);
158  /// @brief Converts a string into an Integer.
159  /// @param ToConvert The string to be converted to an Integer.
160  /// @return Returns an Integer with the converted value.
161  Integer MEZZ_LIB ConvertToInteger(const String& ToConvert);
162  /// @brief Converts a string into an Int8.
163  /// @param ToConvert The string to be converted to an Int8.
164  /// @return Returns an Int8 with the converted value.
165  Int8 MEZZ_LIB ConvertToInt8(const String& ToConvert);
166  /// @brief Converts a string into a UInt8.
167  /// @param ToConvert The string to be converted to a UInt8.
168  /// @return Returns a UInt8 with the converted value.
169  UInt8 MEZZ_LIB ConvertToUInt8(const String& ToConvert);
170  /// @brief Converts a string into an Int16.
171  /// @param ToConvert The string to be converted to an Int16.
172  /// @return Returns an Int16 with the converted value.
173  Int16 MEZZ_LIB ConvertToInt16(const String& ToConvert);
174  /// @brief Converts a string into a UInt16.
175  /// @param ToConvert The string to be converted to a UInt16.
176  /// @return Returns a UInt16 with the converted value.
177  UInt16 MEZZ_LIB ConvertToUInt16(const String& ToConvert);
178  /// @brief Converts an string into an Int32.
179  /// @param ToConvert The string to be converted to an Int32.
180  /// @return Returns an Int32 with the converted value.
181  Int32 MEZZ_LIB ConvertToInt32(const String& ToConvert);
182  /// @brief Converts a string into a UInt32.
183  /// @param ToConvert The string to be converted to a UInt32.
184  /// @return Returns a UInt32 with the converted value.
185  UInt32 MEZZ_LIB ConvertToUInt32(const String& ToConvert);
186 
187  ///////////////////////////////////////////////////////////////////////////////
188  // Convert-To-String functions
189 
190  /// @brief Converts any into a string.
191  /// @param ToConvert Stream class instance to be converted.
192  /// @return Returns a string containing the lexicagraphically converted data.
193  template<typename T>
194  String ConvertToString(const T& ToConvert)
195  {
196  StringStream converter;
197  converter << ToConvert;
198  return converter.str();
199  }
200 
201  // @brief Converts a bool into a string.
202  // @param ToConvert The bool to be converted.
203  // @return Returns "true" if true, or "false" if false.
204  //template<>
205  //String MEZZ_LIB ConvertToString<bool>(const bool& ToConvert)
206  //{
207  // if(ToConvert) return "true";
208  // else return "false";
209  //}
210 
211  /// @brief Converts a Input::InputCode into a string.
212  /// @param Code The input code to be converted.
213  /// @param ShiftPressed Whether or not the shift modifier key has been pressed.
214  /// @return Returns a string(usually with only one character) containing the converted input code.
215  String ConvertToString(const Input::InputCode& Code, bool ShiftPressed);
216 
217  /// @brief Converts a Input::InputCode into a string, assuming shift was not pressed.
218  /// @details this is good for figuring out what key was pressed
219  /// @param Code The input code to be converted.
220  /// @return Returns a string(usually with only one character) containing the converted input code.
221  //template<>
222  //String MEZZ_LIB ConvertToString<Input::InputCode>(const Input::InputCode& Code)
223  // { return ConvertToString(Code, false); }
224 
225  } //stringtool
226 }//Mezzanine
227 
228 #endif