libansilove

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

savefile.c (799B)


      1 /*
      2  * savefile.c
      3  * libansilove 1.3.1
      4  * https://www.ansilove.org
      5  *
      6  * Copyright (c) 2011-2022 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  * SPDX-License-Identifier: BSD-2-Clause
     13  */
     14 
     15 #include <stdio.h>
     16 #include "ansilove.h"
     17 
     18 int
     19 ansilove_savefile(struct ansilove_ctx *ctx, const char *output)
     20 {
     21 	size_t rw;
     22 
     23 	if (ctx == NULL || output == NULL) {
     24 		if (ctx)
     25 			ctx->error = ANSILOVE_INVALID_PARAM;
     26 
     27 		return -1;
     28 	}
     29 
     30 	FILE *file = fopen(output, "wb");
     31 
     32 	if (!file)
     33 		goto err;
     34 
     35 	rw = fwrite(ctx->png.buffer, 1, ctx->png.length, file);
     36 	fclose(file);
     37 
     38 	if (rw != (size_t)ctx->png.length)
     39 		goto err;
     40 
     41 	return 0;
     42 
     43 err:
     44 	ctx->error = ANSILOVE_FILE_WRITE_ERROR;
     45 	return -1;
     46 }