FreeFOAM The Cross-Platform CFD Toolkit
string.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::string
26 
27 Description
28  A class for handling character strings derived from std::string.
29 
30  Strings may contain any characters and therefore are delimited by quotes
31  for IO : "any list of characters".
32 
33  Used as a base class for word and fileName.
34 
35 See Also
36  Foam::findEtcFile() for information about the site/user OpenFOAM
37  configuration directory
38 
39 SourceFiles
40  string.C
41  stringIO.C
42 
43 \*---------------------------------------------------------------------------*/
44 
45 #ifndef string_H
46 #define string_H
47 
48 #include <OpenFOAM/char.H>
49 #include <OpenFOAM/Hasher.H>
50 
51 #include <string>
52 #include <cstring>
53 #include <cstdlib>
54 
55 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56 
57 namespace Foam
58 {
59 
60 // Forward declaration of classes
61 class Istream;
62 class Ostream;
63 
64 // Forward declaration of friend functions and operators
65 class string;
66 Istream& operator>>(Istream&, string&);
67 Ostream& operator<<(Ostream&, const string&);
68 Ostream& operator<<(Ostream&, const std::string&);
69 
70 
71 /*---------------------------------------------------------------------------*\
72  Class string Declaration
73 \*---------------------------------------------------------------------------*/
74 
75 class string
76 :
77  public std::string
78 {
79 public:
80 
81  // Static data members
82 
83  static const char* const typeName;
84  static int debug;
85  static const string null;
86 
87 
88  //- Hashing function class, shared by all the derived classes
89  class hash
90  {
91  public:
92  hash()
93  {}
94 
95  inline unsigned operator()(const string&, unsigned seed = 0) const;
96  };
97 
98 
99  // Constructors
100 
101  //- Construct null
102  inline string();
103 
104  //- Construct from std::string
105  inline string(const std::string&);
106 
107  //- Construct as copy of character array
108  inline string(const char*);
109 
110  //- Construct as copy of specified number of characters
111  inline string(const char*, const size_type);
112 
113  //- Construct from a single character
114  inline string(const char);
115 
116  //- Construct from Istream
117  string(Istream&);
118 
119 
120  // Member Functions
121 
122  //- Count and return the number of a given character in the string
123  size_type count(const char) const;
124 
125  //- Is this string type valid?
126  template<class String>
127  static inline bool valid(const string&);
128 
129  //- Does this string have particular meta-characters?
130  // The meta characters can be optionally quoted.
131  template<class String>
132  static inline bool meta(const string&, const char quote='\\');
133 
134  //- Strip invalid characters from the given string
135  template<class String>
136  static inline bool stripInvalid(string&);
137 
138  //- Return a valid String from the given string
139  template<class String>
140  static inline String validate(const string&);
141 
142  //- Return a String with quoted meta-characters from the given string
143  template<class String>
144  static inline string quotemeta(const string&, const char quote='\\');
145 
146  //- Avoid masking the normal std::string replace
147  using std::string::replace;
148 
149  //- Replace first occurence of sub-string oldStr with newStr
150  // starting at start
151  string& replace
152  (
153  const string& oldStr,
154  const string& newStr,
155  size_type start = 0
156  );
157 
158  //- Replace all occurences of sub-string oldStr with newStr
159  // starting at start
160  string& replaceAll
161  (
162  const string& oldStr,
163  const string& newStr,
164  size_type start = 0
165  );
166 
167  //- Expand initial tildes and all occurences of environment variables
168  // Expansion includes:
169  // -# environment variables
170  // - "$VAR", "${VAR}"
171  // -# current directory
172  // - leading "./" : the current directory
173  // -# tilde expansion
174  // - leading "~/" : home directory
175  // - leading "~user" : home directory for specified user
176  // - leading "~FreeFOAM" : site/user FreeFOAM configuration directory
177  // - leading "~OpenFOAM" : site/user FreeFOAM configuration directory (alias for ~FreeFOAM)
178  //
179  // @sa
180  // Foam::findEtcFile
181  string& expand();
182 
183  //- Remove repeated characters returning true if string changed
184  bool removeRepeated(const char);
185 
186  //- Return string with repeated characters removed
187  string removeRepeated(const char) const;
188 
189  //- Remove trailing character returning true if string changed
190  bool removeTrailing(const char);
191 
192  //- Return string with trailing character removed
193  string removeTrailing(const char) const;
194 
195 
196  // Member Operators
197 
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;
204 
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;
210 
211 
212  // IOstream Operators
213 
214  friend Istream& operator>>(Istream&, string&);
215  friend Ostream& operator<<(Ostream&, const string&);
216 };
217 
218 
219 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
220 
221 } // End namespace Foam
222 
223 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
224 
225 #include "stringI.H"
226 
227 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
228 
229 #endif
230 
231 // ************************ vim: set sw=4 sts=4 et: ************************ //