ansilove

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

sauce.c (3328B)


      1 /*
      2  * sauce.c
      3  * Ansilove 4.1.2
      4  * https://www.ansilove.org
      5  *
      6  * Copyright (c) 2011-2020 Stefan Vogt, Brian Cassidy, and Frederic Cambus
      7  * All rights reserved.
      8  *
      9  * Ansilove is licensed under the BSD 2-Clause License.
     10  * See LICENSE file for details.
     11  */
     12 
     13 #include <stdint.h>
     14 #include <stdlib.h>
     15 #include <string.h>
     16 
     17 #include "sauce.h"
     18 
     19 /* Reads SAUCE via a filename. */
     20 struct sauce *
     21 sauceReadFileName(char *fileName)
     22 {
     23 	FILE *file = fopen(fileName, "r");
     24 	if (file == NULL) {
     25 		return NULL;
     26 	}
     27 
     28 	struct sauce *record = sauceReadFile(file);
     29 	fclose(file);
     30 	return record;
     31 }
     32 
     33 /* Read SAUCE via a FILE pointer. */
     34 struct sauce *
     35 sauceReadFile(FILE *file)
     36 {
     37 	struct sauce *record;
     38 	record = malloc(sizeof *record);
     39 	memset(record, 0, sizeof *record);
     40 
     41 	if (record != NULL) {
     42 		readRecord(file, record);
     43 	}
     44 	return record;
     45 }
     46 
     47 void
     48 readRecord(FILE *file, struct sauce *record)
     49 {
     50 	if (fseek(file, 0 - RECORD_SIZE, SEEK_END) != 0) {
     51 		return;
     52 	}
     53 
     54 	size_t read_status = fread(record->ID, sizeof (record->ID) - 1, 1, file);
     55 	record->ID[sizeof (record->ID) - 1] = '\0';
     56 
     57 	if (read_status != 1 || strcmp(record->ID, SAUCE_ID) != 0) {
     58 		return;
     59 	}
     60 	fread(record->version, sizeof (record->version) - 1, 1, file);
     61 	record->version[sizeof (record->version) - 1] = '\0';
     62 	fread(record->title, sizeof (record->title) - 1, 1, file);
     63 	record->title[sizeof (record->title) - 1] = '\0';
     64 	fread(record->author, sizeof (record->author) - 1, 1, file);
     65 	record->author[sizeof (record->author) - 1] = '\0';
     66 	fread(record->group, sizeof (record->group) - 1, 1, file);
     67 	record->group[sizeof (record->group) - 1] = '\0';
     68 	fread(record->date, sizeof (record->date) - 1, 1, file);
     69 	record->date[sizeof (record->date) - 1] = '\0';
     70 	fread(&(record->fileSize), sizeof (record->fileSize), 1, file);
     71 	fread(&(record->dataType), sizeof (record->dataType), 1, file);
     72 	fread(&(record->fileType), sizeof (record->fileType), 1, file);
     73 	fread(&(record->tinfo1), sizeof (record->tinfo1), 1, file);
     74 	fread(&(record->tinfo2), sizeof (record->tinfo2), 1, file);
     75 	fread(&(record->tinfo3), sizeof (record->tinfo3), 1, file);
     76 	fread(&(record->tinfo4), sizeof (record->tinfo4), 1, file);
     77 	fread(&(record->comments), sizeof (record->comments), 1, file);
     78 	fread(&(record->flags), sizeof (record->flags), 1, file);
     79 	fread(record->tinfos, sizeof (record->tinfos) - 1, 1, file);
     80 	record->tinfos[sizeof (record->tinfos) - 1] = '\0';
     81 
     82 	if (ferror(file) != 0) {
     83 		return;
     84 	}
     85 
     86 	if (record->comments > 0) {
     87 		record->comment_lines = malloc(record->comments *sizeof (*record->comment_lines));
     88 
     89 		if (record->comment_lines != NULL) {
     90 			if (readComments(file, record->comment_lines, record->comments) == -1) {
     91 				record->comments = 0;
     92 			}
     93 		}
     94 	}
     95 }
     96 
     97 int
     98 readComments(FILE *file, char **comment_lines, int32_t comments)
     99 {
    100 	int32_t i;
    101 
    102 	if (fseek(file, 0 - (RECORD_SIZE + 5 + COMMENT_SIZE *comments), SEEK_END) == 0) {
    103 		char ID[6];
    104 		fread(ID, sizeof (ID) - 1, 1, file);
    105 		ID[sizeof (ID) - 1] = '\0';
    106 
    107 		if (strcmp(ID, COMMENT_ID) != 0) {
    108 			return -1;
    109 		}
    110 
    111 		for (i = 0; i < comments; i++) {
    112 			char buf[COMMENT_SIZE + 1] = "";
    113 
    114 			fread(buf, COMMENT_SIZE, 1, file);
    115 			buf[COMMENT_SIZE] = '\0';
    116 
    117 			if (ferror(file) == 0) {
    118 				comment_lines[i] = strdup(buf);
    119 				if (comment_lines[i] == NULL) {
    120 					return -1;
    121 				}
    122 			} else {
    123 				return -1;
    124 			}
    125 		}
    126 		return 0;
    127 	}
    128 	return -1;
    129 }