summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Christian Pohle2016-05-25 17:12:39 +0200
committerMax Christian Pohle2016-05-25 17:12:39 +0200
commit50d5d0761993474bf86ae4832a139c50586fdccb (patch)
tree3687af0875ba1d4b139784a0b77ef899fd1a8ca9
parent7d548770afef79b9b57ab52aff8a263f8fc6d74b (diff)
downloadlibfile_reader-50d5d0761993474bf86ae4832a139c50586fdccb.tar.bz2
libfile_reader-50d5d0761993474bf86ae4832a139c50586fdccb.zip
Improved performance by a single fread
Accidentally uploaded an old version of this library
-rwxr-xr-xfile_reader.c25
1 files changed, 13 insertions, 12 deletions
diff --git a/file_reader.c b/file_reader.c
index eea7761..c262091 100755
--- a/file_reader.c
+++ b/file_reader.c
@@ -6,19 +6,17 @@ File* file_open(const char* filename)
6{ 6{
7 File* retval = malloc(sizeof(File)); 7 File* retval = malloc(sizeof(File));
8 retval->name = filename; 8 retval->name = filename;
9 retval->data = malloc(0); 9
10 retval->size = 0; 10 FILE* file = fopen(retval->name, "rb");
11 FILE* file = fopen(filename, "rb"); 11 fseek(file, 0, SEEK_END); // go to the end of the file
12 if(file) 12 if(file)
13 { 13 {
14 do 14 retval->size = ftell(0); // save the position (end of the file)
15 { 15 retval->data = malloc(retval->size + 1);
16 retval->data = realloc(retval->data, retval->size + BUFFER_SIZE); 16 fseek(file, 0, SEEK_SET); // return to the beginning of the file
17 retval->size += fread(&retval->data[retval->size], 1, BUFFER_SIZE, file); 17 fread(retval->data, retval->size, 1, file);
18 } while(!feof(file));
19 retval->data = realloc(retval->data, retval->size);
20 fclose(file); 18 fclose(file);
21 return retval; 19 return retval; // file is completely stored in the buffer
22 } 20 }
23 else 21 else
24 { 22 {
@@ -29,8 +27,11 @@ File* file_open(const char* filename)
29 27
30void file_close(File* file) 28void file_close(File* file)
31{ 29{
32 free(file->data); 30 if(file)
33 free(file); 31 {
32 free(file->data);
33 free(file);
34 }
34} 35}
35 36
36int main() 37int main()
..