openat: don’t close (-1)
[gnulib.git] / lib / sfl-istream.c
blobed6dd16646907bd4d37cc3c5b20227b271a8cbda
1 /* A string or file based input stream, that keeps track of a line number.
2 Copyright (C) 2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation, either version 3 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2024. */
19 #include <config.h>
21 /* Specification. */
22 #include "sfl-istream.h"
24 void
25 sfl_istream_init_from_file (sfl_istream_t *stream, FILE *fp)
27 sf_istream_init_from_file (&stream->istream, fp);
28 stream->line_number = 1;
31 void
32 sfl_istream_init_from_string (sfl_istream_t *stream, const char *input)
34 sf_istream_init_from_string (&stream->istream, input);
35 stream->line_number = 1;
38 void
39 sfl_istream_init_from_string_desc (sfl_istream_t *stream, string_desc_t input)
41 sf_istream_init_from_string_desc (&stream->istream, input);
42 stream->line_number = 1;
45 void
46 sfl_set_line_number (sfl_istream_t *stream, size_t line_number)
48 stream->line_number = line_number;
51 size_t
52 sfl_get_line_number (sfl_istream_t *stream)
54 return stream->line_number;
57 int
58 sfl_getc (sfl_istream_t *stream)
60 int c = sf_getc (&stream->istream);
61 if (c == '\n')
62 stream->line_number++;
63 return c;
66 int
67 sfl_ferror (sfl_istream_t *stream)
69 return sf_ferror (&stream->istream);
72 void
73 sfl_ungetc (sfl_istream_t *stream, int c)
75 if (c == '\n')
76 stream->line_number--;
77 sf_ungetc (&stream->istream, c);
80 void
81 sfl_free (sfl_istream_t *stream)
83 sf_free (&stream->istream);