raylib-cpp
C++ object-oriented wrapper library for raylib.
Loading...
Searching...
No Matches
FileText.hpp
1#ifndef RAYLIB_CPP_INCLUDE_FILETEXT_HPP_
2#define RAYLIB_CPP_INCLUDE_FILETEXT_HPP_
3
4#include <string>
5#include <utility>
6
7#include "./raylib.hpp"
8#include "./raylib-cpp-utils.hpp"
9
10namespace raylib {
11
12class FileText {
13 public:
14 FileText() = default;
15 FileText(const FileText&) = delete;
16 FileText(FileText&& other) noexcept : data(other.data), length(other.length) {
17 other.data = nullptr;
18 other.length = 0;
19 }
20 FileText& operator=(const FileText&) = delete;
21 FileText& operator=(FileText&& other) noexcept {
22 std::swap(data, other.data);
23 std::swap(length, other.length);
24 return *this;
25 }
26 ~FileText() { Unload(); }
27
28 explicit FileText(const std::string_view fileName) {
29 Load(fileName);
30 }
31
32 GETTER(const char*, Data, data)
33 GETTER(unsigned int, Length, length)
34
35 [[nodiscard]] const char* c_str() const { return data; }
36
37 [[nodiscard]] std::string ToString() const { return data; }
38 explicit operator std::string() const {
39 return data;
40 }
41
42 void Load(const std::string_view fileName) { Load(fileName.data()); }
43 void Load(const char* fileName) {
44 data = ::LoadFileText(fileName);
45 length = ::TextLength(data);
46 }
47
48 void Unload() {
49 if (data != nullptr) {
50 ::UnloadFileText(data);
51 data = nullptr;
52 length = 0;
53 }
54 }
55
56 private:
57 char* data{nullptr};
58 unsigned int length{0};
59};
60
61} // namespace raylib
62
63using RFileText = raylib::FileText;
64
65#endif // RAYLIB_CPP_INCLUDE_FILETEXT_HPP_