treelite
file_utils.h
Go to the documentation of this file.
1 
8 #ifndef TREELITE_DETAIL_FILE_UTILS_H_
9 #define TREELITE_DETAIL_FILE_UTILS_H_
10 
11 #include <treelite/logging.h>
12 
13 #include <cstdio>
14 #include <filesystem>
15 #include <fstream>
16 #include <string>
17 
18 namespace treelite::detail {
19 
20 inline std::ifstream OpenFileForReadAsStream(std::filesystem::path const& filepath) {
21  auto path = std::filesystem::weakly_canonical(filepath);
22  TREELITE_CHECK(std::filesystem::exists(path)) << "Path " << filepath << " does not exist";
23  std::ifstream ifs(path, std::ios::in | std::ios::binary);
24  TREELITE_CHECK(ifs) << "Could not open file " << filepath;
25  ifs.exceptions(std::ios::badbit); // Throw exceptions on error
26  // We don't throw on failbit, since we sometimes want to read
27  // until the end of file in a loop of form `while
28  // (std::getline(...))`, which will set failbit.
29  return ifs;
30 }
31 
32 inline std::ifstream OpenFileForReadAsStream(std::string const& filename) {
33  return OpenFileForReadAsStream(std::filesystem::u8path(filename));
34 }
35 
36 inline std::ifstream OpenFileForReadAsStream(char const* filename) {
37  return OpenFileForReadAsStream(std::string(filename));
38 }
39 
40 inline std::ofstream OpenFileForWriteAsStream(std::filesystem::path const& filepath) {
41  auto path = std::filesystem::weakly_canonical(filepath);
42  TREELITE_CHECK(path.has_filename()) << "Cannot write to a directory; please specify a file";
43  TREELITE_CHECK(std::filesystem::exists(path.parent_path()))
44  << "Path " << path.parent_path() << " does not exist";
45  std::ofstream ofs(path, std::ios::out | std::ios::binary);
46  TREELITE_CHECK(ofs) << "Could not open file " << filepath;
47  ofs.exceptions(std::ios::failbit | std::ios::badbit); // Throw exceptions on error
48  return ofs;
49 }
50 
51 inline std::ofstream OpenFileForWriteAsStream(std::string const& filename) {
52  return OpenFileForWriteAsStream(std::filesystem::u8path(filename));
53 }
54 
55 inline std::ofstream OpenFileForWriteAsStream(char const* filename) {
56  return OpenFileForWriteAsStream(std::string(filename));
57 }
58 
59 inline FILE* OpenFileForReadAsFilePtr(std::filesystem::path const& filepath) {
60  auto path = std::filesystem::weakly_canonical(filepath);
61  TREELITE_CHECK(std::filesystem::exists(path)) << "Path " << filepath << " does not exist";
62  FILE* fp;
63 #ifdef _WIN32
64  fp = _wfopen(path.wstring().c_str(), L"rb");
65 #else
66  fp = std::fopen(path.string().c_str(), "rb");
67 #endif
68  TREELITE_CHECK(fp) << "Could not open file " << filepath;
69  return fp;
70 }
71 
72 inline FILE* OpenFileForReadAsFilePtr(std::string const& filename) {
73  return OpenFileForReadAsFilePtr(std::filesystem::u8path(filename));
74 }
75 
76 inline FILE* OpenFileForReadAsFilePtr(char const* filename) {
77  return OpenFileForReadAsFilePtr(std::string(filename));
78 }
79 
80 } // namespace treelite::detail
81 
82 #endif // TREELITE_DETAIL_FILE_UTILS_H_
logging facility for Treelite
#define TREELITE_CHECK(x)
Definition: logging.h:70
Definition: file_utils.h:18
std::ifstream OpenFileForReadAsStream(std::filesystem::path const &filepath)
Definition: file_utils.h:20
FILE * OpenFileForReadAsFilePtr(std::filesystem::path const &filepath)
Definition: file_utils.h:59
std::ofstream OpenFileForWriteAsStream(std::filesystem::path const &filepath)
Definition: file_utils.h:40