1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
#include <filehandler.h>
#include <stdio.h>
#include <stdlib.h>
void
Filehandler::init(char *path)
{
m_file = NULL;
m_buffer = NULL;
asprintf(&m_path, "%s", path);
}
bool
Filehandler::open(void)
{
m_file = fopen(m_path, "r");
if (m_file == NULL) {
printf("Unable to open %s\n", m_path);
return false;
}
return true;
}
unsigned int
Filehandler::size(void)
{
unsigned int current = ftell(m_file);
fseek(m_file, 0, SEEK_END);
unsigned int s = ftell(m_file);
fseek(m_file, current, SEEK_SET);
return s;
}
char *
Filehandler::read(void)
{
fseek(m_file, 0, SEEK_SET);
m_buffer = (char *) calloc(size(), sizeof(char));
int bytesread = fread(m_buffer, sizeof(char), size(), m_file);
if (bytesread < 0) {
return NULL;
}
return m_buffer;
}
void
Filehandler::close(void)
{
fclose(m_file);
free(m_buffer);
free(m_path);
}
|