initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / primitives / strings / string / string.H
blob97c6dd4d4dd7e50a6670b27704e8a9b24b57e1fb
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Class
26     Foam::string
28 Description
29     A class for handling character strings derived from std::string.
31     Strings may contain any characters and therefore are delimited by quotes
32     for IO : "any list of characters".
34     Used as a base class for word and fileName.
36 See Also
37     Foam::findEtcFile() for information about the site/user OpenFOAM
38     configuration directory
40 SourceFiles
41     string.C
42     stringIO.C
44 \*---------------------------------------------------------------------------*/
46 #ifndef string_H
47 #define string_H
49 #include "char.H"
50 #include "Hasher.H"
52 #include <string>
53 #include <cstring>
54 #include <cstdlib>
56 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
58 namespace Foam
61 // Forward declaration of classes
62 class Istream;
63 class Ostream;
65 // Forward declaration of friend functions and operators
66 class string;
67 Istream& operator>>(Istream&, string&);
68 Ostream& operator<<(Ostream&, const string&);
69 Ostream& operator<<(Ostream&, const std::string&);
72 /*---------------------------------------------------------------------------*\
73                            Class string Declaration
74 \*---------------------------------------------------------------------------*/
76 class string
78     public std::string
80 public:
82     // Static data members
84         static const char* const typeName;
85         static int debug;
86         static const string null;
89     //- Hashing function class, shared by all the derived classes
90     class hash
91     {
92     public:
93         hash()
94         {}
96         inline unsigned operator()(const string&, unsigned seed = 0) const;
97     };
100     // Constructors
102         //- Construct null
103         inline string();
105         //- Construct from std::string
106         inline string(const std::string&);
108         //- Construct as copy of character array
109         inline string(const char*);
111         //- Construct as copy of specified number of characters
112         inline string(const char*, const size_type);
114         //- Construct from a single character
115         inline string(const char);
117         //- Construct from Istream
118         string(Istream&);
121     // Member Functions
123         //- Count and return the number of a given character in the string
124         size_type count(const char) const;
126         //- Is this string type valid?
127         template<class String>
128         static inline bool valid(const string&);
130         //- Does this string have particular meta-characters?
131         //  The meta characters can be optionally quoted.
132         template<class String>
133         static inline bool meta(const string&, const char quote='\\');
135         //- Strip invalid characters from the given string
136         template<class String>
137         static inline bool stripInvalid(string&);
139         //- Return a valid String from the given string
140         template<class String>
141         static inline String validate(const string&);
143         //- Return a String with quoted meta-characters from the given string
144         template<class String>
145         static inline string quotemeta(const string&, const char quote='\\');
147         //- Avoid masking the normal std::string replace
148         using std::string::replace;
150         //- Replace first occurence of sub-string oldStr with newStr
151         //  starting at start
152         string& replace
153         (
154             const string& oldStr,
155             const string& newStr,
156             size_type start = 0
157         );
159         //- Replace all occurences of sub-string oldStr with newStr
160         //  starting at start
161         string& replaceAll
162         (
163             const string& oldStr,
164             const string& newStr,
165             size_type start = 0
166         );
168         //- Expand initial tildes and all occurences of environment variables
169         //  Expansion includes:
170         //  -# environment variables
171         //     - "$VAR", "${VAR}"
172         //  -# current directory
173         //     - leading "./" : the current directory
174         //  -# tilde expansion
175         //     - leading "~/" : home directory
176         //     - leading "~user" : home directory for specified user
177         //     - leading "~OpenFOAM" : site/user OpenFOAM configuration directory
178         //
179         //  @sa
180         //  Foam::findEtcFile
181         string& expand();
183         //- Remove repeated characters returning true if string changed
184         bool removeRepeated(const char);
186         //- Return string with repeated characters removed
187         string removeRepeated(const char) const;
189         //- Remove trailing character returning true if string changed
190         bool removeTrailing(const char);
192         //- Return string with trailing character removed
193         string removeTrailing(const char) const;
196     // Member Operators
198         //- Return the sub-string from the i-th character for @a n characters
199         inline string operator()
200         (
201             const size_type i,
202             const size_type n
203         ) const;
205         //- Return the sub-string from the first character for @a n characters
206         inline string operator()
207         (
208             const size_type n
209         ) const;
212     // IOstream Operators
214         friend Istream& operator>>(Istream&, string&);
215         friend Ostream& operator<<(Ostream&, const string&);
219 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
221 } // End namespace Foam
223 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
225 #include "stringI.H"
227 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
229 #endif
231 // ************************************************************************* //