OSDN Git Service

client 0.6.2 release
[unagi/old-svn-converted.git] / client / tag / 0.5.3 / file.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "file.h"
4
5 int buf_load(u8 *buf, const char *file, int size)
6 {
7         FILE *fp;
8
9         fp = fopen(file, "rb");
10         if(fp == NULL){
11                 return NG;
12         }
13         fseek(fp, 0, SEEK_SET);
14         fread(buf, sizeof(u8), size, fp);
15         fclose(fp);
16         return OK;
17 }
18
19 void* buf_load_full(const char *file, int *size)
20 {
21         FILE *fp;
22         u8 *buf;
23
24         *size = 0;
25         fp = fopen(file, "rb");
26         if(fp == NULL){
27                 return NULL;
28         }
29         fseek(fp, 0, SEEK_END);
30         *size = ftell(fp);
31         if(*size == 0){
32                 fclose(fp);
33                 return NULL;
34         }
35         fseek(fp, 0, SEEK_SET);
36         buf = malloc(*size);
37         fread(buf, sizeof(u8), *size, fp);
38         fclose(fp);
39         return buf;
40 }
41
42 void buf_save(const void *buf, const char *file, int size)
43 {
44         FILE *fp;
45
46         fp = fopen(file, "wb");
47         fseek(fp, 0, SEEK_SET);
48         fwrite(buf, sizeof(u8), size, fp);
49         fclose(fp);
50 }
51