Joedb 9.1.4
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Portable_File.cpp
Go to the documentation of this file.
3
4namespace joedb::detail
5{
6 /////////////////////////////////////////////////////////////////////////////
7 Portable_File_Buffer::Portable_File_Buffer
8 /////////////////////////////////////////////////////////////////////////////
9 (
10 const char * const file_name,
11 const Open_Mode mode
12 )
13 {
14 constexpr auto in = std::ios::binary | std::ios::in;
15
16 if (mode == Open_Mode::read_existing)
17 {
18 filebuf.open(file_name, in);
19 }
20 else if (mode == Open_Mode::write_existing)
21 {
22 filebuf.open(file_name, in | std::ios::out);
23 }
24 else if (mode == Open_Mode::create_new)
25 {
26 if (filebuf.open(file_name, in))
27 throw Exception("File already exists: " + std::string(file_name));
28 filebuf.open(file_name, in | std::ios::out | std::ios::trunc);
29 }
31 {
32 filebuf.open(file_name, in | std::ios::out) ||
33 filebuf.open(file_name, in | std::ios::out | std::ios::trunc);
34 }
35 else
36 {
37 throw Exception
38 (
39 std::string(file_name) + ": unsupported mode for Portable_File"
40 );
41 }
42
43 if (!filebuf.is_open())
44 throw Exception("Cannot open file: " + std::string(file_name));
45 }
46}
Open_Mode
Definition Open_Mode.h:8
@ create_new
fails if already exists, locks the file for writing
@ write_existing
fails if does not exist or locked, locks the file for writing
@ write_existing_or_create_new
either write_existing or create_new depending on whether the file exists. Racy in Posix,...
@ read_existing
fails if does not exist