Xfce Foundation Classes
Main Page | Index | Namespace List | Alphabetical List | Class List | File List

# fileutils.hh File Reference

C++ interface for GDir and the various GLib file-related functions. More...

`#include <xfc/stackobject.hh>`
`#include <xfc/utfstring.hh>`
`#include <glib/gdir.h>`
`#include <glib/gfileutils.h>`
`#include <vector>`
`#include <xfc/glib/inline/fileutils.inl>`

## Namespaces

- namespace Xfc
- namespace Xfc::G

## Classes

- class Xfc::G::TempFile
    A C++ wrapper interface for the g_file_open_tmp() and g_mkstemp() functions. More...
- class Xfc::G::Dir
    A GDir C++ wrapper interface. More...

## File Methods

- FileError file_error_from_errno (int err_no)
    Gets a FileError constant based on the passed-in errno, for example, if you pass in EEXIST this function returns G_FILE_ERROR_EXIST.
- bool file_test (const std::string &filename, FileTestField test)
    Determines whether any of the tests in the bitfield test are true.
- bool file_get_contents (const std::string &filename, std::string &contents, G::Error *error)
    Reads an entire file into allocated memory, with good error checking.

## Typedefs

- typedef unsigned int FileTestField
    G::FileTestField holds one or more bit values from the G::FileTest enumeration.

## Enumerations

- enum FileError {
  FILE_ERROR_EXIST = G_FILE_ERROR_EXIST,
  FILE_ERROR_ISDIR = G_FILE_ERROR_ISDIR,
  FILE_ERROR_ACCES = G_FILE_ERROR_ACCES,
  FILE_ERROR_NAMETOOLONG = G_FILE_ERROR_NAMETOOLONG,
  FILE_ERROR_NOENT = G_FILE_ERROR_NOENT,
  FILE_ERROR_NOTDIR = G_FILE_ERROR_NOTDIR,
  FILE_ERROR_NXIO = G_FILE_ERROR_NXIO,
  FILE_ERROR_NODEV = G_FILE_ERROR_NODEV,
  FILE_ERROR_ROFS = G_FILE_ERROR_ROFS,
  FILE_ERROR_TXTBSY = G_FILE_ERROR_TXTBSY,
  FILE_ERROR_FAULT = G_FILE_ERROR_FAULT,
  FILE_ERROR_LOOP = G_FILE_ERROR_LOOP,
  FILE_ERROR_NOSPC = G_FILE_ERROR_NOSPC,
  FILE_ERROR_NOMEM = G_FILE_ERROR_NOMEM,
  FILE_ERROR_MFILE = G_FILE_ERROR_MFILE,
  FILE_ERROR_NFILE = G_FILE_ERROR_NFILE,
  FILE_ERROR_BADF = G_FILE_ERROR_BADF,
  FILE_ERROR_INVAL = G_FILE_ERROR_INVAL,
  FILE_ERROR_PIPE = G_FILE_ERROR_PIPE,
  FILE_ERROR_AGAIN = G_FILE_ERROR_AGAIN,
  FILE_ERROR_INTR = G_FILE_ERROR_INTR,
  FILE_ERROR_IO = G_FILE_ERROR_IO,
  FILE_ERROR_PERM = G_FILE_ERROR_PERM,
  FILE_ERROR_NOSYS = G_FILE_ERROR_NOSYS,
  FILE_ERROR_FAILED = G_FILE_ERROR_FAILED
  }
    Values corresponding to errno codes returned from file operations on UNIX. More...
- enum FileTest {
  FILE_TEST_IS_REGULAR = G_FILE_TEST_IS_REGULAR,
  FILE_TEST_IS_SYMLINK = G_FILE_TEST_IS_SYMLINK,
  FILE_TEST_IS_DIR = G_FILE_TEST_IS_DIR,
  FILE_TEST_IS_EXECUTABLE = G_FILE_TEST_IS_EXECUTABLE,
  FILE_TEST_EXISTS = G_FILE_TEST_EXISTS
  }
    Specifies a test to perform on a file using G::file_test(). More...

## Detailed Description

C++ interface for GDir and the various GLib file-related functions.

## Enumeration Type Documentation

enum FileError

  | Values corresponding to errno codes returned from file operations on UNIX. Unlike errno codes, FileError values are available on all systems. The exact meaning of each code depends on what sort of file operation you were performing; the UNIX documentation gives more details. The following error code descriptions come from the GNU C Library manual, and are under the copyright of that manual. It's not very portable to make detailed assumptions about exactly which errors will be returned from a given operation. Some errors don't occur on some systems, etc., sometimes there are subtle differences in when a system will report a given error, etc. Enumeration values:
  FILE_ERROR_EXIST | Operation not permitted; only the owner of the file (or other resource) or processes with special privileges can perform the operation.
  FILE_ERROR_ISDIR | File is a directory; you cannot open a directory for writing, or create or remove hard links to it.
  FILE_ERROR_ACCES | Permission denied; the file permissions do not allow the attempted operation.
  FILE_ERROR_NAMETOOLONG | Filename is too long.
  FILE_ERROR_NOENT | No such file or directory; This is a "file doesn't exist" error for ordinary files that are referenced in contexts where they are expected to already exist.
  FILE_ERROR_NOTDIR | A file that isn't a directory was specified when a directory is required.
  FILE_ERROR_NXIO | No such device or address; The system tried to use the device represented by a file you specified, and it couldn't find the device; This can mean that the device file was installed incorrectly, or that the physical device is missing or not correctly attached to the computer.
  FILE_ERROR_NODEV | This file is of a type that doesn't support mapping.
  FILE_ERROR_ROFS | The directory containing the new link can't be modified because it's on a read-only file system.
  FILE_ERROR_TXTBSY | Text file busy.
  FILE_ERROR_FAULT | You passed in a pointer to bad memory (GLib won't reliably return this, don't pass in pointers to bad memory).
  FILE_ERROR_LOOP | Too many levels of symbolic links were encountered in looking up a file name; This often indicates a cycle of symbolic links.
  FILE_ERROR_NOSPC | No space left on device; write operation on a file failed because the disk is full.
  FILE_ERROR_NOMEM | No memory available; The system cannot allocate more virtual memory because its capacity is full.
  FILE_ERROR_MFILE | The current process has too many files open and can't open any more; Duplicate descriptors do count toward this limit.
  FILE_ERROR_NFILE | There are too many distinct file openings in the entire system.
  FILE_ERROR_BADF | Bad file descriptor; for example, I/O on a descriptor that has been closed or reading from a descriptor open only for writing (or vice versa).
  FILE_ERROR_INVAL | Invalid argument; This is used to indicate various kinds of problems with passing the wrong argument to a library function.
  FILE_ERROR_PIPE | Broken pipe; there is no process reading from the other end of a pipe (Every library function that returns this error code also generates a `SIGPIPE' signal; this signal terminates the program if not handled or blocked; Thus, your program will never actually see this code unless it has handled or blocked `SIGPIPE').
  FILE_ERROR_AGAIN | Resource temporarily unavailable; the call might work if you try again later.
  FILE_ERROR_INTR | Interrupted function call; an asynchronous signal occurred and prevented completion of the call (When this happens, you should try the call again).
  FILE_ERROR_IO | Input/output error; usually used for physical read or write errors, that is, the disk or other physical device hardware is returning errors.
  FILE_ERROR_PERM | Operation not permitted; only the owner of the file (or other resource) or processes with special privileges can perform the operation.
  FILE_ERROR_NOSYS | Function not implemented; this indicates that the system is missing some functionality.
  FILE_ERROR_FAILED | Does not correspond to a UNIX error code; this is the standard "failed for unspecified reason" error code present in all GError error code enumerations (Returned if no specific code applies).

enum FileTest

  | Specifies a test to perform on a file using G::file_test(). Enumeration values:
  FILE_TEST_IS_REGULAR | Tests true if the file is a regular file (not a symlink or directory).
  FILE_TEST_IS_SYMLINK | Tests true if the file is a symlink.
  FILE_TEST_IS_DIR | Tests true if the file is a directory.
  FILE_TEST_IS_EXECUTABLE | Tests true if the file is executable.
  FILE_TEST_EXISTS | Tests true if the file exists. It may or may not be a regular file.

## Function Documentation

FileError file_error_from_errno | ( | int | err_no | ) |

  | Gets a FileError constant based on the passed-in errno, for example, if you pass in EEXIST this function returns G_FILE_ERROR_EXIST. Parameters:
  | err_no | An "errno" value
Returns: A FileError corresponding to the given errno. Unlike errno values, you can portably assume that all FileError values will exist. Normally a FileError value goes into a G::Error returned from a method that manipulates files. So you would use G::file_error_from_errno() when constructing a G::Error.

bool file_get_contents | ( | const std::string & | filename,
|  | std::string & | contents,
|  | G::Error * | error
| ) |

  | Reads an entire file into allocated memory, with good error checking. Parameters:
  | filename | A file to read the contents from.
  | contents | The location to store the file contents.
  | error | The return location for a G::Error.
Returns: true on success, false if error is set. If error is set, false is returned, and contents is set to an empty string. If true is returned, error will not be set, and contents will be set to the file contents. The error domain is G_FILE_ERROR. Possible error codes are those in the FileError enumeration.

bool file_test | ( | const std::string & | filename,
|  | FileTestField | test
| ) |

  | Determines whether any of the tests in the bitfield test are true. Parameters:
  | filename | A filename to test.
  | test | A bitfield of G::FileTest flags.
Returns: true if a test was true. For example, (G::FILE_TEST_EXISTS | G::FILE_TEST_IS_DIR) will return true if the file exists; the check whether it's a directory doesn't matter since the existence test is true. With the current set of available tests, there's no point passing in more than one test at a time. Apart from G::FILE_TEST_IS_SYMLINK all tests follow symbolic links, so for a symbolic link to a regular file file_test() will return true for both G::FILE_TEST_IS_SYMLINK and G::FILE_TEST_IS_REGULAR. Note, that for a dangling symbolic link file_test() will return true for G::FILE_TEST_IS_SYMLINK and false for all other flags. You should never use file_test() to test whether it is safe to perform an operaton, because there is always the possibility of the condition changing before you actually perform the operation. For example, you might think you could use G::FILE_TEST_IS_SYMLINK to know whether it is is safe to write to a file without being tricked into writing into a different location. It doesn't work! Example: DON'T DO THIS.
         if (!file_test(filename, FILE_TEST_IS_SYMLINK))
         {
            TempFile file(filename);
            if (file.is_open())
            // write to file
         }
Another thing to note is that G::FILE_TEST_EXISTS and G::FILE_TEST_IS_EXECUTABLE are implemented using the access() system call. This usually doesn't matter, but if your program is setuid or setgid it means that these tests will give you the answer for the real user ID and group ID , rather than the effective user ID and group ID.

Xfce Foundation Classes

Copyright © 2004-2005 The XFC Development Team | XFC 4.3