TrueReality  v0.1.1912
FileUtils.h
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * The Base of this class has been adopted from the Delta3D engine
6 *
7 * This library is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the Free
9 * Software Foundation; either version 3.0 of the License, or (at your option)
10 * any later version.
11 *
12 * This library is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15 * details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Class Inspired by the Delta3D Engine
22 * http://delta3dengine.org/
23 *
24 * @author David Guthrie
25 * @author Maxim Serebrennik
26 */
27 #pragma once
28 
29 #include <trUtil/Export.h>
30 
31 #include <trUtil/Exception.h>
32 #include <trUtil/UnaryFunction.h>
33 
34 #include <osg/Referenced>
35 #include <osg/Version>
36 #include <osgDB/ReaderWriter>
37 #include <osgDB/FileUtils>
38 
39 #include <vector>
40 #include <string>
41 
43 #if OPENSCENEGRAPH_MAJOR_VERSION < 3
44 namespace osgDB
45 {
46  class ArchiveExtended;
47 }
48 #else
49 namespace osgDB
50 {
51  using ArchiveExtended = Archive;
52 }
53 #endif
54 
56 namespace trUtil
57 {
58  class Log;
59 
60  using DirectoryContents = std::vector<std::string>;
61  using FileExtensionList = std::vector<std::string>;
62 
68  enum FileType
69  {
74  };
75 
81  struct FileInfo
82  {
83  std::string fileName;
84  std::string path;
85  std::string baseName;
86  std::string extensionlessFileName;
87  std::string extension;
88  size_t size;
89  time_t lastModified;
91  bool isInArchive;
92 
93  FileInfo() : size(0), lastModified(0), fileType(FILE_NOT_FOUND), isInArchive(false) {}
94  };
95 
102  {
103  public:
104  FileUtilIOException(const std::string& message, const std::string& filename, unsigned int linenum);
105  virtual ~FileUtilIOException() {};
106  };
107 
114  {
115  public:
116  FileNotFoundException(const std::string& message, const std::string& filename, unsigned int linenum);
118  };
119 
125  class TR_UTIL_EXPORT FileUtils : public osg::Referenced
126  {
127  public:
128  static const char PATH_SEPARATOR;
129 
138  {
139  if (mInstance == NULL)
140  {
141  mInstance = new FileUtils();
142  }
143  return *mInstance;
144  }
145 
155  std::string RunCommand(const char* cmd);
156 
166  std::string RunCommand(const std::string cmd);
167 
179  bool FileExists(const std::string& strFile, bool caseInsensitive = false) const;
180 
195  void FileCopy(const std::string& strSrc, const std::string& strDest, bool bOverwrite) const;
196 
212  void FileMove(const std::string& strSrc, const std::string& strDest, bool bOverwrite) const;
213 
223  void FileDelete(const std::string& strFile) const;
224 
239  const struct FileInfo GetFileInfo(const std::string& strFile, bool caseInsensitive = false) const;
240 
251  void CleanupFileString(std::string &strFileOrDir) const;
252 
263  bool IsAbsolutePath(std::string strFileOrDir) const;
264 
279  void MakeDirectoryEX(std::string strDir);
280 
296  void ChangeDirectory(const std::string& path);
297 
305  const std::string& CurrentDirectory() const;
306 
320  void PushDirectory(const std::string& path);
321 
331  void PopDirectory();
332 
346  std::string GetAbsolutePath(const std::string& relativePath, bool removeFinalFile = false) const;
347 
362  DirectoryContents DirGetFiles(const std::string& path, const FileExtensionList& extensions = FileExtensionList()) const;
363 
375  DirectoryContents DirGetSubs(const std::string& path) const;
376 
398  void DirCopy(const std::string& srcPath, const std::string& destPath, bool bOverwrite, bool copyContentsOnly = false) const;
399 
414  bool DirDelete(const std::string& strDir, bool bRecursive);
415 
426  void MakeDirectory(const std::string& strDir) const;
427 
439  bool DirExists(const std::string& strDir, bool caseInsensitive = false) const;
440 
452  std::string RelativePath(const std::string& absolutePath, const std::string& file) const;
453 
462  //void AbsoluteToRelative(const std::string &absPath, std::string& relPath);
463 
480  bool IsSameFile(const std::string& file1, const std::string& file2) const;
481 
492  static std::string ConcatPaths(const std::string& left, const std::string& right);
493 
504  osg::Object* ReadObject(const std::string& filename, osgDB::ReaderWriter::Options* options = NULL);
505 
516  osg::Node* ReadNode(const std::string& filename, osgDB::ReaderWriter::Options* options = NULL);
517 
527  osgDB::ArchiveExtended* FindArchive(const std::string& archiveFileName) const;
528 
529  private:
530 
532  static const int PATH_BUFFER_SIZE = 1024;
533 
539  FileUtils();
540 
546  virtual ~FileUtils();
547 
555  void ChangeDirectoryInternal(const std::string& path);
556 
566  void InternalDirCopy(const std::string& srcPath, const std::string& destPath, bool bOverwrite) const;
567 
575  void RecursDeleteDir(bool bRecursive);
576 
590  bool SplitArchiveFilename(const std::string& fullFilename, std::string& archiveFilename, std::string& fileInArchive) const;
591 
601  void DirGetFilesInArchive(const osgDB::ArchiveExtended& a, const std::string& path, DirectoryContents& result) const;
602 
613  FileType GetFileTypeForFileInArchive(const osgDB::ArchiveExtended& a, const std::string& path) const;
614 
625  FileInfo GetFileInfoForFileInArchive(const osgDB::ArchiveExtended& a, const std::string& path) const;
626 
636  FileType GetFileTypeFromOSGDBFileType(osgDB::FileType ft) const;
637 
647  bool ContainsArchiveExtension(const std::string& path) const;
648 
658  std::string ArchiveRelativeToAbsolute(const std::string& relativeFile) const;
659 
665  const struct FileInfo GetFileInfo_Internal(const std::string& strFile, bool caseInsensitive) const;
666 
677  bool IsSameFile_Internal(const std::string& file1, const std::string& file2) const;
678 
679 
680  static osg::ref_ptr<FileUtils> mInstance;
681 
683 
684  std::string mCurrentDirectory;
685  std::vector<std::string> mStackOfDirectories;
686 
687  };
688 
696  {
697  public:
698 
706  DirectoryPush(const std::string& dir);
707 
713  ~DirectoryPush();
714 
722  bool GetSucceeded();
723 
731  const std::string& GetError();
732  private:
734  std::string mError;
735  };
736 
742  class IsPathSeparator : public trUtil::UnaryFunction<char, bool>
743  {
744  public:
745  bool operator()(char c) const { return c == '/' || c == '\\' || c == FileUtils::PATH_SEPARATOR; }
746  };
747 }
Log class which the engine uses for all of its logging needs.
Definition: Log.h:197
Exception for signaling file utility i/o errors.
Definition: FileUtils.h:101
A unary function created to have a compatibility to the old std::unary_function while still using c++...
Definition: UnaryFunction.h:41
This is the exception class used throughout the engine.
Definition: Exception.h:48
Singleton class implementing basic file operations.
Definition: FileUtils.h:125
bool operator()(char c) const
Definition: FileUtils.h:745
std::string extensionlessFileName
the file name with extension
Definition: FileUtils.h:86
struct holding information about a file.
Definition: FileUtils.h:81
std::vector< std::string > DirectoryContents
Definition: FileUtils.h:60
std::vector< std::string > FileExtensionList
Definition: FileUtils.h:61
For tokenizing paths.
Definition: FileUtils.h:742
Exception for signalling file not found errors.
Definition: FileUtils.h:113
FileType
Values that represent file types.
Definition: FileUtils.h:68
time_t lastModified
when the file was last modified.
Definition: FileUtils.h:89
size_t size
the size of the file in bytes.
Definition: FileUtils.h:88
FileType fileType
The enum value specifying the type of file.
Definition: FileUtils.h:90
A class that represents date time utility.
std::string path
the path to the file
Definition: FileUtils.h:84
static FileUtils & GetInstance()
Character separating the parts of a file path.
Definition: FileUtils.h:137
bool isInArchive
true if the specified file lives within an archive
Definition: FileUtils.h:91
Namespace that holds various utility classes for the engine.
Definition: SmrtPtr.h:208
std::string fileName
the file name with full path
Definition: FileUtils.h:83
std::string mError
Definition: FileUtils.h:734
std::string extension
the file extension
Definition: FileUtils.h:87
static const char PATH_SEPARATOR
Definition: FileUtils.h:128
Simple class to change directory where it automatically pop back out on destruction.
Definition: FileUtils.h:695
std::string baseName
the file name with extension
Definition: FileUtils.h:85