Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
file_io.hpp
Go to the documentation of this file.
1#pragma once
5#include <cstdint>
6#include <cstring>
7#include <fcntl.h>
8#include <fstream>
9#include <ios>
10#include <iostream>
11#include <sstream>
12#include <sys/stat.h>
13#include <unistd.h>
14#include <vector>
15
16namespace bb {
17inline size_t get_file_size(std::string const& filename)
18{
19 // Open the file in binary mode and move to the end.
20 std::ifstream file(filename, std::ios::binary | std::ios::ate);
21 if (!file) {
22 return 0;
23 }
24
25 file.seekg(0, std::ios::end);
26 return (size_t)file.tellg();
27}
28
29inline std::vector<uint8_t> read_file(const std::string& filename, size_t bytes = 0)
30{
31 // Standard input. We'll iterate over the stream and reallocate.
32 if (filename == "-") {
34 }
35
36 std::ifstream file(filename, std::ios::binary);
37 if (!file) {
38 THROW std::runtime_error("Unable to open file: " + filename);
39 }
40
41 // Unseekable, pipe or process substitution. We'll iterate over the stream and reallocate.
42 if (!file.seekg(0, std::ios::end)) {
43 file.clear();
45 }
46
47 // Get the file size.
48 auto size = static_cast<size_t>(file.tellg());
49 file.seekg(0, std::ios::beg);
50
51 // Create a vector preallocated with enough space for the file data and read it.
52 auto to_read = bytes == 0 ? size : bytes;
53 std::vector<uint8_t> fileData(to_read);
54 file.read(reinterpret_cast<char*>(fileData.data()), (std::streamsize)to_read);
55 return fileData;
56}
57
58inline void write_file(const std::string& filename, std::vector<uint8_t> const& data)
59{
60 struct stat st;
61 if (stat(filename.c_str(), &st) == 0 && S_ISFIFO(st.st_mode)) {
62 // Writing to a pipe or file descriptor
63 int fd = open(filename.c_str(), O_WRONLY);
64 if (fd == -1) {
65 THROW std::runtime_error("Failed to open file descriptor: " + filename);
66 }
67
68 size_t total_written = 0;
69 size_t data_size = data.size();
70 while (total_written < data_size) {
71 ssize_t written = ::write(fd, data.data() + total_written, data_size - total_written);
72 if (written == -1) {
73 close(fd);
74 THROW std::runtime_error("Failed to write to file descriptor: " + filename);
75 }
76 total_written += static_cast<size_t>(written);
77 }
78 close(fd);
79 } else {
80 std::ofstream file(filename, std::ios::binary);
81 if (!file) {
82 THROW std::runtime_error("Failed to open data file for writing: " + filename + " (" + strerror(errno) +
83 ")");
84 }
85 file.write(reinterpret_cast<const char*>(data.data()), static_cast<std::streamsize>(data.size()));
86 file.close();
87 }
88}
89
90template <typename Fr> inline std::string field_elements_to_json(const std::vector<Fr>& fields)
91{
92 std::stringstream ss;
93 ss << "[";
94 for (size_t i = 0; i < fields.size(); ++i) {
95 ss << '"' << fields[i] << '"';
96 if (i != fields.size() - 1) {
97 ss << ",";
98 }
99 }
100 ss << "]";
101 return ss.str();
102}
103} // namespace bb
const std::vector< FF > data
Entry point for Barretenberg command-line interface.
void write(B &buf, field2< base_field, Params > const &value)
std::vector< uint8_t > read_file(const std::string &filename, size_t bytes=0)
Definition file_io.hpp:29
void write_file(const std::string &filename, std::vector< uint8_t > const &data)
Definition file_io.hpp:58
std::string field_elements_to_json(const std::vector< Fr > &fields)
Definition file_io.hpp:90
size_t get_file_size(std::string const &filename)
Definition file_io.hpp:17
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
#define THROW