]>
Commit | Line | Data |
---|---|---|
6d7e8eda | 1 | /* Copyright (C) 1996-2023 Free Software Foundation, Inc. |
c84142e8 | 2 | This file is part of the GNU C Library. |
c84142e8 | 3 | |
43bc8ac6 | 4 | This program is free software; you can redistribute it and/or modify |
2e2efe65 RM |
5 | it under the terms of the GNU General Public License as published |
6 | by the Free Software Foundation; version 2 of the License, or | |
7 | (at your option) any later version. | |
c84142e8 | 8 | |
43bc8ac6 | 9 | This program is distributed in the hope that it will be useful, |
c84142e8 | 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
43bc8ac6 UD |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. | |
c84142e8 | 13 | |
43bc8ac6 | 14 | You should have received a copy of the GNU General Public License |
5a82c748 | 15 | along with this program; if not, see <https://www.gnu.org/licenses/>. */ |
19bc17a9 RM |
16 | |
17 | #ifndef _LINEREADER_H | |
18 | #define _LINEREADER_H 1 | |
19 | ||
20 | #include <ctype.h> | |
21 | #include <libintl.h> | |
4b10dd6c | 22 | #include <stdint.h> |
19bc17a9 RM |
23 | #include <stdio.h> |
24 | ||
4b10dd6c | 25 | #include "charmap.h" |
19bc17a9 RM |
26 | #include "error.h" |
27 | #include "locfile-token.h" | |
4b10dd6c | 28 | #include "repertoire.h" |
f16491eb | 29 | #include "record-status.h" |
19bc17a9 | 30 | |
3f02b772 | 31 | typedef const struct keyword_t *(*kw_hash_fct_t) (const char *, size_t); |
19bc17a9 | 32 | struct charset_t; |
47e8b443 | 33 | struct localedef_t; |
19bc17a9 RM |
34 | |
35 | struct token | |
36 | { | |
37 | enum token_t tok; | |
38 | union | |
39 | { | |
40 | struct | |
41 | { | |
4b10dd6c UD |
42 | char *startmb; |
43 | size_t lenmb; | |
44 | uint32_t *startwc; | |
45 | size_t lenwc; | |
19bc17a9 RM |
46 | } str; |
47 | unsigned long int num; | |
48 | struct | |
49 | { | |
4b10dd6c | 50 | /* This element is sized on the safe expectation that no single |
f24a6d08 | 51 | character in any character set uses more than 16 bytes. */ |
4b10dd6c | 52 | unsigned char bytes[16]; |
19bc17a9 RM |
53 | int nbytes; |
54 | } charcode; | |
4b10dd6c | 55 | uint32_t ucs4; |
19bc17a9 RM |
56 | } val; |
57 | }; | |
58 | ||
59 | ||
60 | struct linereader | |
61 | { | |
62 | FILE *fp; | |
63 | const char *fname; | |
64 | char *buf; | |
65 | size_t bufsize; | |
66 | size_t bufact; | |
67 | size_t lineno; | |
68 | ||
69 | size_t idx; | |
70 | ||
71 | char comment_char; | |
72 | char escape_char; | |
73 | ||
74 | struct token token; | |
75 | ||
76 | int translate_strings; | |
4b10dd6c | 77 | int return_widestr; |
19bc17a9 RM |
78 | |
79 | kw_hash_fct_t hash_fct; | |
80 | }; | |
81 | ||
82 | ||
83 | /* Functions defined in linereader.c. */ | |
4b10dd6c | 84 | extern struct linereader *lr_open (const char *fname, kw_hash_fct_t hf); |
47e8b443 UD |
85 | extern struct linereader *lr_create (FILE *fp, const char *fname, |
86 | kw_hash_fct_t hf); | |
4b10dd6c UD |
87 | extern int lr_eof (struct linereader *lr); |
88 | extern void lr_close (struct linereader *lr); | |
89 | extern int lr_next (struct linereader *lr); | |
90 | extern struct token *lr_token (struct linereader *lr, | |
91 | const struct charmap_t *charmap, | |
47e8b443 | 92 | struct localedef_t *locale, |
93693c4d UD |
93 | const struct repertoire_t *repertoire, |
94 | int verbose); | |
dd9423a6 | 95 | extern void lr_ignore_rest (struct linereader *lr, int verbose); |
19bc17a9 RM |
96 | |
97 | ||
f16491eb CD |
98 | static inline void |
99 | __attribute__ ((__format__ (__printf__, 2, 3), nonnull (1, 2))) | |
100 | lr_error (struct linereader *lr, const char *fmt, ...) | |
101 | { | |
102 | char *str; | |
103 | va_list arg; | |
104 | struct locale_state ls; | |
105 | int ret; | |
106 | ||
107 | va_start (arg, fmt); | |
108 | ls = push_locale (); | |
109 | ||
110 | ret = vasprintf (&str, fmt, arg); | |
111 | if (ret == -1) | |
112 | abort (); | |
19bc17a9 | 113 | |
f16491eb CD |
114 | pop_locale (ls); |
115 | va_end (arg); | |
116 | ||
117 | error_at_line (0, 0, lr->fname, lr->lineno, "%s", str); | |
118 | ||
119 | free (str); | |
120 | } | |
19bc17a9 RM |
121 | |
122 | ||
123 | static inline int | |
dd9423a6 | 124 | __attribute ((always_inline)) |
19bc17a9 RM |
125 | lr_getc (struct linereader *lr) |
126 | { | |
127 | if (lr->idx == lr->bufact) | |
128 | { | |
129 | if (lr->bufact != 0) | |
130 | if (lr_next (lr) < 0) | |
131 | return EOF; | |
132 | ||
133 | if (lr->bufact == 0) | |
134 | return EOF; | |
135 | } | |
136 | ||
19d49444 | 137 | return lr->buf[lr->idx++] & 0xff; |
19bc17a9 RM |
138 | } |
139 | ||
140 | ||
141 | static inline int | |
dd9423a6 | 142 | __attribute ((always_inline)) |
19bc17a9 RM |
143 | lr_ungetc (struct linereader *lr, int ch) |
144 | { | |
145 | if (lr->idx == 0) | |
146 | return -1; | |
147 | ||
f126ef67 UD |
148 | if (ch != EOF) |
149 | lr->buf[--lr->idx] = ch; | |
19bc17a9 RM |
150 | return 0; |
151 | } | |
152 | ||
153 | ||
154 | static inline int | |
ba1ffaa1 | 155 | lr_ungetn (struct linereader *lr, size_t n) |
19bc17a9 RM |
156 | { |
157 | if (lr->idx < n) | |
158 | return -1; | |
159 | ||
160 | lr->idx -= n; | |
161 | return 0; | |
162 | } | |
163 | ||
164 | ||
19bc17a9 | 165 | #endif /* linereader.h */ |