cake
/
libg1m
Archived
1
0
Fork 0
This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
libg1m/src/core/open.c

80 lines
2.3 KiB
C

/* *****************************************************************************
* core/open.c -- open a file and decode it.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* 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 <http://www.gnu.org/licenses/>.
* ************************************************************************** */
#include <libg1m/internals.h>
#include <libg1m/internals/stdio_ext.h>
#include <stdlib.h>
#ifndef G1M_DISABLED_FILE
/**
* g1m_open:
* Open handle using path.
*
* @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_t **handle, const char *path,
g1m_type_t expected)
{
/* open stream */
FILE *f = fopen(path, "r");
/* open using `g1m_fopen` */
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:
* Open handle using pointer on FILE.
*
* 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_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 = {
.cookie = stream,
.read = g1m_filebuffer_read
};
/* decode opened file */
return (g1m_decode(handle, path, &buffer, expected));
}
#endif