Branch data Line data Source code
# 1 : : // Copyright (c) 2015-2020 The Bitcoin Core developers
# 2 : : // Copyright (c) 2017 The Zcash developers
# 3 : : // Distributed under the MIT software license, see the accompanying
# 4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
# 5 : :
# 6 : : #include <fs.h>
# 7 : :
# 8 : : #include <limits>
# 9 : : #include <stdio.h>
# 10 : : #include <string>
# 11 : : #include <utility>
# 12 : :
# 13 : : std::pair<bool,std::string> ReadBinaryFile(const fs::path &filename, size_t maxsize=std::numeric_limits<size_t>::max())
# 14 : 0 : {
# 15 : 0 : FILE *f = fsbridge::fopen(filename, "rb");
# 16 [ # # ]: 0 : if (f == nullptr)
# 17 : 0 : return std::make_pair(false,"");
# 18 : 0 : std::string retval;
# 19 : 0 : char buffer[128];
# 20 : 0 : do {
# 21 : 0 : const size_t n = fread(buffer, 1, sizeof(buffer), f);
# 22 : : // Check for reading errors so we don't return any data if we couldn't
# 23 : : // read the entire file (or up to maxsize)
# 24 [ # # ]: 0 : if (ferror(f)) {
# 25 : 0 : fclose(f);
# 26 : 0 : return std::make_pair(false,"");
# 27 : 0 : }
# 28 : 0 : retval.append(buffer, buffer+n);
# 29 [ # # ][ # # ]: 0 : } while (!feof(f) && retval.size() <= maxsize);
# 30 : 0 : fclose(f);
# 31 : 0 : return std::make_pair(true,retval);
# 32 : 0 : }
# 33 : :
# 34 : : bool WriteBinaryFile(const fs::path &filename, const std::string &data)
# 35 : 0 : {
# 36 : 0 : FILE *f = fsbridge::fopen(filename, "wb");
# 37 [ # # ]: 0 : if (f == nullptr)
# 38 : 0 : return false;
# 39 [ # # ]: 0 : if (fwrite(data.data(), 1, data.size(), f) != data.size()) {
# 40 : 0 : fclose(f);
# 41 : 0 : return false;
# 42 : 0 : }
# 43 [ # # ]: 0 : if (fclose(f) != 0) {
# 44 : 0 : return false;
# 45 : 0 : }
# 46 : 0 : return true;
# 47 : 0 : }
|