diff --git a/inc/starlet-serializer/parser/mesh/meshParserBase.hpp b/inc/starlet-serializer/parser/mesh/meshParserBase.hpp index a754c08..b8b1490 100644 --- a/inc/starlet-serializer/parser/mesh/meshParserBase.hpp +++ b/inc/starlet-serializer/parser/mesh/meshParserBase.hpp @@ -6,13 +6,13 @@ namespace Starlet::Serializer { - struct MeshData; +struct MeshData; - class MeshParserBase : public Parser { - public: - virtual ~MeshParserBase() = default; +class MeshParserBase : public Parser { +public: + virtual ~MeshParserBase() = default; - virtual bool parse(const std::string& path, MeshData& out) = 0; - }; + virtual bool parse(const std::string& path, MeshData& out) = 0; +}; } \ No newline at end of file diff --git a/inc/starlet-serializer/parser/meshParser.hpp b/inc/starlet-serializer/parser/meshParser.hpp index 9b4fd83..46ceb1b 100644 --- a/inc/starlet-serializer/parser/meshParser.hpp +++ b/inc/starlet-serializer/parser/meshParser.hpp @@ -4,19 +4,19 @@ namespace Starlet::Serializer { - struct MeshData; +struct MeshData; - class MeshParser : public Parser { - public: - bool parse(const std::string& path, MeshData& out); +class MeshParser : public Parser { +public: + bool parse(const std::string& path, MeshData& out); - private: - enum class MeshFormat { - PLY, - UNKNOWN - }; - - MeshFormat detectFormat(const std::string& path); +private: + enum class MeshFormat { + PLY, + UNKNOWN }; + MeshFormat detectFormat(const std::string& path); +}; + } \ No newline at end of file diff --git a/inc/starlet-serializer/parser/parser.hpp b/inc/starlet-serializer/parser/parser.hpp index 50f1efb..b051be4 100644 --- a/inc/starlet-serializer/parser/parser.hpp +++ b/inc/starlet-serializer/parser/parser.hpp @@ -8,11 +8,9 @@ namespace Starlet { namespace Math { - -template struct Vec2; -template struct Vec3; -template struct Vec4; - + template struct Vec2; + template struct Vec3; + template struct Vec4; } namespace Serializer { @@ -20,23 +18,23 @@ namespace Serializer { #ifndef STARLET_PARSE_OR #define STARLET_PARSE_OR(onFail, parser, target, errorMsg) \ do { \ - if (!(parser(p, target))) { \ - if((errorMsg) && *(errorMsg) != '\0') fprintf(stderr, "[Parser ERROR]: Failed to parse %s\n", errorMsg); \ - onFail; \ + if (!(parser(p, target))) { \ + if((errorMsg) && *(errorMsg) != '\0') fprintf(stderr, "[Parser ERROR]: Failed to parse %s\n", errorMsg); \ + onFail; \ } \ } while(0) #endif #ifndef STARLET_PARSE_STRING_OR #define STARLET_PARSE_STRING_OR(onFail, p, target, size, label) \ - do {\ - char temp[size]{}; \ - if (!parseToken(p, reinterpret_cast(temp), size) || strlen(temp) == 0) { \ - fprintf(stderr, "[Parser ERROR] Failed to parse %s\n", label); \ - onFail; \ - } \ - target = temp; \ - } while (0) + do {\ + char temp[size]{}; \ + if (!parseToken(p, reinterpret_cast(temp), size) || strlen(temp) == 0) { \ + fprintf(stderr, "[Parser ERROR] Failed to parse %s\n", label); \ + onFail; \ + } \ + target = temp; \ + } while (0) #endif class Parser { diff --git a/src/parser/image/bmpParser.cpp b/src/parser/image/bmpParser.cpp index 7041038..c8c1e5b 100644 --- a/src/parser/image/bmpParser.cpp +++ b/src/parser/image/bmpParser.cpp @@ -31,7 +31,7 @@ bool BmpParser::parse(const std::string& path, ImageData& out) { const bool bottomUp = (height > 0); const uint32_t absHeight = static_cast(bottomUp ? height : -height); - if (!allocatePixelBuffer(out, width, absHeight)) + if (!allocatePixelBuffer(out, width, absHeight)) return false; if (!copyPixelData(p, file->size(), dataOffset, out, width, absHeight, bottomUp)) @@ -44,10 +44,10 @@ bool BmpParser::parseHeader(const unsigned char* p, size_t fileSize, uint32_t& w if (!validateFileSignature(p, fileSize)) return false; dataOffset = readUint32(p, 10); - if (dataOffset >= fileSize) return Logger::error("BmpParser", "parseBmpHeader", "Invalid data offset: " + std::to_string(dataOffset)); + if (dataOffset >= fileSize) return Logger::error("BmpParser", "parseHeader", "Invalid data offset: " + std::to_string(dataOffset)); uint32_t dibSize = readUint32(p, 14); - if (dibSize < BMP_DIB_HEADER_SIZE_MIN) return Logger::error("BmpParser", "parseBmpHeader", "Unsupported DIB header size: " + std::to_string(dibSize)); + if (dibSize < BMP_DIB_HEADER_SIZE_MIN) return Logger::error("BmpParser", "parseHeader", "Unsupported DIB header size: " + std::to_string(dibSize)); width = readUint32(p, 18); height = static_cast(readUint32(p, 22)); @@ -55,13 +55,13 @@ bool BmpParser::parseHeader(const unsigned char* p, size_t fileSize, uint32_t& w if (!validateDimensions(width, absHeight)) return false; uint16_t planes = readUint16(p, 26); - if (planes != BMP_PLANES_EXPECTED) return Logger::error("BmpParser", "parseBmpHeader", "Planes != 1: " + std::to_string(planes)); + if (planes != BMP_PLANES_EXPECTED) return Logger::error("BmpParser", "parseHeader", "Planes != 1: " + std::to_string(planes)); uint16_t bpp = readUint16(p, 28); - if (bpp != BMP_BPP_24) return Logger::error("BmpParser", "parseBmpHeader", "Only 24bpp supported: " + std::to_string(bpp)); + if (bpp != BMP_BPP_24) return Logger::error("BmpParser", "parseHeader", "Only 24bpp supported: " + std::to_string(bpp)); uint32_t compression = readUint32(p, 30); - if (compression != BMP_COMPRESSION_NONE) return Logger::error("BmpParser", "parseBmpHeader", "Compressed BMP not supported: " + std::to_string(compression)); + if (compression != BMP_COMPRESSION_NONE) return Logger::error("BmpParser", "parseHeader", "Compressed BMP not supported: " + std::to_string(compression)); return true; } diff --git a/src/parser/image/imageParserBase.cpp b/src/parser/image/imageParserBase.cpp index 6e85225..483e3af 100644 --- a/src/parser/image/imageParserBase.cpp +++ b/src/parser/image/imageParserBase.cpp @@ -14,12 +14,12 @@ void ImageParserBase::clearImageData(ImageData& data) const { std::optional> ImageParserBase::loadImageData(const std::string& path) { std::vector file; if (!loadBinaryFile(file, path)) { - Logger::error("ImageParser", "loadImageData", std::string("Failed to load file: ") + path); + Logger::error("ImageParserBase", "loadImageData", std::string("Failed to load file: ") + path); return std::nullopt; } if (file.empty()) { - Logger::error("ImageParser", "loadImageData", "File is empty"); + Logger::error("ImageParserBase", "loadImageData", "File is empty"); return std::nullopt; } @@ -27,11 +27,11 @@ std::optional> ImageParserBase::loadImageData(const s } bool ImageParserBase::validateDimensions(uint32_t width, uint32_t height) const { - if (width == 0) return Logger::error("ImageParser", "validateDimensions", "Invalid width: " + std::to_string(width)); - if (height == 0) return Logger::error("ImageParser", "validateDimensions", "Invalid height: " + std::to_string(height)); + if (width == 0) return Logger::error("ImageParserBase", "validateDimensions", "Invalid width: " + std::to_string(width)); + if (height == 0) return Logger::error("ImageParserBase", "validateDimensions", "Invalid height: " + std::to_string(height)); constexpr size_t MAX_DIMENSION = 65536; - if (width > MAX_DIMENSION) return Logger::error("ImageParser", "validateDimensions", "Width exceeds maximum: " + std::to_string(width)); - if (height > MAX_DIMENSION) return Logger::error("ImageParser", "validateDimensions", "Height exceeds maximum: " + std::to_string(height)); + if (width > MAX_DIMENSION) return Logger::error("ImageParserBase", "validateDimensions", "Width exceeds maximum: " + std::to_string(width)); + if (height > MAX_DIMENSION) return Logger::error("ImageParserBase", "validateDimensions", "Height exceeds maximum: " + std::to_string(height)); return true; } diff --git a/src/parser/image/tgaParser.cpp b/src/parser/image/tgaParser.cpp index dcb0550..41be84b 100644 --- a/src/parser/image/tgaParser.cpp +++ b/src/parser/image/tgaParser.cpp @@ -44,10 +44,10 @@ bool TgaParser::parseHeader(const unsigned char* p, size_t fileSize, uint32_t& w const uint8_t imageType = p[2]; if (imageType != TGA_IMAGE_TYPE_UNCOMPRESSED_TRUE_COLOR) - return Logger::error("TgaParser", "parseTgaHeader", "Unsupported TGA image type (only uncompressed true-colour TGA supported (2)), got: " + std::to_string(imageType)); + return Logger::error("TgaParser", "parseHeader", "Unsupported TGA image type (only uncompressed true-colour TGA supported (2)), got: " + std::to_string(imageType)); if (colourMapType != TGA_COLOUR_MAP_TYPE_NONE) - return Logger::error("TgaParser", "parseTgaHeader", "Unsupported TGA colour map type (only no colour map supported (0)), got: " + std::to_string(colourMapType)); + return Logger::error("TgaParser", "parseHeader", "Unsupported TGA colour map type (only no colour map supported (0)), got: " + std::to_string(colourMapType)); width = static_cast(readUint16(p, 12)); height = static_cast(readUint16(p, 14)); @@ -56,14 +56,14 @@ bool TgaParser::parseHeader(const unsigned char* p, size_t fileSize, uint32_t& w bpp = p[16]; if (bpp != 24 && bpp != 32) - return Logger::error("TgaParser", "parseTgaHeader", "Unsupported TGA bits per pixel (only 24 and 32 supported), got: " + std::to_string(bpp)); + return Logger::error("TgaParser", "parseHeader", "Unsupported TGA bits per pixel (only 24 and 32 supported), got: " + std::to_string(bpp)); const uint8_t imageDescriptor = p[17]; topDown = (imageDescriptor & TGA_ORIGIN_TOP_BIT) != 0; dataOffset = TGA_HEADER_SIZE + idLength; if (dataOffset >= fileSize) - return Logger::error("TgaParser", "parseTgaHeader", "Invalid data offset"); + return Logger::error("TgaParser", "parseHeader", "Invalid data offset"); return true; } diff --git a/src/parser/imageParser.cpp b/src/parser/imageParser.cpp index fcc372a..68daa6d 100644 --- a/src/parser/imageParser.cpp +++ b/src/parser/imageParser.cpp @@ -10,10 +10,8 @@ namespace Starlet::Serializer { bool ImageParser::parse(const std::string& path, ImageData& out) { - ImageFormat format = detectFormat(path); - std::unique_ptr parser; - switch (format) { + switch (detectFormat(path)) { case ImageFormat::BMP: parser = std::make_unique(); break; @@ -30,14 +28,13 @@ bool ImageParser::parse(const std::string& path, ImageData& out) { ImageParser::ImageFormat ImageParser::detectFormat(const std::string& path) { size_t dotPos = path.find_last_of('.'); - if (dotPos == std::string::npos || dotPos == path.length() - 1) { + if (dotPos == std::string::npos || dotPos == path.length() - 1) return ImageFormat::UNKNOWN; - } std::string extension = path.substr(dotPos + 1); for (char& c : extension) c = static_cast(tolower(c)); - if (extension == "bmp") return ImageFormat::BMP; + if (extension == "bmp") return ImageFormat::BMP; else if (extension == "tga") return ImageFormat::TGA; else return ImageFormat::UNKNOWN; } diff --git a/src/parser/mesh/plyParser.cpp b/src/parser/mesh/plyParser.cpp index 5c802b6..f333466 100644 --- a/src/parser/mesh/plyParser.cpp +++ b/src/parser/mesh/plyParser.cpp @@ -21,7 +21,7 @@ bool PlyParser::parse(const std::string& path, MeshData& out) { if (!loadBinaryFile(file, path)) return false; - if (file.empty()) return Logger::error("PlyParser", "parsePlyMesh", "Input pointer is null\n"); + if (file.empty()) return Logger::error("PlyParser", "parse", "Input pointer is null\n"); const unsigned char* p = file.data(); std::string errorMsg; @@ -55,11 +55,11 @@ bool PlyParser::parse(const std::string& path, MeshData& out) { out.indices.clear(); out.vertices.clear(); out.numVertices = out.numIndices = out.numTriangles = 0; - return Logger::error("PlyParser", "LoadModelFromFile", ("Failed to parse " + errorMsg + '\n').c_str()); + return Logger::error("PlyParser", "parse", ("Failed to parse " + errorMsg + '\n').c_str()); } bool PlyParser::parseHeaderLine(const unsigned char*& p, unsigned int& numVerticesOut, unsigned int& numTrianglesOut, bool& hasNormalsOut, bool& hasColoursOut, bool& hasTexCoordsOut) { - if (!p) return Logger::error("PlyParser", "parsePlyHeader", "Input pointer is null\n"); + if (!p) return Logger::error("PlyParser", "parseHeaderLine", "Input pointer is null\n"); p = skipWhitespace(p); bool hasNx = false, hasNy = false, hasNz = false; @@ -92,16 +92,16 @@ bool PlyParser::parseHeaderLine(const unsigned char*& p, unsigned int& numVertic else if (!(strncasecmp((const char*)p, "ply", 3) == 0) && !(strncasecmp((const char*)p, "format", 6) == 0) && !(strncasecmp((const char*)p, "comment", 7) == 0)) - Logger::debug("parsePlyHeader", "Unknown line in PLY header: %.*s\n" + static_cast(lineEnd - p), (const char*)p); + Logger::debug("plyParser", "parseHeaderLine", ("Unknown line in PLY header: %.*s\n" + std::string((const char*)p, static_cast(lineEnd - p))).c_str()); p = nextLine; } - return Logger::error("plyParser", "parsePlyHeader", "Failed, end of buffer reached"); + return Logger::error("plyParser", "parseHeaderLine", "Failed, end of buffer reached"); } bool PlyParser::parseElementLine(const unsigned char*& p, unsigned int& verticesOut, unsigned int& trianglesOut) { - if (!p) return Logger::error("PlyParser", "parsePlyHeader", "Input pointer is null\n"); + if (!p) return Logger::error("PlyParser", "parseElementLine", "Input pointer is null\n"); p = skipWhitespace(p += 7); if (strncmp((const char*)p, "vertex", 6) == 0 && (p[6] == ' ' || p[6] == '\t')) { @@ -115,12 +115,12 @@ bool PlyParser::parseElementLine(const unsigned char*& p, unsigned int& vertices return false; } bool PlyParser::parsePropertyLine(const unsigned char*& p, bool& hasNx, bool& hasNy, bool& hasNz, bool& hasR, bool& hasG, bool& hasB, bool& hasU, bool& hasV) { - if (!p) return Logger::error("PlyParser", "parsePlyPropertyLine", "Input pointer is null\n"); + if (!p) return Logger::error("PlyParser", "parsePropertyLine", "Input pointer is null\n"); p = skipWhitespace(p += 8); char type[32]{}; if (!parseToken(p, (unsigned char*)type, sizeof(type))) - return Logger::error("PlyParser", "parsePlyPropertyLine", "Failed to parse property type :" + std::string(type)); + return Logger::error("PlyParser", "parsePropertyLine", "Failed to parse property type :" + std::string(type)); if (strcmp(type, "list") == 0) { /* @@ -131,14 +131,14 @@ bool PlyParser::parsePropertyLine(const unsigned char*& p, bool& hasNx, bool& ha char property[3][32]{}; for (int i = 0; i < 3; ++i) if (!parseToken(p, reinterpret_cast(property[i]), sizeof(property[i]))) - return Logger::error("PlyParser", "parsePlyPropertyLine", "Failed to parse property list type, number: " + std::to_string(i)); + return Logger::error("PlyParser", "parsePropertyLine", "Failed to parse property list type, number: " + std::to_string(i)); return true; } char propertyName[32]{}; if (!parseToken(p, (unsigned char*)propertyName, sizeof(propertyName))) - return Logger::error("PlyParser", "parsePlyPropertyLine", "Failed to parse property name :" + std::string(propertyName)); + return Logger::error("PlyParser", "parsePropertyLine", "Failed to parse property name :" + std::string(propertyName)); if (strcmp(propertyName, "nx") == 0 || strcmp(propertyName, "normal_x") == 0) hasNx = true; else if (strcmp(propertyName, "ny") == 0 || strcmp(propertyName, "normal_y") == 0) hasNy = true; diff --git a/src/parser/meshParser.cpp b/src/parser/meshParser.cpp index 9aec008..7313cc5 100644 --- a/src/parser/meshParser.cpp +++ b/src/parser/meshParser.cpp @@ -8,33 +8,30 @@ namespace Starlet::Serializer { - bool MeshParser::parse(const std::string& path, MeshData& out) { - MeshFormat format = detectFormat(path); - - std::unique_ptr parser; - switch (format) { - case MeshFormat::PLY: - parser = std::make_unique(); - break; - default: - Logger::error("ImageParser", "parse", "Unsupported image format: " + path); - return false; - } - - return parser->parse(path, out); +bool MeshParser::parse(const std::string& path, MeshData& out) { + std::unique_ptr parser; + switch (detectFormat(path)) { + case MeshFormat::PLY: + parser = std::make_unique(); + break; + default: + Logger::error("MeshParser", "parse", "Unsupported mesh format: " + path); + return false; } - MeshParser::MeshFormat MeshParser::detectFormat(const std::string& path) { - size_t dotPos = path.find_last_of('.'); - if (dotPos == std::string::npos || dotPos == path.length() - 1) { - return MeshFormat::UNKNOWN; - } + return parser->parse(path, out); +} - std::string extension = path.substr(dotPos + 1); - for (char& c : extension) c = static_cast(tolower(c)); +MeshParser::MeshFormat MeshParser::detectFormat(const std::string& path) { + size_t dotPos = path.find_last_of('.'); + if (dotPos == std::string::npos || dotPos == path.length() - 1) + return MeshFormat::UNKNOWN; - if (extension == "ply") return MeshFormat::PLY; - else return MeshFormat::UNKNOWN; - } + std::string extension = path.substr(dotPos + 1); + for (char& c : extension) c = static_cast(tolower(c)); + + if (extension == "ply") return MeshFormat::PLY; + else return MeshFormat::UNKNOWN; +} } diff --git a/src/parser/parser.cpp b/src/parser/parser.cpp index 8668be9..cb767ac 100644 --- a/src/parser/parser.cpp +++ b/src/parser/parser.cpp @@ -13,12 +13,12 @@ namespace Starlet::Serializer { bool Parser::loadFile(std::string& out, const std::string& path) { FILE* file = fopen(path.c_str(), "rb"); - if (!file) return Logger::error("FileParser", "loadFile", "Failed to open file: " + path); + if (!file) return Logger::error("Parser", "loadFile", "Failed to open file: " + path); size_t fileSize; if (!getFileSize(file, fileSize)) { fclose(file); - return Logger::error("FileParser", "loadFile", "Failed to get file size"); + return Logger::error("Parser", "loadFile", "Failed to get file size"); } out.resize(fileSize); @@ -30,7 +30,7 @@ bool Parser::loadFile(std::string& out, const std::string& path) { if (ferror(file)) { fclose(file); out.clear(); - return Logger::error("FileParser", "loadFile", "fread failed at byte " + std::to_string(bytesRead)); + return Logger::error("Parser", "loadFile", "fread failed at byte " + std::to_string(bytesRead)); } break; } @@ -41,19 +41,19 @@ bool Parser::loadFile(std::string& out, const std::string& path) { if (bytesRead != fileSize) { out.clear(); - return Logger::error("FileParser", "loadFile", "fread failed. Expected " + std::to_string(fileSize) + ", got " + std::to_string(bytesRead)); + return Logger::error("Parser", "loadFile", "fread failed. Expected " + std::to_string(fileSize) + ", got " + std::to_string(bytesRead)); } return true; } bool Parser::loadBinaryFile(std::vector& dataOut, const std::string& path) { FILE* file = fopen(path.c_str(), "rb"); - if (!file) return Logger::error("FileParser", "loadBinaryFile", "Failed to open file: " + path); + if (!file) return Logger::error("Parser", "loadBinaryFile", "Failed to open file: " + path); size_t fileSize; if (!getFileSize(file, fileSize)) { fclose(file); - return Logger::error("FileParser", "loadBinaryFile", "Failed to get file size"); + return Logger::error("Parser", "loadBinaryFile", "Failed to get file size"); } dataOut.resize(fileSize); @@ -62,7 +62,7 @@ bool Parser::loadBinaryFile(std::vector& dataOut, const std::stri if (bytesRead != fileSize) { dataOut.clear(); - return Logger::error("FileParser", "loadBinaryFile", "fread failed. Expected " + std::to_string(fileSize) + ", got " + std::to_string(bytesRead)); + return Logger::error("Parser", "loadBinaryFile", "fread failed. Expected " + std::to_string(fileSize) + ", got " + std::to_string(bytesRead)); } return true; @@ -239,15 +239,15 @@ const unsigned char* Parser::trimEOL(const unsigned char* p, const unsigned char bool Parser::getFileSize(FILE* file, size_t& sizeOut) const { - if (fseek(file, 0, SEEK_END) != 0) return Logger::error("FileParser", "getFileSize", "Failed to seek end of file"); + if (fseek(file, 0, SEEK_END) != 0) return Logger::error("Parser", "getFileSize", "Failed to seek end of file"); const long size = ftell(file); - if (size == -1L) return Logger::error("FileParser", "getFileSize", "Invalid file, ftell failed"); + if (size == -1L) return Logger::error("Parser", "getFileSize", "Invalid file, ftell failed"); if (size < 0 || static_cast(size) > MAX_SIZE) - return Logger::error("FileParser", "getFileSize", "Invalid file size"); + return Logger::error("Parser", "getFileSize", "Invalid file size"); - if (fseek(file, 0, SEEK_SET) != 0) return Logger::error("FileParser", "getFileSize", "Failed to rewind file"); + if (fseek(file, 0, SEEK_SET) != 0) return Logger::error("Parser", "getFileSize", "Failed to rewind file"); sizeOut = static_cast(size); return true; diff --git a/src/parser/scene/colourParser.cpp b/src/parser/scene/colourParser.cpp index b5bd566..841aad0 100644 --- a/src/parser/scene/colourParser.cpp +++ b/src/parser/scene/colourParser.cpp @@ -50,7 +50,7 @@ bool SceneParser::parseNamedColour(const unsigned char*& p, Math::Vec4& c else if (name == "yellow") colour = { 1.0f, 1.0f, 0.0f, 1.0f }; else if (name == "white") colour = { 1.0f, 1.0f, 1.0f, 1.0f }; else if (name == "gray" - || name == "grey") colour = { 0.5f, 0.5f, 0.5f, 1.0f }; + || name == "grey") colour = { 0.5f, 0.5f, 0.5f, 1.0f }; else return false; return true; diff --git a/src/parser/scene/lightParser.cpp b/src/parser/scene/lightParser.cpp index b2d5dc1..6cba91b 100644 --- a/src/parser/scene/lightParser.cpp +++ b/src/parser/scene/lightParser.cpp @@ -17,7 +17,7 @@ bool SceneParser::parseLightType(const unsigned char*& p, LightType& type) { case 0: type = LightType::Point; break; case 1: type = LightType::Spot; break; case 2: type = LightType::Directional; break; - default: return Logger::error("Parser", "parseLightType", "Unknown light type"); + default: return Logger::error("SceneParser", "parseLightType", "Unknown light type"); } return true; @@ -29,8 +29,8 @@ bool SceneParser::parseLightType(const unsigned char*& p, LightType& type) { p = original; return false; } - - if (strcmp((char*)typeName, "Point") == 0) type = LightType::Point; + + if (strcmp((char*)typeName, "Point") == 0) type = LightType::Point; else if (strcmp((char*)typeName, "Spot") == 0) type = LightType::Spot; else if (strcmp((char*)typeName, "Directional") == 0) type = LightType::Directional; else return Logger::error("Parser", "parseLightType", "Unknown light type"); diff --git a/src/parser/sceneParser.cpp b/src/parser/sceneParser.cpp index e0cb710..9a255df 100644 --- a/src/parser/sceneParser.cpp +++ b/src/parser/sceneParser.cpp @@ -7,7 +7,7 @@ namespace Starlet::Serializer { bool SceneParser::parse(const std::string& path, SceneData& scene) { std::string src{}; - if (!loadFile(src, path)) return Logger::error("SceneLoader", "loadScene", "Failed to load path: " + path); + if (!loadFile(src, path)) return Logger::error("SceneParser", "parse", "Failed to load path: " + path); const unsigned char* p = reinterpret_cast(src.c_str()); while (*p) { @@ -26,7 +26,7 @@ bool SceneParser::parse(const std::string& path, SceneData& scene) { std::string errorMsg; errorMsg.reserve(len); errorMsg.append(reinterpret_cast(p), len); - return Logger::error("SceneManager", "loadTxtScene", std::string("Failed to process scene line: \"") + errorMsg + "\""); + return Logger::error("SceneParser", "parse", std::string("Failed to process scene line: \"") + errorMsg + "\""); } if (p < nextLine) p = nextLine; @@ -51,7 +51,7 @@ bool SceneParser::parseSceneLine(const unsigned char*& p, SceneData& scene) { else if (strcmp(nameStr, "model") == 0) { ModelData model; if (!parseModel(p, model)) - return Logger::error("SceneLoader", "processSceneLine", "Failed to parse model"); + return Logger::error("SceneParser", "parseSceneLine", "Failed to parse model"); scene.models.push_back(model); return true; @@ -59,7 +59,7 @@ bool SceneParser::parseSceneLine(const unsigned char*& p, SceneData& scene) { else if (strcmp(nameStr, "light") == 0) { LightData light; if(!parseLight(p, light)) - return Logger::error("SceneLoader", "processSceneLine", "Failed to parse light"); + return Logger::error("SceneParser", "parseSceneLine", "Failed to parse light"); scene.lights.push_back(light); return true; @@ -67,7 +67,7 @@ bool SceneParser::parseSceneLine(const unsigned char*& p, SceneData& scene) { else if (strcmp(nameStr, "camera") == 0) { CameraData camera; if(!parseCamera(p, camera)) - return Logger::error("SceneLoader", "processSceneLine", "Failed to parse camera"); + return Logger::error("SceneParser", "parseSceneLine", "Failed to parse camera"); scene.cameras.push_back(camera); return true; @@ -75,7 +75,7 @@ bool SceneParser::parseSceneLine(const unsigned char*& p, SceneData& scene) { else if (strcmp(nameStr, "texture") == 0) { TextureData texture; if(!parseTexture(p, texture)) - return Logger::error("SceneLoader", "processSceneLine", "Failed to parse texture"); + return Logger::error("SceneParser", "parseSceneLine", "Failed to parse texture"); scene.textures.push_back(texture); return true; @@ -83,7 +83,7 @@ bool SceneParser::parseSceneLine(const unsigned char*& p, SceneData& scene) { else if (strcmp(nameStr, "textureCube") == 0) { TextureData texture; if(!parseCubeTexture(p, texture)) - return Logger::error("SceneLoader", "processSceneLine", "Failed to parse cube texture"); + return Logger::error("SceneParser", "parseSceneLine", "Failed to parse cube texture"); scene.textures.push_back(texture); return true; @@ -93,7 +93,7 @@ bool SceneParser::parseSceneLine(const unsigned char*& p, SceneData& scene) { STARLET_PARSE_STRING_OR(return false, p, data.modelName, 64, "texture connection model name"); STARLET_PARSE_OR(return false, parseUInt, data.slot, "texture connection slot"); if (data.slot >= ModelData::NUM_TEXTURES) - return Logger::error("Parser", "textureAdd", "Invalid texture slot index: " + std::to_string(data.slot) + " for model: " + data.modelName); + return Logger::error("SceneParser", "parseSceneLine", "Invalid texture slot index: " + std::to_string(data.slot) + " for model: " + data.modelName); STARLET_PARSE_STRING_OR(return false, p, data.textureName, 128, "texture connection name"); STARLET_PARSE_OR(return false, parseFloat, data.mix, "texture connection mix"); @@ -104,7 +104,7 @@ bool SceneParser::parseSceneLine(const unsigned char*& p, SceneData& scene) { else if (strcmp(nameStr, "triangle") == 0) { PrimitiveData primitive; if(!parseTriangle(p, primitive)) - return Logger::error("SceneLoader", "processSceneLine", "Failed to parse triangle"); + return Logger::error("SceneParser", "parseSceneLine", "Failed to parse triangle"); scene.primitives.push_back(primitive); return true; @@ -112,7 +112,7 @@ bool SceneParser::parseSceneLine(const unsigned char*& p, SceneData& scene) { else if (strcmp(nameStr, "square") == 0) { PrimitiveData primitive; if(!parseSquare(p, primitive)) - return Logger::error("SceneLoader", "processSceneLine", "Failed to parse square"); + return Logger::error("SceneParser", "parseSceneLine", "Failed to parse square"); scene.primitives.push_back(primitive); return true; @@ -120,7 +120,7 @@ bool SceneParser::parseSceneLine(const unsigned char*& p, SceneData& scene) { else if (strcmp(nameStr, "cube") == 0) { PrimitiveData primitive; if(!parseCube(p, primitive)) - return Logger::error("SceneLoader", "processSceneLine", "Failed to parse cube"); + return Logger::error("SceneParser", "parseSceneLine", "Failed to parse cube"); scene.primitives.push_back(primitive); return true; @@ -128,7 +128,7 @@ bool SceneParser::parseSceneLine(const unsigned char*& p, SceneData& scene) { else if (strcmp(nameStr, "squareGrid") == 0) { GridData grid; if(!parseSquareGrid(p, grid)) - return Logger::error("SceneLoader", "processSceneLine", "Failed to parse square grid"); + return Logger::error("SceneParser", "parseSceneLine", "Failed to parse square grid"); scene.grids.push_back(grid); return true; @@ -136,7 +136,7 @@ bool SceneParser::parseSceneLine(const unsigned char*& p, SceneData& scene) { else if (strcmp(nameStr, "cubeGrid") == 0) { GridData grid; if(!parseCubeGrid(p, grid)) - return Logger::error("SceneLoader", "processSceneLine", "Failed to parse cube grid"); + return Logger::error("SceneParser", "parseSceneLine", "Failed to parse cube grid"); scene.grids.push_back(grid); return true; @@ -149,7 +149,7 @@ bool SceneParser::parseSceneLine(const unsigned char*& p, SceneData& scene) { velocity.modelName = reinterpret_cast(nameToken); if(!parseVelocity(p, velocity)) - return Logger::error("SceneLoader", "processSceneLine", "Failed to parse velocity"); + return Logger::error("SceneParser", "parseSceneLine", "Failed to parse velocity"); scene.velocities.push_back(velocity); return true; @@ -167,20 +167,20 @@ bool SceneParser::parseSceneLine(const unsigned char*& p, SceneData& scene) { strcmp(s, "off") == 0 || strcmp(s, "1") == 0 || strcmp(s, "0") == 0)) - return Logger::error("SceneLoader", "processSceneLine", "Ambient missing enabled boolean"); + return Logger::error("SceneParser", "parseSceneLine", "Ambient missing enabled boolean"); bool enabled{ false }; - if (!parseBool(p, enabled)) return Logger::error("SceneLoader", "processSceneLine", "Failed to parse ambient enabled"); + if (!parseBool(p, enabled)) return Logger::error("SceneParser", "parseSceneLine", "Failed to parse ambient enabled"); Math::Vec3 colour{ 0.0f, 0.0f, 0.0f }; - if (!parseVec3f(p, colour)) return Logger::error("SceneLoader", "processSceneLine", "Failed to parse ambient colour"); + if (!parseVec3f(p, colour)) return Logger::error("SceneParser", "parseSceneLine", "Failed to parse ambient colour"); scene.ambientEnabled = enabled; scene.ambientLight = colour; return true; } - return Logger::error("SceneManager", "processSceneLine", "Failed to handle: " + std::string(nameStr)); + return Logger::error("SceneParser", "parseSceneLine", "Failed to handle: " + std::string(nameStr)); } } \ No newline at end of file diff --git a/tests/image_parser_test.cpp b/tests/image_parser_test.cpp index 5dd20b9..dadafae 100644 --- a/tests/image_parser_test.cpp +++ b/tests/image_parser_test.cpp @@ -2,85 +2,109 @@ #include "starlet-serializer/parser/imageParser.hpp" #include "starlet-serializer/data/imageData.hpp" - +#include "test_helpers.hpp" + #include #include namespace SSerializer = Starlet::Serializer; -namespace { - void createTestFile(const std::string& path, const std::string_view& content) { - std::filesystem::path p(path); - std::filesystem::create_directories(p.parent_path()); - std::ofstream file(path, std::ios::binary); - file.write(content.data(), content.size()); - } -} - - // BMP format detection -TEST(ImageParserTest, DetectFormatBmpLowercase) { - SSerializer::ImageParser parser; - SSerializer::ImageData out; +TEST_F(ImageParserTest, DetectFormatBmpLowercase) { createTestFile("test_data/image.bmp", "BM"); + + testing::internal::CaptureStderr(); EXPECT_FALSE(parser.parse("test_data/image.bmp", out)); + std::string output = testing::internal::GetCapturedStderr(); + + // BMP extension was properly detected and routed to BmpParser + EXPECT_NE(output.find("BmpParser"), std::string::npos); } -TEST(ImageParserTest, DetectFormatBmpUppercase) { - SSerializer::ImageParser parser; - SSerializer::ImageData out; +TEST_F(ImageParserTest, DetectFormatBmpUppercase) { createTestFile("test_data/image.BMP", "BM"); + + testing::internal::CaptureStderr(); EXPECT_FALSE(parser.parse("test_data/image.BMP", out)); + std::string output = testing::internal::GetCapturedStderr(); + + // BMP extension was properly detected and routed to BmpParser + EXPECT_NE(output.find("BmpParser"), std::string::npos); } -TEST(ImageParserTest, DetectFormatBmpMixedCase) { - SSerializer::ImageParser parser; - SSerializer::ImageData out; +TEST_F(ImageParserTest, DetectFormatBmpMixedCase) { createTestFile("test_data/image.BmP", "BM"); + + testing::internal::CaptureStderr(); EXPECT_FALSE(parser.parse("test_data/image.BmP", out)); + std::string output = testing::internal::GetCapturedStderr(); + + // BMP extension was properly detected and routed to BmpParser + EXPECT_NE(output.find("BmpParser"), std::string::npos); } // TGA format detection -TEST(ImageParserTest, DetectFormatTgaLowercase) { - SSerializer::ImageParser parser; - SSerializer::ImageData out; +TEST_F(ImageParserTest, DetectFormatTgaLowercase) { createTestFile("test_data/image.tga", std::string(18, '\0')); + + testing::internal::CaptureStderr(); EXPECT_FALSE(parser.parse("test_data/image.tga", out)); + std::string output = testing::internal::GetCapturedStderr(); + + // TGA extension was properly detected and routed to TgaParser + EXPECT_NE(output.find("TgaParser"), std::string::npos); } -TEST(ImageParserTest, DetectFormatTgaUppercase) { - SSerializer::ImageParser parser; - SSerializer::ImageData out; +TEST_F(ImageParserTest, DetectFormatTgaUppercase) { createTestFile("test_data/image.TGA", std::string(18, '\0')); + + testing::internal::CaptureStderr(); EXPECT_FALSE(parser.parse("test_data/image.TGA", out)); + std::string output = testing::internal::GetCapturedStderr(); + + // TGA extension was properly detected and routed to TgaParser + EXPECT_NE(output.find("TgaParser"), std::string::npos); } -TEST(ImageParserTest, DetectFormatTgaMixedCase) { - SSerializer::ImageParser parser; - SSerializer::ImageData out; +TEST_F(ImageParserTest, DetectFormatTgaMixedCase) { createTestFile("test_data/image.TgA", std::string(18, '\0')); + + testing::internal::CaptureStderr(); EXPECT_FALSE(parser.parse("test_data/image.TgA", out)); + std::string output = testing::internal::GetCapturedStderr(); + + // TGA extension was properly detected and routed to TgaParser + EXPECT_NE(output.find("TgaParser"), std::string::npos); } // Edge cases -TEST(ImageParserTest, DetectFormatNoExtension) { - SSerializer::ImageParser parser; - SSerializer::ImageData out; - createTestFile("test_data/image", "BM"); +TEST_F(ImageParserTest, DetectFormatNoExtension) { + createTestFile("test_data/image", "No extension"); + + testing::internal::CaptureStderr(); EXPECT_FALSE(parser.parse("test_data/image", out)); + std::string output = testing::internal::GetCapturedStderr(); + + EXPECT_NE(output.find("Unsupported image format: test_data/image"), std::string::npos); } -TEST(ImageParserTest, DetectFormatUnknownExtension) { - SSerializer::ImageParser parser; - SSerializer::ImageData out; +TEST_F(ImageParserTest, DetectFormatUnknownExtension) { createTestFile("test_data/image.unknown", "BM"); + + testing::internal::CaptureStderr(); EXPECT_FALSE(parser.parse("test_data/image.unknown", out)); + std::string output = testing::internal::GetCapturedStderr(); + + EXPECT_NE(output.find("Unsupported image format: test_data/image.unknown"), std::string::npos); } -TEST(ImageParserTest, DetectFormatEmptyExtension) { - SSerializer::ImageParser parser; - SSerializer::ImageData out; +TEST_F(ImageParserTest, DetectFormatEmptyExtension) { createTestFile("test_data/image.", "BM"); + + testing::internal::CaptureStderr(); EXPECT_FALSE(parser.parse("test_data/image.", out)); + std::string output = testing::internal::GetCapturedStderr(); + + EXPECT_NE(output.find("Unsupported image format: test_data/image."), std::string::npos); } diff --git a/tests/mesh/ply_parser_test.cpp b/tests/mesh/ply_parser_test.cpp index 8054d9f..8197ae6 100644 --- a/tests/mesh/ply_parser_test.cpp +++ b/tests/mesh/ply_parser_test.cpp @@ -2,22 +2,13 @@ #include "starlet-serializer/parser/meshParser.hpp" #include "starlet-serializer/data/meshData.hpp" +#include "../test_helpers.hpp" #include #include namespace SSerializer = Starlet::Serializer; -namespace { - void createTestFile(const std::string& path, const std::string_view& content) { - std::filesystem::path p(path); - std::filesystem::create_directories(p.parent_path()); - std::ofstream file(path); - file << content; - } -} - - // Valid PLY parsing tests TEST(PlyParserTest, ValidPlyMinimal) { const std::string_view plyContent = R"(ply diff --git a/tests/mesh_parser_test.cpp b/tests/mesh_parser_test.cpp index 03a1099..6cddc91 100644 --- a/tests/mesh_parser_test.cpp +++ b/tests/mesh_parser_test.cpp @@ -2,62 +2,83 @@ #include "starlet-serializer/parser/meshParser.hpp" #include "starlet-serializer/data/meshData.hpp" +#include "test_helpers.hpp" #include #include namespace SSerializer = Starlet::Serializer; -namespace { - void createTestFile(const std::string& path, const std::string_view& content) { - std::filesystem::path p(path); - std::filesystem::create_directories(p.parent_path()); - std::ofstream file(path); - file << content; - } -} +class MeshParserTest : public ::testing::Test { +protected: + SSerializer::MeshParser parser; + SSerializer::MeshData meshData; + const std::string testFileName = "test_data/test_mesh.ply"; +}; // PLY format detection -TEST(MeshParserTest, DetectFormatPlyLowercase) { - SSerializer::MeshParser parser; - SSerializer::MeshData out; +TEST_F(MeshParserTest, DetectFormatPlyLowercase) { createTestFile("test_data/model.ply", "ply\nformat ascii 1.0\nelement vertex 0\nelement face 0\nend_header\n"); - EXPECT_FALSE(parser.parse("test_data/model.ply", out)); + + testing::internal::CaptureStderr(); + EXPECT_FALSE(parser.parse("test_data/model.ply", meshData)); + std::string output = testing::internal::GetCapturedStderr(); + + // PLY extension was properly detected and routed to PlyParser + EXPECT_NE(output.find("PlyParser"), std::string::npos); } -TEST(MeshParserTest, DetectFormatPlyUppercase) { - SSerializer::MeshParser parser; - SSerializer::MeshData out; - createTestFile("test_data/model.PLY", "PLY\nformat ascii 1.0\nelement vertex 0\nelement face 0\nend_header\n"); - EXPECT_FALSE(parser.parse("test_data/model.PLY", out)); +TEST_F(MeshParserTest, DetectFormatPlyUppercase) { + createTestFile("test_data/model.PLY", "ply\nformat ascii 1.0\nelement vertex 0\nelement face 0\nend_header\n"); + + testing::internal::CaptureStderr(); + EXPECT_FALSE(parser.parse("test_data/model.PLY", meshData)); + std::string output = testing::internal::GetCapturedStderr(); + + // PLY extension was properly detected and routed to PlyParser + EXPECT_NE(output.find("PlyParser"), std::string::npos); } -TEST(MeshParserTest, DetectFormatPlyMixedCase) { - SSerializer::MeshParser parser; - SSerializer::MeshData out; - createTestFile("test_data/model.PlY", "PlY\nformat ascii 1.0\nelement vertex 0\nelement face 0\nend_header\n"); - EXPECT_FALSE(parser.parse("test_data/model.PlY", out)); +TEST_F(MeshParserTest, DetectFormatPlyMixedCase) { + createTestFile("test_data/model.PlY", "ply\nformat ascii 1.0\nelement vertex 0\nelement face 0\nend_header\n"); + + testing::internal::CaptureStderr(); + EXPECT_FALSE(parser.parse("test_data/model.PlY", meshData)); + std::string output = testing::internal::GetCapturedStderr(); + + // PLY extension was properly detected and routed to PlyParser + EXPECT_NE(output.find("PlyParser"), std::string::npos); } + // Edge cases -TEST(MeshParserTest, DetectFormatNoExtension) { - SSerializer::MeshParser parser; - SSerializer::MeshData out; +TEST_F(MeshParserTest, DetectFormatNoExtension) { createTestFile("test_data/model", "ply\nformat ascii 1.0\nelement vertex 0\nelement face 0\nend_header\n"); - EXPECT_FALSE(parser.parse("test_data/model", out)); + + testing::internal::CaptureStderr(); + EXPECT_FALSE(parser.parse("test_data/model", meshData)); + std::string output = testing::internal::GetCapturedStderr(); + + EXPECT_NE(output.find("Unsupported mesh format: test_data/model"), std::string::npos); } -TEST(MeshParserTest, DetectFormatUnknownExtension) { - SSerializer::MeshParser parser; - SSerializer::MeshData out; +TEST_F(MeshParserTest, DetectFormatUnknownExtension) { createTestFile("test_data/model.unknown", "ply\nformat ascii 1.0\nelement vertex 0\nelement face 0\nend_header\n"); - EXPECT_FALSE(parser.parse("test_data/model.unknown", out)); + + testing::internal::CaptureStderr(); + EXPECT_FALSE(parser.parse("test_data/model.unknown", meshData)); + std::string output = testing::internal::GetCapturedStderr(); + + EXPECT_NE(output.find("Unsupported mesh format: test_data/model.unknown"), std::string::npos); } -TEST(MeshParserTest, DetectFormatEmptyExtension) { - Starlet::Serializer::MeshParser parser; - Starlet::Serializer::MeshData out; +TEST_F(MeshParserTest, DetectFormatEmptyExtension) { createTestFile("test_data/model.", "ply\n"); - EXPECT_FALSE(parser.parse("test_data/model.", out)); + + testing::internal::CaptureStderr(); + EXPECT_FALSE(parser.parse("test_data/model.", meshData)); + std::string output = testing::internal::GetCapturedStderr(); + + EXPECT_NE(output.find("Unsupported mesh format: test_data/model."), std::string::npos); } \ No newline at end of file diff --git a/tests/parser_test.cpp b/tests/parser_test.cpp index 514df76..1323fbb 100644 --- a/tests/parser_test.cpp +++ b/tests/parser_test.cpp @@ -1,6 +1,8 @@ #include #include "starlet-serializer/parser/parser.hpp" +#include "test_helpers.hpp" + #include "starlet-math/vec2.hpp" #include "starlet-math/vec3.hpp" #include "starlet-math/vec4.hpp" @@ -11,23 +13,6 @@ namespace SSerializer = Starlet::Serializer; -namespace { - void createTestFile(const std::string& path, const std::string& content) { - std::filesystem::path p(path); - std::filesystem::create_directories(p.parent_path()); - std::ofstream file(path); - file << content; - } - - void createBinaryFile(const std::string& path, const std::vector& data) { - std::filesystem::path p(path); - std::filesystem::create_directories(p.parent_path()); - std::ofstream file(path, std::ios::binary); - file.write(reinterpret_cast(data.data()), data.size()); - } -} - - // File Loading Tests TEST(ParserTest, LoadFileSuccess) { createTestFile("test_data/valid.txt", "test content"); diff --git a/tests/scene_parser_test.cpp b/tests/scene_parser_test.cpp index 2b41ac2..47c9b0c 100644 --- a/tests/scene_parser_test.cpp +++ b/tests/scene_parser_test.cpp @@ -1,7 +1,9 @@ #include "gtest/gtest.h" -#include "test_helpers.hpp" + #include "starlet-serializer/parser/sceneParser.hpp" #include "starlet-serializer/data/sceneData.hpp" +#include "test_helpers.hpp" + #include "starlet-math/vec4.hpp" using namespace Starlet::Serializer; diff --git a/tests/test_helpers.hpp b/tests/test_helpers.hpp index 582a36c..6d26979 100644 --- a/tests/test_helpers.hpp +++ b/tests/test_helpers.hpp @@ -15,6 +15,12 @@ inline void createTestFile(const std::string& path, const std::string_view& cont std::ofstream file(path, std::ios::binary); file.write(content.data(), content.size()); } +inline void createBinaryFile(const std::string& path, const std::vector& data) { + std::filesystem::path p(path); + std::filesystem::create_directories(p.parent_path()); + std::ofstream file(path, std::ios::binary); + file.write(reinterpret_cast(data.data()), data.size()); +} class ImageParserTest : public ::testing::Test { protected: