mirror of
https://github.com/OpenVPN/openvpn3.git
synced 2024-09-20 20:13:05 +02:00
54 lines
1.4 KiB
C++
54 lines
1.4 KiB
C++
#ifndef OPENVPN_COMMON_FILE_H
|
|
#define OPENVPN_COMMON_FILE_H
|
|
|
|
#include <string>
|
|
#include <fstream>
|
|
|
|
#include <openvpn/common/exception.hpp>
|
|
#include <openvpn/buffer/buffer.hpp>
|
|
|
|
namespace openvpn {
|
|
|
|
OPENVPN_EXCEPTION(open_file_error);
|
|
|
|
std::string read_text(const std::string& filename)
|
|
{
|
|
std::ifstream ifs(filename.c_str());
|
|
if (!ifs)
|
|
OPENVPN_THROW(open_file_error, "cannot open " << filename);
|
|
const std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
|
|
if (!ifs)
|
|
OPENVPN_THROW(open_file_error, "cannot read " << filename);
|
|
return str;
|
|
}
|
|
|
|
BufferPtr read_binary(const std::string& filename, const unsigned int buffer_flags = 0)
|
|
{
|
|
std::ifstream ifs(filename.c_str(), std::ios::binary);
|
|
if (!ifs)
|
|
OPENVPN_THROW(open_file_error, "cannot open " << filename);
|
|
|
|
// get length of file
|
|
ifs.seekg (0, std::ios::end);
|
|
const std::streamsize length = ifs.tellg();
|
|
ifs.seekg (0, std::ios::beg);
|
|
|
|
// allocate buffer
|
|
BufferPtr b = new BufferAllocated(size_t(length), buffer_flags | BufferAllocated::ARRAY);
|
|
|
|
// read data
|
|
ifs.read((char *)b->data(), length);
|
|
|
|
// check for errors
|
|
if (ifs.gcount() != length)
|
|
OPENVPN_THROW(open_file_error, "read length inconsistency " << filename);
|
|
if (!ifs)
|
|
OPENVPN_THROW(open_file_error, "cannot read " << filename);
|
|
|
|
return b;
|
|
}
|
|
|
|
} // namespace openvpn
|
|
|
|
#endif // OPENVPN_COMMON_FILE_H
|