summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xMakefile26
-rwxr-xr-xfile_reader.c42
-rw-r--r--file_reader.h21
3 files changed, 89 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100755
index 0000000..a4ef8a1
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,26 @@
1CFLAGS=-Wall -g -shared -fPIC
2
3rebuild: clean all
4
5all: file_reader.o file_reader.a file_reader.so
6 $(CC) -Wall -o file_reader ./file_reader.o
7 rm -f file_reader.o
8
9clean:
10 rm -f file_reader{.so,.a,.o,}
11
12test: all
13 ./main
14
15
16
17#.o:
18# cc -shared -fPIC -c -o $@ $<
19
20%.so: %.o
21 $(CC) $(INCLUDES) $(CFLAGS) -o $@ $<
22
23%.a: %.o
24 ar rcs $@ $<
25
26
diff --git a/file_reader.c b/file_reader.c
new file mode 100755
index 0000000..eea7761
--- /dev/null
+++ b/file_reader.c
@@ -0,0 +1,42 @@
1#include "file_reader.h"
2
3#define BUFFER_SIZE 16384
4
5File* file_open(const char* filename)
6{
7 File* retval = malloc(sizeof(File));
8 retval->name = filename;
9 retval->data = malloc(0);
10 retval->size = 0;
11 FILE* file = fopen(filename, "rb");
12 if(file)
13 {
14 do
15 {
16 retval->data = realloc(retval->data, retval->size + BUFFER_SIZE);
17 retval->size += fread(&retval->data[retval->size], 1, BUFFER_SIZE, file);
18 } while(!feof(file));
19 retval->data = realloc(retval->data, retval->size);
20 fclose(file);
21 return retval;
22 }
23 else
24 {
25 perror(filename);
26 return NULL;
27 }
28}
29
30void file_close(File* file)
31{
32 free(file->data);
33 free(file);
34}
35
36int main()
37{
38 File* file = file_open("strict.dtd");
39 puts(file->data);
40 file_close(file);
41 return 0;
42}
diff --git a/file_reader.h b/file_reader.h
new file mode 100644
index 0000000..6c68618
--- /dev/null
+++ b/file_reader.h
@@ -0,0 +1,21 @@
1#ifndef FILE_READER
2#define FILE_READER
3
4#include <stdlib.h>
5#include <stdio.h>
6
7typedef struct
8{
9 const char* name;
10 char* data;
11 size_t size;
12} File;
13
14File* file_open(const char* filename);
15void file_close(File* file);
16
17#define file_getName(FILE) FILE->name
18#define file_getSize(FILE) FILE->size
19#define file_toChar(FILE) FILE->data
20
21#endif
..