git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

gbdk-support/png2hicolorgb/src/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, size_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 = (uint8_t *)malloc(fsize);
     24             if (filedata) {
     25                 if (fsize != (long)fread(filedata, 1, fsize, file_in)) {
     26                     printf("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 = (size_t)fsize;
     31             } else printf("ERROR: Failed to allocate memory to read file %s\n", filename);
     32 
     33         } else printf("ERROR: Failed to read size of file %s\n", filename);
     34 
     35         fclose(file_in);
     36     } else printf("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, size_t data_len) {
     45 
     46     bool status = false;
     47     FILE * file_out = fopen(filename, "wb");
     48 
     49     if (file_out) {
     50         if (data_len == fwrite(p_buf, 1, data_len, file_out))
     51             status = true;
     52         else
     53             printf("Warning: File write size didn't match expected for %s\n", filename);
     54 
     55         fclose(file_out);
     56     } else
     57       printf("Warning: Unable to open file: %s\n", filename);
     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, size_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 = (char *)malloc(fsize);
     82             if (filedata) {
     83                 if (fsize != (long)fread(filedata, 1, fsize, file_in)) {
     84                     printf("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 = (size_t)fsize;
     89             } else printf("ERROR: Failed to allocate memory to read file %s\n", filename);
     90 
     91         } else printf("ERROR: Failed to read size of file %s\n", filename);
     92 
     93         fclose(file_in);
     94     } else printf("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, size_t data_len) {
    103 
    104     bool status = false;
    105     FILE * file_out = fopen(filename, "w");
    106 
    107     if (file_out) {
    108         if (data_len == fwrite(p_buf, 1, data_len, file_out))
    109             status = true;
    110         else
    111             printf("Warning: File write size didn't match expected for %s\n", filename);
    112 
    113         fclose(file_out);
    114     }
    115     return status;
    116 }

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.