helper functions error return improvements

This commit is contained in:
Gered 2021-03-20 12:52:13 -04:00
parent f99b0be52a
commit c12e78c415
3 changed files with 22 additions and 12 deletions

View file

@ -17,15 +17,15 @@ int main(int argc, char *argv[]) {
uint32_t server_data_size = 0; uint32_t server_data_size = 0;
uint32_t client_data_size = 0; uint32_t client_data_size = 0;
uint8_t *server_data;
uint8_t *client_data;
uint8_t *server_data = read_file(server_packet_file, &server_data_size); if (read_file(server_packet_file, &server_data, &server_data_size)) {
if (!server_data) {
printf("Error reading server packet data file: %s\n", server_packet_file); printf("Error reading server packet data file: %s\n", server_packet_file);
return 1; return 1;
} }
uint8_t *client_data = read_file(client_packet_file, &client_data_size); if (read_file(client_packet_file, &client_data, &client_data_size)) {
if (!client_data) {
printf("Error reading client packet data file: %s\n", client_packet_file); printf("Error reading client packet data file: %s\n", client_packet_file);
free(server_data); free(server_data);
return 1; return 1;

20
utils.c
View file

@ -3,13 +3,15 @@
#include <string.h> #include <string.h>
#include <malloc.h> #include <malloc.h>
void* read_file(const char *filename, uint32_t *out_file_size) { #include "utils.h"
if (!out_file_size)
return NULL; int read_file(const char *filename, uint8_t** out_file_data, uint32_t *out_file_size) {
if (!out_file_size || !out_file_data)
return ERROR_INVALID_PARAMS;
FILE *fp = fopen(filename, "rb"); FILE *fp = fopen(filename, "rb");
if (!fp) if (!fp)
return NULL; return ERROR_FILE_NOT_FOUND;
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
*out_file_size = ftell(fp); *out_file_size = ftell(fp);
@ -30,19 +32,23 @@ void* read_file(const char *filename, uint32_t *out_file_size) {
} }
} while (read); } while (read);
return result; *out_file_data = result;
return SUCCESS;
} }
int get_filesize(const char *filename, size_t *out_size) { int get_filesize(const char *filename, size_t *out_size) {
if (!filename || !out_size)
return ERROR_INVALID_PARAMS;
FILE *fp = fopen(filename, "rb"); FILE *fp = fopen(filename, "rb");
if (!fp) if (!fp)
return 1; return ERROR_FILE_NOT_FOUND;
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
*out_size = ftell(fp); *out_size = ftell(fp);
fclose(fp); fclose(fp);
return 0; return SUCCESS;
} }
const char* path_to_filename(const char *path) { const char* path_to_filename(const char *path) {

View file

@ -3,7 +3,11 @@
#include <stdint.h> #include <stdint.h>
void* read_file(const char *filename, uint32_t *out_file_size); #define SUCCESS 0
#define ERROR_INVALID_PARAMS 1
#define ERROR_FILE_NOT_FOUND 2
int read_file(const char *filename, uint8_t** out_file_data, uint32_t *out_file_size);
int get_filesize(const char *filename, size_t *out_size); int get_filesize(const char *filename, size_t *out_size);
const char* path_to_filename(const char *path); const char* path_to_filename(const char *path);
char* append_string(const char *a, const char *b); char* append_string(const char *a, const char *b);