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
14reproduced, in whole or part, in any form, or by any means of electronic,
15mechanical, or otherwise, without the written permission of DiSTI. Said
16permission may be derived through the purchase of applicable DiSTI product
17licenses which detail the distribution rights of this content and any
18Derivative Works based on this or other copyrighted DiSTI Software.
19
20 NO WARRANTY. THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND,
21AND ANY USE OF THIS SOFTWARE PRODUCT IS AT YOUR OWN RISK. TO THE MAXIMUM EXTENT
22PERMITTED BY APPLICABLE LAW, DISTI AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES
23AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
24IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY AND/OR FITNESS FOR A
25PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE SOFTWARE.
26
27 LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW,
28IN NO EVENT SHALL DISTI OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
29INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION,
30DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
31INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
32INABILITY TO USE THE SOFTWARE, EVEN IF DISTI HAS BEEN ADVISED OF THE POSSIBILITY
33OF SUCH DAMAGES. DISTI'S ENTIRE LIABILITY AND YOUR EXCLUSIVE REMEDY SHALL NOT
34EXCEED FIVE DOLLARS (US$5.00).
35
36 The aforementioned terms and restrictions are governed by the laws of the
37State 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"
45#include "util.h" // This is glstudio specific
46#include <fstream>
47#include <string>
48
49namespace 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 */
63{
64protected:
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 */
72
73public:
74 /** Default construction. Path() is initially "" and invalid */
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.
88 {
89 *this = other;
90 }
91
92 /// Copy assignment
93 /// \param other The object to copy from.
94 /// \return The resulting object (this).
96 {
97 if( this != &other )
98 {
99 _path = other._path;
101 }
102 return *this;
103 }
104
105#if defined( DISTI_HAS_RVAL_REFS )
106 /// Move construction
107 /// \param other The object to move from.
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).
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 */
197
198 /// \return A file path set to the current working directory.
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.
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. */
246{
247protected:
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
254public:
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 )
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
290 {
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 {
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
341 CallCallback();
342 }
343
344 return instr;
345 }
346};
347
348/** This is a FilePathClass attribute that will save/read the path relative to the current working directory. */
350{
351 FilePathClass _parentDocumentPath; ///< The parent document path, this is updated when we read and write.
352public:
353 /// This constructor has the storage external.
354 /// \param callback
355 /// \param name
356 /// \param attribPtr
357 /// \param useEmptyTag [optional, defaults to false] true to use _emptyTag when writing path with WriteValue()
358 /// if path does not have a filename
359 GLS_EXPORT DistiAttributeCWDRelativePath( CallbackMethodCallerBase* callback, const AttributeName& name, FilePathClass* attribPtr, bool useEmptyTag = false )
360 : DistiAttributeFilePathClass( _parentDocumentPath, callback, name, attribPtr, useEmptyTag )
361 {
362 }
363
364 /// Creates local storage, and will resize as needed.
365 /// \param callback
366 /// \param name
367 /// \param initialValue
368 /// \param useEmptyTag [optional, defaults to false] true to use _emptyTag when writing path with WriteValue()
369 /// if path does not have a filename
370 GLS_EXPORT DistiAttributeCWDRelativePath( CallbackMethodCallerBase* callback, const AttributeName& name, FilePathClass initialValue, bool useEmptyTag = false )
371 : DistiAttributeFilePathClass( _parentDocumentPath, callback, name, initialValue, useEmptyTag )
372 {
373 }
374
375 virtual GLS_EXPORT std::ostream& WriteValue( std::ostream& outstr ) DISTI_METHOD_OVERRIDE
376 {
377 // update base path
378 _parentDocumentPath.SetToCWD();
379
380 // write value as normal
382 }
383
384 virtual GLS_EXPORT std::istream& ReadValue( std::istream& instr ) DISTI_METHOD_OVERRIDE
385 {
386 // update base path
387 _parentDocumentPath.SetToCWD();
388
389 // read value as normal
391 }
392};
393
394} // end namespace disti
395
396#endif //_PATH_CLASS_H
Definition: disti_metadata.h:87
Definition: callback_caller_base.h:56
Definition: disti_metadata.h:220
virtual void CallCallback()
Calls callback CallType3 if it has been set.
virtual DistiAttributeBase & operator=(const DistiAttributeBase &oldClass)
Definition: file_path_class.h:350
virtual std::istream & ReadValue(std::istream &instr) override
Definition: file_path_class.h:384
DistiAttributeCWDRelativePath(CallbackMethodCallerBase *callback, const AttributeName &name, FilePathClass *attribPtr, bool useEmptyTag=false)
Definition: file_path_class.h:359
DistiAttributeCWDRelativePath(CallbackMethodCallerBase *callback, const AttributeName &name, FilePathClass initialValue, bool useEmptyTag=false)
Definition: file_path_class.h:370
virtual std::ostream & WriteValue(std::ostream &outstr) override
Definition: file_path_class.h:375
Definition: file_path_class.h:246
static const char * GetEmptyTag()
Definition: file_path_class.h:287
DistiAttributeFilePathClass(const FilePathClass &basePath, CallbackMethodCallerBase *callback, const AttributeName &name, FilePathClass initialValue, bool useEmptyTag=false)
Definition: file_path_class.h:277
static const char _emptyTag[]
Backing storage for the string used to represent empty paths.
Definition: file_path_class.h:252
FilePathClass * _attribPtr
Pointer to the actual storage location.
Definition: file_path_class.h:248
DistiAttributeFilePathClass(const FilePathClass &basePath, CallbackMethodCallerBase *callback, const AttributeName &name, FilePathClass *attribPtr, bool useEmptyTag=false)
Definition: file_path_class.h:262
const FilePathClass & _basePath
Used for reading relative paths.
Definition: file_path_class.h:250
virtual std::istream & ReadValue(std::istream &instr) override
Definition: file_path_class.h:326
virtual DistiAttributeBase & operator=(const DistiAttributeBase &oldClass) override
Definition: file_path_class.h:289
bool _useEmptyTag
true to use _emptyTag when writing path with WriteValue() if path does not have a filename.
Definition: file_path_class.h:251
FilePathClass _localStoragePath
Only used by local storage constructor.
Definition: file_path_class.h:249
virtual std::ostream & WriteValue(std::ostream &outstr) override
Definition: file_path_class.h:305
Definition: file_path_class.h:63
static const std::string & InvalidPathChars()
void WriteAsAbsolutePath(bool value)
Definition: file_path_class.h:220
std::string _path
Definition: file_path_class.h:67
FilePathClass & operator=(const FilePathClass &other)
Definition: file_path_class.h:95
static bool StringIsRelativePath(const std::string &strPath)
std::string Directory() const
static bool StringIsAbsolutePath(const std::string &strPath)
std::string FileName() const
bool _writeAsAbsolutePath
Definition: file_path_class.h:71
bool operator==(const FilePathClass &path) const
void FileExtension(const std::string &ext)
std::string PathRelativeTo(const FilePathClass &basePath) const
bool FileExists() const
static FilePathClass GetCWD()
Definition: file_path_class.h:199
FilePathClass(const std::string &stringPath)
FilePathClass & operator=(FilePathClass &&other)
Definition: file_path_class.h:116
friend std::istream & operator>>(std::istream &instr, FilePathClass &path)
Definition: file_path_class.h:188
bool operator!=(const FilePathClass &path) const
Definition: file_path_class.h:172
FilePathClass(FilePathClass &&other)
Definition: file_path_class.h:108
static bool StringIsValidPath(const std::string &strPath)
bool ReadPathString(const FilePathClass &basePath, const std::string &strPath)
bool IsValid() const
void WritePathString(const FilePathClass &basePath, std::string *strPath)
const std::string & Path() const
Definition: file_path_class.h:146
bool WriteAsAbsolutePath()
Definition: file_path_class.h:223
FilePathClass(const FilePathClass &other)
Definition: file_path_class.h:87
FilePathClass(const char *stringPath)
std::string FileExtension() const
friend std::ostream & operator<<(std::ostream &outstr, const FilePathClass &path)
Definition: file_path_class.h:178
The disti metadata.
Macros and helper code to determine what subset of C++11/14/17 is available.
#define DISTI_METHOD_OVERRIDE
Macro to wrap the override keyword, removed on compilers that don't support it.
Definition: gls_cpp_lang_support.h:214
#define GLS_EXPORT
Macro denoting which functions should be visible from the runtime library.
Definition: gls_include.h:52
Force inclusion of the DirectShow library.
Definition: bmpimage.h:47
bool GetToEnd(std::istream &instr, std::string &result, bool decode)
Generally useful defines, macros, enumerations and function prototypes.