Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/util/String.h @ 1892

Last change on this file since 1892 was 1049, checked in by landauf, 17 years ago

config-value accepts now strings with special chars like \n or \t and hopefully every other. strings are enclosed by "…", but the quotes are only visible in the config-file. if you want something like " ← whitespace" you must add quotes, otherwise this would be stripped to "← whitespace".

File size: 5.7 KB
RevLine 
[792]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      Benjamin Grauer
25 *
26 */
27
[933]28#ifndef _Util_String_H__
29#define _Util_String_H__
[792]30
31#include <string>
32#include <sstream>
33
34#include "UtilPrereqs.h"
35
[1006]36_UtilExport void         strip(std::string* str);
37_UtilExport std::string  getStripped(const std::string& str);
[792]38
[1006]39_UtilExport std::string  removeTrailingWhitespaces(const std::string& str);
[994]40
[1020]41_UtilExport unsigned int getNextQuote(const std::string& str, unsigned int start);
[1049]42_UtilExport bool         isBetweenQuotes(const std::string& str, unsigned int pos);
[1020]43
[1006]44_UtilExport bool         hasStringBetweenQuotes(const std::string& str);
45_UtilExport std::string  getStringBetweenQuotes(const std::string& str);
[792]46
[1049]47_UtilExport std::string  stripEnclosingQuotes(const std::string& str);
[792]48
[1006]49_UtilExport bool         isEmpty(const std::string& str);
50_UtilExport bool         isComment(const std::string& str);
51_UtilExport bool         isNumeric(const std::string& str);
[792]52
[1049]53_UtilExport std::string  addSlashes(const std::string& str);
54_UtilExport std::string  removeSlashes(const std::string& str);
55
[1006]56_UtilExport void         lowercase(std::string* str);
57_UtilExport std::string  getLowercase(const std::string& str);
[792]58
[1006]59_UtilExport void         uppercase(std::string* str);
60_UtilExport std::string  getUppercase(const std::string& str);
61
62_UtilExport int          nocaseCmp(const std::string& s1, const std::string& s2);
63_UtilExport int          nocaseCmp(const std::string& s1, const std::string& s2, unsigned int len);
64
65_UtilExport bool         hasComment(const std::string& str);
66_UtilExport std::string  getComment(const std::string& str);
67_UtilExport unsigned int getCommentPosition(const std::string& str);
[1049]68_UtilExport unsigned int getNextCommentPosition(const std::string& str, unsigned int start = 0);
[1006]69
[792]70//! The Convert class has some static member functions to convert strings to values and values to strings.
71class _UtilExport Convert
72{
73    public:
74        /**
75            @brief Converts a value of any type to a string.
76            @param output The string to write the result in
77            @param input The variable to convert
78            @return True if the conversion succeded
79
80            @example
81            float f = 3.14;
82            std::string output;
[848]83            bool success = Convert::ToString(&output, f);
[792]84        */
85        template <typename T>
86        static bool ToString(std::string* output, T input)
87        {
88            std::ostringstream oss;
89            if (oss << input)
90            {
91                (*output) = oss.str();
92                return true;
93            }
94
95            return false;
96        }
97
98        /**
99            @brief Converts a value of any type to a string and assigns a defaultvalue if the conversion fails.
100            @param output The string to write the result in
101            @param input The variable to convert
102            @param fallbackString The assigned string if the conversion fails.
103            @return True if the conversion succeeded
104
105            @example
106            float f = 3.14;
107            std::string output;
[848]108            bool success = Convert::ToString(&output, f, "0.000000");
[792]109        */
110        template <typename T>
111        static bool ToString(std::string* output, T input, const std::string& fallbackString)
112        {
113            if (Convert::ToString(output, input))
114                return true;
115
116            (*output) = fallbackString;
117            return false;
118        }
119
120        /**
121            @brief Converts a string to a value of any type.
122            @param output The variable to assign the result to
123            @param input The string to convert
124            @return True if the conversion succeeded
125
126            @example
127            std::string input = "3.14";
128            float f;
[848]129            bool success = string2Number(&f, input);
[792]130        */
131        template <typename T>
132        static bool FromString(T* output, const std::string& input)
133        {
134            std::istringstream iss(input);
135            if (iss >> (*output))
136                return true;
137
138            return false;
139        }
140
141        /**
142            @brief Converts a string to a value of any type.
143            @param output The variable to assign the result to
144            @param input The string to convert
145            @param fallbackValue The assigned value if the conversion fails
146            @return True if the conversion succeeded
147
148            @example
149            std::string input = "3.14";
150            float f;
[848]151            bool success = string2Number(&f, input, 0.000000);
[792]152        */
153        template <typename T>
154        static bool FromString(T* output, const std::string& input, T fallbackValue)
155        {
156            if (Convert::FromString(output, input))
157                return true;
158
159            (*output) = fallbackValue;
160            return false;
161        }
162};
163
[933]164#endif /* _Util_String_H__ */
Note: See TracBrowser for help on using the repository browser.