OSDN Git Service

readme 更新
[yamml/yamml-git.git] / src / driver / file_cpp.cpp
1
2 #ifndef _WIN32
3
4 #include <cstdint>
5
6 #include <fstream>
7 #include <ios>
8 #include <iostream>
9 #include <sstream>
10 #include <stdexcept>
11 #include <string>
12 #include <vector>
13
14 #include "file.hpp"
15
16 namespace YAMML
17 {
18
19 namespace Driver
20 {
21
22 std::string ReadTextFile(const std::string& filePath)
23 {
24     std::ifstream ifs(filePath);
25
26     if (!ifs)
27     {
28         throw FileOpenException(filePath);
29     }
30
31     std::ostringstream oss;
32     oss << ifs.rdbuf();
33
34     if (!oss)
35     {
36         throw IOException();
37     }
38
39     return oss.str();
40 }
41
42 void WriteBinaryFile(const std::string& filePath, const std::vector<std::uint8_t>& buffer)
43 {
44     std::ofstream ofs(filePath, std::ios_base::binary);
45
46     if (!ofs)
47     {
48         throw FileOpenException(filePath);
49     }
50
51     ofs.write(reinterpret_cast<const char*>(buffer.data()), buffer.size());
52
53     if (ofs.bad())
54     {
55         throw IOException();
56     }
57 }
58
59 } // namespace Driver
60
61 } // namespace YAMML
62
63 #endif // _WIN32