2019-06-27 07:55:34 -04:00
|
|
|
/* Icecast
|
|
|
|
*
|
|
|
|
* This program is distributed under the GNU General Public License, version 2.
|
|
|
|
* A copy of this license is included with this source.
|
|
|
|
*
|
|
|
|
* Copyright 2019, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <igloo/io.h>
|
2019-10-03 07:04:36 -04:00
|
|
|
#include <igloo/error.h>
|
2019-06-27 07:55:34 -04:00
|
|
|
|
|
|
|
static int __free(igloo_INTERFACE_BASIC_ARGS)
|
|
|
|
{
|
|
|
|
fclose(*backend_userdata);
|
|
|
|
*backend_userdata = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-03 07:04:36 -04:00
|
|
|
static ssize_t __read(igloo_INTERFACE_BASIC_ARGS, void *buffer, size_t len, igloo_error_t *error)
|
2019-06-27 07:55:34 -04:00
|
|
|
{
|
2019-10-03 07:04:36 -04:00
|
|
|
ssize_t ret = fread(buffer, 1, len, *backend_userdata);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
*error = igloo_ERROR_IO;
|
|
|
|
} else {
|
|
|
|
*error = igloo_ERROR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2019-06-27 07:55:34 -04:00
|
|
|
}
|
2019-10-03 07:04:36 -04:00
|
|
|
static ssize_t __write(igloo_INTERFACE_BASIC_ARGS, const void *buffer, size_t len, igloo_error_t *error)
|
2019-06-27 07:55:34 -04:00
|
|
|
{
|
2019-10-03 07:04:36 -04:00
|
|
|
ssize_t ret = fwrite(buffer, 1, len, *backend_userdata);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
*error = igloo_ERROR_IO;
|
|
|
|
} else {
|
|
|
|
*error = igloo_ERROR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2019-06-27 07:55:34 -04:00
|
|
|
}
|
|
|
|
|
2019-10-03 07:04:36 -04:00
|
|
|
static igloo_error_t __flush(igloo_INTERFACE_BASIC_ARGS, igloo_io_opflag_t flags)
|
2019-06-27 07:55:34 -04:00
|
|
|
{
|
2019-10-03 07:04:36 -04:00
|
|
|
return fflush(*backend_userdata) == 0 ? igloo_ERROR_NONE : igloo_ERROR_GENERIC;
|
2019-06-27 07:55:34 -04:00
|
|
|
}
|
2019-10-03 07:04:36 -04:00
|
|
|
static igloo_error_t __sync(igloo_INTERFACE_BASIC_ARGS, igloo_io_opflag_t flags)
|
2019-06-27 07:55:34 -04:00
|
|
|
{
|
2019-10-03 07:04:36 -04:00
|
|
|
return igloo_ERROR_NONE;
|
2019-06-27 07:55:34 -04:00
|
|
|
}
|
2019-10-03 07:04:36 -04:00
|
|
|
static igloo_error_t __get_blockingmode(igloo_INTERFACE_BASIC_ARGS, libigloo_io_blockingmode_t *mode)
|
2019-06-27 07:55:34 -04:00
|
|
|
{
|
2019-10-03 07:04:36 -04:00
|
|
|
*mode = igloo_IO_BLOCKINGMODE_FULL;
|
|
|
|
return igloo_ERROR_NONE;
|
2019-06-27 07:55:34 -04:00
|
|
|
}
|
2019-10-03 07:04:36 -04:00
|
|
|
static igloo_error_t __get_fd_for_systemcall(igloo_INTERFACE_BASIC_ARGS, int *fd)
|
2019-06-27 07:55:34 -04:00
|
|
|
{
|
2019-10-03 07:04:36 -04:00
|
|
|
*fd = fileno(*backend_userdata);
|
|
|
|
return igloo_ERROR_NONE;
|
2019-06-27 07:55:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const igloo_io_ifdesc_t igloo_stdio_ifdesc = {
|
2019-06-27 08:36:16 -04:00
|
|
|
igloo_INTERFACE_DESCRIPTION_BASE(igloo_io_ifdesc_t,
|
|
|
|
.free = __free
|
|
|
|
),
|
2019-06-27 07:55:34 -04:00
|
|
|
.read = __read,
|
|
|
|
.write = __write,
|
|
|
|
.flush = __flush,
|
|
|
|
.sync = __sync,
|
|
|
|
.get_blockingmode = __get_blockingmode,
|
|
|
|
.get_fd_for_systemcall = __get_fd_for_systemcall
|
|
|
|
};
|
|
|
|
|
2019-09-14 08:24:58 -04:00
|
|
|
igloo_io_t * igloo_stdio_new_file(const char *filename, const char *mode, const char *name, igloo_ro_t associated, igloo_ro_t instance)
|
2019-06-27 07:55:34 -04:00
|
|
|
{
|
|
|
|
FILE *file = fopen(filename, mode);
|
|
|
|
igloo_io_t *io;
|
|
|
|
|
|
|
|
if (!file)
|
|
|
|
return NULL;
|
|
|
|
|
2019-09-14 08:24:58 -04:00
|
|
|
io = igloo_io_new(&igloo_stdio_ifdesc, igloo_RO_NULL, file, name, associated, instance);
|
2019-06-27 07:55:34 -04:00
|
|
|
if (!io) {
|
|
|
|
fclose(file);
|
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
|
|
|
return io;
|
|
|
|
}
|