00001 /* 00002 * The information in this file is 00003 * Copyright(c) 2007 Ball Aerospace & Technologies Corporation 00004 * and is subject to the terms and conditions of the 00005 * GNU Lesser General Public License Version 2.1 00006 * The license text is available from 00007 * http://www.gnu.org/licenses/lgpl.html 00008 */ 00009 00010 #ifndef STRINGUTILITIES_H 00011 #define STRINGUTILITIES_H 00012 00013 #include "LocationType.h" 00014 00015 #include <stdio.h> 00016 #include <string> 00017 #include <vector> 00018 00019 /** 00020 * This namespace contains helper functions for converting types to and from strings along with other 00021 * convenience string functions. 00022 */ 00023 namespace StringUtilities 00024 { 00025 // constants for the readSTLString class 00026 const bool STL_APPEND = true; 00027 const bool STL_OVERWRITE = false; 00028 00029 /** 00030 * Reads readSize number of characters from pInputFile and places them in 00031 * string target. 00032 * 00033 * @param pInputFile 00034 * The file from which the string is read. 00035 * @param readSize 00036 * The number of bytes to read 00037 * @param target 00038 * Where to put the bytes that are read in. 00039 * @param append 00040 * Whether to append to the existing string, or to overwrite it. 00041 * 00042 * @return Returns false if the file does not exist, or if there is a problem 00043 * reading the requested bytes from the file. Otherwise, returns true. 00044 */ 00045 bool readSTLString(FILE* pInputFile, size_t readSize, std::string &target, bool append = STL_OVERWRITE); 00046 00047 /** 00048 * Returns a copy of the string source, with all whitespace stripped from 00049 * the left side. 00050 * 00051 * @param source 00052 * The string from which the whitespace will be stripped. 00053 * @return Returns a copy of the string with whitespace removed from the left side. 00054 */ 00055 std::string stripLeftWhitespace(const std::string& source); 00056 00057 /** 00058 * Returns a copy of the string source, with all whitespace stripped from 00059 * the right side. 00060 * 00061 * @param source 00062 * The string from which the whitespace will be stripped. 00063 * @return Returns a copy of the string with whitespace removed from the right side. 00064 */ 00065 std::string stripRightWhitespace(const std::string& source); 00066 00067 /** 00068 * Returns a copy of the string source, with all whitespace stripped from 00069 * both sides. 00070 * 00071 * @param source 00072 * The string from which the whitespace will be stripped. 00073 * @return Returns a copy of the string with whitespace removed from both sides. 00074 */ 00075 std::string stripWhitespace(const std::string& source); 00076 00077 /** 00078 * Returns true if the string consists entirely of space characters. 00079 * 00080 * @param source 00081 * The string to test 00082 * @return True if the string contains only space characters or is empty, 00083 * false otherwise. 00084 */ 00085 bool isAllBlank(const std::string &source); 00086 00087 /** 00088 * Converts all uppercase letters to lowercase; 00089 * 00090 * @param source 00091 * The source string for the conversion process. 00092 * @return Returns a copy of the string with all uppercase letters converted to lowercase. 00093 */ 00094 std::string toLower(const std::string& source); 00095 00096 /** 00097 * Converts all lowercase letters to uppercase; 00098 * 00099 * @param source 00100 * The source string for the conversion process. 00101 * @return Returns a copy of the string with all lowercase letters converted to uppercase. 00102 */ 00103 std::string toUpper(const std::string& source); 00104 00105 /** 00106 * Breaks up a string into a set of substrings. 00107 * 00108 * This method breaks up a delimited string into its component substrings. 00109 * @code 00110 * string text("abc/123/def/456"); 00111 * vector<string> components = split(text, '/'); 00112 * // components has 4 strings: "abc", "123", "def", "456" 00113 * @endcode 00114 * 00115 * @param source 00116 * The string to be split up. 00117 * 00118 * @param separator 00119 * This character will be used as a delimiter to break the source 00120 * string up into substrings. 00121 * 00122 * @return Returns a vector containing the component strings. 00123 */ 00124 std::vector<std::string> split(const std::string &source, char separator); 00125 00126 /** 00127 * Joins a vector of strings into a single string. 00128 * 00129 * @param source 00130 * The strings to join. 00131 * 00132 * @param separator 00133 * The string to insert between each source string. 00134 * 00135 * @return The joined string. 00136 */ 00137 std::string join(const std::vector<std::string> &source, const std::string &separator); 00138 00139 /** 00140 * Converts a LocationType whose values are in D.ddddd Lat/Lon format to a string. 00141 * 00142 * @param latLonCoords 00143 * The source LocationType to be converted 00144 * @return Returns a string displaying the Latitude/Longitude represented by the LocationType passed in. 00145 */ 00146 std::string latLonToText(LocationType latLonCoords); 00147 00148 /** 00149 * Expand the variables in this string and return the expanded result. 00150 * 00151 * The syntax for referencing variables is $V(varname), $E(varname) or $C(varname). 00152 * The $V(varname) syntax only recognizes the following variable names: 00153 * APP_HOME, APP_VERSION, or USER_DOCS. These can be expanded to the location where this application 00154 * was installed, the version of the application that is running, or the location of the user's documents folder. 00155 * For example, $V(APP_HOME) could expand to "C:\Program Files\Application\4.0.0", 00156 * $V(APP_VERSION) could expand to "4.0.0", and $V(USER_DOCS) could expand to "C:\Documents and Settings\user". 00157 * The $E(varname) syntax looks for an environment variable defined with the same name and substitutes the value. 00158 * If the environment variable cannot be found, the $E(varname) is left in the string. 00159 * The $C(varname) syntax looks for a setting in ConfigurationSettings with the same 00160 * name and if found substitute's in the value. If the setting cannot be found, the 00161 * $C(varname) is left in the string. If you need to include a $ in the string, please 00162 * use $$ to escape the $. 00163 * 00164 * @param originalString 00165 * The string that should be expanded. 00166 * @param ignoredExpansions 00167 * The list of variable expansions that should be ignored. For example, 00168 * providing a vector with 'E' and 'C' in it, would cause $E(varname) and 00169 * $C(varname) to be left in the string as is (i.e. no expansion would occur). 00170 * 00171 * @return the expanded string. 00172 */ 00173 std::string expandVariables(const std::string& originalString, 00174 const std::vector<std::string>& ignoredExpansions = std::vector<std::string>()); 00175 00176 /** 00177 * Convert the given value into a string. The 00178 * resulting string will be in a XML-friendly representation. In 00179 * addition, the string representation of the value returned by this 00180 * method should be subject to less change than the value returned from 00181 * toDisplayString. 00182 * 00183 * @param value 00184 * The value to convert into a string. For a list of types 00185 * supported, see \ref stringutilities_types. 00186 * 00187 * @param pError 00188 * If this value is non-NULL, the bool will be set to true 00189 * if there was an error while converting the value into 00190 * a string, and false if the conversion was successful. 00191 * 00192 * @return Returns the string representation of the given value. If there 00193 * was an error, an empty string will be returned. 00194 */ 00195 template<typename T> 00196 std::string toXmlString(const T& value, bool* pError = NULL); 00197 00198 /** 00199 * Parse the given string into the given type. This method 00200 * should only be used to parse string values that were returned 00201 * from toXmlString. For a list of types supported see 00202 * \ref stringutilities_types. 00203 * 00204 * @param value 00205 * The string to parse. 00206 * 00207 * @param pError 00208 * If this value is non-NULL, the bool will be set to 00209 * true if there was an error while parsing the string, 00210 * and false if the parse was successful. 00211 * 00212 * @return Returns the parsed value of the given type. If there 00213 * was an error, a default constructed value of the 00214 * given type will be returned. If the type is a pointer 00215 * then NULL will be returned. 00216 */ 00217 template<typename T> 00218 T fromXmlString(std::string value, bool* pError = NULL); 00219 00220 /** 00221 * Convert the given value into a string. The 00222 * resulting string will be in a user-friendly representation. 00223 * The resulting string is intended to be used when the value 00224 * needs to be converted into a string in order to display to a 00225 * end-user. The resulting string should not be expected to 00226 * remain the same between different versions of the application. 00227 * 00228 * @param value 00229 * The value to convert into a string. For a list of types 00230 * supported, see \ref stringutilities_types. 00231 * 00232 * @param pError 00233 * If this value is non-NULL, the bool will be set to true 00234 * if there was an error while converting the value into 00235 * a string, and false if the conversion was successful. 00236 * 00237 * @return Returns the string representation of the given value. If there 00238 * was an error, an empty string will be returned. 00239 */ 00240 template<typename T> 00241 std::string toDisplayString(const T& value, bool* pError = NULL); 00242 00243 /** 00244 * Parse the given string into the given type. This method 00245 * should only be used to parse string values that were returned 00246 * from toDisplayString. For a list of types supported see 00247 * \ref stringutilities_types. 00248 * 00249 * @param value 00250 * The string to parse. 00251 * 00252 * @param pError 00253 * If this value is non-NULL, the bool will be set to 00254 * true if there was an error while parsing the string, 00255 * and false if the parse was successful. 00256 * 00257 * @return Returns the parsed value of the given type. If there 00258 * was an error, a default constructed value of the 00259 * given type will be returned. If the type is a pointer 00260 * then NULL will be returned. 00261 */ 00262 template<typename T> 00263 T fromDisplayString(std::string value, bool* pError = NULL); 00264 00265 /** 00266 * Provides a list of all enum values for the given enum provided 00267 * to the template. This method is only supported by the enum types 00268 * listed in \ref stringutilities_types. 00269 * 00270 * @return Returns a list all enum values for the given enum type. 00271 * 00272 * @see getAllEnumValuesAsDisplayString(), getAllEnumValuesAsXmlString() 00273 */ 00274 template<typename T> 00275 std::vector<T> getAllEnumValues(); 00276 00277 /** 00278 * Provides a list of display strings for all of the enum values for 00279 * the given enum type. This method is only supported by the enum types 00280 * listed in \ref stringutilities_types. 00281 * 00282 * @return Returns the result of calling toDisplayString() on every value 00283 * in the vector that would be returned by getAllEnumValues(). The 00284 * ordering of the vectors returned by this method and getAllEnumValues() 00285 * is guaranteed to be the same. 00286 * 00287 * @see getAllEnumValues(), getAllEnumValuesAsXmlString() 00288 */ 00289 template<typename T> 00290 std::vector<std::string> getAllEnumValuesAsDisplayString(); 00291 00292 /** 00293 * Provides a list of xml strings for all of the enum values for 00294 * the given enum type. This method is only supported by the enum types 00295 * listed in \ref stringutilities_types. 00296 * 00297 * @return Returns the result of calling toXmlString() on every value 00298 * in the vector that would be returned by getAllEnumValues(). The 00299 * ordering of the vectors returned by this method and getAllEnumValues() 00300 * is guaranteed to be the same. 00301 * 00302 * @see getAllEnumValues(), getAllEnumValuesAsDisplayString() 00303 */ 00304 template<typename T> 00305 std::vector<std::string> getAllEnumValuesAsXmlString(); 00306 00307 /** 00308 * Tokenizes string on spaces, with escape support. 00309 * 00310 * This function will start tokenizing a string provided by textIn, 00311 * or continue tokenizing if the default parameter is used. Whitespace 00312 * to the left and right of the token is removed. Characters 00313 * may be escaped with the '\\' character. The following escape codes 00314 * are understood: 00315 * 00316 * - '\\' -- gives a single backslash. 00317 * - ' ' -- gives a single space character. 00318 * 00319 * This function is not thread safe. 00320 * 00321 * @param textIn 00322 * Input string, of which a local copy is made. Tokenizing starts 00323 * from the beginning of the string, or where left off if the default 00324 * parameter is used. 00325 * 00326 * @return Returns the first token, with escaped text replaced as appropriate. 00327 * This method owns the memory returned. 00328 */ 00329 const char* escapedToken(const std::string &textIn = std::string()); 00330 } 00331 00332 #endif