source: gaycko/trunk/src/io/file.c @ 4748

Last change on this file since 4748 was 4748, checked in by Jean-Yves Lamoureux, 9 years ago
  • Added copyright notices
File size: 1.1 KB
Line 
1/*
2 *  Gaycko        Text mode web browser
3 *  Copyright (c) 2011 Jean-Yves Lamoureux <jylam@lnxscene.org>
4 *                All Rights Reserved
5 *
6 *  This library is free software. It comes without any warranty, to
7 *  the extent permitted by applicable law. You can redistribute it
8 *  and/or modify it under the terms of the Do What The Fuck You Want
9 *  To Public License, Version 2, as published by Sam Hocevar. See
10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12#include "file.h"
13#include <stdlib.h>
14#include <string.h>
15
16gFile *file_open(char* filename) {
17
18        gFile *ret = malloc(sizeof(gFile));
19        ret->fp = fopen((char*)filename, "r");
20        if(!ret->fp) {
21                free(ret);
22                return NULL;
23        }
24
25        fseek(ret->fp, 0L, SEEK_END);
26        ret->size = ftell(ret->fp);
27        fseek(ret->fp, 0L, SEEK_SET);
28
29
30        ret->data = malloc(ret->size+1); // \0
31
32        int offset = 0;
33        while(!feof(ret->fp)) {
34                int c = fgetc(ret->fp);
35                if(c!=EOF) ret->data[offset++] = (char)c;
36        }
37        fclose(ret->fp);
38
39        ret->data[offset] = 0;
40
41        return ret;   
42}
43
44
45void file_close(gFile *f) {
46        if(!f) return;
47
48        memset(f->data, 0, f->size);
49        free(f->data);
50        f->size = 0;
51        free(f);
52}
Note: See TracBrowser for help on using the repository browser.