| GL Studio C++ Runtime API

file_path_class.h
Go to the documentation of this file.
1/*! \file
2 \brief A class to handle file paths.
3
4 \par Copyright Information
5
6 Copyright (c) 2017 by The DiSTI Corporation.<br>
7 11301 Corporate Blvd; Suite 100<br>
8 Orlando, Florida 32817<br>
9 USA<br>
10 <br>
11 All rights reserved.<br>
12
13 This Software contains proprietary trade secrets of DiSTI and may not be
14 reproduced, in whole or part, in any form, or by any means of electronic,
15 mechanical, or otherwise, without the written permission of DiSTI. Said
16 permission may be derived through the purchase of applicable DiSTI product
17 licenses which detail the distribution rights of this content and any
18 Derivative Works based on this or other copyrighted DiSTI Software.
19
20 NO WARRANTY. THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND,
21 AND ANY USE OF THIS SOFTWARE PRODUCT IS AT YOUR OWN RISK. TO THE MAXIMUM EXTENT
22 PERMITTED BY APPLICABLE LAW, DISTI AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES
23 AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
24 IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY AND/OR FITNESS FOR A
25 PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE SOFTWARE.
26
27 LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW,
28 IN NO EVENT SHALL DISTI OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
29 INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION,
30 DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
31 INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
32 INABILITY TO USE THE SOFTWARE, EVEN IF DISTI HAS BEEN ADVISED OF THE POSSIBILITY
33 OF SUCH DAMAGES. DISTI'S ENTIRE LIABILITY AND YOUR EXCLUSIVE REMEDY SHALL NOT
34 EXCEED FIVE DOLLARS (US$5.00).
35
36 The aforementioned terms and restrictions are governed by the laws of the
37 State of Florida and the United States of America.
38
39*/
40#ifndef INCLUDED_DISTI_FILE_PATH_CLASS_H
41#define INCLUDED_DISTI_FILE_PATH_CLASS_H
42
43#include "disti_metadata.h"
44#include "gls_cpp_lang_support.h"
45#include "util.h" // This is glstudio specific
46#include <fstream>
47#include <string>
48
49 namespace disti
50{
51/** \class FilePathClass
52 *
53 * The FilePathClass represents a single absolute path.
54 * There are methods for getting the directory, filename,
55 * file extension, and checking the validity of the path.
56 * You can get a relative path string by comparing two FilePathClass objects.
57 * FilePathClass also has methods for reading/writing absolute or relative
58 * path strings, the class remembers which was last read and writes out
59 * the same way. You can change this behavior by modifying the
60 * WriteAsAbsolutePath() flag.
61 */
62 class FilePathClass
63{
64 protected:
65 /** The complete path: format is "<directory>/<filename>.<extension>" where /
66 * is last slash and . is the first dot after the last slash */
67 std::string _path;
68
69 /** Controls how the Path is written out when WritePathString is called
70 * Defaults to false */
71 bool _writeAsAbsolutePath;
72
73 public:
74 /** Default construction. Path() is initially "" and invalid */
75 GLS_EXPORT FilePathClass();
76
77 /// Implicit conversion
78 /// \param stringPath The path to set.
79 GLS_EXPORT FilePathClass( const std::string& stringPath );
80
81 /// Implicit conversion
82 /// \param stringPath The path to set.
83 GLS_EXPORT FilePathClass( const char* stringPath );
84
85 /// Copy construction
86 /// \param other The object to copy from.
87 GLS_EXPORT FilePathClass( const FilePathClass& other )
88 {
89 *this = other;
90 }
91
92 /// Copy assignment
93 /// \param other The object to copy from.
94 /// \return The resulting object (this).
95 GLS_EXPORT FilePathClass& operator=( const FilePathClass& other )
96 {
97 if( this != &other )
98 {
99 _path = other._path;
100 _writeAsAbsolutePath = other._writeAsAbsolutePath;
101 }
102 return *this;
103 }
104
105#if defined( DISTI_HAS_RVAL_REFS )
106 /// Move construction
107 /// \param other The object to move from.
108 FilePathClass( FilePathClass&& other )
109 {
110 *this = std::move( other );
111 }
112
113 /// Move assignment
114 /// \param other The object to move from.
115 /// \return The resulting object (this).
116 FilePathClass& operator=( FilePathClass&& other )
117 {
118 if( this != &other )
119 {
120 _path = std::move( other._path );
121 _writeAsAbsolutePath = other._writeAsAbsolutePath;
122 }
123 return *this;
124 }
125#endif
126
127 /// \return True if the path points to a directory or file that COULD exist.
128 /// (i.e. Path() will return a valid path as close as we can verify it.)
129 GLS_EXPORT bool IsValid() const;
130
131 /// \return A string containing the characters that invalid to use in a path.
132 static GLS_EXPORT const std::string& InvalidPathChars();
133
134 /// \return True if this file path references a file that exists.
135 /// \note Applies only to files; returns false for directories.
136 /// Returns true for symbolic links that exist, regardless whether the link's target exists.
137 GLS_EXPORT bool FileExists() const;
138
139 /// \param basePath The path to calculate the relative path from.
140 /// \return A string containing the relative path to this path from the given path's directory.
141 /// \note Returns Path() if the relative path cannot be determined.
142 GLS_EXPORT std::string PathRelativeTo( const FilePathClass& basePath ) const;
143
144 /// \return A string containing the complete path.
145 /// \note This method returns whatever is stored in the path whether it is valid or not.
146 const std::string& Path() const { return _path; }
147
148 /// \return The directory containing the referenced file (includes trailing slash) or "" if the path is not valid.
149 GLS_EXPORT std::string Directory() const;
150
151 /// \return The filename of the file (including the extension) or "" if the path is not valid or doesn't reference a file.
152 GLS_EXPORT std::string FileName() const;
153
154 /// \return The file extension for the file (everything after the last ".") or "" if the path is not valid, doesn't reference a file, or the file has no extension.
155 GLS_EXPORT std::string FileExtension() const;
156
157 /// Replace the current file extension with the specified string.
158 /// This will also insert a '.' into the filename if needed.
159 /// \param ext The new extension (e.g. "txt")
160 GLS_EXPORT void FileExtension( const std::string& ext );
161
162 /// Equality operator
163 /// If paths are equal they reference the same file.
164 /// \param path The path to compare.
165 /// \return Whether or not the paths are equal.
166 GLS_EXPORT bool operator==( const FilePathClass& path ) const;
167
168 /// Inequality operator
169 /// If paths are not equal it doesn't guarantee that they reference different files.
170 /// \param path The path to compare.
171 /// \return Whether or not the paths are not equal.
172 inline bool operator!=( const FilePathClass& path ) const { return !( *this == path ); }
173
174 /// Stream out operator
175 /// \param outstr Stream to write path to.
176 /// \param path Path to write to the stream.
177 /// \return The stream in its current state.
178 friend std::ostream& operator<<( std::ostream& outstr, const FilePathClass& path )
179 {
180 outstr << path.Path();
181 return outstr;
182 }
183
184 /// Stream in operator
185 /// \param instr The stream to read path from.
186 /// \param path Returned path read from the stream.
187 /// \return The stream in its current state.
188 friend std::istream& operator>>( std::istream& instr, FilePathClass& path )
189 {
190 instr >> path._path;
191 return instr;
192 }
193
194 /** Set this object to point to the current working directory
195 * \return true on success, false if there was an error */
196 GLS_EXPORT bool SetToCWD();
197
198 /// \return A file path set to the current working directory.
199 static GLS_EXPORT FilePathClass GetCWD()
200 {
201 FilePathClass path;
202 path.SetToCWD();
203 return path;
204 }
205
206 /// \param strPath The string to check.
207 /// \return true If the given string contains an absolute path.
208 static GLS_EXPORT bool StringIsAbsolutePath( const std::string& strPath );
209
210 /// \param strPath The string to check.
211 /// \return True if the given string contains an relative path.
212 static GLS_EXPORT bool StringIsRelativePath( const std::string& strPath );
213
214 /// \param strPath The string to check.
215 /// \return true If the given string contains a valid or absolute path as close as we can verify it.
216 static GLS_EXPORT bool StringIsValidPath( const std::string& strPath );
217
218 /// Set whether or not paths will be written as absolute paths.
219 /// \param value The new path write value to set.
220 inline void WriteAsAbsolutePath( bool value ) { _writeAsAbsolutePath = value; }
221
222 /// \return Whether or not paths will be written as absolute.
223 inline bool WriteAsAbsolutePath() { return _writeAsAbsolutePath; }
224
225 /** Read a relative or absolute path from a string
226 * \pre None
227 * \post If strPath was an absolute path or invalid then this path will be set to strPath and WriteAsAbsolutePath() will be true
228 * If strPath was relative, this path will be set to basePath + strPath and WriteAsAbsolutePath() will be false
229 * \param basePath Used to build a complete path if strPath contains a relative path
230 * \param strPath The string to read into
231 * \returns true on success or false if there was an error (basePath or strPath are invalid)
232 */
233 GLS_EXPORT bool ReadPathString( const FilePathClass& basePath, const std::string& strPath );
234
235 /** Get the path string to write to a file
236 * This strPath will be either the absolute or relative path depending
237 * on the current setting of the WriteAsAbsolutePath() flag
238 * \param basePath Used to build a relative path if WriteAsAbsolutePath is false
239 * \param strPath The string to write to
240 */
241 GLS_EXPORT void WritePathString( const FilePathClass& basePath, std::string* strPath );
242};
243
244/** Attribute for reading and writing FilePathClass objects as a clear string. */
245 class DistiAttributeFilePathClass : public DistiAttributeBase
246{
247 protected:
248 FilePathClass* _attribPtr; ///< Pointer to the actual storage location.
249 FilePathClass _localStoragePath; ///< Only used by local storage constructor.
250 const FilePathClass& _basePath; ///< Used for reading relative paths.
251 bool _useEmptyTag; ///< true to use _emptyTag when writing path with WriteValue() if path does not have a filename.
252 static GLS_EXPORT const char _emptyTag[]; ///< Backing storage for the string used to represent empty paths.
253
254 public:
255 /// This constructor has the storage external.
256 /// \param basePath
257 /// \param callback
258 /// \param name
259 /// \param attribPtr
260 /// \param useEmptyTag [optional, defaults to false] true to use _emptyTag when writing path with WriteValue()
261 /// if path does not have a filename
262 GLS_EXPORT DistiAttributeFilePathClass( const FilePathClass& basePath, CallbackMethodCallerBase* callback, const AttributeName& name, FilePathClass* attribPtr, bool useEmptyTag = false )
263 : DistiAttributeBase( callback, name, false )
264 , _attribPtr( attribPtr )
265 , _basePath( basePath )
266 , _useEmptyTag( useEmptyTag )
267 {
268 }
269
270 /// Creates local storage, and will resize as needed.
271 /// \param basePath
272 /// \param callback
273 /// \param name
274 /// \param initialValue
275 /// \param useEmptyTag [optional, defaults to false] true to use _emptyTag when writing path with WriteValue()
276 /// if path does not have a filename
277 GLS_EXPORT DistiAttributeFilePathClass( const FilePathClass& basePath, CallbackMethodCallerBase* callback, const AttributeName& name, FilePathClass initialValue, bool useEmptyTag = false )
278 : DistiAttributeBase( callback, name, true )
279 , _attribPtr( &_localStoragePath )
280 , _localStoragePath( initialValue )
281 , _basePath( basePath )
282 , _useEmptyTag( useEmptyTag )
283 {
284 }
285
286 /// \return The string value used to represent an empty path.
287 static const char* GetEmptyTag() { return _emptyTag; }
288
289 virtual GLS_EXPORT DistiAttributeBase& operator=( const DistiAttributeBase& oldClass ) DISTI_METHOD_OVERRIDE
290 {
291 const DistiAttributeFilePathClass* ptr;
292 ptr = dynamic_cast<const DistiAttributeFilePathClass*>( &oldClass );
293 if( ptr )
294 {
295 *_attribPtr = *( ptr->_attribPtr );
296 CallCallback();
297 }
298 else
299 {
300 return DistiAttributeBase::operator=( oldClass );
301 }
302 return *this;
303 }
304
305 virtual GLS_EXPORT std::ostream& WriteValue( std::ostream& outstr ) DISTI_METHOD_OVERRIDE
306 {
307 if( _attribPtr )
308 {
309 std::string strPath;
310
311 if( _useEmptyTag && _attribPtr->FileName().empty() )
312 {
313 strPath = _emptyTag;
314 }
315 else
316 {
317 _attribPtr->WritePathString( _basePath, &strPath );
318 }
319
320 outstr << strPath;
321 }
322
323 return outstr;
324 }
325
326 virtual GLS_EXPORT std::istream& ReadValue( std::istream& instr ) DISTI_METHOD_OVERRIDE
327 {
328 std::string strPath;
329 GetToEnd( instr, strPath, true );
330
331 if( _attribPtr )
332 {
333 // We have to always check for the empty tag
334 // for any backward compatability
335 if( strPath == _emptyTag )
336 {
337 strPath = "";
338 }
339
340 _attribPtr->ReadPathString( _basePath, strPath );
341 CallCallback();
342 }
343
344 return instr;
345 }
346
347 GLS_EXPORT std::string Type() const DISTI_METHOD_OVERRIDE
348 {
349 return Demangle<FilePathClass>();
350 }
351};
352
353/** This is a FilePathClass attribute that will save/read the path relative to the current working directory. */
354 class DistiAttributeCWDRelativePath : public DistiAttributeFilePathClass
355{
356 FilePathClass _parentDocumentPath; ///< The parent document path, this is updated when we read and write.
357 public:
358 /// This constructor has the storage external.
359 /// \param callback
360 /// \param name
361 /// \param attribPtr
362 /// \param useEmptyTag [optional, defaults to false] true to use _emptyTag when writing path with WriteValue()
363 /// if path does not have a filename
364 GLS_EXPORT DistiAttributeCWDRelativePath( CallbackMethodCallerBase* callback, const AttributeName& name, FilePathClass* attribPtr, bool useEmptyTag = false )
365 : DistiAttributeFilePathClass( _parentDocumentPath, callback, name, attribPtr, useEmptyTag )
366 {
367 }
368
369 /// Creates local storage, and will resize as needed.
370 /// \param callback
371 /// \param name
372 /// \param initialValue
373 /// \param useEmptyTag [optional, defaults to false] true to use _emptyTag when writing path with WriteValue()
374 /// if path does not have a filename
375 GLS_EXPORT DistiAttributeCWDRelativePath( CallbackMethodCallerBase* callback, const AttributeName& name, FilePathClass initialValue, bool useEmptyTag = false )
376 : DistiAttributeFilePathClass( _parentDocumentPath, callback, name, initialValue, useEmptyTag )
377 {
378 }
379
380 virtual GLS_EXPORT std::ostream& WriteValue( std::ostream& outstr ) DISTI_METHOD_OVERRIDE
381 {
382 // update base path
383 _parentDocumentPath.SetToCWD();
384
385 // write value as normal
386 return DistiAttributeFilePathClass::WriteValue( outstr );
387 }
388
389 virtual GLS_EXPORT std::istream& ReadValue( std::istream& instr ) DISTI_METHOD_OVERRIDE
390 {
391 // update base path
392 _parentDocumentPath.SetToCWD();
393
394 // read value as normal
395 return DistiAttributeFilePathClass::ReadValue( instr );
396 }
397
398 GLS_EXPORT std::string Type() const DISTI_METHOD_OVERRIDE
399 {
400 return Demangle<FilePathClass>();
401 }
402};
403
404} // end namespace disti
405
406#endif //_PATH_CLASS_H
disti::AttributeName
Definition: disti_metadata.h:87
disti::CallbackMethodCallerBase
Definition: callback_caller_base.h:56
disti::DistiAttributeBase
Definition: disti_metadata.h:220
disti::DistiAttributeBase::CallCallback
virtual void CallCallback()
Calls callback CallType3 if it has been set.
disti::DistiAttributeBase::operator=
virtual DistiAttributeBase & operator=(const DistiAttributeBase &oldClass)
disti::DistiAttributeCWDRelativePath
Definition: file_path_class.h:355
disti::DistiAttributeCWDRelativePath::Type
std::string Type() const override
Definition: file_path_class.h:398
disti::DistiAttributeCWDRelativePath::ReadValue
virtual std::istream & ReadValue(std::istream &instr) override
Definition: file_path_class.h:389
disti::DistiAttributeCWDRelativePath::DistiAttributeCWDRelativePath
DistiAttributeCWDRelativePath(CallbackMethodCallerBase *callback, const AttributeName &name, FilePathClass *attribPtr, bool useEmptyTag=false)
Definition: file_path_class.h:364
disti::DistiAttributeCWDRelativePath::DistiAttributeCWDRelativePath
DistiAttributeCWDRelativePath(CallbackMethodCallerBase *callback, const AttributeName &name, FilePathClass initialValue, bool useEmptyTag=false)
Definition: file_path_class.h:375
disti::DistiAttributeCWDRelativePath::WriteValue
virtual std::ostream & WriteValue(std::ostream &outstr) override
Definition: file_path_class.h:380
disti::DistiAttributeFilePathClass
Definition: file_path_class.h:246
disti::DistiAttributeFilePathClass::GetEmptyTag
static const char * GetEmptyTag()
Definition: file_path_class.h:287
disti::DistiAttributeFilePathClass::DistiAttributeFilePathClass
DistiAttributeFilePathClass(const FilePathClass &basePath, CallbackMethodCallerBase *callback, const AttributeName &name, FilePathClass initialValue, bool useEmptyTag=false)
Definition: file_path_class.h:277
disti::DistiAttributeFilePathClass::_emptyTag
static const char _emptyTag[]
Backing storage for the string used to represent empty paths.
Definition: file_path_class.h:252
disti::DistiAttributeFilePathClass::_attribPtr
FilePathClass * _attribPtr
Pointer to the actual storage location.
Definition: file_path_class.h:248
disti::DistiAttributeFilePathClass::Type
std::string Type() const override
Definition: file_path_class.h:347
disti::DistiAttributeFilePathClass::DistiAttributeFilePathClass
DistiAttributeFilePathClass(const FilePathClass &basePath, CallbackMethodCallerBase *callback, const AttributeName &name, FilePathClass *attribPtr, bool useEmptyTag=false)
Definition: file_path_class.h:262
disti::DistiAttributeFilePathClass::_basePath
const FilePathClass & _basePath
Used for reading relative paths.
Definition: file_path_class.h:250
disti::DistiAttributeFilePathClass::ReadValue
virtual std::istream & ReadValue(std::istream &instr) override
Definition: file_path_class.h:326
disti::DistiAttributeFilePathClass::operator=
virtual DistiAttributeBase & operator=(const DistiAttributeBase &oldClass) override
Definition: file_path_class.h:289
disti::DistiAttributeFilePathClass::_useEmptyTag
bool _useEmptyTag
true to use _emptyTag when writing path with WriteValue() if path does not have a filename.
Definition: file_path_class.h:251
disti::DistiAttributeFilePathClass::_localStoragePath
FilePathClass _localStoragePath
Only used by local storage constructor.
Definition: file_path_class.h:249
disti::DistiAttributeFilePathClass::WriteValue
virtual std::ostream & WriteValue(std::ostream &outstr) override
Definition: file_path_class.h:305
disti::FilePathClass
Definition: file_path_class.h:63
disti::FilePathClass::InvalidPathChars
static const std::string & InvalidPathChars()
disti::FilePathClass::WriteAsAbsolutePath
void WriteAsAbsolutePath(bool value)
Definition: file_path_class.h:220
disti::FilePathClass::_path
std::string _path
Definition: file_path_class.h:67
disti::FilePathClass::operator=
FilePathClass & operator=(const FilePathClass &other)
Definition: file_path_class.h:95
disti::FilePathClass::StringIsRelativePath
static bool StringIsRelativePath(const std::string &strPath)
disti::FilePathClass::Directory
std::string Directory() const
disti::FilePathClass::StringIsAbsolutePath
static bool StringIsAbsolutePath(const std::string &strPath)
disti::FilePathClass::FilePathClass
FilePathClass()
disti::FilePathClass::FileName
std::string FileName() const
disti::FilePathClass::_writeAsAbsolutePath
bool _writeAsAbsolutePath
Definition: file_path_class.h:71
disti::FilePathClass::operator==
bool operator==(const FilePathClass &path) const
disti::FilePathClass::FileExtension
void FileExtension(const std::string &ext)
disti::FilePathClass::PathRelativeTo
std::string PathRelativeTo(const FilePathClass &basePath) const
disti::FilePathClass::FileExists
bool FileExists() const
disti::FilePathClass::GetCWD
static FilePathClass GetCWD()
Definition: file_path_class.h:199
disti::FilePathClass::FilePathClass
FilePathClass(const std::string &stringPath)
disti::FilePathClass::operator=
FilePathClass & operator=(FilePathClass &&other)
Definition: file_path_class.h:116
disti::FilePathClass::operator>>
friend std::istream & operator>>(std::istream &instr, FilePathClass &path)
Definition: file_path_class.h:188
disti::FilePathClass::operator!=
bool operator!=(const FilePathClass &path) const
Definition: file_path_class.h:172
disti::FilePathClass::FilePathClass
FilePathClass(FilePathClass &&other)
Definition: file_path_class.h:108
disti::FilePathClass::StringIsValidPath
static bool StringIsValidPath(const std::string &strPath)
disti::FilePathClass::ReadPathString
bool ReadPathString(const FilePathClass &basePath, const std::string &strPath)
disti::FilePathClass::IsValid
bool IsValid() const
disti::FilePathClass::WritePathString
void WritePathString(const FilePathClass &basePath, std::string *strPath)
disti::FilePathClass::Path
const std::string & Path() const
Definition: file_path_class.h:146
disti::FilePathClass::WriteAsAbsolutePath
bool WriteAsAbsolutePath()
Definition: file_path_class.h:223
disti::FilePathClass::FilePathClass
FilePathClass(const FilePathClass &other)
Definition: file_path_class.h:87
disti::FilePathClass::SetToCWD
bool SetToCWD()
disti::FilePathClass::FilePathClass
FilePathClass(const char *stringPath)
disti::FilePathClass::FileExtension
std::string FileExtension() const
disti::FilePathClass::operator<<
friend std::ostream & operator<<(std::ostream &outstr, const FilePathClass &path)
Definition: file_path_class.h:178
disti_metadata.h
The disti metadata.
gls_cpp_lang_support.h
Macros and helper code to determine what subset of C++11/14/17 is available.
DISTI_METHOD_OVERRIDE
#define DISTI_METHOD_OVERRIDE
Macro to wrap the override keyword, removed on compilers that don't support it.
Definition: gls_cpp_lang_support.h:222
GLS_EXPORT
#define GLS_EXPORT
Macro denoting which functions should be visible from the runtime library.
Definition: gls_include.h:52
disti
Definition: bmpimage.h:47
disti::GetToEnd
bool GetToEnd(std::istream &instr, std::string &result, bool decode)
util.h
Generally useful defines, macros, enumerations and function prototypes.