Version: SMASH-1.5
smash::RenamingFilePtr Class Reference

#include <file.h>

A RAII type to replace std::FILE *.

While open, the file name ends with ".unfinished".

Automatically closes and renames the file to the original when it goes out of scope.

Definition at line 73 of file file.h.

Collaboration diagram for smash::RenamingFilePtr:
[legend]

Public Member Functions

 RenamingFilePtr (const bf::path &filename, const std::string &mode)
 Construct a RenamingFilePtr. More...
 
FILE * get ()
 Get the underlying FILE* pointer. More...
 
 ~RenamingFilePtr ()
 Close the file and rename it. More...
 

Private Attributes

FILE * file_
 Internal file pointer. More...
 
bf::path filename_
 Path of the finished file. More...
 
bf::path filename_unfinished_
 Path of the unfinished file. More...
 

Constructor & Destructor Documentation

◆ RenamingFilePtr()

smash::RenamingFilePtr::RenamingFilePtr ( const bf::path &  filename,
const std::string &  mode 
)

Construct a RenamingFilePtr.

Parameters
[in]filenamePath to the file.
[in]modeThe mode in which the file should be opened (see std::fopen).
Returns
The constructed object.

Definition at line 19 of file file.cc.

20  {
21  filename_ = filename;
22  filename_unfinished_ = filename;
23  filename_unfinished_ += ".unfinished";
24  file_ = std::fopen(filename_unfinished_.c_str(), mode.c_str());
25 }
FilePtr fopen(const bf::path &filename, const std::string &mode)
Open a file with given mode.
Definition: file.cc:14
FILE * file_
Internal file pointer.
Definition: file.h:91
bf::path filename_
Path of the finished file.
Definition: file.h:93
bf::path filename_unfinished_
Path of the unfinished file.
Definition: file.h:95
Here is the call graph for this function:

◆ ~RenamingFilePtr()

smash::RenamingFilePtr::~RenamingFilePtr ( )

Close the file and rename it.

Definition at line 29 of file file.cc.

29  {
30  std::fclose(file_);
31  bf::rename(filename_unfinished_, filename_);
32 }
FILE * file_
Internal file pointer.
Definition: file.h:91
bf::path filename_
Path of the finished file.
Definition: file.h:93
bf::path filename_unfinished_
Path of the unfinished file.
Definition: file.h:95

Member Function Documentation

◆ get()

FILE * smash::RenamingFilePtr::get ( )

Get the underlying FILE* pointer.

Definition at line 27 of file file.cc.

27 { return file_; }
FILE * file_
Internal file pointer.
Definition: file.h:91
Here is the caller graph for this function:

Member Data Documentation

◆ file_

FILE* smash::RenamingFilePtr::file_
private

Internal file pointer.

Definition at line 91 of file file.h.

◆ filename_

bf::path smash::RenamingFilePtr::filename_
private

Path of the finished file.

Definition at line 93 of file file.h.

◆ filename_unfinished_

bf::path smash::RenamingFilePtr::filename_unfinished_
private

Path of the unfinished file.

Definition at line 95 of file file.h.


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