/* ***************************************************************************** * utils/file.c -- FILE-related functions. * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey * * This file is part of libg1m. * libg1m is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 3.0 of the License, * or (at your option) any later version. * * libg1m is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with libg1m; if not, see . * ************************************************************************** */ #include #include #include #ifndef G1M_DISABLED_FILE /* ************************************************************************** */ /* Open a FILE */ /* ************************************************************************** */ /** * g1m_open: * Open a file using its path, and decode it. * * @arg handle the handle to create. * @arg path the path of the file to open. * @arg expected the expected types (0 if none). * @return the error code (0 if ok). */ int g1m_open(g1m_handle_t **handle, const char *path, g1m_type_t expected) { /* open stream and decode */ FILE *f = fopen(path, "r"); int err = g1m_fopen(handle, path, f, expected); /* close opened stream and return error code (or 0 if ok) */ if (f) fclose(f); return (err); } /** * g1m_fopen: * Decode a file using a FILE structure. * * Will not seek, and will not keep the stream. * Make sure to fclose the stream after use. * * @arg handle the handle to create. * @arg path the file path. * @arg stream the stream. * @arg expected the expected types (0 if none). * @return the error code (0 if ok) */ int g1m_fopen(g1m_handle_t **handle, const char *path, FILE *stream, g1m_type_t expected) { /* check stream */ if (!stream) return (g1m_error_nostream); if (!__freadable(stream)) return (g1m_error_noread); /* make the buffer */ g1m_buffer_t buffer = { .g1m_buffer_cookie = stream, .g1m_buffer_read = g1m_filebuffer_read }; /* decode opened file */ return (g1m_decode(handle, path, &buffer, expected)); } /* ************************************************************************** */ /* Open a FILE and write into it */ /* ************************************************************************** */ /** * g1m_write: * Open a file and encode the file into it. * * @arg handle the handle to encode. * @arg path the file path. * @return the error code (0 if ok). */ int g1m_write(g1m_handle_t *handle, const char *path) { /* open the stream and write */ FILE *f = fopen(path, "w"); int err = g1m_fwrite(handle, f); /* close opened stream in case of error */ if (f) fclose(f); if (f && err) remove(path); return (err); } /** * g1m_fwrite: * Write to a FILE pointer. * * @arg handle the handle to encode. * @arg stream the FILE stream. * @return the error code (0 if ok). */ int g1m_fwrite(g1m_handle_t *handle, FILE *stream) { /* check stream */ if (!stream) return (g1m_error_nostream); if (!__fwritable(stream)) return (g1m_error_nowrite); /* make the buffer */ g1m_buffer_t buffer = { .g1m_buffer_cookie = stream, .g1m_buffer_write = g1m_filebuffer_write }; /* encode to file */ return (g1m_encode(handle, &buffer)); } #endif