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