FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

string.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software: you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "string.H"
00027 #include <OpenFOAM/OSspecific.H>
00028 
00029 /* * * * * * * * * * * * * * * Static Member Data  * * * * * * * * * * * * * */
00030 
00031 const char* const Foam::string::typeName = "string";
00032 int Foam::string::debug(debug::debugSwitch(string::typeName, 0));
00033 const Foam::string Foam::string::null;
00034 
00035 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00036 
00037 // Count and return the number of a given character in the string
00038 Foam::string::size_type Foam::string::count(const char c) const
00039 {
00040     register size_type cCount = 0;
00041 
00042     for (const_iterator iter = begin(); iter != end(); ++iter)
00043     {
00044         if (*iter == c)
00045         {
00046             cCount++;
00047         }
00048     }
00049 
00050     return cCount;
00051 }
00052 
00053 
00054 // Replace first occurence of sub-string oldStr with newStr
00055 Foam::string& Foam::string::replace
00056 (
00057     const string& oldStr,
00058     const string& newStr,
00059     size_type start
00060 )
00061 {
00062     size_type newStart = start;
00063 
00064     if ((newStart = find(oldStr, newStart)) != npos)
00065     {
00066         std::string::replace(newStart, oldStr.size(), newStr);
00067     }
00068 
00069     return *this;
00070 }
00071 
00072 
00073 // Replace all occurences of sub-string oldStr with newStr
00074 Foam::string& Foam::string::replaceAll
00075 (
00076     const string& oldStr,
00077     const string& newStr,
00078     size_type start
00079 )
00080 {
00081     if (oldStr.size())
00082     {
00083         size_type newStart = start;
00084 
00085         while ((newStart = find(oldStr, newStart)) != npos)
00086         {
00087             std::string::replace(newStart, oldStr.size(), newStr);
00088             newStart += newStr.size();
00089         }
00090     }
00091 
00092     return *this;
00093 }
00094 
00095 
00096 // Expand all occurences of environment variables and initial tilde sequences
00097 Foam::string& Foam::string::expand()
00098 {
00099     size_type startEnvar = 0;
00100 
00101     // Expand $VARS
00102     // Repeat until nothing more is found
00103     while
00104     (
00105         (startEnvar = find('$', startEnvar)) != npos
00106      && startEnvar < size()-1
00107     )
00108     {
00109         if (startEnvar == 0 || operator[](startEnvar-1) != '\\')
00110         {
00111             // Find end of first occurrence
00112             size_type endEnvar = startEnvar;
00113             size_type nd = 0;
00114 
00115             if (operator[](startEnvar+1) == '{')
00116             {
00117                 endEnvar = find('}', startEnvar);
00118                 nd = 1;
00119             }
00120             else
00121             {
00122                 iterator iter = begin() + startEnvar + 1;
00123 
00124                 while (iter != end() && (isalnum(*iter) || *iter == '_'))
00125                 {
00126                     ++iter;
00127                     ++endEnvar;
00128                 }
00129             }
00130 
00131             if (endEnvar != npos && endEnvar != startEnvar)
00132             {
00133                 string enVar = substr
00134                 (
00135                     startEnvar + 1 + nd,
00136                     endEnvar - startEnvar - 2*nd
00137                 );
00138 
00139                 string enVarString = getEnv(enVar);
00140 
00141                 if (enVarString.size())
00142                 {
00143                     std::string::replace
00144                     (
00145                         startEnvar,
00146                         endEnvar - startEnvar + 1,
00147                         enVarString
00148                     );
00149                     startEnvar += enVarString.size();
00150                 }
00151                 else
00152                 {
00153                     //startEnvar = endEnvar;
00154 
00155                     FatalErrorIn("string::expand()")
00156                         << "Unknown variable name " << enVar << '.'
00157                         << exit(FatalError);
00158                 }
00159             }
00160             else
00161             {
00162                 break;
00163             }
00164         }
00165         else
00166         {
00167             startEnvar++;
00168         }
00169     }
00170 
00171     if (size())
00172     {
00173         if (operator[](0) == '~')
00174         {
00175             // Expand initial ~
00176             //   ~/        => home directory
00177             //   ~FreeFOAM => site/user FreeFOAM configuration directory
00178             //   ~OpenFOAM => site/user FreeFOAM configuration directory (alias for FreeFOAM)
00179             //   ~user     => home directory for specified user
00180 
00181             word user;
00182             fileName file;
00183 
00184             if ((startEnvar = find('/')) != npos)
00185             {
00186                 user = substr(1, startEnvar - 1);
00187                 file = substr(startEnvar + 1);
00188             }
00189             else
00190             {
00191                 user = substr(1);
00192             }
00193 
00194             // NB: be a bit lazy and expand ~unknownUser as an
00195             // empty string rather than leaving it untouched.
00196             // otherwise add extra test
00197             if (user == "FreeFOAM" || user == "OpenFOAM")
00198             {
00199                 *this = findEtcFile(file);
00200             }
00201             else
00202             {
00203                 *this = home(user)/file;
00204             }
00205         }
00206         else if (operator[](0) == '.')
00207         {
00208             // Expand initial '.' and './' into cwd
00209             if (size() == 1)
00210             {
00211                 *this = cwd();
00212             }
00213             else if (operator[](1) == '/')
00214             {
00215                 std::string::replace(0, 1, cwd());
00216             }
00217         }
00218     }
00219 
00220     return *this;
00221 }
00222 
00223 
00224 // Remove repeated characters returning true if string changed
00225 bool Foam::string::removeRepeated(const char character)
00226 {
00227     bool changed = false;
00228 
00229     if (character && find(character) != npos)
00230     {
00231         register string::size_type nChar=0;
00232         iterator iter2 = begin();
00233 
00234         register char prev = 0;
00235 
00236         for
00237         (
00238             string::const_iterator iter1 = iter2;
00239             iter1 != end();
00240             iter1++
00241         )
00242         {
00243             register char c = *iter1;
00244 
00245             if (prev == c && c == character)
00246             {
00247                 changed = true;
00248             }
00249             else
00250             {
00251                 *iter2 = prev = c;
00252                 ++iter2;
00253                 ++nChar;
00254             }
00255         }
00256         resize(nChar);
00257     }
00258 
00259     return changed;
00260 }
00261 
00262 
00263 // Return string with repeated characters removed
00264 Foam::string Foam::string::removeRepeated(const char character) const
00265 {
00266     string str(*this);
00267     str.removeRepeated(character);
00268     return str;
00269 }
00270 
00271 
00272 // Remove trailing character returning true if string changed
00273 bool Foam::string::removeTrailing(const char character)
00274 {
00275     bool changed = false;
00276 
00277     string::size_type nChar = size();
00278     if (character && nChar > 1 && operator[](nChar-1) == character)
00279     {
00280         resize(nChar-1);
00281         changed = true;
00282     }
00283 
00284     return changed;
00285 }
00286 
00287 
00288 // Return string with trailing character removed
00289 Foam::string Foam::string::removeTrailing(const char character) const
00290 {
00291     string str(*this);
00292     str.removeTrailing(character);
00293     return str;
00294 }
00295 
00296 
00297 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines