]> git.ipfire.org Git - thirdparty/glibc.git/blame - locale/programs/linereader.h
Update.
[thirdparty/glibc.git] / locale / programs / linereader.h
CommitLineData
93693c4d 1/* Copyright (C) 1996,1997,1998,1999,2000,2001 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
UD
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
c84142e8
UD
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 13 Lesser General Public License for more details.
c84142e8 14
41bdb6e2
AJ
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19bc17a9
RM
19
20#ifndef _LINEREADER_H
21#define _LINEREADER_H 1
22
23#include <ctype.h>
24#include <libintl.h>
4b10dd6c 25#include <stdint.h>
19bc17a9
RM
26#include <stdio.h>
27
4b10dd6c 28#include "charmap.h"
19bc17a9
RM
29#include "error.h"
30#include "locfile-token.h"
4b10dd6c 31#include "repertoire.h"
19bc17a9
RM
32
33
4b10dd6c 34typedef const struct keyword_t *(*kw_hash_fct_t) (const char *, unsigned int);
19bc17a9 35struct charset_t;
47e8b443 36struct localedef_t;
19bc17a9
RM
37
38struct token
39{
40 enum token_t tok;
41 union
42 {
43 struct
44 {
4b10dd6c
UD
45 char *startmb;
46 size_t lenmb;
47 uint32_t *startwc;
48 size_t lenwc;
19bc17a9
RM
49 } str;
50 unsigned long int num;
51 struct
52 {
4b10dd6c
UD
53 /* This element is sized on the safe expectation that no single
54 character in any character set uses more then 16 bytes. */
55 unsigned char bytes[16];
19bc17a9
RM
56 int nbytes;
57 } charcode;
4b10dd6c 58 uint32_t ucs4;
19bc17a9
RM
59 } val;
60};
61
62
63struct linereader
64{
65 FILE *fp;
66 const char *fname;
67 char *buf;
68 size_t bufsize;
69 size_t bufact;
70 size_t lineno;
71
72 size_t idx;
73
74 char comment_char;
75 char escape_char;
76
77 struct token token;
78
79 int translate_strings;
4b10dd6c 80 int return_widestr;
19bc17a9
RM
81
82 kw_hash_fct_t hash_fct;
83};
84
85
86/* Functions defined in linereader.c. */
4b10dd6c 87extern struct linereader *lr_open (const char *fname, kw_hash_fct_t hf);
47e8b443
UD
88extern struct linereader *lr_create (FILE *fp, const char *fname,
89 kw_hash_fct_t hf);
4b10dd6c
UD
90extern int lr_eof (struct linereader *lr);
91extern void lr_close (struct linereader *lr);
92extern int lr_next (struct linereader *lr);
93extern struct token *lr_token (struct linereader *lr,
94 const struct charmap_t *charmap,
47e8b443 95 struct localedef_t *locale,
93693c4d
UD
96 const struct repertoire_t *repertoire,
97 int verbose);
19bc17a9
RM
98
99
100#define lr_error(lr, fmt, args...) \
101 error_at_line (0, 0, lr->fname, lr->lineno, fmt, ## args)
102
103
104
105static inline int
106lr_getc (struct linereader *lr)
107{
108 if (lr->idx == lr->bufact)
109 {
110 if (lr->bufact != 0)
111 if (lr_next (lr) < 0)
112 return EOF;
113
114 if (lr->bufact == 0)
115 return EOF;
116 }
117
118 return lr->buf[lr->idx] == '\32' ? EOF : lr->buf[lr->idx++];
119}
120
121
122static inline int
123lr_ungetc (struct linereader *lr, int ch)
124{
125 if (lr->idx == 0)
126 return -1;
127
f126ef67
UD
128 if (ch != EOF)
129 lr->buf[--lr->idx] = ch;
19bc17a9
RM
130 return 0;
131}
132
133
134static inline int
ba1ffaa1 135lr_ungetn (struct linereader *lr, size_t n)
19bc17a9
RM
136{
137 if (lr->idx < n)
138 return -1;
139
140 lr->idx -= n;
141 return 0;
142}
143
144
145static inline void
146lr_ignore_rest (struct linereader *lr, int verbose)
147{
148 if (verbose)
149 {
150 while (isspace (lr->buf[lr->idx]) && lr->buf[lr->idx] != '\n'
151 && lr->buf[lr->idx] != lr->comment_char)
152 if (lr->buf[lr->idx] == '\0')
153 {
154 if (lr_next (lr) < 0)
155 return;
156 }
157 else
158 ++lr->idx;
159
f126ef67
UD
160 if (lr->buf[lr->idx] != '\n' && ! feof (lr->fp)
161 && lr->buf[lr->idx] != lr->comment_char)
19bc17a9
RM
162 lr_error (lr, _("trailing garbage at end of line"));
163 }
164
165 /* Ignore continued line. */
166 while (lr->bufact > 0 && lr->buf[lr->bufact - 1] != '\n')
167 if (lr_next (lr) < 0)
168 break;
169
170 lr->idx = lr->bufact;
171}
172
173
174#endif /* linereader.h */