libansilove

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

commit b676ec5cdb3680c322012b8f7acd3e37a29b8112
parent 389a8405b6be7e93ed789b9e6d05fb11cd75b378
Author: Frederic Cambus <fred@statdns.com>
Date:   Mon, 16 Jul 2018 20:07:31 +0200

Add an ansilove_loadfile() function to load a file from disk

Diffstat:
Minclude/ansilove.h | 2++
Asrc/loadfile.c | 48++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 50 insertions(+), 0 deletions(-)

diff --git a/include/ansilove.h b/include/ansilove.h @@ -40,6 +40,8 @@ struct ansilove_options { }; void ansilove_init(struct ansilove_ctx *, struct ansilove_options *); +int ansilove_loadfile(struct ansilove_ctx *, char *); + int ansilove_ansi(struct ansilove_ctx *, struct ansilove_options *); int ansilove_artworx(struct ansilove_ctx *, struct ansilove_options *); int ansilove_binary(struct ansilove_ctx *, struct ansilove_options *); diff --git a/src/loadfile.c b/src/loadfile.c @@ -0,0 +1,48 @@ +// +// loadfile.c +// AnsiLove/C +// +// Copyright (c) 2011-2018 Stefan Vogt, Brian Cassidy, and Frederic Cambus. +// All rights reserved. +// +// This source code is licensed under the BSD 2-Clause License. +// See the LICENSE file for details. +// + +#include <fcntl.h> +#include <unistd.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include "ansilove.h" + +int +ansilove_loadfile(struct ansilove_ctx *ctx, char *input) { + int fd; + struct stat st; + + // load input file + if ((fd = open(input, O_RDONLY)) == -1) { + // perror("File error"); + return -1; + } + + // get the file size (bytes) + if (fstat(fd, &st) == -1) { + // perror("Can't stat file"); + return -1; + } + + ctx->length = st.st_size; + + // mmap input file into memory + ctx->buffer = mmap(NULL, ctx->length, PROT_READ, MAP_PRIVATE, fd, 0); + if (ctx->buffer == MAP_FAILED) { + // perror("Memory error"); + return -1; + } + + // close input file, we don't need it anymore + close(fd); + + return 0; +}