gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
gbdk-support/gbcompress/files.c
1 #include <stdio.h>
2 #include <stdbool.h>
3 #include <stdint.h>
4 #include <stdlib.h>
5
6
7
8 // Read from a file into a buffer (will allocate needed memory)
9 // Returns NULL if reading file didn't succeed
10 uint8_t * file_read_into_buffer(char * filename, uint32_t *ret_size) {
11
12 long fsize;
13 FILE * file_in = fopen(filename, "rb");
14 uint8_t * filedata = NULL;
15
16 if (file_in) {
17 // Get file size
18 fseek(file_in, 0, SEEK_END);
19 fsize = ftell(file_in);
20 if (fsize != -1L) {
21 fseek(file_in, 0, SEEK_SET);
22
23 filedata = malloc(fsize);
24 if (filedata) {
25 if (fsize != fread(filedata, 1, fsize, file_in)) {
26 printf("gbcompress: Warning: File read size didn't match expected for %s\n", filename);
27 filedata = NULL;
28 }
29 // Read was successful, set return size
30 *ret_size = fsize;
31 } else printf("gbcompress: ERROR: Failed to allocate memory to read file %s\n", filename);
32
33 } else printf("gbcompress: ERROR: Failed to read size of file %s\n", filename);
34
35 fclose(file_in);
36 } else printf("gbcompress: ERROR: Failed to open input file %s\n", filename);
37
38 return filedata;
39 }
40
41
42
43 // Writes a buffer to a file
44 bool file_write_from_buffer(char * filename, uint8_t * p_buf, uint32_t data_len) {
45
46 bool status = false;
47 size_t wrote_bytes;
48 FILE * file_out = fopen(filename, "wb");
49
50 if (file_out) {
51 if (data_len == fwrite(p_buf, 1, data_len, file_out))
52 status = true;
53 else
54 printf("gbcompress: Warning: File write size didn't match expected for %s\n", filename);
55
56 fclose(file_out);
57 }
58 return status;
59 }
60
61
62
63
64 // Read from a file into a buffer (will allocate needed memory)
65 // Returns NULL if reading file didn't succeed
66 char * file_read_into_buffer_char(char * filename, uint32_t *ret_size) {
67
68 long fsize;
69 // On windows windows fread() will auto-translate CRLF to just LF and
70 // report it as one byte read, messing up the size check
71 FILE * file_in = fopen(filename, "rb");
72 char * filedata = NULL;
73
74 if (file_in) {
75 // Get file size
76 fseek(file_in, 0, SEEK_END);
77 fsize = ftell(file_in);
78 if (fsize != -1L) {
79 fseek(file_in, 0, SEEK_SET);
80
81 filedata = malloc(fsize);
82 if (filedata) {
83 if (fsize != fread(filedata, 1, fsize, file_in)) {
84 printf("gbcompress: Warning: File read size didn't match expected for %s\n", filename);
85 filedata = NULL;
86 }
87 // Read was successful, set return size
88 *ret_size = fsize;
89 } else printf("gbcompress: ERROR: Failed to allocate memory to read file %s\n", filename);
90
91 } else printf("gbcompress: ERROR: Failed to read size of file %s\n", filename);
92
93 fclose(file_in);
94 } else printf("gbcompress: ERROR: Failed to open input file %s\n", filename);
95
96 return filedata;
97 }
98
99
100
101 // Writes a buffer to a file
102 bool file_write_from_buffer_char(char * filename, char * p_buf, uint32_t data_len) {
103
104 bool status = false;
105 size_t wrote_bytes;
106 FILE * file_out = fopen(filename, "w");
107
108 if (file_out) {
109 if (data_len == fwrite(p_buf, 1, data_len, file_out))
110 status = true;
111 else
112 printf("gbcompress: Warning: File write size didn't match expected for %s\n", filename);
113
114 fclose(file_out);
115 }
116 return status;
117 }
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.