FileUtils 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSGDB_FILEUTILS
  14. #define OSGDB_FILEUTILS 1
  15. #include <osgDB/Registry>
  16. #include <vector>
  17. #include <deque>
  18. #include <string>
  19. #include <stdio.h>
  20. namespace osgDB {
  21. /** Overload of the standard fopen function. If OSG_USE_UTF8_FILENAME is defined,
  22. * filename will be expanded from UTF8 to UTF16 and _wfopen will be called. */
  23. extern OSGDB_EXPORT FILE* fopen(const char* filename, const char* mode);
  24. /** Make a new directory. Returns true if directory exists or was created. */
  25. extern OSGDB_EXPORT bool makeDirectory( const std::string &directoryPath );
  26. /** Make a new directory for a given file. */
  27. extern OSGDB_EXPORT bool makeDirectoryForFile( const std::string &filePath );
  28. /** Get current working directory. */
  29. extern OSGDB_EXPORT std::string getCurrentWorkingDirectory( void );
  30. /** Set current working directory. */
  31. extern OSGDB_EXPORT bool setCurrentWorkingDirectory( const std::string &newCurrentWorkingDirectory );
  32. /** return true if a file exists. */
  33. extern OSGDB_EXPORT bool fileExists(const std::string& filename);
  34. enum FileType
  35. {
  36. FILE_NOT_FOUND,
  37. REGULAR_FILE,
  38. DIRECTORY
  39. };
  40. /** return type of file. */
  41. extern OSGDB_EXPORT FileType fileType(const std::string& filename);
  42. /** find specified file in specified file path.*/
  43. extern OSGDB_EXPORT std::string findFileInPath(const std::string& filename, const FilePathList& filePath,CaseSensitivity caseSensitivity=CASE_SENSITIVE);
  44. /** return the directory/filename of a file if its is contained within specified directory.
  45. * return "" if directory does not contain file. If caseInsensitive is set to true then
  46. * a case insensitive comparison is used to compare fileName to directory contents.
  47. * This is useful when unix programs attempt read case insensitive windows filenames.
  48. */
  49. extern OSGDB_EXPORT std::string findFileInDirectory(const std::string& fileName,const std::string& dirName,CaseSensitivity caseSensitivity=CASE_SENSITIVE);
  50. /** simple list of names to represent a directory's contents. */
  51. typedef std::vector<std::string> DirectoryContents;
  52. /** Return the contents of a directory.
  53. * Return value will contain filenames only, not absolute paths.
  54. * Returns an empty array on any error.*/
  55. extern OSGDB_EXPORT DirectoryContents getDirectoryContents(const std::string& dirName);
  56. /** Return the contents of a directory, sorting the names into alphabetic and numberical order.
  57. * Return value will contain filenames only, not absolute paths.
  58. * Returns an empty array on any error.*/
  59. extern OSGDB_EXPORT DirectoryContents getSortedDirectoryContents(const std::string& dirName);
  60. /** Return the list of filenames that match the given filename with wildcards.
  61. * Will only expand '*', and will not expand wildcards in directory, only in
  62. * filename part of the given filename.
  63. * Return value will contain path+filename so that if ever the above
  64. * limitation (expanding wildcards in directory) is fixed, client code will
  65. * still work unchanged. */
  66. extern OSGDB_EXPORT DirectoryContents expandWildcardsInFilename(const std::string& filename);
  67. enum CopyFileResult
  68. {
  69. COPY_FILE_OK, /**< Operation done. */
  70. COPY_FILE_SOURCE_EQUALS_DESTINATION, /**< Operation is useless (source == destination). */
  71. COPY_FILE_BAD_ARGUMENT,
  72. COPY_FILE_SOURCE_MISSING, /**< Source file doesn't exist. */
  73. COPY_FILE_SOURCE_NOT_OPENED, /**< Error opening source file. */
  74. COPY_FILE_DESTINATION_NOT_OPENED, /**< Error opening destination file. */
  75. COPY_FILE_READ_ERROR,
  76. COPY_FILE_WRITE_ERROR
  77. };
  78. /** Copy a file to another location, overwriting if necessary.
  79. * You must provide full path for both source and destination.
  80. * \return true on success, or if source and destination are the same.
  81. */
  82. extern OSGDB_EXPORT CopyFileResult copyFile(const std::string & source, const std::string & destination);
  83. inline void setDataFilePathList(const FilePathList& filepath) { osgDB::Registry::instance()->setDataFilePathList(filepath); }
  84. inline void setDataFilePathList(const std::string& paths) { osgDB::Registry::instance()->setDataFilePathList(paths); }
  85. inline FilePathList& getDataFilePathList() { return osgDB::Registry::instance()->getDataFilePathList(); }
  86. /** Search for specified file in file system, checking the DataFilePathList for possible paths,
  87. * returning the full path of the first valid file found, return an empty string if no string is found.
  88. */
  89. extern OSGDB_EXPORT std::string findDataFile(const std::string& filename,CaseSensitivity caseSensitivity=CASE_SENSITIVE);
  90. /** Search for specified file in file system, checking first the database path set in the Options structure, then the DataFilePathList for possible paths,
  91. * returning the full path of the first valid file found, return an empty string if no string is found.
  92. */
  93. extern OSGDB_EXPORT std::string findDataFile(const std::string& filename,const Options* options, CaseSensitivity caseSensitivity=CASE_SENSITIVE);
  94. inline void setLibraryFilePathList(const FilePathList& filepaths) { osgDB::Registry::instance()->setLibraryFilePathList(filepaths); }
  95. inline void setLibraryFilePathList(const std::string& paths) { osgDB::Registry::instance()->setLibraryFilePathList(paths); }
  96. inline FilePathList& getLibraryFilePathList() { return osgDB::Registry::instance()->getLibraryFilePathList(); }
  97. extern OSGDB_EXPORT std::string findLibraryFile(const std::string& filename,CaseSensitivity caseSensitivity=CASE_SENSITIVE);
  98. /** convert a string containing a list of paths delimited either with ';' (Windows) or ':' (All other platforms) into FilePath representation.*/
  99. extern OSGDB_EXPORT void convertStringPathIntoFilePathList(const std::string& paths,FilePathList& filepath);
  100. /** Return true if FilePathList contains a filepath that is significies checking of the current working directory.*/
  101. extern OSGDB_EXPORT bool containsCurrentWorkingDirectoryReference(const FilePathList& paths);
  102. extern OSGDB_EXPORT void appendPlatformSpecificLibraryFilePaths(FilePathList& filepath);
  103. extern OSGDB_EXPORT void appendPlatformSpecificResourceFilePaths(FilePathList& filepath);
  104. } // namespace osgDB
  105. #endif