Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
TempFile.cxx
Go to the documentation of this file.
1
36#ifndef _POSIX_C_SOURCE
37#define _POSIX_C_SOURCE 200112L
38#endif
39
40#if defined(__MACH__)
41#define _DARWIN_C_SOURCE // mkdtemp
42#endif
43
44#if defined(ESP_NONOS)
45#define __sh__ // for ftruncate
46#endif
47
48#include "os/TempFile.hxx"
49
50#include <unistd.h>
51
53#if !defined(__FreeRTOS__) && !defined(__WINNT__)
55{
56#if defined(__linux__) || defined(__MACH__)
57 dirName_ = "/tmp/openmrntmpdirXXXXXX";
58#else
59 dirName_ = "./openmrntmpdirXXXXXX";
60#endif
61 dirName_.c_str();
62 HASSERT(mkdtemp(&dirName_[0]));
63}
64#endif
65
66//
67// TempFile::TempFile()
68//
69TempFile::TempFile(const TempDir& dir, const string& basename)
70{
71 fileName_ = dir.name() + "/" + basename + ".XXXXXX";
72 fileName_.c_str();
73 fd_ = mkstemp((char*)fileName_.c_str());
74}
75
76//
77// TempFile::rewrite()
78//
79void TempFile::rewrite(const string& s)
80{
81 ::lseek(fd_, 0, SEEK_SET);
82 ::ftruncate(fd_, 0);
83 write(s);
84}
85
This class creates a temporary directory for the test, and removes it when the test is done.
Definition TempFile.hxx:52
const string & name() const
Definition TempFile.hxx:73
string dirName_
name of the temporary directory.
Definition TempFile.hxx:79
string fileName_
The full path name.
Definition TempFile.hxx:136
TempFile(const TempDir &dir, const string &basename)
Constructor.
Definition TempFile.cxx:69
int fd_
The file descriptor.
Definition TempFile.hxx:138
void rewrite(const string &s)
writes the given data to the temporary file from offset 0.
Definition TempFile.cxx:79
void write(const uint8_t byte)
writes a single byte to the temporary file.
Definition TempFile.hxx:110
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138