Version: SMASH-1.5
file.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2018
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #include "smash/file.h"
11 
12 namespace smash {
13 
14 FilePtr fopen(const bf::path& filename, const std::string& mode) {
15  FilePtr f{std::fopen(filename.c_str(), mode.c_str())};
16  return f;
17 }
18 
19 RenamingFilePtr::RenamingFilePtr(const bf::path& filename,
20  const std::string& mode) {
21  filename_ = filename;
22  filename_unfinished_ = filename;
23  filename_unfinished_ += ".unfinished";
24  file_ = std::fopen(filename_unfinished_.c_str(), mode.c_str());
25 }
26 
27 FILE* RenamingFilePtr::get() { return file_; }
28 
30  std::fclose(file_);
31  bf::rename(filename_unfinished_, filename_);
32 }
33 
34 } // namespace smash
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
FILE * get()
Get the underlying FILE* pointer.
Definition: file.cc:27
bf::path filename_unfinished_
Path of the unfinished file.
Definition: file.h:95
std::unique_ptr< std::FILE, FileDeleter > FilePtr
A RAII type to replace std::FILE *.
Definition: file.h:63
RenamingFilePtr(const bf::path &filename, const std::string &mode)
Construct a RenamingFilePtr.
Definition: file.cc:19
~RenamingFilePtr()
Close the file and rename it.
Definition: file.cc:29
Definition: action.h:24