source: gaycko/src/io/file.c @ 4716

Last change on this file since 4716 was 4716, checked in by Jean-Yves Lamoureux, 9 years ago
  • Initial commit -This line, and those below, will be ignored--

A gaycko/tests
A gaycko/tests/lnx.html
A gaycko/tests/html.html
A gaycko/tests/simple.html
A gaycko/configure.ac
A gaycko/src
A gaycko/src/dom
A gaycko/src/dom/dom.c
A gaycko/src/dom/dom.h
A gaycko/src/helpers
A gaycko/src/helpers/str.c
A gaycko/src/helpers/str.h
A gaycko/src/gaycko.h
A gaycko/src/parsing
A gaycko/src/parsing/parse.h
A gaycko/src/parsing/parse.c
A gaycko/src/io
A gaycko/src/io/file.c
A gaycko/src/io/http.c
A gaycko/src/io/file.h
A gaycko/src/io/http.h
A gaycko/src/io/io.c
A gaycko/src/io/io.h
A gaycko/src/Makefile.am
A gaycko/src/gaycko.c
A gaycko/Makefile.am
AM gaycko/bootstrap

File size: 651 bytes
Line 
1#include "file.h"
2#include <stdlib.h>
3#include <string.h>
4
5gFile *file_open(char* filename) {
6
7        gFile *ret = malloc(sizeof(gFile));
8        ret->fp = fopen((char*)filename, "r");
9        if(!ret->fp) {
10                free(ret);
11                return NULL;
12        }
13
14        fseek(ret->fp, 0L, SEEK_END);
15        ret->size = ftell(ret->fp);
16        fseek(ret->fp, 0L, SEEK_SET);
17
18
19        ret->data = malloc(ret->size+1); // \0
20
21        int offset = 0;
22        while(!feof(ret->fp)) {
23                int c = fgetc(ret->fp);
24                if(c!=EOF) ret->data[offset++] = (char)c;
25        }
26        fclose(ret->fp);
27
28        ret->data[offset] = 0;
29
30        return ret;   
31}
32
33
34void file_close(gFile *f) {
35        if(!f) return;
36
37        memset(f->data, 0, f->size);
38        free(f->data);
39        f->size = 0;
40        free(f);
41}
Note: See TracBrowser for help on using the repository browser.