diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-08 13:33:58 +0530 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-08 13:33:58 +0530 |
commit | 1de0b57e396d1964aba2c8ddc663bd55d01f5058 (patch) | |
tree | ccc6fe94ca738136e3da27cd4ff0fbfd230c3ee7 | |
parent | 25d651a1a4100be0d7b148030d8e3bca6996f53b (diff) |
filehandler: init
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | include/filehandler.h | 24 | ||||
-rw-r--r-- | src/filehandler.cc | 55 | ||||
-rw-r--r-- | src/main.cc | 22 | ||||
-rw-r--r-- | src/scanner.cc | 2 |
5 files changed, 87 insertions, 17 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index c6ac1c6..aa52d73 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ project(minni CXX) set(SRC src/main.cc src/scanner.cc + src/filehandler.cc ) add_executable(minni ${SRC}) diff --git a/include/filehandler.h b/include/filehandler.h new file mode 100644 index 0000000..ac8fa88 --- /dev/null +++ b/include/filehandler.h @@ -0,0 +1,24 @@ +#ifndef __FILEHANDLER_H +#define __FILEHANDLER_H + +#include <stdbool.h> +#include <stdio.h> + +class Filehandler +{ +private: + FILE *f; + char *path; + char *buffer; + +private: + unsigned int size(void); + +public: + Filehandler(char *path); + bool open(void); + char *read(void); + void close(void); +}; + +#endif diff --git a/src/filehandler.cc b/src/filehandler.cc new file mode 100644 index 0000000..a901fc5 --- /dev/null +++ b/src/filehandler.cc @@ -0,0 +1,55 @@ +#include <filehandler.h> +#include <stdio.h> +#include <stdlib.h> + +Filehandler::Filehandler(char *path) +{ + f = NULL; + buffer = NULL; + this->path = path; +} + +bool +Filehandler::open(void) +{ + f = fopen(path, "r"); + if (f == NULL) { + printf("Unable to open %s\n", path); + return false; + } + + return true; +} + +unsigned int +Filehandler::size(void) +{ + unsigned int current = ftell(f); + + fseek(f, 0, SEEK_END); + unsigned int s = ftell(f); + + fseek(f, current, SEEK_SET); + return s; +} + +char * +Filehandler::read(void) +{ + fseek(f, 0, SEEK_SET); + + buffer = (char *) calloc(size(), sizeof(char)); + int bytesread = fread(buffer, sizeof(char), size(), f); + + if (bytesread < 0) { + return NULL; + } + return buffer; +} + +void +Filehandler::close(void) +{ + fclose(f); + free(buffer); +} diff --git a/src/main.cc b/src/main.cc index d357756..ea28324 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,3 +1,4 @@ +#include <filehandler.h> #include <scanner.h> #include <stdio.h> #include <stdlib.h> @@ -5,34 +6,23 @@ int main(int argc, char **argv) { - (void) argc; - (void) argv; - if (argc != 2) { printf("Usage: %s <script>\n", argv[0]); return 1; } - FILE *f = fopen(argv[1], "r"); - if (f == NULL) { + Filehandler f(argv[1]); + bool success = f.open(); + if (!success) { printf("Unable to open %s\n", argv[1]); return 1; } - fseek(f, 0, SEEK_END); - unsigned long fsize = ftell(f); - fseek(f, 0, SEEK_SET); - - char *buffer = (char *) calloc(fsize, sizeof(char)); - int bytesread = fread(buffer, sizeof(char), fsize, f); - if (bytesread < 0) { - printf("Unable to open %s\n", argv[1]); - return 1; - } + char *buffer = f.read(); Scanner s(buffer); s.scan_tokens(); - free(buffer); + f.close(); return 0; } diff --git a/src/scanner.cc b/src/scanner.cc index f193562..cff7676 100644 --- a/src/scanner.cc +++ b/src/scanner.cc @@ -1,5 +1,5 @@ -#include <cstdio> #include <scanner.h> +#include <stdio.h> Scanner::Scanner(char *script) : script(script) {} |