From c59c838696bd4927c91b52e8c4b6cbb1da77a368 Mon Sep 17 00:00:00 2001 From: AfonsoCMSousa Date: Fri, 13 Jun 2025 13:23:20 +0100 Subject: [PATCH] File opperations to parse OBJ files into Matrixs. --- README.md | 6 +-- include/io/io_file_op.cpp | 96 ++++++++++++++++++++++++++++++++++++++- include/io/io_file_op.hpp | 15 ++++++ source/main.cpp | 13 ++++++ 4 files changed, 126 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9b6921b..e1b031c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # CPP-Renderer -A lightweight CPU-based raytracer written in modern C++. -Features support for basic lighting, reflections, and shadows using ray-sphere intersections. -Renders to .ppm images — no dependencies, just math. +A lightweight CPU-based raytracer written in modern C++. +Features support for basic lighting, reflections, and shadows using ray-sphere intersections. +Renders to .ppm images — no dependencies, just math. Built from scratch for fun, learning, and low-level flexin’. \ No newline at end of file diff --git a/include/io/io_file_op.cpp b/include/io/io_file_op.cpp index 6a2df98..f7e1edc 100644 --- a/include/io/io_file_op.cpp +++ b/include/io/io_file_op.cpp @@ -1 +1,95 @@ -#include "io_file_op.hpp" \ No newline at end of file +#include "io_file_op.hpp" +#include + +FILE *w_open_file(const char *filepath) +{ + if (filepath == nullptr) + { + return nullptr; + } + FILE *file = fopen(filepath, "rwb"); + return file; +} + +file_content w_read_file(FILE *file) +{ + file_content __result = init_file_content(); + + if (file == nullptr) + { + fprintf(stderr, "Error: Invalid file in w_read_file\n"); + return __result; + } + + fseek(file, 0, SEEK_END); + long file_size = ftell(file); + if (file_size < 0) + { + fprintf(stderr, "Error: Could not determine file size.\n"); + return init_file_content(); + } + rewind(file); + + size_t size = (size_t)file_size; + char *buffer = (char *)malloc(sizeof(char) * (size + 1)); + if (buffer == NULL) + { + fprintf(stderr, "Error: Cant Allocate buffer to read.\n"); + return init_file_content(); + } + + size_t result = fread(buffer, 1, size, file); + if (result != size) + { + fprintf(stderr, "Error: Reading error in w_read_file.\n"); + free(buffer); + return init_file_content(); + } + buffer[size] = '\0'; + + size_t line_start = 0; + char **lines = NULL; + + for (size_t i = 0; i <= size; i++) + { + if (buffer[i] == '\n' || buffer[i] == '\0') + { + size_t line_length = i - line_start; + char *line = (char *)malloc(line_length + 1); + if (line == NULL) + { + for (int j = 0; j < __result.num_lines; j++) + free(lines[j]); + free(lines); + free(buffer); + fprintf(stderr, "Error: Cant Allocate line buffer.\n"); + return init_file_content(); + } + memcpy(line, &buffer[line_start], line_length); + line[line_length] = '\0'; + char **new_lines = (char **)realloc(lines, ((size_t)(__result.num_lines + 1)) * sizeof(char *)); + if (new_lines == NULL) + { + for (int j = 0; j < __result.num_lines; j++) + free(lines[j]); + free(line); + free(lines); + free(buffer); + fprintf(stderr, "Error: Cant realloc lines array.\n"); + return init_file_content(); + } + lines = new_lines; + lines[__result.num_lines++] = line; + line_start = i + 1; + } + } + return __result; +} + +file_content init_file_content() +{ + file_content r; + r.lines = NULL; + r.num_lines = 0; + return r; +} \ No newline at end of file diff --git a/include/io/io_file_op.hpp b/include/io/io_file_op.hpp index bcfd437..fc240a1 100644 --- a/include/io/io_file_op.hpp +++ b/include/io/io_file_op.hpp @@ -1,4 +1,19 @@ #ifndef IO_FILE_OP_HPP #define IO_FILE_OP_HPP +#include +#include +#include + +typedef struct +{ + char **lines; + int num_lines; +} file_content; + +FILE *w_open_file(const char *filepath); +file_content w_read_file(FILE *file); + +file_content init_file_content(); + #endif // !IO_FILE_OP_HPP \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index a6c8864..f77af87 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -2,9 +2,22 @@ // Include the vector utilities header file #include "vector/vec_utils.hpp" +#include "io/io_file_op.hpp" int main() { + file_content file_info = w_read_file(w_open_file("./CUBE.obj")); + for (size_t i = 0; i < (u_int32_t)file_info.num_lines; i++) + { + printf("Line %04zu: \t", i); + for (size_t j = 0; file_info.lines[i][j] != '\0'; j++) + { + printf("%c", file_info.lines[i][j]); + } + printf("\n"); + } + + free(file_info.lines); return 0; } \ No newline at end of file