7 #ifndef ZEN_XML_IO_HEADER_8917640501480763248343343
8 #define ZEN_XML_IO_HEADER_8917640501480763248343343
12 #include <zen/scope_guard.h>
23 #if !defined(ZEN_WIN) && !defined(ZEN_LINUX) && !defined(ZEN_MAC)
24 #error Please specify your platform: #define ZEN_WIN, ZEN_LINUX or ZEN_MAC
31 typedef int ErrorCode;
40 namespace implemenation
42 template <
class String>
inline
43 FILE* fopen(
const String& filename,
const wchar_t* mode)
46 FILE* handle =
nullptr;
47 errno_t rv = ::_wfopen_s(&handle, utfCvrtTo<std::wstring>(filename).c_str(), mode);
51 return ::_wfopen(utfCvrtTo<std::wstring>(filename).c_str(), mode);
65 template <
class String>
66 void saveStream(
const std::string& stream,
const String& filename)
69 FILE* handle = implemenation::fopen(utfCvrtTo<std::wstring>(filename).c_str(), L
"wb");
71 FILE* handle = ::fopen(utfCvrtTo<std::string>(filename).c_str(),
"w");
73 if (handle ==
nullptr)
75 ZEN_ON_SCOPE_EXIT(::fclose(handle));
77 const size_t bytesWritten = ::fwrite(stream.c_str(), 1, stream.size(), handle);
78 if (::ferror(handle) != 0)
82 assert(bytesWritten == stream.size());
93 template <
class String>
97 FILE* handle = implemenation::fopen(utfCvrtTo<std::wstring>(filename).c_str(), L
"rb");
99 FILE* handle = ::fopen(utfCvrtTo<std::string>(filename).c_str(),
"r");
101 if (handle ==
nullptr)
103 ZEN_ON_SCOPE_EXIT(::fclose(handle));
106 const size_t blockSize = 64 * 1024;
109 stream.resize(stream.size() + blockSize);
111 const size_t bytesRead = ::fread(&*(stream.begin() + stream.size() - blockSize), 1, blockSize, handle);
112 if (::ferror(handle))
114 if (bytesRead > blockSize)
116 if (bytesRead < blockSize)
117 stream.resize(stream.size() - (blockSize - bytesRead));
119 while (!::feof(handle));
125 #endif //ZEN_XML_IO_HEADER_8917640501480763248343343
void saveStream(const std::string &stream, const String &filename)
Save byte stream to a file.
Definition: io.h:66
std::string loadStream(const String &filename)
Load byte stream from a file.
Definition: io.h:94
ErrorCode lastError
Native error code: errno.
Definition: io.h:35
Exception thrown due to failed file I/O.
Definition: io.h:29
Exception base class for zen::Xml.
Definition: error.h:13