libansilove

Library for converting ANSI, ASCII, and other formats to PNG
Log | Files | Refs | README | LICENSE

savefile.c (749B)


      1 /*
      2  * savefile.c
      3  * libansilove 1.2.4
      4  * https://www.ansilove.org
      5  *
      6  * Copyright (c) 2011-2020 Stefan Vogt, Brian Cassidy, and Frederic Cambus
      7  * All rights reserved.
      8  *
      9  * libansilove is licensed under the BSD 2-Clause License.
     10  * See LICENSE file for details.
     11  */
     12 
     13 #include <stdio.h>
     14 #include "ansilove.h"
     15 
     16 int
     17 ansilove_savefile(struct ansilove_ctx *ctx, char *output)
     18 {
     19 	size_t rw;
     20 
     21 	if (ctx == NULL || output == NULL) {
     22 		if (ctx)
     23 			ctx->error = ANSILOVE_INVALID_PARAM;
     24 
     25 		return -1;
     26 	}
     27 
     28 	FILE *file = fopen(output, "wb");
     29 
     30 	if (!file)
     31 		goto err;
     32 
     33 	rw = fwrite(ctx->png.buffer, 1, ctx->png.length, file);
     34 	fclose(file);
     35 
     36 	if (rw != (size_t)ctx->png.length)
     37 		goto err;
     38 
     39 	return 0;
     40 
     41 err:
     42 	ctx->error = ANSILOVE_FILE_WRITE_ERROR;
     43 	return -1;
     44 }