14#ifndef OPENVPN_COMMON_FILE_H
15#define OPENVPN_COMMON_FILE_H
28#if defined(OPENVPN_PLATFORM_WIN)
46 std::ifstream ifs(filename.c_str());
48 OPENVPN_THROW(open_file_error,
"cannot open for read: " << filename);
49 const std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
57 const std::uint64_t max_size = 0,
58 const unsigned int buffer_flags = 0)
60#if defined(OPENVPN_PLATFORM_WIN)
62 std::filesystem::path path(filenamew.get());
63 std::ifstream ifs(path, std::ios::binary);
65 std::ifstream ifs(filename.c_str(), std::ios::binary);
69 OPENVPN_THROW(open_file_error,
"cannot open for read: " << filename);
72 ifs.seekg(0, std::ios::end);
73 const std::streamsize length = ifs.tellg();
74 if (max_size && std::uint64_t(length) > max_size)
75 OPENVPN_THROW(file_too_large,
"file too large [" << length <<
'/' << max_size <<
"]: " << filename);
76 ifs.seekg(0, std::ios::beg);
82 ifs.read((
char *)b->data(), length);
85 if (ifs.gcount() != length)
86 OPENVPN_THROW(open_file_error,
"read length inconsistency: " << filename);
96 const std::uint64_t max_size = 0,
97 const size_t block_size = 1024)
99 std::ifstream ifs(filename.c_str(), std::ios::binary);
101 OPENVPN_THROW(open_file_error,
"cannot open for read: " << filename);
104 std::streamsize total_size = 0;
108 ifs.read((
char *)b->data(), b->remaining());
109 const std::streamsize size = ifs.gcount();
112 b->set_size(
static_cast<size_t>(size));
114 if (max_size && std::uint64_t(total_size) > max_size)
115 OPENVPN_THROW(file_too_large,
"file too large [" << total_size <<
'/' << max_size <<
"]: " << filename);
116 buflist.push_back(std::move(b));
123 return buflist.
join();
127inline std::string
read_text(
const std::string &filename,
const std::uint64_t max_size = 0)
130 if (bp->contains_null())
131 OPENVPN_THROW(file_is_binary,
"file is binary: " << filename);
132 return std::string((
const char *)bp->c_data(), bp->size());
136inline std::string
read_text_utf8(
const std::string &filename,
const std::uint64_t max_size = 0)
141 if (bp->contains_null())
142 OPENVPN_THROW(file_is_binary,
"file is binary: " << filename);
147 const unsigned char *data = bp->c_data();
148 if (data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF)
154 OPENVPN_THROW(file_not_utf8,
"file is not UTF8: " << filename);
156 return std::string((
const char *)bp->c_data(), bp->size());
164 while (std::getline(std::cin, line))
175 std::ofstream ofs(filename.c_str(), std::ios::binary);
177 OPENVPN_THROW(open_file_error,
"cannot open for write: " << filename);
178 ofs.write((
const char *)buf.
c_data(), buf.
size());
180 OPENVPN_THROW(open_file_error,
"cannot write: " << filename);
184template <
typename BUFLIST>
187 std::ofstream ofs(filename.c_str(), std::ios::binary);
189 OPENVPN_THROW(open_file_error,
"cannot open for write: " << filename);
190 for (
auto &buf : buflist)
192 ofs.write((
const char *)buf->c_data(), buf->size());
194 OPENVPN_THROW(open_file_error,
"cannot write: " << filename);
199inline void write_string(
const std::string &filename,
const std::string &str)
const T * c_data() const
Returns a const pointer to the start of the buffer.
size_t size() const
Returns the size of the buffer in T objects.
static Ptr Create(ArgsT &&...args)
Creates a new instance of RcEnable with the given arguments.
#define OPENVPN_UNTAGGED_EXCEPTION(C)
#define OPENVPN_UNTAGGED_EXCEPTION_INHERIT(B, C)
#define OPENVPN_THROW(exc, stuff)
bool is_valid_utf8_uchar_buf(const unsigned char *source, size_t size, const size_t max_len_flags=0)
std::unique_ptr< wchar_t[]> UTF16
wchar_t * utf16(const std::string &str, int cp=CP_UTF8)
Support deferred server-side state creation when client connects.
std::string read_text(const std::string &filename, const std::uint64_t max_size=0)
std::string read_text_utf8(const std::string &filename, const std::uint64_t max_size=0)
BufferPtr read_binary_linear(const std::string &filename, const std::uint64_t max_size=0, const size_t block_size=1024)
BufferPtr read_binary(const std::string &filename, const std::uint64_t max_size=0, const unsigned int buffer_flags=0)
void write_binary(const std::string &filename, const Buffer &buf)
void write_string(const std::string &filename, const std::string &str)
std::string read_text_simple(const std::string &filename)
BufferPtr buf_from_string(const std::string &str)
void write_binary_list(const std::string &filename, const BUFLIST &buflist)
@ ARRAY
if enabled, use as array
BufferPtr join(const size_t headroom, const size_t tailroom, const bool size_1_optim) const