/* **************************************************************************** * stream/write.c -- write to a stream. * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey * * This file is part of libcasio. * libcasio 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. * * libcasio 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 libcasio; if not, see . * ************************************************************************* */ #include "stream.h" /** * casio_write: * Write to a libcasio stream. * * @arg stream the stream to write to. * @arg data the data to write. * @arg size the amount of bytes to write. * @arg timeout timeout for the entire write (in ms). * @return the error code (0 if ok). */ int CASIO_EXPORT casio_write(casio_stream_t *stream, const void *data, size_t size, unsigned int timeout) { int err; casio_stream_write_t *wr; /* check if we can write */ if (~stream->casio_stream_mode & CASIO_OPENMODE_WRITE || !(wr = stream->casio_stream_callbacks.casio_streamfuncs_write)) { err = casio_error_write; goto fail; } /* Check that the job isn't already done. */ if (!size) return (0); /* Write the goddamn thing. */ if ((err = (*wr)(stream->casio_stream_cookie, data, size, timeout))) { msg((ll_error, "Stream writing failure: %s", casio_strerror(err))); goto fail; } /* Move the cursor and return. */ stream->casio_stream_offset += size; err = 0; fail: stream->casio_stream_lasterr = err; return (err); } /** * casio_write_char: * Write a character to a libcasio stream. * * @arg stream the stream to write to. * @arg car the character to write. * @arg timeout the timeout to write it. * @return the error code (0 if ok). */ int CASIO_EXPORT casio_write_char(casio_stream_t *stream, int car, unsigned int timeout) { char ccar = (char)car; return (casio_write(stream, &ccar, 1, timeout)); }