2012-03-22 01:36:20 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2003-01-22 00:45:36 +08:00
*
2012-03-22 01:36:20 +08:00
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
2003-01-22 00:45:36 +08:00
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
2012-03-22 01:36:20 +08:00
*
2003-01-22 00:45:36 +08:00
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2012-03-22 01:36:20 +08:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2003-01-22 00:45:36 +08:00
* OpenSceneGraph Public License for more details.
*/
2001-10-04 23:12:57 +08:00
2001-09-20 05:08:56 +08:00
#ifndef OSGDB_FILEUTILS
#define OSGDB_FILEUTILS 1
2002-06-18 05:50:37 +08:00
#include <osgDB/Registry>
2001-09-20 05:08:56 +08:00
#include <vector>
2002-06-18 05:50:37 +08:00
#include <deque>
2001-09-20 05:08:56 +08:00
#include <string>
2009-01-09 20:17:40 +08:00
#include <stdio.h>
2001-09-20 05:08:56 +08:00
namespace osgDB {
2010-01-28 18:45:40 +08:00
/** Overload of the standard fopen function. If OSG_USE_UTF8_FILENAME is defined,
* filename will be expanded from UTF8 to UTF16 and _wfopen will be called. */
2008-11-07 23:08:08 +08:00
extern OSGDB_EXPORT FILE* fopen(const char* filename, const char* mode);
2010-01-28 18:45:40 +08:00
/** Make a new directory. Returns true if directory exists or was created. */
2004-09-01 22:49:18 +08:00
extern OSGDB_EXPORT bool makeDirectory( const std::string &directoryPath );
2010-01-28 18:45:40 +08:00
/** Make a new directory for a given file. */
2004-09-01 22:49:18 +08:00
extern OSGDB_EXPORT bool makeDirectoryForFile( const std::string &filePath );
2012-03-22 01:36:20 +08:00
2010-01-28 18:45:40 +08:00
/** Get current working directory. */
2009-11-23 18:19:37 +08:00
extern OSGDB_EXPORT std::string getCurrentWorkingDirectory( void );
2010-01-28 18:45:40 +08:00
/** Set current working directory. */
2009-11-23 18:19:37 +08:00
extern OSGDB_EXPORT bool setCurrentWorkingDirectory( const std::string &newCurrentWorkingDirectory );
2001-09-20 05:08:56 +08:00
2007-12-11 01:30:18 +08:00
/** return true if a file exists. */
2002-12-05 20:43:08 +08:00
extern OSGDB_EXPORT bool fileExists(const std::string& filename);
2001-09-20 05:08:56 +08:00
2010-01-28 18:45:40 +08:00
enum FileType
{
FILE_NOT_FOUND,
REGULAR_FILE,
DIRECTORY
};
2004-05-08 14:35:32 +08:00
/** return type of file. */
extern OSGDB_EXPORT FileType fileType(const std::string& filename);
2001-09-20 05:08:56 +08:00
2002-06-18 05:50:37 +08:00
/** find specified file in specified file path.*/
2003-11-25 17:04:41 +08:00
extern OSGDB_EXPORT std::string findFileInPath(const std::string& filename, const FilePathList& filePath,CaseSensitivity caseSensitivity=CASE_SENSITIVE);
2001-09-20 05:08:56 +08:00
/** return the directory/filename of a file if its is contained within specified directory.
* return "" if directory does not contain file. If caseInsensitive is set to true then
2001-09-28 20:36:40 +08:00
* a case insensitive comparison is used to compare fileName to directory contents.
2007-12-11 01:30:18 +08:00
* This is useful when unix programs attempt read case insensitive windows filenames.
2001-09-20 05:08:56 +08:00
*/
2003-11-25 17:04:41 +08:00
extern OSGDB_EXPORT std::string findFileInDirectory(const std::string& fileName,const std::string& dirName,CaseSensitivity caseSensitivity=CASE_SENSITIVE);
2001-09-20 05:08:56 +08:00
2002-06-18 05:50:37 +08:00
/** simple list of names to represent a directory's contents. */
typedef std::vector<std::string> DirectoryContents;
2010-11-01 19:06:12 +08:00
/** Return the contents of a directory.
* Return value will contain filenames only, not absolute paths.
* Returns an empty array on any error.*/
2002-12-05 20:43:08 +08:00
extern OSGDB_EXPORT DirectoryContents getDirectoryContents(const std::string& dirName);
2001-09-20 05:08:56 +08:00
2012-09-13 00:02:02 +08:00
/** Return the contents of a directory, sorting the names into alphabetic and numberical order.
* Return value will contain filenames only, not absolute paths.
* Returns an empty array on any error.*/
extern OSGDB_EXPORT DirectoryContents getSortedDirectoryContents(const std::string& dirName);
2010-11-01 19:06:12 +08:00
/** Return the list of filenames that match the given filename with wildcards.
2012-03-22 01:36:20 +08:00
* Will only expand '*', and will not expand wildcards in directory, only in
* filename part of the given filename.
* Return value will contain path+filename so that if ever the above
* limitation (expanding wildcards in directory) is fixed, client code will
2010-11-01 19:06:12 +08:00
* still work unchanged. */
extern OSGDB_EXPORT DirectoryContents expandWildcardsInFilename(const std::string& filename);
2002-06-18 05:50:37 +08:00
2010-01-28 18:45:40 +08:00
namespace FileOpResult {
enum Value
{
OK, /**< Operation done. */
SOURCE_EQUALS_DESTINATION, /**< Operation is useless (source == destination). */
BAD_ARGUMENT,
SOURCE_MISSING, /**< Source file doesn't exist. */
SOURCE_NOT_OPENED, /**< Error opening source file. */
DESTINATION_NOT_OPENED, /**< Error opening destination file. */
READ_ERROR,
WRITE_ERROR
};
}
/** Copy a file to another location, overwriting if necessary.
* You must provide full path for both source and destination.
* \return true on success, or if source and destination are the same.
* \todo Replace the implementation with filesystem functions from TR2 when available.
*/
extern OSGDB_EXPORT FileOpResult::Value copyFile(const std::string & source, const std::string & destination);
2002-06-18 05:50:37 +08:00
inline void setDataFilePathList(const FilePathList& filepath) { osgDB::Registry::instance()->setDataFilePathList(filepath); }
inline void setDataFilePathList(const std::string& paths) { osgDB::Registry::instance()->setDataFilePathList(paths); }
inline FilePathList& getDataFilePathList() { return osgDB::Registry::instance()->getDataFilePathList(); }
2012-03-22 01:36:20 +08:00
/** Search for specified file in file system, checking the DataFilePathList for possible paths,
2004-11-22 22:10:12 +08:00
* returning the full path of the first valid file found, return an empty string if no string is found.
*/
2003-11-25 17:04:41 +08:00
extern OSGDB_EXPORT std::string findDataFile(const std::string& filename,CaseSensitivity caseSensitivity=CASE_SENSITIVE);
2002-06-18 05:50:37 +08:00
2012-03-22 01:36:20 +08:00
/** Search for specified file in file system, checking first the database path set in the Options structure, then the DataFilePathList for possible paths,
2004-11-22 22:10:12 +08:00
* returning the full path of the first valid file found, return an empty string if no string is found.
*/
2009-05-09 16:49:27 +08:00
extern OSGDB_EXPORT std::string findDataFile(const std::string& filename,const Options* options, CaseSensitivity caseSensitivity=CASE_SENSITIVE);
2002-06-18 05:50:37 +08:00
inline void setLibraryFilePathList(const FilePathList& filepaths) { osgDB::Registry::instance()->setLibraryFilePathList(filepaths); }
inline void setLibraryFilePathList(const std::string& paths) { osgDB::Registry::instance()->setLibraryFilePathList(paths); }
inline FilePathList& getLibraryFilePathList() { return osgDB::Registry::instance()->getLibraryFilePathList(); }
2003-11-25 17:04:41 +08:00
extern OSGDB_EXPORT std::string findLibraryFile(const std::string& filename,CaseSensitivity caseSensitivity=CASE_SENSITIVE);
2002-06-18 05:50:37 +08:00
2009-11-23 18:19:37 +08:00
/** convert a string containing a list of paths delimited either with ';' (Windows) or ':' (All other platforms) into FilePath representation.*/
2004-08-28 00:14:21 +08:00
extern OSGDB_EXPORT void convertStringPathIntoFilePathList(const std::string& paths,FilePathList& filepath);
2012-02-09 22:28:39 +08:00
/** Return true if FilePathList contains a filepath that is significies checking of the current working directory.*/
extern OSGDB_EXPORT bool containsCurrentWorkingDirectoryReference(const FilePathList& paths);
2004-08-28 00:14:21 +08:00
extern OSGDB_EXPORT void appendPlatformSpecificLibraryFilePaths(FilePathList& filepath);
2005-07-27 05:07:31 +08:00
extern OSGDB_EXPORT void appendPlatformSpecificResourceFilePaths(FilePathList& filepath);
2002-06-18 05:50:37 +08:00
2010-01-28 18:45:40 +08:00
} // namespace osgDB
2001-09-20 05:08:56 +08:00
#endif