Reference Manual

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

# Inti::G::TempFile Class Reference

A C++ wrapper interface for the g_file_open_tmp() and g_mkstemp() functions. More...

`#include <inti/glib/fileutils.h>`

Inheritance diagram for Inti::G::TempFile:

List of all members.

## Public Member Functions

Constructors

- TempFile ()
    Construct an empty temporary file.
- TempFile (std::string &template_filename)
    Construct and open a temporary file with the template filename template_filename.
- TempFile (const std::string &template_filename, std::string &actual_filename, G::Error *error)
    Construct and open a temporary file with the template filename template_filename.
- ~TempFile ()
    Destructor; Closes the temporary file if it's open.

Accessors

- bool is_open () const
    Determines whether the temporary file is open.

Methods

- bool open (std::string &actual_filename, G::Error *error)
    Opens a file for writing in the preferred directory for temporary files (as returned by g_get_tmp_dir()).
- bool open (const std::string &template_filename, std::string &actual_filename, G::Error *error)
    Opens a file for writing in the preferred directory for temporary files (as returned by g_get_tmp_dir()).
- bool mkstemp (std::string &template_filename)
    Opens a temporary file.
- bool close ()
    Closes an open temporary file.

## Detailed Description

A C++ wrapper interface for the g_file_open_tmp() and g_mkstemp() functions.

TempFile is a convenient wrapper class for the g_file_open_tmp() and g_mkstemp() functions. It is a StackObject and must created on the stack. You can't allocate a TempFile dynamically.

Example 1: You can construct an empty temporary file using the default constructor and then call either one of the open() methods or mkstemp().

        G::TempFile file;
        std::string filename("/home/testfile");
        if (file.mkstemp(filename))
        {
            // write to filename
        }
        file.close();

Example 2: Alternatively you can construct and open a temporary file in one step by calling one of the parameterized constructors.

        std::string filename;
        G::Error error;
        G::TempFile file("testfile", filename, &error);
        if (file.is_open())
        {
            // write to filename
        }
        file.close();

You don't have to explicitly call TempFile::close() as above, unless you want to. When a TempFile object goes out of scope its destructor will check for an open file and close it.

## Constructor & Destructor Documentation

Inti::G::TempFile::TempFile | ( | std::string & | template_filename | ) |

Construct and open a temporary file with the template filename template_filename.

Parameters:

  template_filename | The template for the filename.

See mkstemp() for details.

Inti::G::TempFile::TempFile | ( | const std::string & | template_filename,
|  | std::string & | actual_filename,
|  | G::Error * | error
| ) |

Construct and open a temporary file with the template filename template_filename.

Parameters:

  template_filename | The prefix template for the file name, but basename only.
  actual_filename | The location to store actual filename used, includes full path.
  error | The return location for a G::Error, or null.

See open() for details.

## Member Function Documentation

bool Inti::G::TempFile::is_open | ( |  | ) | const

Determines whether the temporary file is open.

Returns:
  true if the file is open.

bool Inti::G::TempFile::mkstemp | ( | std::string & | template_filename | ) |

Opens a temporary file.

Parameters:

  template_filename | The template file name including any directory paths.

Returns:
  true if the file was opened for reading and writing.

The file is opened in binary mode on platforms where there is a difference. If the file is not explicitly closed with close() it will automatically be closed when the file goes out of scope. If an error occurs false is returned and error will be set. Otherwise true is returned.

Do not suffix template_filename with any 'X' characters, this method will do that for you. On returning template_filename will have been modified to form the name of a file that didn't exist.

bool Inti::G::TempFile::open | ( | const std::string & | template_filename,
|  | std::string & | actual_filename,
|  | G::Error * | error
| ) |

Opens a file for writing in the preferred directory for temporary files (as returned by g_get_tmp_dir()).

Parameters:

  template_filename | The prefix template for the file name, but basename only.
  actual_filename | The location to store actual filename used, includes full path.
  error | The return location for a G::Error, or null.

Returns:
  true if the file was opened for reading and writing.

The file is opened in binary mode on platforms where there is a difference. If the file is not explicitly closed with close() it will automatically be closed when the file goes out of scope. If an error occurs false is returned and error will be set. Otherwise true is returned.

Do not suffix template_filename with any 'X' characters, this method will do that for you. Unlike mkstemp() template_filename should only be a basename, no directory components are allowed. Note that in contrast to mkstemp() template_filename is not modified, and might thus be a read-only literal string. On return the actual_filename holds the full path and filename of the temporary file.

bool Inti::G::TempFile::open | ( | std::string & | actual_filename,
|  | G::Error * | error
| ) |

Opens a file for writing in the preferred directory for temporary files (as returned by g_get_tmp_dir()).

Parameters:

  actual_filename | The location to store actual filename used, includes full path.
  error | The return location for a G::Error, or null.

Returns:
  true if the file was opened for reading and writing.

This version of open() uses a default template (see open(const std::string&, std::string&, G::Error*)).

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

- fileutils.h

Main Page - Footer