mirror of
https://github.com/Almamu/linux-wallpaperengine.git
synced 2025-09-14 13:56:48 +08:00

~ fixed a write to null error on CDirectory that wasn't taking into account that length can be null Signed-off-by: Alexis Maiquez <almamu@almamu.com>
52 lines
1.4 KiB
C++
52 lines
1.4 KiB
C++
#include "CContainer.h"
|
|
|
|
#include <cstring>
|
|
#include <utility>
|
|
|
|
using namespace WallpaperEngine::Assets;
|
|
|
|
CTexture* CContainer::readTexture (std::string filename)
|
|
{
|
|
// get the texture's filename (usually .tex)
|
|
filename = "materials/" + filename + ".tex";
|
|
|
|
void* textureContents = this->readFile (filename, nullptr);
|
|
|
|
return new CTexture (textureContents);
|
|
}
|
|
|
|
std::string CContainer::readVertexShader (const std::string& filename)
|
|
{
|
|
return this->readFileAsString ("shaders/" + filename + ".vert");
|
|
}
|
|
|
|
std::string CContainer::readFragmentShader (const std::string& filename)
|
|
{
|
|
return this->readFileAsString ("shaders/" + filename + ".frag");
|
|
}
|
|
|
|
std::string CContainer::readIncludeShader (const std::string& filename)
|
|
{
|
|
return this->readFileAsString ("shaders/" + filename);
|
|
}
|
|
|
|
std::string CContainer::readFileAsString (std::string filename)
|
|
{
|
|
uint32_t length = 0;
|
|
|
|
// read file contents and allocate a buffer for a string
|
|
void* contents = this->readFile (std::move (filename), &length);
|
|
char* buffer = new char [length + 1];
|
|
|
|
// ensure there's a 0 at the end
|
|
memset (buffer, 0, length + 1);
|
|
// copy over the data
|
|
memcpy (buffer, contents, length);
|
|
// now build the std::string to use
|
|
std::string result = buffer;
|
|
|
|
// free the intermediate buffer used to generate the std::string
|
|
delete[] buffer;
|
|
|
|
return result;
|
|
} |