OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

fileName.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-2009 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 the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     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, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "fileName.H"
00028 #include "wordList.H"
00029 #include "DynamicList.H"
00030 #include "debug.H"
00031 #include "OSspecific.H"
00032 
00033 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00034 
00035 const char* const Foam::fileName::typeName = "fileName";
00036 int Foam::fileName::debug(debug::debugSwitch(fileName::typeName, 0));
00037 const Foam::fileName Foam::fileName::null;
00038 
00039 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00040 
00041 Foam::fileName::fileName(const wordList& lst)
00042 {
00043     forAll(lst, elemI)
00044     {
00045         operator=((*this)/lst[elemI]);
00046     }
00047 }
00048 
00049 
00050 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00051 
00052 Foam::fileName::Type Foam::fileName::type() const
00053 {
00054     return ::Foam::type(*this);
00055 }
00056 
00057 
00058 bool Foam::fileName::clean()
00059 {
00060     bool changed = false;
00061 
00062     changed = this->removeRepeated('/') || changed;
00063     changed = this->removeTrailing('/') || changed;
00064 
00065     return changed;
00066 }
00067 
00068 
00069 // Return string with repeated characters removed
00070 Foam::fileName Foam::fileName::clean() const
00071 {
00072     fileName fName(*this);
00073     fName.clean();
00074     return fName;
00075 }
00076 
00077 
00078 
00079 //  Return file name (part beyond last /)
00080 //
00081 //  behaviour compared to /usr/bin/basename:
00082 //    input           name()          basename
00083 //    -----           ------          --------
00084 //    "foo"           "foo"           "foo"
00085 //    "/foo"          "foo"           "foo"
00086 //    "foo/bar"       "bar"           "bar"
00087 //    "/foo/bar"      "bar"           "bar"
00088 //    "/foo/bar/"     ""              "bar"
00089 //
00090 Foam::word Foam::fileName::name() const
00091 {
00092     size_type i = rfind('/');
00093 
00094     if (i == npos)
00095     {
00096         return *this;
00097     }
00098     else
00099     {
00100         return substr(i+1, npos);
00101     }
00102 }
00103 
00104 
00105 //  Return directory path name (part before last /)
00106 //
00107 //  behaviour compared to /usr/bin/dirname:
00108 //    input           path()          dirname
00109 //    -----           ------          -------
00110 //    "foo"           "."             "."
00111 //    "/foo"          "/"             "foo"
00112 //    "foo/bar"       "foo"           "foo"
00113 //    "/foo/bar"      "/foo"          "/foo"
00114 //    "/foo/bar/"     "/foo/bar/"     "/foo"
00115 //
00116 Foam::fileName Foam::fileName::path() const
00117 {
00118     size_type i = rfind('/');
00119 
00120     if (i == npos)
00121     {
00122         return ".";
00123     }
00124     else if (i)
00125     {
00126         return substr(0, i);
00127     }
00128     else
00129     {
00130         return "/";
00131     }
00132 }
00133 
00134 
00135 //  Return file name without extension (part before last .)
00136 Foam::fileName Foam::fileName::lessExt() const
00137 {
00138     size_type i = find_last_of("./");
00139 
00140     if (i == npos || i == 0 || operator[](i) == '/')
00141     {
00142         return *this;
00143     }
00144     else
00145     {
00146         return substr(0, i);
00147     }
00148 }
00149 
00150 
00151 //  Return file name extension (part after last .)
00152 Foam::word Foam::fileName::ext() const
00153 {
00154     size_type i = find_last_of("./");
00155 
00156     if (i == npos || i == 0 || operator[](i) == '/')
00157     {
00158         return word::null;
00159     }
00160     else
00161     {
00162         return substr(i+1, npos);
00163     }
00164 }
00165 
00166 
00167 // Return the components of the file name as a wordList
00168 // note that concatenating the components will not necessarily retrieve
00169 // the original input fileName
00170 //
00171 //  behaviour
00172 //    input           components()
00173 //    -----           ------
00174 //    "foo"           1("foo")
00175 //    "/foo"          1("foo")
00176 //    "foo/bar"       2("foo", "bar")
00177 //    "/foo/bar"      2("foo", "bar")
00178 //    "/foo/bar/"     2("foo", "bar")
00179 //
00180 Foam::wordList Foam::fileName::components(const char delimiter) const
00181 {
00182     DynamicList<word> wrdList(20);
00183 
00184     size_type start=0, end=0;
00185 
00186     while ((end = find(delimiter, start)) != npos)
00187     {
00188         // avoid empty element (caused by doubled slashes)
00189         if (start < end)
00190         {
00191             wrdList.append(substr(start, end-start));
00192         }
00193         start = end + 1;
00194     }
00195 
00196     // avoid empty trailing element
00197     if (start < size())
00198     {
00199         wrdList.append(substr(start, npos));
00200     }
00201 
00202     // transfer to wordList
00203     return wordList(wrdList.xfer());
00204 }
00205 
00206 
00207 // Return a component of the file name
00208 Foam::word Foam::fileName::component
00209 (
00210     const size_type cmpt,
00211     const char delimiter
00212 ) const
00213 {
00214     return components(delimiter)[cmpt];
00215 }
00216 
00217 
00218 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00219 
00220 const Foam::fileName& Foam::fileName::operator=(const fileName& str)
00221 {
00222     string::operator=(str);
00223     return *this;
00224 }
00225 
00226 
00227 const Foam::fileName& Foam::fileName::operator=(const word& str)
00228 {
00229     string::operator=(str);
00230     return *this;
00231 }
00232 
00233 
00234 const Foam::fileName& Foam::fileName::operator=(const string& str)
00235 {
00236     string::operator=(str);
00237     stripInvalid();
00238     return *this;
00239 }
00240 
00241 
00242 const Foam::fileName& Foam::fileName::operator=(const std::string& str)
00243 {
00244     string::operator=(str);
00245     stripInvalid();
00246     return *this;
00247 }
00248 
00249 
00250 const Foam::fileName& Foam::fileName::operator=(const char* str)
00251 {
00252     string::operator=(str);
00253     stripInvalid();
00254     return *this;
00255 }
00256 
00257 
00258 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
00259 
00260 Foam::fileName Foam::operator/(const string& a, const string& b)
00261 {
00262     if (a.size())           // First string non-null
00263     {
00264         if (b.size())       // Second string non-null
00265         {
00266             return fileName(a + '/' + b);
00267         }
00268         else                // Second string null
00269         {
00270             return a;
00271         }
00272     }
00273     else                    // First string null
00274     {
00275         if (b.size())       // Second string non-null
00276         {
00277             return b;
00278         }
00279         else                // Second string null
00280         {
00281             return fileName();
00282         }
00283     }
00284 }
00285 
00286 
00287 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd