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 bool exists(const std::string& file)
30 {
31 std::ifstream f(file.c_str());
32 return f.good();
33 }
34
43 std::vector<uint8_t> slurp(const std::string& file, bool optional = false)
44 {
45 std::ifstream f(file, std::ios::binary | std::ios::ate);
46
47 if (!f)
48 {
49 if (optional)
50 {
51 return {};
52 }
53 else
54 {
55 std::cerr << "Could not open file " << file << std::endl;
56 exit(-1);
57 }
58 }
59
60 auto size = f.tellg();
61 f.seekg(0, std::ios::beg);
62
63 std::vector<uint8_t> data(size);
64 f.read((char*)data.data(), size);
65
66 if (!optional && !f)
67 {
68 std::cerr << "Could not read file " << file << std::endl;
69 exit(-1);
70 }
71 return data;
72 }
73
82 std::string slurp_string(const std::string& file, bool optional = false)
83 {
84 auto v = slurp(file, optional);
85 return {v.begin(), v.end()};
86 }
87
88 std::optional<std::string> try_slurp_string(const std::string& file)
89 {
90 if (!fs::exists(file))
91 {
92 return std::nullopt;
93 }
94 return files::slurp_string(file);
95 }
96
106 nlohmann::json slurp_json(const std::string& file, bool optional = false)
107 {
108 auto v = slurp(file, optional);
109 if (v.size() == 0)
110 return nlohmann::json();
111
112 return nlohmann::json::parse(v.begin(), v.end());
113 }
114
121 void dump(const std::vector<uint8_t>& data, const std::string& file)
122 {
123 using namespace std;
124 ofstream f(file, ios::binary | ios::trunc);
125 f.write((char*)data.data(), data.size());
126 if (!f)
127 throw logic_error("Failed to write to file: " + file);
128 }
129
136 void dump(const std::string& data, const std::string& file)
137 {
138 return dump(std::vector<uint8_t>(data.begin(), data.end()), file);
139 }
140
141 void rename(const fs::path& src, const fs::path& dst)
142 {
143 std::error_code ec;
144 fs::rename(src, dst, ec);
145 if (ec)
146 {
147 throw std::logic_error(fmt::format(
148 "Could not rename file {} to {}: {}",
149 src.string(),
150 dst.string(),
151 ec.message()));
152 }
153 }
154}
Definition files.h:20
std::vector< uint8_t > slurp(const std::string &file, bool optional=false)
Tries to read a file as byte vector.
Definition files.h:43
void dump(const std::vector< uint8_t > &data, const std::string &file)
Writes the content of a vector to a file.
Definition files.h:121
void rename(const fs::path &src, const fs::path &dst)
Definition files.h:141
std::string slurp_string(const std::string &file, bool optional=false)
Tries to read a file as string.
Definition files.h:82
nlohmann::json slurp_json(const std::string &file, bool optional=false)
Tries to read a file as JSON.
Definition files.h:106
bool exists(const std::string &file)
Checks if a path exists.
Definition files.h:29
std::optional< std::string > try_slurp_string(const std::string &file)
Definition files.h:88
STL namespace.