Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FileData and FileText #300

Merged
merged 2 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 13 additions & 0 deletions tests/raylib_cpp_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ int main(int argc, char *argv[]) {
Assert(passed, "Expected to have a RaylibException to be thrown");
}

// Load FileData
{
raylib::FileData file(path + "/resources/weird.wav");
Assert(file.GetBytesRead() > 0, "Expected file to be loaded correctly");
}

// Load FileText
{
raylib::FileText text(path + "/resources/lorem.txt");
Assert(text.GetLength() > 0, "Expected file to be loaded correctly");
AssertEqual(text.ToString().substr(0, 5), "Lorem");
}

TraceLog(LOG_INFO, "TEST: raylib-cpp test");
TraceLog(LOG_INFO, "---------------------");
return 0;
Expand Down
4 changes: 4 additions & 0 deletions tests/resources/lorem.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.