1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
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
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 \*---------------------------------------------------------------------------*/
28 #include "OSspecific.H"
30 /* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */
32 const char* const Foam::string::typeName = "string";
33 int Foam::string::debug(debug::debugSwitch(string::typeName, 0));
34 const Foam::string Foam::string::null;
36 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
38 // Count and return the number of a given character in the string
39 Foam::string::size_type Foam::string::count(const char c) const
41 register size_type cCount = 0;
43 for (const_iterator iter = begin(); iter != end(); ++iter)
55 // Replace first occurence of sub-string oldStr with newStr
56 Foam::string& Foam::string::replace
63 size_type newStart = start;
65 if ((newStart = find(oldStr, newStart)) != npos)
67 std::string::replace(newStart, oldStr.size(), newStr);
74 // Replace all occurences of sub-string oldStr with newStr
75 Foam::string& Foam::string::replaceAll
84 size_type newStart = start;
86 while ((newStart = find(oldStr, newStart)) != npos)
88 std::string::replace(newStart, oldStr.size(), newStr);
89 newStart += newStr.size();
97 // Expand all occurences of environment variables and initial tilde sequences
98 Foam::string& Foam::string::expand()
100 size_type startEnvar = 0;
103 // Repeat until nothing more is found
106 (startEnvar = find('$', startEnvar)) != npos
107 && startEnvar < size()-1
110 if (startEnvar == 0 || operator[](startEnvar-1) != '\\')
112 // Find end of first occurrence
113 size_type endEnvar = startEnvar;
116 if (operator[](startEnvar+1) == '{')
118 endEnvar = find('}', startEnvar);
123 iterator iter = begin() + startEnvar + 1;
125 while (iter != end() && (isalnum(*iter) || *iter == '_'))
132 if (endEnvar != npos && endEnvar != startEnvar)
134 string enVar = substr
137 endEnvar - startEnvar - 2*nd
140 string enVarString = getEnv(enVar);
142 if (enVarString.size())
147 endEnvar - startEnvar + 1,
150 startEnvar += enVarString.size();
154 //startEnvar = endEnvar;
156 FatalErrorIn("string::expand()")
157 << "Unknown variable name " << enVar << '.'
174 if (operator[](0) == '~')
177 // ~/ => home directory
178 // ~OpenFOAM => site/user OpenFOAM configuration directory
179 // ~user => home directory for specified user
184 if ((startEnvar = find('/')) != npos)
186 user = substr(1, startEnvar - 1);
187 file = substr(startEnvar + 1);
194 // NB: be a bit lazy and expand ~unknownUser as an
195 // empty string rather than leaving it untouched.
196 // otherwise add extra test
197 if (user == "OpenFOAM")
199 *this = findEtcFile(file);
203 *this = home(user)/file;
206 else if (operator[](0) == '.')
208 // Expand initial '.' and './' into cwd
213 else if (operator[](1) == '/')
215 std::string::replace(0, 1, cwd());
224 // Remove repeated characters returning true if string changed
225 bool Foam::string::removeRepeated(const char character)
227 bool changed = false;
229 if (character && find(character) != npos)
231 register string::size_type nChar=0;
232 iterator iter2 = begin();
234 register char prev = 0;
238 string::const_iterator iter1 = iter2;
243 register char c = *iter1;
245 if (prev == c && c == character)
263 // Return string with repeated characters removed
264 Foam::string Foam::string::removeRepeated(const char character) const
267 str.removeRepeated(character);
272 // Remove trailing character returning true if string changed
273 bool Foam::string::removeTrailing(const char character)
275 bool changed = false;
277 string::size_type nChar = size();
278 if (character && nChar > 1 && operator[](nChar-1) == character)
288 // Return string with trailing character removed
289 Foam::string Foam::string::removeTrailing(const char character) const
292 str.removeTrailing(character);
297 // ************************************************************************* //