| 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 | |
|---|
| 14 | #ifndef OSGDB_FILEUTILS |
|---|
| 15 | #define OSGDB_FILEUTILS 1 |
|---|
| 16 | |
|---|
| 17 | #include <osgDB/Registry> |
|---|
| 18 | |
|---|
| 19 | #include <vector> |
|---|
| 20 | #include <deque> |
|---|
| 21 | #include <string> |
|---|
| 22 | #include <stdio.h> |
|---|
| 23 | |
|---|
| 24 | namespace osgDB { |
|---|
| 25 | |
|---|
| 26 | /** Overload of the standard fopen function. If OSG_USE_UTF8_FILENAME is defined, |
|---|
| 27 | * filename will be expanded from UTF8 to UTF16 and _wfopen will be called. */ |
|---|
| 28 | extern OSGDB_EXPORT FILE* fopen(const char* filename, const char* mode); |
|---|
| 29 | |
|---|
| 30 | /** Make a new directory. Returns true if directory exists or was created. */ |
|---|
| 31 | extern OSGDB_EXPORT bool makeDirectory( const std::string &directoryPath ); |
|---|
| 32 | |
|---|
| 33 | /** Make a new directory for a given file. */ |
|---|
| 34 | extern OSGDB_EXPORT bool makeDirectoryForFile( const std::string &filePath ); |
|---|
| 35 | |
|---|
| 36 | /** Get current working directory. */ |
|---|
| 37 | extern OSGDB_EXPORT std::string getCurrentWorkingDirectory( void ); |
|---|
| 38 | |
|---|
| 39 | /** Set current working directory. */ |
|---|
| 40 | extern OSGDB_EXPORT bool setCurrentWorkingDirectory( const std::string &newCurrentWorkingDirectory ); |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | /** return true if a file exists. */ |
|---|
| 44 | extern OSGDB_EXPORT bool fileExists(const std::string& filename); |
|---|
| 45 | |
|---|
| 46 | enum FileType |
|---|
| 47 | { |
|---|
| 48 | FILE_NOT_FOUND, |
|---|
| 49 | REGULAR_FILE, |
|---|
| 50 | DIRECTORY |
|---|
| 51 | }; |
|---|
| 52 | |
|---|
| 53 | /** return type of file. */ |
|---|
| 54 | extern OSGDB_EXPORT FileType fileType(const std::string& filename); |
|---|
| 55 | |
|---|
| 56 | /** find specified file in specified file path.*/ |
|---|
| 57 | extern OSGDB_EXPORT std::string findFileInPath(const std::string& filename, const FilePathList& filePath,CaseSensitivity caseSensitivity=CASE_SENSITIVE); |
|---|
| 58 | |
|---|
| 59 | /** return the directory/filename of a file if its is contained within specified directory. |
|---|
| 60 | * return "" if directory does not contain file. If caseInsensitive is set to true then |
|---|
| 61 | * a case insensitive comparison is used to compare fileName to directory contents. |
|---|
| 62 | * This is useful when unix programs attempt read case insensitive windows filenames. |
|---|
| 63 | */ |
|---|
| 64 | extern OSGDB_EXPORT std::string findFileInDirectory(const std::string& fileName,const std::string& dirName,CaseSensitivity caseSensitivity=CASE_SENSITIVE); |
|---|
| 65 | |
|---|
| 66 | /** simple list of names to represent a directory's contents. */ |
|---|
| 67 | typedef std::vector<std::string> DirectoryContents; |
|---|
| 68 | |
|---|
| 69 | /** Return the contents of a directory. |
|---|
| 70 | * Return value will contain filenames only, not absolute paths. |
|---|
| 71 | * Returns an empty array on any error.*/ |
|---|
| 72 | extern OSGDB_EXPORT DirectoryContents getDirectoryContents(const std::string& dirName); |
|---|
| 73 | |
|---|
| 74 | /** Return the list of filenames that match the given filename with wildcards. |
|---|
| 75 | * Will only expand '*', and will not expand wildcards in directory, only in |
|---|
| 76 | * filename part of the given filename. |
|---|
| 77 | * Return value will contain path+filename so that if ever the above |
|---|
| 78 | * limitation (expanding wildcards in directory) is fixed, client code will |
|---|
| 79 | * still work unchanged. */ |
|---|
| 80 | extern OSGDB_EXPORT DirectoryContents expandWildcardsInFilename(const std::string& filename); |
|---|
| 81 | |
|---|
| 82 | namespace FileOpResult { |
|---|
| 83 | enum Value |
|---|
| 84 | { |
|---|
| 85 | OK, /**< Operation done. */ |
|---|
| 86 | SOURCE_EQUALS_DESTINATION, /**< Operation is useless (source == destination). */ |
|---|
| 87 | BAD_ARGUMENT, |
|---|
| 88 | SOURCE_MISSING, /**< Source file doesn't exist. */ |
|---|
| 89 | SOURCE_NOT_OPENED, /**< Error opening source file. */ |
|---|
| 90 | DESTINATION_NOT_OPENED, /**< Error opening destination file. */ |
|---|
| 91 | READ_ERROR, |
|---|
| 92 | WRITE_ERROR |
|---|
| 93 | }; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | /** Copy a file to another location, overwriting if necessary. |
|---|
| 97 | * You must provide full path for both source and destination. |
|---|
| 98 | * \return true on success, or if source and destination are the same. |
|---|
| 99 | * \todo Replace the implementation with filesystem functions from TR2 when available. |
|---|
| 100 | */ |
|---|
| 101 | extern OSGDB_EXPORT FileOpResult::Value copyFile(const std::string & source, const std::string & destination); |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | inline void setDataFilePathList(const FilePathList& filepath) { osgDB::Registry::instance()->setDataFilePathList(filepath); } |
|---|
| 106 | |
|---|
| 107 | inline void setDataFilePathList(const std::string& paths) { osgDB::Registry::instance()->setDataFilePathList(paths); } |
|---|
| 108 | |
|---|
| 109 | inline FilePathList& getDataFilePathList() { return osgDB::Registry::instance()->getDataFilePathList(); } |
|---|
| 110 | |
|---|
| 111 | /** Search for specified file in file system, checking the DataFilePathList for possible paths, |
|---|
| 112 | * returning the full path of the first valid file found, return an empty string if no string is found. |
|---|
| 113 | */ |
|---|
| 114 | extern OSGDB_EXPORT std::string findDataFile(const std::string& filename,CaseSensitivity caseSensitivity=CASE_SENSITIVE); |
|---|
| 115 | |
|---|
| 116 | /** Search for specified file in file system, checking first the database path set in the Options structure, then the DataFilePathList for possible paths, |
|---|
| 117 | * returning the full path of the first valid file found, return an empty string if no string is found. |
|---|
| 118 | */ |
|---|
| 119 | extern OSGDB_EXPORT std::string findDataFile(const std::string& filename,const Options* options, CaseSensitivity caseSensitivity=CASE_SENSITIVE); |
|---|
| 120 | |
|---|
| 121 | inline void setLibraryFilePathList(const FilePathList& filepaths) { osgDB::Registry::instance()->setLibraryFilePathList(filepaths); } |
|---|
| 122 | |
|---|
| 123 | inline void setLibraryFilePathList(const std::string& paths) { osgDB::Registry::instance()->setLibraryFilePathList(paths); } |
|---|
| 124 | |
|---|
| 125 | inline FilePathList& getLibraryFilePathList() { return osgDB::Registry::instance()->getLibraryFilePathList(); } |
|---|
| 126 | |
|---|
| 127 | extern OSGDB_EXPORT std::string findLibraryFile(const std::string& filename,CaseSensitivity caseSensitivity=CASE_SENSITIVE); |
|---|
| 128 | |
|---|
| 129 | /** convert a string containing a list of paths delimited either with ';' (Windows) or ':' (All other platforms) into FilePath representation.*/ |
|---|
| 130 | extern OSGDB_EXPORT void convertStringPathIntoFilePathList(const std::string& paths,FilePathList& filepath); |
|---|
| 131 | |
|---|
| 132 | /** Return true if FilePathList contains a filepath that is significies checking of the current working directory.*/ |
|---|
| 133 | extern OSGDB_EXPORT bool containsCurrentWorkingDirectoryReference(const FilePathList& paths); |
|---|
| 134 | |
|---|
| 135 | extern OSGDB_EXPORT void appendPlatformSpecificLibraryFilePaths(FilePathList& filepath); |
|---|
| 136 | extern OSGDB_EXPORT void appendPlatformSpecificResourceFilePaths(FilePathList& filepath); |
|---|
| 137 | |
|---|
| 138 | } // namespace osgDB |
|---|
| 139 | |
|---|
| 140 | #endif |
|---|