Better error management.
This commit is contained in:
parent
44773819d6
commit
1511b460db
|
|
@ -1,7 +1,8 @@
|
||||||
# Source files
|
# Source files
|
||||||
ASSET_BUILDER_SOURCES := \
|
ASSET_BUILDER_SOURCES := \
|
||||||
main.c \
|
main.c \
|
||||||
asset_file.c
|
asset_file.c \
|
||||||
|
errors.c
|
||||||
|
|
||||||
# Compiler and flags
|
# Compiler and flags
|
||||||
ASSET_BUILDER_CC = gcc
|
ASSET_BUILDER_CC = gcc
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <png.h>
|
#include <png.h>
|
||||||
#include <stdlib.h>
|
#include "errors.h"
|
||||||
|
|
||||||
int parse_png_for_header(const char *input_file_name, asset_header_def_t *asset_header_def);
|
int parse_png_for_header(const char *input_file_name, asset_header_def_t *asset_header_def);
|
||||||
int parse_png_for_content(const char *input_file_name, FILE *output_file);
|
int parse_png_for_content(const char *input_file_name, FILE *output_file);
|
||||||
|
|
@ -14,7 +14,7 @@ int parse_file_for_header(const char *input_file_name, asset_header_def_t *asset
|
||||||
|
|
||||||
if(!file_name_dot)
|
if(!file_name_dot)
|
||||||
{
|
{
|
||||||
return -1; // no extension
|
return NO_EXTENSION;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *file_name_ext = file_name_dot + 1;
|
const char *file_name_ext = file_name_dot + 1;
|
||||||
|
|
@ -23,38 +23,40 @@ int parse_file_for_header(const char *input_file_name, asset_header_def_t *asset
|
||||||
{
|
{
|
||||||
return parse_png_for_header(input_file_name, asset_header_def);
|
return parse_png_for_header(input_file_name, asset_header_def);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
return -2; // extension not supported
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return EXTENSION_UNSUPPORTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
int write_header_to_file(asset_file_header_t *asset_file_header, FILE *output_file)
|
int write_header_to_file(asset_file_header_t *asset_file_header, FILE *output_file)
|
||||||
{
|
{
|
||||||
size_t ret = fwrite(&asset_file_header->assets_number, sizeof(size_t), 1, output_file);
|
size_t ret = fwrite(&asset_file_header->assets_number, sizeof(size_t), 1, output_file);
|
||||||
if(ret != 1)
|
if(ret != 1)
|
||||||
return -8; // failed writing output file
|
return WRITE_FAILED;
|
||||||
|
|
||||||
for(size_t i = 0; i < asset_file_header->assets_number; i++)
|
for(size_t i = 0; i < asset_file_header->assets_number; i++)
|
||||||
{
|
{
|
||||||
size_t name_length = strlen(asset_file_header->assets_header_def[i].name) + 1;
|
size_t name_length = strlen(asset_file_header->assets_header_def[i].name) + 1;
|
||||||
ret = fwrite(asset_file_header->assets_header_def[i].name, sizeof(unsigned char), name_length, output_file);
|
ret = fwrite(asset_file_header->assets_header_def[i].name, sizeof(unsigned char), name_length, output_file);
|
||||||
if(ret != name_length)
|
if(ret != name_length)
|
||||||
return -8; // failed writing output file
|
return WRITE_FAILED;
|
||||||
|
|
||||||
ret = fwrite(&asset_file_header->assets_header_def[i].start, sizeof(size_t), 1, output_file);
|
ret = fwrite(&asset_file_header->assets_header_def[i].start, sizeof(size_t), 1, output_file);
|
||||||
if(ret != 1)
|
if(ret != 1)
|
||||||
return -8; // failed writing output file
|
return WRITE_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return NO_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
int parse_file_for_content_and_write(const char *input_file_name, FILE *output_file)
|
int parse_file_for_content_and_write(const char *input_file_name, FILE *output_file)
|
||||||
{
|
{
|
||||||
const char *file_name_dot = strrchr(input_file_name, '.');
|
const char *file_name_dot = strrchr(input_file_name, '.');
|
||||||
|
|
||||||
|
if(!file_name_dot)
|
||||||
|
{
|
||||||
|
return NO_EXTENSION;
|
||||||
|
}
|
||||||
|
|
||||||
const char *file_name_ext = file_name_dot + 1;
|
const char *file_name_ext = file_name_dot + 1;
|
||||||
|
|
||||||
if(!strcmp(file_name_ext, "png"))
|
if(!strcmp(file_name_ext, "png"))
|
||||||
|
|
@ -62,7 +64,7 @@ int parse_file_for_content_and_write(const char *input_file_name, FILE *output_f
|
||||||
return parse_png_for_content(input_file_name, output_file);
|
return parse_png_for_content(input_file_name, output_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return EXTENSION_UNSUPPORTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
// File specific functions.
|
// File specific functions.
|
||||||
|
|
@ -72,7 +74,7 @@ int parse_png_for_header(const char *file_name, asset_header_def_t *asset_header
|
||||||
|
|
||||||
if(!input_file)
|
if(!input_file)
|
||||||
{
|
{
|
||||||
return -3; // failed to open file
|
return OPEN_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check png autenticity.
|
// Check png autenticity.
|
||||||
|
|
@ -81,7 +83,7 @@ int parse_png_for_header(const char *file_name, asset_header_def_t *asset_header
|
||||||
if(!png_check_sig(sig, 8))
|
if(!png_check_sig(sig, 8))
|
||||||
{
|
{
|
||||||
fclose(input_file);
|
fclose(input_file);
|
||||||
return -4; // bad signature
|
return BAD_SIGNATURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// PNG structs.
|
// PNG structs.
|
||||||
|
|
@ -89,7 +91,7 @@ int parse_png_for_header(const char *file_name, asset_header_def_t *asset_header
|
||||||
if(!png_ptr)
|
if(!png_ptr)
|
||||||
{
|
{
|
||||||
fclose(input_file);
|
fclose(input_file);
|
||||||
return -5; // out of memory
|
return MEMORY_ALLOC_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
png_infop info_ptr = png_create_info_struct(png_ptr);
|
png_infop info_ptr = png_create_info_struct(png_ptr);
|
||||||
|
|
@ -97,7 +99,7 @@ int parse_png_for_header(const char *file_name, asset_header_def_t *asset_header
|
||||||
{
|
{
|
||||||
png_destroy_read_struct(&png_ptr, NULL, NULL);
|
png_destroy_read_struct(&png_ptr, NULL, NULL);
|
||||||
fclose(input_file);
|
fclose(input_file);
|
||||||
return -5; // out of memory
|
return MEMORY_ALLOC_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fill structs and load data.
|
// Fill structs and load data.
|
||||||
|
|
@ -115,7 +117,7 @@ int parse_png_for_header(const char *file_name, asset_header_def_t *asset_header
|
||||||
{
|
{
|
||||||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
||||||
fclose(input_file);
|
fclose(input_file);
|
||||||
return -6; // png bit depth not supported
|
return PNG_BIT_DEPTH_UNSUPPORTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch(color_type)
|
switch(color_type)
|
||||||
|
|
@ -127,7 +129,7 @@ int parse_png_for_header(const char *file_name, asset_header_def_t *asset_header
|
||||||
default:
|
default:
|
||||||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
||||||
fclose(input_file);
|
fclose(input_file);
|
||||||
return -7; // png color type not supported
|
return PNG_COLOR_TYPE_UNSUPPORTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fill asset def.
|
// Fill asset def.
|
||||||
|
|
@ -137,7 +139,7 @@ int parse_png_for_header(const char *file_name, asset_header_def_t *asset_header
|
||||||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
||||||
fclose(input_file);
|
fclose(input_file);
|
||||||
|
|
||||||
return 0;
|
return NO_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
int parse_png_for_content(const char *input_file_name, FILE *output_file)
|
int parse_png_for_content(const char *input_file_name, FILE *output_file)
|
||||||
|
|
@ -147,7 +149,7 @@ int parse_png_for_content(const char *input_file_name, FILE *output_file)
|
||||||
if(!input_file)
|
if(!input_file)
|
||||||
{
|
{
|
||||||
fclose(input_file);
|
fclose(input_file);
|
||||||
return -3; // failed to open file
|
return OPEN_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check png autenticity.
|
// Check png autenticity.
|
||||||
|
|
@ -156,7 +158,7 @@ int parse_png_for_content(const char *input_file_name, FILE *output_file)
|
||||||
if(!png_check_sig(sig, 8))
|
if(!png_check_sig(sig, 8))
|
||||||
{
|
{
|
||||||
fclose(input_file);
|
fclose(input_file);
|
||||||
return -4; // bad signature
|
return BAD_SIGNATURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// PNG structs.
|
// PNG structs.
|
||||||
|
|
@ -164,7 +166,7 @@ int parse_png_for_content(const char *input_file_name, FILE *output_file)
|
||||||
if(!png_ptr)
|
if(!png_ptr)
|
||||||
{
|
{
|
||||||
fclose(input_file);
|
fclose(input_file);
|
||||||
return -5; // out of memory
|
return MEMORY_ALLOC_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
png_infop info_ptr = png_create_info_struct(png_ptr);
|
png_infop info_ptr = png_create_info_struct(png_ptr);
|
||||||
|
|
@ -172,7 +174,7 @@ int parse_png_for_content(const char *input_file_name, FILE *output_file)
|
||||||
{
|
{
|
||||||
png_destroy_read_struct(&png_ptr, NULL, NULL);
|
png_destroy_read_struct(&png_ptr, NULL, NULL);
|
||||||
fclose(input_file);
|
fclose(input_file);
|
||||||
return -5; // out of memory
|
return MEMORY_ALLOC_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fill structs and load data.
|
// Fill structs and load data.
|
||||||
|
|
@ -190,7 +192,7 @@ int parse_png_for_content(const char *input_file_name, FILE *output_file)
|
||||||
{
|
{
|
||||||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
||||||
fclose(input_file);
|
fclose(input_file);
|
||||||
return -8; // failed writing to output file
|
return WRITE_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = fwrite(&height, sizeof(size_t), 1, output_file);
|
ret = fwrite(&height, sizeof(size_t), 1, output_file);
|
||||||
|
|
@ -198,7 +200,7 @@ int parse_png_for_content(const char *input_file_name, FILE *output_file)
|
||||||
{
|
{
|
||||||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
||||||
fclose(input_file);
|
fclose(input_file);
|
||||||
return -8; // failed writing to output file
|
return WRITE_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare storage.
|
// Prepare storage.
|
||||||
|
|
@ -226,7 +228,7 @@ int parse_png_for_content(const char *input_file_name, FILE *output_file)
|
||||||
{
|
{
|
||||||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
||||||
fclose(input_file);
|
fclose(input_file);
|
||||||
return -8; // failed writing to output file
|
return WRITE_FAILED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -234,5 +236,5 @@ int parse_png_for_content(const char *input_file_name, FILE *output_file)
|
||||||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
||||||
fclose(input_file);
|
fclose(input_file);
|
||||||
|
|
||||||
return 0;
|
return NO_ERROR;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
#include "errors.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void print_error(const char *content)
|
||||||
|
{
|
||||||
|
printf("\033[31merror\033[m: %s\n", content);
|
||||||
|
}
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
#define ASSET_FILE_H
|
#define ASSET_FILE_H
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
typedef struct asset_header_def_t {
|
typedef struct asset_header_def_t {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
#ifndef ERROR_H
|
||||||
|
#define ERROR_H
|
||||||
|
|
||||||
|
typedef enum error_t {
|
||||||
|
NO_ERROR,
|
||||||
|
NO_EXTENSION,
|
||||||
|
EXTENSION_UNSUPPORTED,
|
||||||
|
OPEN_FAILED,
|
||||||
|
WRITE_FAILED,
|
||||||
|
BAD_SIGNATURE,
|
||||||
|
MEMORY_ALLOC_FAILED,
|
||||||
|
|
||||||
|
// File-format specific errors
|
||||||
|
PNG_BIT_DEPTH_UNSUPPORTED,
|
||||||
|
PNG_COLOR_TYPE_UNSUPPORTED
|
||||||
|
} error_t;
|
||||||
|
|
||||||
|
void print_error(const char *content);
|
||||||
|
|
||||||
|
#endif // ERROR_H
|
||||||
|
|
@ -2,22 +2,15 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "asset_file.h"
|
#include "asset_file.h"
|
||||||
|
#include "errors.h"
|
||||||
void print_error(const char *content)
|
|
||||||
{
|
|
||||||
printf("\033[31merror\033[m: %s\n", content);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int exit_status = EXIT_SUCCESS;
|
|
||||||
|
|
||||||
// Verify arguments minimum number.
|
// Verify arguments minimum number.
|
||||||
if(argc < 3)
|
if(argc < 3)
|
||||||
{
|
{
|
||||||
puts("\033[33musage\033[m: asset_builder output_file input_file [input_file_2 ...]");
|
puts("\033[33musage\033[m: asset_builder output_file input_file [input_file_2 ...]");
|
||||||
exit_status = EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
goto end;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Header size and content.
|
// Header size and content.
|
||||||
|
|
@ -52,8 +45,7 @@ int main(int argc, char *argv[])
|
||||||
if(!output_asset_file)
|
if(!output_asset_file)
|
||||||
{
|
{
|
||||||
print_error("could not create output file.");
|
print_error("could not create output file.");
|
||||||
exit_status = EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
goto end;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for(size_t i = 0; i < asset_file_header.assets_number; i++)
|
for(size_t i = 0; i < asset_file_header.assets_number; i++)
|
||||||
|
|
@ -64,41 +56,33 @@ int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
switch(ret)
|
switch(ret)
|
||||||
{
|
{
|
||||||
case -1:
|
case NO_EXTENSION:
|
||||||
print_error("file does not have an extension.");
|
print_error("file does not have an extension."); break;
|
||||||
return -1;
|
|
||||||
|
|
||||||
case -2:
|
case EXTENSION_UNSUPPORTED:
|
||||||
print_error("extension not yet supported.");
|
print_error("extension not yet supported."); break;
|
||||||
return -1;
|
|
||||||
|
|
||||||
case -3:
|
case OPEN_FAILED:
|
||||||
print_error("failed to open file.");
|
print_error("failed to open file."); break;
|
||||||
return -1;
|
|
||||||
|
|
||||||
case -4:
|
case BAD_SIGNATURE:
|
||||||
print_error("file not valid.");
|
print_error("file not valid."); break;
|
||||||
return -1;
|
|
||||||
|
|
||||||
case -5:
|
case MEMORY_ALLOC_FAILED:
|
||||||
print_error("memory allocation failed.");
|
print_error("memory allocation failed."); break;
|
||||||
return -1;
|
|
||||||
|
|
||||||
case -6:
|
case PNG_BIT_DEPTH_UNSUPPORTED:
|
||||||
print_error("png bit depth not supported.");
|
print_error("png bit depth not supported."); break;
|
||||||
return -1;
|
|
||||||
|
|
||||||
case -7:
|
case PNG_COLOR_TYPE_UNSUPPORTED:
|
||||||
print_error("png color type not supported.");
|
print_error("png color type not supported."); break;
|
||||||
return -1;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
print_error("unknown error");
|
print_error("unknown error"); break;
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exit_status = EXIT_FAILURE;
|
fclose(output_asset_file);
|
||||||
goto end;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate absolute position of assets in the file.
|
// Calculate absolute position of assets in the file.
|
||||||
|
|
@ -110,8 +94,8 @@ int main(int argc, char *argv[])
|
||||||
if(write_header_to_file(&asset_file_header, output_asset_file))
|
if(write_header_to_file(&asset_file_header, output_asset_file))
|
||||||
{
|
{
|
||||||
print_error("can't write to output file.");
|
print_error("can't write to output file.");
|
||||||
exit_status = EXIT_FAILURE;
|
fclose(output_asset_file);
|
||||||
goto end;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(size_t i = 0; i < asset_file_header.assets_number; i++)
|
for(size_t i = 0; i < asset_file_header.assets_number; i++)
|
||||||
|
|
@ -122,42 +106,38 @@ int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
switch(ret)
|
switch(ret)
|
||||||
{
|
{
|
||||||
case -3:
|
case NO_EXTENSION:
|
||||||
print_error("failed to open file.");
|
print_error("file does not have an extension."); break;
|
||||||
return -1;
|
|
||||||
|
|
||||||
case -4:
|
case EXTENSION_UNSUPPORTED:
|
||||||
print_error("file not valid.");
|
print_error("extension not yet supported."); break;
|
||||||
return -1;
|
|
||||||
|
|
||||||
case -5:
|
case OPEN_FAILED:
|
||||||
print_error("memory allocation failed.");
|
print_error("failed to open file."); break;
|
||||||
return -1;
|
|
||||||
|
|
||||||
case -6:
|
case WRITE_FAILED:
|
||||||
print_error("png bit depth not supported.");
|
print_error("failed to write to output file."); break;
|
||||||
return -1;
|
|
||||||
|
|
||||||
case -7:
|
case BAD_SIGNATURE:
|
||||||
print_error("png color type not supported.");
|
print_error("file not valid."); break;
|
||||||
return -1;
|
|
||||||
|
|
||||||
case -8:
|
case MEMORY_ALLOC_FAILED:
|
||||||
print_error("failed writing to output file.");
|
print_error("memory allocation failed."); break;
|
||||||
return -1;
|
|
||||||
|
case PNG_BIT_DEPTH_UNSUPPORTED:
|
||||||
|
print_error("png bit depth not supported."); break;
|
||||||
|
|
||||||
|
case PNG_COLOR_TYPE_UNSUPPORTED:
|
||||||
|
print_error("png color type not supported."); break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
print_error("unknown error");
|
print_error("unknown error"); break;
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exit_status = EXIT_FAILURE;
|
fclose(output_asset_file);
|
||||||
goto end;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
end:
|
return EXIT_SUCCESS;
|
||||||
if(output_asset_file) fclose(output_asset_file);
|
|
||||||
|
|
||||||
return exit_status;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue