Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 994 was 994, checked in by landauf, 16 years ago
  • added some symbols to the CommandExecutor: a) expression | expression: the pipe leads the output from the right expression into the left one b) expression > file: writes the output of the expression into a file c) expression < file: reads a file and uses it's content as input for the expression
  • added new console commands: a) echo text: returns the input b) read file: reads a file and returns the content c) write file text: writes text into a file d) append file text: appends text to a file
  • added stripEnclosingQuotes function to String.h, that removes enclosing quotes (if there are some). whitespaces outside the quotes are stripped, whitespaces inside the quotes stay. removes the quotes only if there is nothing else than whitespaces outside of them. what it changes: "expression" → expression what it let unchanged:
    • ex"press"ion
    • a"expression"b
    • a"expression"
    • "expression"b
    • express"ion
  • extended SubString: added some bools to determine the behaviour when dividing a string like the following up into pieces: mytext "this is a quoted area" blub (0, 1, 2)

this usually results in:
mytext / this is a quoted area / blub / 0, 1, 2

but now you can change it to:
mytext / "this is a quoted area" / blub / (0, 1, 2)

this is important if the string wents through several substring splitups and the quotes and brackets should stay.

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