Revert "Moved the construction of the static "null" strings to stringsGlobals.C which...
[freefoam.git] / src / OpenFOAM / primitives / strings / fileName / fileName.C
blobf96faae4d64f506dabc23dd604b23610a887781b
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 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 \*---------------------------------------------------------------------------*/
27 #include <OpenFOAM/fileName.H>
28 #include <OpenFOAM/wordList.H>
29 #include <OpenFOAM/debug.H>
30 #include <OpenFOAM/OSspecific.H>
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 const char* const Foam::fileName::typeName = "fileName";
35 int Foam::fileName::debug(debug::debugSwitch(fileName::typeName, 0));
36 const Foam::fileName Foam::fileName::null;
38 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
40 Foam::fileName::fileName(const wordList& wrdList)
42     if (wrdList.size() != 0)
43     {
44         forAll(wrdList, i)
45         {
46             operator=((*this)/wrdList[i]);
47         }
48     }
52 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
54 //  Return file name (part beyond last /)
56 //  behaviour compared to /usr/bin/basename:
57 //    input           name()          basename
58 //    -----           ------          --------
59 //    "foo"           "foo"           "foo"
60 //    "/foo"          "foo"           "foo"
61 //    "foo/bar"       "bar"           "bar"
62 //    "/foo/bar"      "bar"           "bar"
63 //    "/foo/bar/"     ""              "bar"
65 Foam::word Foam::fileName::name() const
67     size_type i = rfind('/');
69     if (i == npos)
70     {
71         return *this;
72     }
73     else
74     {
75         return substr(i+1, npos);
76     }
80 //- Return directory path name (part before last /)
82 //  behaviour compared to /usr/bin/dirname:
83 //    input           path()          dirname
84 //    -----           ------          -------
85 //    "foo"           "."             "."
86 //    "/foo"          "/"             "foo"
87 //    "foo/bar"       "foo"           "foo"
88 //    "/foo/bar"      "/foo"          "/foo"
89 //    "/foo/bar/"     "/foo/bar/"     "/foo"
91 Foam::fileName Foam::fileName::path() const
93     size_type i = rfind('/');
95     if (i == npos)
96     {
97         return ".";
98     }
99     else if (i == 0)
100     {
101         return "/";
102     }
103     else
104     {
105         return substr(0, i);
106     }
110 //  Return file name without extension (part before last .)
111 Foam::fileName Foam::fileName::lessExt() const
113     size_type i = find_last_of("./");
115     if (i == npos || i == 0 || operator[](i) == '/')
116     {
117         return *this;
118     }
119     else
120     {
121         return substr(0, i);
122     }
126 //  Return file name extension (part after last .)
127 Foam::word Foam::fileName::ext() const
129     size_type i = find_last_of("./");
131     if (i == npos || i == 0 || operator[](i) == '/')
132     {
133         return word::null;
134     }
135     else
136     {
137         return substr(i+1, npos);
138     }
142 // Return the components of the file name as a wordList
143 // note that concatenating the components will not necessarily retrieve
144 // the original input fileName
146 //  behaviour
147 //    input           components()
148 //    -----           ------
149 //    "foo"           1("foo")
150 //    "/foo"          1("foo")
151 //    "foo/bar"       2("foo", "foo")
152 //    "/foo/bar"      2("foo", "foo")
153 //    "/foo/bar/"     2("foo", "bar")
155 Foam::wordList Foam::fileName::components(const char delimiter) const
157     wordList wrdList(20);
159     size_type start=0, end=0;
160     label nWords=0;
162     while ((end = find(delimiter, start)) != npos)
163     {
164         // avoid empty element (caused by doubled slashes)
165         if (start < end)
166         {
167             wrdList[nWords++] = substr(start, end-start);
169             if (nWords == wrdList.size())
170             {
171                 wrdList.setSize(2*wrdList.size());
172             }
173         }
174         start = end + 1;
175     }
177     // avoid empty trailing element
178     if (start < size())
179     {
180         wrdList[nWords++] = substr(start, npos);
181     }
183     wrdList.setSize(nWords);
185     return wrdList;
189 // Return a component of the file name
190 Foam::word Foam::fileName::component
192     const size_type cmpt,
193     const char delimiter
194 ) const
196     return components(delimiter)[cmpt];
200 Foam::fileName::Type Foam::fileName::type() const
202     return ::Foam::type(*this);
206 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
208 void Foam::fileName::operator=(const fileName& q)
210     string::operator=(q);
214 void Foam::fileName::operator=(const word& q)
216     string::operator=(q);
220 void Foam::fileName::operator=(const string& q)
222     string::operator=(q);
223     stripInvalid();
227 void Foam::fileName::operator=(const std::string& q)
229     string::operator=(q);
230     stripInvalid();
234 void Foam::fileName::operator=(const char* q)
236     string::operator=(q);
237     stripInvalid();
241 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
243 Foam::fileName Foam::operator/(const string& a, const string& b)
245     if (a.size() > 0)       // First string non-null
246     {
247         if (b.size() > 0)   // Second string non-null
248         {
249             return fileName(a + '/' + b);
250         }
251         else                // Second string null
252         {
253             return a;
254         }
255     }
256     else                    // First string null
257     {
258         if (b.size() > 0)   // Second string non-null
259         {
260             return b;
261         }
262         else                // Second string null
263         {
264             return fileName();
265         }
266     }
270 // ************************ vim: set sw=4 sts=4 et: ************************ //