cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

files.c (3400B)


      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
     10uint8_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
     44bool 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
     66char * file_read_into_buffer_char(char * filename, uint32_t *ret_size) {
     67
     68    long fsize;
     69    FILE * file_in = fopen(filename, "r");
     70    char * filedata = NULL;
     71
     72    if (file_in) {
     73        // Get file size
     74        fseek(file_in, 0, SEEK_END);
     75        fsize = ftell(file_in);
     76        if (fsize != -1L) {
     77            fseek(file_in, 0, SEEK_SET);
     78
     79            filedata = malloc(fsize);
     80            if (filedata) {
     81                if (fsize != fread(filedata, 1, fsize, file_in)) {
     82                    printf("gbcompress: Warning: File read size didn't match expected for %s\n", filename);
     83                    filedata = NULL;
     84                }
     85                // Read was successful, set return size
     86                *ret_size = fsize;
     87            } else printf("gbcompress: ERROR: Failed to allocate memory to read file %s\n", filename);
     88
     89        } else printf("gbcompress: ERROR: Failed to read size of file %s\n", filename);
     90
     91        fclose(file_in);
     92    } else printf("gbcompress: ERROR: Failed to open input file %s\n", filename);
     93
     94    return filedata;
     95}
     96
     97
     98
     99// Writes a buffer to a file
    100bool file_write_from_buffer_char(char * filename, char * p_buf, uint32_t data_len) {
    101
    102    bool status = false;
    103    size_t wrote_bytes;
    104    FILE * file_out = fopen(filename, "w");
    105
    106    if (file_out) {
    107        if (data_len == fwrite(p_buf, 1, data_len, file_out))
    108            status = true;
    109        else
    110            printf("gbcompress: Warning: File write size didn't match expected for %s\n", filename);
    111
    112        fclose(file_out);
    113    }
    114    return status;
    115}