ansilove

ANSI and ASCII art to PNG converter in C
Log | Files | Refs | README | LICENSE

commit 0f657ec2e0b2e3de09035d77dc545e811d08611f
parent 315e94efa3c69208ad773edd583bef83e11bbecb
Author: Frederic Cambus <fred@statdns.com>
Date:   Sat, 16 Jan 2016 17:23:49 +0100

Move SAUCE functions into their own file

Diffstat:
MCMakeLists.txt | 2+-
Msrc/ansilove.c | 117-------------------------------------------------------------------------------
Msrc/ansilove.h | 32+-------------------------------
Msrc/main.c | 1+
Asrc/sauce.c | 129+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/sauce.h | 50++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 182 insertions(+), 149 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -17,7 +17,7 @@ find_path(GD_INCLUDE_DIRS gd.h) find_library(GD_LIBRARIES NAMES gd REQUIRED) include_directories(${GD_INCLUDE_DIRS}) -set(SRC src/main.c src/albinfonts.c src/ansilove.c src/explode.c src/filesize.c src/strtolower.c src/substr.c) +set(SRC src/main.c src/albinfonts.c src/ansilove.c src/explode.c src/filesize.c src/strtolower.c src/substr.c src/sauce.c) if(NOT HAVE_STRTONUM) set (SRC ${SRC} compat/strtonum.c) diff --git a/src/ansilove.c b/src/ansilove.c @@ -1892,120 +1892,3 @@ void alXbinLoader(char *input, char *output, char *retinaout, bool createRetinaR // nuke garbage gdImageDestroy(im_XBIN); } - -// Reads SAUCE via a filename. -sauce *sauceReadFileName(char *fileName) -{ - FILE *file = fopen(fileName, "r"); - if (file == NULL) { - return NULL; - } - - sauce *record = sauceReadFile(file); - fclose(file); - return record; -} - -// Read SAUCE via a FILE pointer. -sauce *sauceReadFile(FILE *file) -{ - sauce *record; - record = malloc(sizeof *record); - - if (record != NULL) { - readRecord(file, record); - } - return record; -} - -void readRecord(FILE *file, sauce *record) -{ - if (fseek(file, 0 - RECORD_SIZE, SEEK_END) != EXIT_SUCCESS) { - free(record); - return; - } - - size_t read_status = fread(record->ID, sizeof(record->ID) - 1, 1, file); - record->ID[sizeof(record->ID) - 1] = '\0'; - - if (read_status != 1 || strcmp(record->ID, SAUCE_ID) != 0) { - free(record); - return; - } - fread(record->version, sizeof(record->version) - 1, 1, file); - record->version[sizeof(record->version) - 1] = '\0'; - fread(record->title, sizeof(record->title) - 1, 1, file); - record->title[sizeof(record->title) - 1] = '\0'; - fread(record->author, sizeof(record->author) - 1, 1, file); - record->author[sizeof(record->author) - 1] = '\0'; - fread(record->group, sizeof(record->group) - 1, 1, file); - record->group[sizeof(record->group) - 1] = '\0'; - fread(record->date, sizeof(record->date) - 1, 1, file); - record->date[sizeof(record->date) - 1] = '\0'; - fread(&(record->fileSize), sizeof(record->fileSize), 1, file); - fread(&(record->dataType), sizeof(record->dataType), 1, file); - fread(&(record->fileType), sizeof(record->fileType), 1, file); - fread(&(record->tinfo1), sizeof(record->tinfo1), 1, file); - fread(&(record->tinfo2), sizeof(record->tinfo2), 1, file); - fread(&(record->tinfo3), sizeof(record->tinfo3), 1, file); - fread(&(record->tinfo4), sizeof(record->tinfo4), 1, file); - fread(&(record->comments), sizeof(record->comments), 1, file); - fread(&(record->flags), sizeof(record->flags), 1, file); - fread(record->filler, sizeof(record->filler) - 1, 1, file); - record->filler[sizeof(record->filler) - 1] = '\0'; - - if (ferror(file) != EXIT_SUCCESS) { - free(record); - return; - } - - if (record->comments > 0) { - record->comment_lines = malloc(record->comments *sizeof(*record->comment_lines)); - - if (record->comment_lines != NULL) { - readComments(file, record->comment_lines, record->comments); - } - else { - free(record); - return; - } - } -} - -void readComments(FILE *file, char **comment_lines, int32_t comments) -{ - int32_t i; - - if (fseek(file, 0 - (RECORD_SIZE + 5 + COMMENT_SIZE *comments), SEEK_END) == EXIT_SUCCESS) { - char ID[6]; - fread(ID, sizeof(ID) - 1, 1, file); - ID[sizeof(ID) - 1] = '\0'; - - if (strcmp(ID, COMMENT_ID) != 0) { - free(comment_lines); - return; - } - - for (i = 0; i < comments; i++) { - char buf[COMMENT_SIZE + 1] = ""; - - fread(buf, COMMENT_SIZE, 1, file); - buf[COMMENT_SIZE] = '\0'; - - if (ferror(file) == EXIT_SUCCESS) { - comment_lines[i] = strdup(buf); - if (comment_lines[i] == NULL) { - free(comment_lines); - return; - } - } - else { - free(comment_lines); - return; - } - } - return; - } - free(comment_lines); - return; -} diff --git a/src/ansilove.h b/src/ansilove.h @@ -22,6 +22,7 @@ #include "substr.h" #include "explode.h" #include "filesize.h" +#include "sauce.h" #ifndef ansilove_h #define ansilove_h @@ -69,35 +70,4 @@ struct fontStruct { bool isAmigaFont; }; -// sauce records -#define RECORD_SIZE 128 -#define COMMENT_SIZE 64 -#define SAUCE_ID "SAUCE" -#define COMMENT_ID "COMNT" - -typedef struct { - char ID[6]; - char version[3]; - char title[36]; - char author[21]; - char group[21]; - char date[9]; - int32_t fileSize; - unsigned char dataType; - unsigned char fileType; - unsigned short tinfo1; - unsigned short tinfo2; - unsigned short tinfo3; - unsigned short tinfo4; - unsigned char comments; - unsigned char flags; - char filler[23]; - char **comment_lines; -} sauce; - -sauce *sauceReadFileName(char *fileName); -sauce *sauceReadFile(FILE *file); -void readRecord(FILE *file, sauce *record); -void readComments(FILE *file, char **comment_lines, int32_t comments); - #endif diff --git a/src/main.c b/src/main.c @@ -23,6 +23,7 @@ #include "strtolower.h" #include "substr.h" #include "ansilove.h" +#include "sauce.h" // prototypes void showHelp(void); diff --git a/src/sauce.c b/src/sauce.c @@ -0,0 +1,129 @@ +// +// sauce.c +// AnsiLove/C +// +// Copyright (C) 2011-2016 Stefan Vogt, Brian Cassidy, Frederic Cambus. +// All rights reserved. +// +// This source code is licensed under the BSD 3-Clause License. +// See the file LICENSE for details. +// + +#include "sauce.h" + +// Reads SAUCE via a filename. +sauce *sauceReadFileName(char *fileName) +{ + FILE *file = fopen(fileName, "r"); + if (file == NULL) { + return NULL; + } + + sauce *record = sauceReadFile(file); + fclose(file); + return record; +} + +// Read SAUCE via a FILE pointer. +sauce *sauceReadFile(FILE *file) +{ + sauce *record; + record = malloc(sizeof *record); + + if (record != NULL) { + readRecord(file, record); + } + return record; +} + +void readRecord(FILE *file, sauce *record) +{ + if (fseek(file, 0 - RECORD_SIZE, SEEK_END) != EXIT_SUCCESS) { + free(record); + return; + } + + size_t read_status = fread(record->ID, sizeof(record->ID) - 1, 1, file); + record->ID[sizeof(record->ID) - 1] = '\0'; + + if (read_status != 1 || strcmp(record->ID, SAUCE_ID) != 0) { + free(record); + return; + } + fread(record->version, sizeof(record->version) - 1, 1, file); + record->version[sizeof(record->version) - 1] = '\0'; + fread(record->title, sizeof(record->title) - 1, 1, file); + record->title[sizeof(record->title) - 1] = '\0'; + fread(record->author, sizeof(record->author) - 1, 1, file); + record->author[sizeof(record->author) - 1] = '\0'; + fread(record->group, sizeof(record->group) - 1, 1, file); + record->group[sizeof(record->group) - 1] = '\0'; + fread(record->date, sizeof(record->date) - 1, 1, file); + record->date[sizeof(record->date) - 1] = '\0'; + fread(&(record->fileSize), sizeof(record->fileSize), 1, file); + fread(&(record->dataType), sizeof(record->dataType), 1, file); + fread(&(record->fileType), sizeof(record->fileType), 1, file); + fread(&(record->tinfo1), sizeof(record->tinfo1), 1, file); + fread(&(record->tinfo2), sizeof(record->tinfo2), 1, file); + fread(&(record->tinfo3), sizeof(record->tinfo3), 1, file); + fread(&(record->tinfo4), sizeof(record->tinfo4), 1, file); + fread(&(record->comments), sizeof(record->comments), 1, file); + fread(&(record->flags), sizeof(record->flags), 1, file); + fread(record->filler, sizeof(record->filler) - 1, 1, file); + record->filler[sizeof(record->filler) - 1] = '\0'; + + if (ferror(file) != EXIT_SUCCESS) { + free(record); + return; + } + + if (record->comments > 0) { + record->comment_lines = malloc(record->comments *sizeof(*record->comment_lines)); + + if (record->comment_lines != NULL) { + readComments(file, record->comment_lines, record->comments); + } + else { + free(record); + return; + } + } +} + +void readComments(FILE *file, char **comment_lines, int32_t comments) +{ + int32_t i; + + if (fseek(file, 0 - (RECORD_SIZE + 5 + COMMENT_SIZE *comments), SEEK_END) == EXIT_SUCCESS) { + char ID[6]; + fread(ID, sizeof(ID) - 1, 1, file); + ID[sizeof(ID) - 1] = '\0'; + + if (strcmp(ID, COMMENT_ID) != 0) { + free(comment_lines); + return; + } + + for (i = 0; i < comments; i++) { + char buf[COMMENT_SIZE + 1] = ""; + + fread(buf, COMMENT_SIZE, 1, file); + buf[COMMENT_SIZE] = '\0'; + + if (ferror(file) == EXIT_SUCCESS) { + comment_lines[i] = strdup(buf); + if (comment_lines[i] == NULL) { + free(comment_lines); + return; + } + } + else { + free(comment_lines); + return; + } + } + return; + } + free(comment_lines); + return; +} diff --git a/src/sauce.h b/src/sauce.h @@ -0,0 +1,50 @@ +// +// sauce.h +// AnsiLove/C +// +// Copyright (C) 2011-2016 Stefan Vogt, Brian Cassidy, Frederic Cambus. +// All rights reserved. +// +// This source code is licensed under the BSD 3-Clause License. +// See the file LICENSE for details. +// + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#ifndef sauce_h +#define sauce_h + +// sauce records +#define RECORD_SIZE 128 +#define COMMENT_SIZE 64 +#define SAUCE_ID "SAUCE" +#define COMMENT_ID "COMNT" + +typedef struct { + char ID[6]; + char version[3]; + char title[36]; + char author[21]; + char group[21]; + char date[9]; + int32_t fileSize; + unsigned char dataType; + unsigned char fileType; + unsigned short tinfo1; + unsigned short tinfo2; + unsigned short tinfo3; + unsigned short tinfo4; + unsigned char comments; + unsigned char flags; + char filler[23]; + char **comment_lines; +} sauce; + +sauce *sauceReadFileName(char *fileName); +sauce *sauceReadFile(FILE *file); +void readRecord(FILE *file, sauce *record); +void readComments(FILE *file, char **comment_lines, int32_t comments); + +#endif