Open Inventor Release 2024.2.0
 
Loading...
Searching...
No Matches
SbFileHelper Class Reference

VSG extension Utilities class for file management. More...

#include <Inventor/helpers/SbFileHelper.h>

Public Types

enum  FileAccess {
  EXISTENCE = F_OK ,
  WRITE = W_OK ,
  READ = R_OK ,
  READ_WRITE = W_OK + R_OK ,
  EXECUTE = X_OK
}
 File access mode enum. More...
 
typedef struct stat64 Stat
 

Static Public Member Functions

static FILE * open (const SbString &filename, const char *flags)
 Open a file and return a handle to it.
 
static void close (FILE *fp)
 Close a file opened using the open function.
 
static unsigned long long getFileSize (const SbString &filename)
 Returns the size of the specified file in bytes.
 
static SbBool isAccessible (const SbString &filename, FileAccess mode=READ)
 Returns TRUE if a file is accessible

 
static SbString getExtension (const SbString &filename)
 Returns the extension part of the specified filename.
 
static SbBool isUrl (const SbString &filename)
 Returns TRUE if the filename is a network url like: http:// or ftp://.
 
static SbBool isFileSystemUrl (const SbString &filename)
 Returns TRUE if the filename is a file system URL like: file://...
 
static SbString makeFileSystemURL (const SbString &filename)
 Converts a filename to a file system URL.
 
static SbString fileSystemURLToFilename (const SbString &url)
 Converts a file system URL to a regular filename
E.g "file:///c:/temp/test.dat" becomes "c:/temp/test.dat".
 
static SbString getBaseName (const SbString &fullFilePath)
 Returns the filename part of a full path string.
 
static SbString getDirName (const SbString &fullFilePath)
 Returns the directory path part of a full path string.
 
static SbString toUnixPath (const SbString &path)
 Converts a Microsoft Windows path (with '\') to a Unix path (with '/').
 
static SbString toWindowsPath (const SbString &path)
 Converts a Unix path (with '/') to a Microsoft Windows path (with '\').
 
static SbString getTempDirectory ()
 Returns the path to the temporary directory used by the system.
 
static SbString getTempFilename (const SbString &path, const SbString &prefix)
 Returns a temporary filename.
 
static int createDirectory (const SbString &directory)
 Create the specified directory.
 
static SbString getCurrentDirectory ()
 Returns the current directory.
 
static int setCurrentDirectory (const SbString &directory)
 Sets the current working directory.
 
static bool containsWindowsDrive (const SbString &path)
 Returns true if the specified path starts with a Microsoft Windows drive letter.
 
static int removeFile (const SbString &fileName)
 Removes the specified file.
 
static bool getStat (const SbString &filename, SbFileHelper::Stat &stat)
 This function fills the stat structure with information about the specified file.
 
static SbString cleanUpPath (const SbString &path)
 Removes all multiple occurences of '/' in the path.
 
static SbString expandString (const SbString &string)
 Returns a new version of the string with "$NAME" SoPreference variable names expanded to their defined value.
 
static void listFiles (const SbString &path, const SbString &pattern, std::vector< SbString > &files)
 Returns a list of the files in directory 'path' that match a 'pattern' Pattern may include the basic wild card characters: '*' matches anything and '?' matches any single character.
 

Detailed Description

VSG extension Utilities class for file management.

Portable utility functions to manage files and manage file path strings.

File path strings can contain SoPreference (environment) variable names preceded by the '$' (dollar sign) character. These names are expanded to their defined value before passing the string to the operating system. For example, "$OIVHOME/data/models/bird.iv" could be used to access a file installed by the Open Inventor SDK. If the specified name does not have a defined value, the name is used as-is.

Definition at line 59 of file SbFileHelper.h.

Member Typedef Documentation

◆ Stat

typedef struct stat64 SbFileHelper::Stat

Definition at line 66 of file SbFileHelper.h.

Member Enumeration Documentation

◆ FileAccess

File access mode enum.

Enumerator
EXISTENCE 

Check file for existence only.

WRITE 

Check file for Write access only.

READ 

Check file for Read access only.

READ_WRITE 

Check file for Read and Write access.

EXECUTE 

Check file for Execution access.

Definition at line 100 of file SbFileHelper.h.

Member Function Documentation

◆ cleanUpPath()

static SbString SbFileHelper::cleanUpPath ( const SbString path)
static

Removes all multiple occurences of '/' in the path.

◆ close()

static void SbFileHelper::close ( FILE *  fp)
static

Close a file opened using the open function.

Parameters
fpThe handle of the file to be closed.

◆ containsWindowsDrive()

static bool SbFileHelper::containsWindowsDrive ( const SbString path)
static

Returns true if the specified path starts with a Microsoft Windows drive letter.

◆ createDirectory()

static int SbFileHelper::createDirectory ( const SbString directory)
static

Create the specified directory.

Parameters
directoryThe path of the directory to create.
Returns
-1 if creation failed.

◆ expandString()

static SbString SbFileHelper::expandString ( const SbString string)
static

Returns a new version of the string with "$NAME" SoPreference variable names expanded to their defined value.

If the specified name does not have a defined value, the name is copied as-is.

◆ fileSystemURLToFilename()

static SbString SbFileHelper::fileSystemURLToFilename ( const SbString url)
static

Converts a file system URL to a regular filename
E.g "file:///c:/temp/test.dat" becomes "c:/temp/test.dat".

Parameters
urlThe URL to convert.
Returns
The converted filename.

◆ getBaseName()

static SbString SbFileHelper::getBaseName ( const SbString fullFilePath)
static

Returns the filename part of a full path string.


E.g If the full path is "/tmp/a.tmp", the function will return "a.tmp"

Parameters
fullFilePathThe full path string
Returns
The extracted filename

◆ getCurrentDirectory()

static SbString SbFileHelper::getCurrentDirectory ( )
static

Returns the current directory.

◆ getDirName()

static SbString SbFileHelper::getDirName ( const SbString fullFilePath)
static

Returns the directory path part of a full path string.


E.g. If the full path is "/tmp/a.tmp", returns "/tmp/"

Parameters
fullFilePathThe full path string
Returns
The directory path string

◆ getExtension()

static SbString SbFileHelper::getExtension ( const SbString filename)
static

Returns the extension part of the specified filename.


It does not include the '.' used to separate the file name and the extension!
E.g. If the filename is "test.dat", the function returns "dat".

◆ getFileSize()

static unsigned long long SbFileHelper::getFileSize ( const SbString filename)
static

Returns the size of the specified file in bytes.

◆ getStat()

static bool SbFileHelper::getStat ( const SbString filename,
SbFileHelper::Stat stat 
)
static

This function fills the stat structure with information about the specified file.

The stat structure contains the following entries:

  • The file size in bytes.
  • The access information.
  • The creation/modification time.
Parameters
filenameThe full path to the file.
statThe stat structure filled with the information about the file.
Returns
True if successful.

◆ getTempDirectory()

static SbString SbFileHelper::getTempDirectory ( )
static

Returns the path to the temporary directory used by the system.

Useful with the getTempFilename() method. The path ends with an operating system dependent directory separator.

◆ getTempFilename()

static SbString SbFileHelper::getTempFilename ( const SbString path,
const SbString prefix 
)
static

Returns a temporary filename.

Parameters
pathThe path to the directory which will contain the file (see getTempDirectory()).
prefixThe filename prefix.
Returns
A temporary filename.

◆ isAccessible()

static SbBool SbFileHelper::isAccessible ( const SbString filename,
FileAccess  mode = READ 
)
static

Returns TRUE if a file is accessible

The default value of the mode parameter is READ_ONLY.

Note that this method cannot be used to test if it is possible to create a file for writing. It only works on existing files.

Parameters
filenameThe file to test.
modeThe file access mode
Returns
TRUE if the file is accessible, FALSE otherwise.

◆ isFileSystemUrl()

static SbBool SbFileHelper::isFileSystemUrl ( const SbString filename)
static

Returns TRUE if the filename is a file system URL like: file://...

◆ isUrl()

static SbBool SbFileHelper::isUrl ( const SbString filename)
static

Returns TRUE if the filename is a network url like: http:// or ftp://.

◆ listFiles()

static void SbFileHelper::listFiles ( const SbString path,
const SbString pattern,
std::vector< SbString > &  files 
)
static

Returns a list of the files in directory 'path' that match a 'pattern' Pattern may include the basic wild card characters: '*' matches anything and '?' matches any single character.

The file names are sorted by time of last access (stat.st_atime) with newest files first.

On Microsoft Windows systems, note:
Since Windows NT, updates to time of last access on NTFS file systems are disabled by default. So normally the access time is the same as the creation time and effectively the file names are sorted by creation time. It is possible to change the file system behavior, see Microsoft documentation.

Parameters
[in]pathThe path to the directory where the files are located
[in]patternThe string to match against the name of files in path.
[out]filesThe list of files matching the pattern in path.

Example:

std::vector<SbString> files;
listFiles( "C:/my/path", "*.tif", files );
static void listFiles(const SbString &path, const SbString &pattern, std::vector< SbString > &files)
Returns a list of the files in directory 'path' that match a 'pattern' Pattern may include the basic ...

◆ makeFileSystemURL()

static SbString SbFileHelper::makeFileSystemURL ( const SbString filename)
static

Converts a filename to a file system URL.


Actually it just adds the string file:// to the filename, if and only if the filename is not already a file system URL.

Parameters
filenameThe file name to convert
Returns
The converted filename.

◆ open()

static FILE * SbFileHelper::open ( const SbString filename,
const char *  flags 
)
static

Open a file and return a handle to it.

Parameters
filenameThe filename (can be Latin-1/ASCII or unicode).
flagsRegular flags used by fopen system call.
Returns
Returns a handle to the file.

◆ removeFile()

static int SbFileHelper::removeFile ( const SbString fileName)
static

Removes the specified file.

Parameters
fileNameThe file to remove.
Returns
An error code, 0 if there was no error.

◆ setCurrentDirectory()

static int SbFileHelper::setCurrentDirectory ( const SbString directory)
static

Sets the current working directory.

Returns 0 if successful, else one of the POSIX "chdir" error codes.

◆ toUnixPath()

static SbString SbFileHelper::toUnixPath ( const SbString path)
static

Converts a Microsoft Windows path (with '\') to a Unix path (with '/').


A leading "\\" (used for network paths) is not converted.

Parameters
pathThe path to convert.
Returns
The converted path

◆ toWindowsPath()

static SbString SbFileHelper::toWindowsPath ( const SbString path)
static

Converts a Unix path (with '/') to a Microsoft Windows path (with '\').

Parameters
pathThe path to convert.
Returns
The converted path

The documentation for this class was generated from the following file: