0
0
mirror of https://github.com/OpenVPN/openvpn3.git synced 2024-09-20 20:13:05 +02:00
openvpn3/openvpn/common/file.hpp
James Yonan b37eb264d3 iOS client changes:
OpenVPN 1.0 Beta 4

* Fixed issue where large profiles were hitting against a limitation
  in Apple VPN API (configd[14] <Error>: VPN Controller: failed to
  write to VPN control socket - msgtype: 2050).  The fix is to pass the
  profile to the plugin via a temporary file rather than putting the
  file content into the plist.

* Added Help section toggle button.

* Added Private Tunnel import.

* Added anti-race sequencing to prevent connection request processing
  until after callbacks and event stream subscriptions have been
  set up.
2012-08-12 00:32:15 +00:00

59 lines
1.6 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);
inline 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;
}
inline 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;
}
inline std::string read_text_fast(const std::string& filename)
{
BufferPtr bp = read_binary(filename);
return std::string((const char *)bp->c_data(), bp->size());
}
} // namespace openvpn
#endif // OPENVPN_COMMON_FILE_H