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 /src/filehandler.cc | |
| parent | 25d651a1a4100be0d7b148030d8e3bca6996f53b (diff) | |
filehandler: init
Diffstat (limited to 'src/filehandler.cc')
| -rw-r--r-- | src/filehandler.cc | 55 |
1 files changed, 55 insertions, 0 deletions
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); +} |
