]> git.mxchange.org Git - simgear.git/blob - simgear/misc/strutils.hxx
Changing SGPath APIs, using SGPath in more places.
[simgear.git] / simgear / misc / strutils.hxx
1 /**
2  * \file strutils.hxx
3  * String utilities.
4  */
5
6 // Written by Bernie Bright, started 1998
7 //
8 // Copyright (C) 1998  Bernie Bright - bbright@bigpond.net.au
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Library General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // Library General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23 //
24 // $Id$
25
26
27 #ifndef STRUTILS_H
28 #define STRUTILS_H
29
30 #include <simgear/compiler.h>
31
32 #include <string>
33 #include <vector>
34 #include <cstdlib>
35
36 typedef std::vector < std::string > string_list;
37
38 namespace simgear {
39   namespace strutils {
40
41         /**
42          * utf8ToLatin1() convert utf8 to latin, useful for accent character (i.e éâàîè...)
43          */
44         std::string utf8ToLatin1( std::string & s_utf8 );
45
46 //      /** 
47 //       * atof() wrapper for "string" type
48 //       */
49 //      inline double
50 //      atof( const string& str )
51 //      {
52 //          return ::atof( str.c_str() );
53 //      }
54
55 //      /**
56 //       * atoi() wrapper for "string" type
57 //       */
58 //      inline int
59 //      atoi( const string& str )
60 //      {
61 //          return ::atoi( str.c_str() );
62 //      }
63
64         /**
65          * Strip leading and/or trailing whitespace from s.
66          * @param s String to strip.
67          * @return The stripped string.
68          */
69         std::string lstrip( const std::string& s );
70         std::string rstrip( const std::string& s );
71         std::string strip( const std::string& s );
72
73         /**
74          * Right-padding of a string to a given length
75          * @param s String to pad
76          * @param length The total length of the resulting string
77          * @param c The character to pad with
78          * @return The padded string
79          */
80         std::string rpad( const std::string & s, size_t length, char c );
81
82         /**
83          * Left-padding of a string to a given length
84          * @param s String to pad
85          * @param length The total length of the resulting string
86          * @param c The character to pad with
87          * @return The padded string
88          */
89         std::string lpad( const std::string & s, size_t length, char c );
90
91         /**
92          * Split a string into a words using 'sep' as the delimiter string.
93          * Produces a result similar to the perl and python functions of the
94          * same name.
95          * 
96          * @param s The string to split into words,
97          * @param sep Word delimiters.  If not specified then any whitespace is a separator,
98          * @param maxsplit If given, splits at no more than maxsplit places,
99          * resulting in at most maxsplit+1 words.
100          * @return Array of words.
101          */
102         string_list
103         split( const std::string& s,
104                const char* sep = 0,
105                int maxsplit = 0 );
106
107        /**
108         * create a single string by joining the elements of a list with
109         * another string.
110         */
111        std::string join(const string_list& l, const std::string& joinWith = "");
112
113         /**
114          * Test if a string starts with a string 
115          *
116          * @param s The string to be tested
117          * @param substr The string to test
118          * @return True, if s starts with substr, False otherwise
119          */
120         bool starts_with( const std::string & s, const std::string & substr );
121
122         /**
123          * Test if a string ends with a string 
124          *
125          * @param s The string to be tested
126          * @param substr The string to test
127          * @return True, if s ends with substr, False otherwise
128          */
129         bool ends_with( const std::string & s, const std::string & substr );
130   
131     /**
132      * Strip all leading/trailing whitespace, and transform all interal
133      * whitespace into a single ' ' character - i.e newlines/carriage returns/
134      * tabs/multiple spaces will be condensed.
135      */
136     std::string simplify(const std::string& s);
137     
138     /**
139      * convert a string representing a decimal number, to an int
140      */
141     int to_int(const std::string& s, int base = 10);
142     
143     /**
144      * Like strcmp(), but for dotted versions strings NN.NN.NN
145      * any number of terms are supported.
146      * @return 0 if versions match, -ve number if v1 is lower, +ve if v1
147      * is greater
148      */
149     int compare_versions(const std::string& v1, const std::string& v2);
150
151     /**
152      * Convert a string to upper case.
153      * @return upper case string
154      */
155     std::string uppercase(const std::string &s);
156
157    /**
158     * Convert a string to lower case.
159     * @return lower case string
160     */
161    std::string lowercase(const std::string &s);
162    
163   /**
164    * Convert a string to lower case in place
165    */
166   void lowercase(std::string &s);
167   
168         /**
169      * convert a string in the local Windows 8-bit encoding to UTF-8
170      * (no-op on other platforms)
171      */
172     std::string convertWindowsLocal8BitToUtf8(const std::string& a);
173
174       /**
175        *
176        */
177       std::string convertUtf8ToWindowsLocal8Bit(const std::string& a);
178
179 #if defined(SG_WINDOWS)
180     typedef std::vector<wchar_t> WCharVec;
181     WCharVec convertUtf8ToWString(const std::string& a);
182 #endif
183
184     /**
185      * Get md5 hash of raw data.
186      */
187     std::string md5(const unsigned char* data, size_t num);
188     std::string md5(const char* data, size_t num);
189     std::string md5(const std::string& str);
190
191     /**
192      * convert base-64 encoded data to raw bytes (possibly with embedded
193      * NULs). Throws an exception if input data is not base64, or is
194      * malformed
195      */
196       void decodeBase64(const std::string& a, std::vector<unsigned char>& output);
197     
198     /**
199      * convert bytes to hexadecimal equivalent
200      */
201     std::string encodeHex(const std::string& bytes);
202     
203     std::string encodeHex(const unsigned char* rawBytes, unsigned int length);
204
205     /**
206      * Unescape string.
207      *
208      * @param str String possibly containing escaped characters.
209      * @return string with escaped characters replaced by single character
210      *         values.
211      */
212     std::string unescape(const char* str);
213
214     inline std::string unescape(const std::string& str)
215     { return unescape(str.c_str()); }
216       
217       /**
218        * Check a printf-style format string for dangerous (buffer-overflowing,
219        * memory re-writing) format tokens. If a problematic token is
220        * found, logs an error (SG_WARN) and returns an empty format string.
221        */
222       std::string sanitizePrintfFormat(const std::string& input);
223
224     /**
225      * Get the message corresponding to a given value of errno.
226      *
227      * Similar to strerror(), except it should be thread-safe and returns an
228      * std::string.
229      */
230     std::string error_string(int errnum);
231
232   } // end namespace strutils
233 } // end namespace simgear
234
235 #endif // STRUTILS_H
236