1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2008 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 \*---------------------------------------------------------------------------*/
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)
46 operator=((*this)/wrdList[i]);
52 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
54 // Return file name (part beyond last /)
56 // behaviour compared to /usr/bin/basename:
57 // input name() basename
58 // ----- ------ --------
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('/');
75 return substr(i+1, npos);
80 //- Return directory path name (part before last /)
82 // behaviour compared to /usr/bin/dirname:
83 // input path() dirname
84 // ----- ------ -------
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('/');
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) == '/')
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) == '/')
137 return substr(i+1, npos);
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
147 // input components()
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;
162 while ((end = find(delimiter, start)) != npos)
164 // avoid empty element (caused by doubled slashes)
167 wrdList[nWords++] = substr(start, end-start);
169 if (nWords == wrdList.size())
171 wrdList.setSize(2*wrdList.size());
177 // avoid empty trailing element
180 wrdList[nWords++] = substr(start, npos);
183 wrdList.setSize(nWords);
189 // Return a component of the file name
190 Foam::word Foam::fileName::component
192 const size_type cmpt,
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);
227 void Foam::fileName::operator=(const std::string& q)
229 string::operator=(q);
234 void Foam::fileName::operator=(const char* q)
236 string::operator=(q);
241 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
243 Foam::fileName Foam::operator/(const string& a, const string& b)
245 if (a.size() > 0) // First string non-null
247 if (b.size() > 0) // Second string non-null
249 return fileName(a + '/' + b);
251 else // Second string null
256 else // First string null
258 if (b.size() > 0) // Second string non-null
262 else // Second string null
270 // ************************ vim: set sw=4 sts=4 et: ************************ //