diff options
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); +} |