[01cc7b4] | 1 | /*
|
---|
| 2 | * Copyright (c) 2018 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libc
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <stdio.h>
|
---|
| 36 | #include <str.h>
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <adt/list.h>
|
---|
| 39 | #include <wchar.h>
|
---|
| 40 | #include "../private/stdio.h"
|
---|
| 41 | #include "../private/sstream.h"
|
---|
| 42 |
|
---|
| 43 | static size_t stdio_str_read(void *, size_t, size_t, FILE *);
|
---|
| 44 | static size_t stdio_str_write(const void *, size_t, size_t, FILE *);
|
---|
| 45 | static int stdio_str_flush(FILE *);
|
---|
| 46 |
|
---|
| 47 | static __stream_ops_t stdio_str_ops = {
|
---|
| 48 | .read = stdio_str_read,
|
---|
| 49 | .write = stdio_str_write,
|
---|
| 50 | .flush = stdio_str_flush
|
---|
| 51 | };
|
---|
| 52 |
|
---|
| 53 | /** Read from string stream. */
|
---|
| 54 | static size_t stdio_str_read(void *buf, size_t size, size_t nmemb, FILE *stream)
|
---|
| 55 | {
|
---|
| 56 | size_t nread;
|
---|
| 57 | char *cp = (char *)stream->arg;
|
---|
| 58 | char *bp = (char *)buf;
|
---|
| 59 |
|
---|
| 60 | nread = 0;
|
---|
| 61 | while (nread < size * nmemb) {
|
---|
| 62 | if (*cp == '\0') {
|
---|
| 63 | stream->eof = true;
|
---|
| 64 | break;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | bp[nread] = *cp;
|
---|
| 68 | ++nread;
|
---|
| 69 | ++cp;
|
---|
| 70 | stream->arg = (void *)cp;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | return (nread / size);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /** Write to string stream. */
|
---|
| 77 | static size_t stdio_str_write(const void *buf, size_t size, size_t nmemb,
|
---|
| 78 | FILE *stream)
|
---|
| 79 | {
|
---|
| 80 | return 0;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /** Flush string stream. */
|
---|
| 84 | static int stdio_str_flush(FILE *stream)
|
---|
| 85 | {
|
---|
| 86 | return EOF;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | /** Initialize string stream.
|
---|
| 90 | *
|
---|
| 91 | * @param str String used as backend for reading
|
---|
| 92 | * @param stream Stream to initialize
|
---|
| 93 | */
|
---|
| 94 | void __sstream_init(const char *str, FILE *stream)
|
---|
| 95 | {
|
---|
| 96 | memset(stream, 0, sizeof(FILE));
|
---|
| 97 | stream->ops = &stdio_str_ops;
|
---|
| 98 | stream->arg = (void *)str;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | /** @}
|
---|
| 102 | */
|
---|