Library of Assembled Shared Sources
file_attribute.cpp
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2024 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#include "lass_common.h"
44#include "file_attribute.h"
47
48#include <stdio.h>
49
50#if LASS_PLATFORM_TYPE == LASS_PLATFORM_TYPE_WIN32
51# pragma warning(disable: 4996) // 'fopen': This function or variable may be unsafe
52#endif
53
54namespace lass
55{
56namespace io
57{
58
59// --- public --------------------------------------------------------------------------------------
60
61
62
63// --- protected -----------------------------------------------------------------------------------
64
65
66
67// --- private -------------------------------------------------------------------------------------
68
69
70
71// --- free ----------------------------------------------------------------------------------------
72
73/** @ingroup FileAttribute
74 * return true if file exists
75 */
76bool fileDoesExist(const std::string& fileName)
77{
78#if LASS_HAVE_WCHAR_SUPPORT && LASS_HAVE_WFOPEN
79 return fileDoesExist(util::utf8ToWchar(fileName));
80#else
81 FILE* file = fopen(fileName.c_str(), "r");
82 if (file == 0)
83 {
84 return false;
85 }
86 fclose(file);
87 return true;
88#endif
89}
90
91
92#if LASS_HAVE_WCHAR_SUPPORT
93
94/** @ingroup FileAttribute
95 * return true if file exists
96 */
97bool fileDoesExist(const std::wstring& fileName)
98{
99#if LASS_HAVE_WFOPEN
100 FILE* file = _wfopen(fileName.c_str(), L"r");
101 if (file == 0)
102 {
103 return false;
104 }
105 fclose(file);
106 return true;
107#else
108 return fileDoesExist(util::wcharToUtf8(fileName));
109#endif
110}
111
112#endif
113
114
115/** @ingroup FileAttribute
116 * return the part of the file name behind the last dot.
117 * return an empty string if there's no dot.
118 * e.g. @e foo returns an empty string, @e foo.bar returns @e bar,
119 * @e foo.bar.fun returns @e fun.
120 */
121std::string fileExtension(const std::string& fileName)
122{
123 std::string::size_type lastDot = fileName.rfind(extensionSeperator);
124 return lastDot!= std::string::npos ? fileName.substr(lastDot + 1) : "";
125}
126
127
128#if LASS_HAVE_WCHAR_SUPPORT
129
130/** @ingroup FileAttribute
131 * return the part of the file name behind the last dot.
132 * return an empty string if there's no dot.
133 * e.g. @e foo returns an empty string, @e foo.bar returns @e bar,
134 * @e foo.bar.fun returns @e fun.
135 */
136std::wstring fileExtension(const std::wstring& fileName)
137{
138 std::wstring::size_type lastDot = fileName.rfind(wextensionSeperator);
139 return lastDot!= std::wstring::npos ? fileName.substr(lastDot + 1) : L"";
140}
141
142#endif
143
144
145/** @ingroup FileAttribute
146 * return the part of the file name before the last dot.
147 * return an original string if there's no dot.
148 * e.g. @e foo returns @e foo, @e foo.bar returns @e foo,
149 * @e foo.bar.fun returns @e foo.bar .
150 */
151std::string fileWithoutExtension(const std::string& fileName)
152{
153 return fileName.substr(0, fileName.rfind(extensionSeperator));
154}
155
156
157#if LASS_HAVE_WCHAR_SUPPORT
158
159/** @ingroup FileAttribute
160 * return the part of the file name before the last dot.
161 * return an original string if there's no dot.
162 * e.g. @e foo returns @e foo, @e foo.bar returns @e foo,
163 * @e foo.bar.fun returns @e foo.bar .
164 */
165std::wstring fileWithoutExtension(const std::wstring& fileName)
166{
167 return fileName.substr(0, fileName.rfind(wextensionSeperator));
168}
169
170#endif
171
172
173/** @ingroup FileAttribute
174 * return the part of the file name before the last (back)slash.
175 * return an empty string if there's no (back)slash.
176 * e.g. @e foo returns an empty string, @e foo/bar returns @e foo,
177 * @e foo/bar/fun returns @e foo/bar.
178 */
179std::string filePath(const std::string& fileName)
180{
181 const char seperators[] = { pathSeperator, pathAlternativeSeperator, '\0' };
182 std::string::size_type lastSlash = fileName.find_last_of(seperators);
183 return lastSlash != std::string::npos ? fileName.substr(0, lastSlash) : "";
184}
185
186
187#if LASS_HAVE_WCHAR_SUPPORT
188
189/** @ingroup FileAttribute
190 * return the part of the file name before the last (back)slash.
191 * return an empty string if there's no (back)slash.
192 * e.g. @e foo returns an empty string, @e foo/bar returns @e foo,
193 * @e foo/bar/fun returns @e foo/bar.
194 */
195std::wstring filePath(const std::wstring& fileName)
196{
197 const wchar_t seperators[] = { wpathSeperator, wpathAlternativeSeperator, L'\0' };
198 std::wstring::size_type lastSlash = fileName.find_last_of(seperators);
199 return lastSlash != std::wstring::npos ? fileName.substr(0, lastSlash) : L"";
200}
201
202#endif
203
204
205/** @ingroup FileAttribute
206 * return the part of the file name behind the last (back)slash.
207 * return the original string if there's no (back)slash.
208 * e.g. @e foo returns @e foo, @e foo/bar returns @e bar,
209 * @e foo/bar/fun returns @e fun .
210 */
211std::string fileWithoutPath(const std::string& fileName)
212{
213 const char seperators[] = { pathSeperator, pathAlternativeSeperator, '\0' };
214 std::string::size_type lastSlash = fileName.find_last_of(seperators);
215 return lastSlash != std::string::npos ? fileName.substr(lastSlash + 1) : fileName;
216}
217
218
219#if LASS_HAVE_WCHAR_SUPPORT
220
221/** @ingroup FileAttribute
222 * return the part of the file name behind the last (back)slash.
223 * return the original string if there's no (back)slash.
224 * e.g. @e foo returns @e foo, @e foo/bar returns @e bar,
225 * @e foo/bar/fun returns @e fun .
226 */
227std::wstring fileWithoutPath(const std::wstring& fileName)
228{
229 const wchar_t seperators[] = { wpathSeperator, wpathAlternativeSeperator, L'\0' };
230 std::wstring::size_type lastSlash = fileName.find_last_of(seperators);
231 return lastSlash != std::wstring::npos ? fileName.substr(lastSlash + 1) : fileName;
232}
233
234#endif
235
236
237/** @ingroup FileAttribute
238 */
239std::string fileJoinExtension(const std::string& fileName, const std::string& extension)
240{
241 if (stde::begins_with(extension, extensionSeperator))
242 {
243 return fileName + extension;
244 }
245 return fileName + extensionSeperator + extension;
246}
247
248
249#if LASS_HAVE_WCHAR_SUPPORT
250
251/** @ingroup FileAttribute
252 */
253std::wstring fileJoinExtension(const std::wstring& fileName, const std::wstring& extension)
254{
255 if (stde::begins_with(extension, wextensionSeperator))
256 {
257 return fileName + extension;
258 }
259 return fileName + wextensionSeperator + extension;
260}
261
262#endif
263
264
265/** @ingroup FileAttribute
266 */
267std::string fileJoinPath(const std::string& path, const std::string& fileName)
268{
269 if (stde::ends_with(path, pathSeperator))
270 {
271 return path + fileName;
272 }
273 if (pathAlternativeSeperator && stde::ends_with(path, pathAlternativeSeperator))
274 {
275 return path + fileName;
276 }
277 return path + pathSeperator + fileName;
278}
279
280
281#if LASS_HAVE_WCHAR_SUPPORT
282
283/** @ingroup FileAttribute
284 */
285std::wstring fileJoinPath(const std::wstring& path, const std::wstring& fileName)
286{
287 if (stde::ends_with(path, wpathSeperator))
288 {
289 return path + fileName;
290 }
291 if (wpathAlternativeSeperator && stde::ends_with(path, wpathAlternativeSeperator))
292 {
293 return path + fileName;
294 }
295 return path + wpathSeperator + fileName;
296}
297
298#endif
299
300
301}
302
303}
304
305// EOF
bool fileDoesExist(const std::string &fileName)
return true if file exists
std::string fileWithoutPath(const std::string &fileName)
return the part of the file name behind the last (back)slash.
std::string filePath(const std::string &fileName)
return the part of the file name before the last (back)slash.
std::string fileExtension(const std::string &fileName)
return the part of the file name behind the last dot.
std::string fileWithoutExtension(const std::string &fileName)
return the part of the file name before the last dot.
bool begins_with(const std::basic_string< Char, Traits, Alloc > &input, const std::basic_string< Char, Traits, Alloc > &prefix)
returns true if input begins with the input prefix
bool ends_with(const std::basic_string< Char, Traits, Alloc > &input, const std::basic_string< Char, Traits, Alloc > &suffix)
returns true if input ends with the input suffix
streams, binary streams, vrmlstreams, ...
Library for Assembled Shared Sources.
Definition config.h:53