Skip to content

Commit

Permalink
Add FileData and FileText
Browse files Browse the repository at this point in the history
  • Loading branch information
furudbat committed Mar 2, 2024
1 parent b3bc196 commit cc8b01a
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
56 changes: 56 additions & 0 deletions include/FileData.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#ifndef RAYLIB_CPP_INCLUDE_FILEDATA_HPP_
#define RAYLIB_CPP_INCLUDE_FILEDATA_HPP_

#include <string>
#include <utility>

#include "./raylib.hpp"
#include "./raylib-cpp-utils.hpp"

namespace raylib {

class FileData {
public:
FileData() = default;
FileData(const FileData&) = delete;
FileData(FileData&& other) noexcept : data(other.data), bytesRead(other.bytesRead) {
other.data = nullptr;
other.bytesRead = 0;
}
FileData& operator=(const FileData&) = delete;
FileData& operator=(FileData&& other) noexcept {
std::swap(data, other.data);
std::swap(bytesRead, other.bytesRead);
return *this;
}
~FileData() { Unload(); }

explicit FileData(const std::string& fileName) {
Load(fileName);
}

GETTER(const unsigned char*, Data, data)
GETTER(int, BytesRead, bytesRead)

void Load(const std::string& fileName) { Load(fileName.c_str()); }
void Load(const char* fileName) {
data = ::LoadFileData(fileName, &bytesRead);
}

void Unload() {
if (data != nullptr) {
::UnloadFileData(data);
data = nullptr;
}
}

private:
unsigned char* data{nullptr};
int bytesRead{0};
};

} // namespace raylib

using RFileData = raylib::FileData;

#endif // RAYLIB_CPP_INCLUDE_FILEDATA_HPP_
65 changes: 65 additions & 0 deletions include/FileText.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#ifndef RAYLIB_CPP_INCLUDE_FILETEXT_HPP_
#define RAYLIB_CPP_INCLUDE_FILETEXT_HPP_

#include <string>
#include <utility>

#include "./raylib.hpp"
#include "./raylib-cpp-utils.hpp"

namespace raylib {

class FileText {
public:
FileText() = default;
FileText(const FileText&) = delete;
FileText(FileText&& other) noexcept : data(other.data), length(other.length) {
other.data = nullptr;
other.length = 0;
}
FileText& operator=(const FileText&) = delete;
FileText& operator=(FileText&& other) noexcept {
std::swap(data, other.data);
std::swap(length, other.length);
return *this;
}
~FileText() { Unload(); }

explicit FileText(const std::string& fileName) {
Load(fileName);
}

GETTER(const char*, Data, data)
GETTER(unsigned int, Length, length)

[[nodiscard]] const char* c_str() const { return data; }

[[nodiscard]] std::string ToString() const { return data; }
explicit operator std::string() const {
return data;
}

void Load(const std::string& fileName) { Load(fileName.c_str()); }
void Load(const char* fileName) {
data = ::LoadFileText(fileName);
length = ::TextLength(data);
}

void Unload() {
if (data != nullptr) {
::UnloadFileText(data);
data = nullptr;
length = 0;
}
}

private:
char* data{nullptr};
unsigned int length{0};
};

} // namespace raylib

using RFileText = raylib::FileText;

#endif // RAYLIB_CPP_INCLUDE_FILETEXT_HPP_
13 changes: 13 additions & 0 deletions include/raylib-cpp-utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,17 @@
void Set##method(type value) { name = value; }
#endif

#ifndef GETTER
/**
* A utility to build get methods on top of a property.
*
* @param type The type of the property.
* @param method The human-readable name for the method.
* @param name The machine-readable name of the property.
*/
#define GETTER(type, method, name) \
/** Retrieves the name value for the object. @return The name value of the object. */ \
type Get##method() const { return name; }
#endif

#endif // RAYLIB_CPP_INCLUDE_RAYLIB_CPP_UTILS_HPP_
2 changes: 2 additions & 0 deletions include/raylib-cpp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#include "./Camera2D.hpp"
#include "./Camera3D.hpp"
#include "./Color.hpp"
#include "./FileData.hpp"
#include "./FileText.hpp"
#include "./Font.hpp"
#include "./Functions.hpp"
#include "./Gamepad.hpp"
Expand Down

0 comments on commit cc8b01a

Please sign in to comment.