File: misc.cpp

package info (click to toggle)
martchus-cpp-utilities 5.33.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,396 kB
  • sloc: cpp: 12,679; awk: 18; ansic: 12; makefile: 10
file content (67 lines) | stat: -rw-r--r-- 2,218 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#define CPP_UTILITIES_IOMISC_STRING_VIEW

#include "./misc.h"
#include "./nativefilestream.h"

#include <iterator>

using namespace std;

namespace CppUtilities {

/*!
 * \brief Reads all contents of the specified file in a single call.
 * \throws Throws std::ios_base::failure when an error occurs or the specified \a maxSize
 *         would be exceeded.
 */
std::string readFile(const std::string &path, std::string::size_type maxSize)
{
    auto file = NativeFileStream();
    file.exceptions(ios_base::failbit | ios_base::badbit);
    file.open(path, ios_base::in | ios_base::binary);
    file.seekg(0, ios_base::end);
    string res;
    const auto size = static_cast<string::size_type>(file.tellg());
    if (maxSize != string::npos && size > maxSize) {
        throw ios_base::failure("File exceeds max size");
    }
    res.reserve(size);
    file.seekg(ios_base::beg);
    // ignore warning about null pointer dereference from GCC 12 for now (which is *likely* not correct)
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wnull-dereference"
#endif
    res.assign((istreambuf_iterator<char>(file)), istreambuf_iterator<char>());
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
    return res;
}

/*!
 * \brief Reads all contents of the specified file in a single call.
 * \throws Throws std::ios_base::failure when an error occurs or the specified \a maxSize
 *         would be exceeded.
 */
std::string readFile(std::string_view path, std::string_view::size_type maxSize)
{
    return readFile(std::string(path), maxSize);
}

/*!
 * \brief Writes all \a contents to the specified file in a single call.
 * \throws Throws std::ios_base::failure when an error occurs.
 * \remarks Closing the file manually to prevent flushing the file within the d'tor which
 *          would suppress an exception
 */
void writeFile(std::string_view path, std::string_view contents)
{
    auto file = NativeFileStream();
    file.exceptions(ios_base::failbit | ios_base::badbit);
    file.open(std::string(path), ios_base::out | ios_base::trunc | ios_base::binary);
    file.write(contents.data(), static_cast<std::streamoff>(contents.size()));
    file.close();
}

} // namespace CppUtilities