CCF
Loading...
Searching...
No Matches
files.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
5#include <cstring>
6#include <fstream>
7#include <glob.h>
8#include <iostream>
9#include <nlohmann/json.hpp>
10#include <optional>
11#include <sstream>
12#include <string>
13#include <vector>
14
15#define FMT_HEADER_ONLY
16#include <fmt/format.h>
17#include <fmt/ostream.h>
18
19namespace files
20{
21 namespace fs = std::filesystem;
22
29 static bool exists(const std::string& file)
30 {
31 std::ifstream f(file.c_str());
32 return f.good();
33 }
34
43 static std::vector<uint8_t> slurp(
44 const std::string& file, bool optional = false)
45 {
46 std::ifstream f(file, std::ios::binary | std::ios::ate);
47
48 if (!f)
49 {
50 if (optional)
51 {
52 return {};
53 }
54 std::cerr << "Could not open file " << file << std::endl;
55 exit(-1); // NOLINT(concurrency-mt-unsafe)
56 }
57
58 auto size = f.tellg();
59 f.seekg(0, std::ios::beg);
60
61 std::vector<uint8_t> data(size);
62 f.read(reinterpret_cast<char*>(data.data()), size);
63
64 if (!optional && !f)
65 {
66 std::cerr << "Could not read file " << file << std::endl;
67 exit(-1); // NOLINT(concurrency-mt-unsafe)
68 }
69 return data;
70 }
71
80 static std::string slurp_string(
81 const std::string& file, bool optional = false)
82 {
83 auto v = slurp(file, optional);
84 return {v.begin(), v.end()};
85 }
86
87 static std::optional<std::string> try_slurp_string(const std::string& file)
88 {
89 if (!fs::exists(file))
90 {
91 return std::nullopt;
92 }
93 return files::slurp_string(file);
94 }
95
105 static nlohmann::json slurp_json(
106 const std::string& file, bool optional = false)
107 {
108 auto v = slurp(file, optional);
109 if (v.empty())
110 {
111 return {};
112 }
113
114 return nlohmann::json::parse(v.begin(), v.end());
115 }
116
123 static void dump(const std::vector<uint8_t>& data, const std::string& file)
124 {
125 using namespace std;
126 ofstream f(file, ios::binary | ios::trunc);
127 f.write(reinterpret_cast<const char*>(data.data()), data.size());
128 if (!f)
129 {
130 throw logic_error("Failed to write to file: " + file);
131 }
132 }
133
140 static void dump(const std::string& data, const std::string& file)
141 {
142 dump(std::vector<uint8_t>(data.begin(), data.end()), file);
143 }
144
145 static void rename(const fs::path& src, const fs::path& dst)
146 {
147 std::error_code ec;
148 fs::rename(src, dst, ec);
149 if (ec)
150 {
151 throw std::logic_error(fmt::format(
152 "Could not rename file {} to {}: {}",
153 src.string(),
154 dst.string(),
155 ec.message()));
156 }
157 }
158
159 static void create_directory(const fs::path& dir)
160 {
161 std::error_code ec;
162 fs::create_directory(dir, ec);
163 if (ec && ec != std::errc::file_exists)
164 {
165 throw std::logic_error(fmt::format(
166 "Could not create directory {}: {}", dir.string(), ec.message()));
167 }
168 }
169}
Definition files.h:20
STL namespace.