Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1020 was 1020, checked in by landauf, 16 years ago

changed ConfigValueContainer to use ConfigFileManager, but there is still an error

File size: 5.5 KB
Line 
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
28#ifndef _Util_String_H__
29#define _Util_String_H__
30
31#include <string>
32#include <sstream>
33
34#include "UtilPrereqs.h"
35
36_UtilExport void         strip(std::string* str);
37_UtilExport std::string  getStripped(const std::string& str);
38
39_UtilExport std::string  removeTrailingWhitespaces(const std::string& str);
40
41_UtilExport unsigned int getNextQuote(const std::string& str, unsigned int start);
42
43_UtilExport bool         hasStringBetweenQuotes(const std::string& str);
44_UtilExport std::string  getStringBetweenQuotes(const std::string& str);
45
46_UtilExport void         stripEnclosingQuotes(std::string* str);
47_UtilExport std::string  getStrippedEnclosingQuotes(const std::string& str);
48
49_UtilExport bool         isEmpty(const std::string& str);
50_UtilExport bool         isComment(const std::string& str);
51_UtilExport bool         isNumeric(const std::string& str);
52
53_UtilExport void         lowercase(std::string* str);
54_UtilExport std::string  getLowercase(const std::string& str);
55
56_UtilExport void         uppercase(std::string* str);
57_UtilExport std::string  getUppercase(const std::string& str);
58
59_UtilExport int          nocaseCmp(const std::string& s1, const std::string& s2);
60_UtilExport int          nocaseCmp(const std::string& s1, const std::string& s2, unsigned int len);
61
62_UtilExport bool         hasComment(const std::string& str);
63_UtilExport std::string  getComment(const std::string& str);
64_UtilExport unsigned int getCommentPosition(const std::string& str);
65
66//! The Convert class has some static member functions to convert strings to values and values to strings.
67class _UtilExport Convert
68{
69    public:
70        /**
71            @brief Converts a value of any type to a string.
72            @param output The string to write the result in
73            @param input The variable to convert
74            @return True if the conversion succeded
75
76            @example
77            float f = 3.14;
78            std::string output;
79            bool success = Convert::ToString(&output, f);
80        */
81        template <typename T>
82        static bool ToString(std::string* output, T input)
83        {
84            std::ostringstream oss;
85            if (oss << input)
86            {
87                (*output) = oss.str();
88                return true;
89            }
90
91            return false;
92        }
93
94        /**
95            @brief Converts a value of any type to a string and assigns a defaultvalue if the conversion fails.
96            @param output The string to write the result in
97            @param input The variable to convert
98            @param fallbackString The assigned string if the conversion fails.
99            @return True if the conversion succeeded
100
101            @example
102            float f = 3.14;
103            std::string output;
104            bool success = Convert::ToString(&output, f, "0.000000");
105        */
106        template <typename T>
107        static bool ToString(std::string* output, T input, const std::string& fallbackString)
108        {
109            if (Convert::ToString(output, input))
110                return true;
111
112            (*output) = fallbackString;
113            return false;
114        }
115
116        /**
117            @brief Converts a string to a value of any type.
118            @param output The variable to assign the result to
119            @param input The string to convert
120            @return True if the conversion succeeded
121
122            @example
123            std::string input = "3.14";
124            float f;
125            bool success = string2Number(&f, input);
126        */
127        template <typename T>
128        static bool FromString(T* output, const std::string& input)
129        {
130            std::istringstream iss(input);
131            if (iss >> (*output))
132                return true;
133
134            return false;
135        }
136
137        /**
138            @brief Converts a string to a value of any type.
139            @param output The variable to assign the result to
140            @param input The string to convert
141            @param fallbackValue The assigned value if the conversion fails
142            @return True if the conversion succeeded
143
144            @example
145            std::string input = "3.14";
146            float f;
147            bool success = string2Number(&f, input, 0.000000);
148        */
149        template <typename T>
150        static bool FromString(T* output, const std::string& input, T fallbackValue)
151        {
152            if (Convert::FromString(output, input))
153                return true;
154
155            (*output) = fallbackValue;
156            return false;
157        }
158};
159
160#endif /* _Util_String_H__ */
Note: See TracBrowser for help on using the repository browser.