]> git.ipfire.org Git - thirdparty/glibc.git/blame - locale/programs/linereader.h
Update.
[thirdparty/glibc.git] / locale / programs / linereader.h
CommitLineData
c84142e8
UD
1/* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper, <drepper@gnu.ai.mit.edu>.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
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
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19bc17a9
RM
19
20#ifndef _LINEREADER_H
21#define _LINEREADER_H 1
22
23#include <ctype.h>
24#include <libintl.h>
25#include <stdio.h>
26
27#include "error.h"
28#include "locfile-token.h"
29
30
31typedef const struct keyword_t *(*kw_hash_fct_t) (const char *, int);
32struct charset_t;
33
34
35struct token
36{
37 enum token_t tok;
38 union
39 {
40 struct
41 {
42 char *start;
43 size_t len;
44 } str;
45 unsigned long int num;
46 struct
47 {
48 unsigned int val;
49 int nbytes;
50 } charcode;
51 } val;
52};
53
54
55struct linereader
56{
57 FILE *fp;
58 const char *fname;
59 char *buf;
60 size_t bufsize;
61 size_t bufact;
62 size_t lineno;
63
64 size_t idx;
65
66 char comment_char;
67 char escape_char;
68
69 struct token token;
70
71 int translate_strings;
72
73 kw_hash_fct_t hash_fct;
74};
75
76
77/* Functions defined in linereader.c. */
78struct linereader *lr_open (const char *fname, kw_hash_fct_t hf);
79int lr_eof (struct linereader *lr);
80void lr_close (struct linereader *lr);
81int lr_next (struct linereader *lr);
82struct token *lr_token (struct linereader *lr,
83 const struct charset_t *charset);
84
85
86#define lr_error(lr, fmt, args...) \
87 error_at_line (0, 0, lr->fname, lr->lineno, fmt, ## args)
88
89
90
91static inline int
92lr_getc (struct linereader *lr)
93{
94 if (lr->idx == lr->bufact)
95 {
96 if (lr->bufact != 0)
97 if (lr_next (lr) < 0)
98 return EOF;
99
100 if (lr->bufact == 0)
101 return EOF;
102 }
103
104 return lr->buf[lr->idx] == '\32' ? EOF : lr->buf[lr->idx++];
105}
106
107
108static inline int
109lr_ungetc (struct linereader *lr, int ch)
110{
111 if (lr->idx == 0)
112 return -1;
113
114 lr->buf[--lr->idx] = ch;
115 return 0;
116}
117
118
119static inline int
ba1ffaa1 120lr_ungetn (struct linereader *lr, size_t n)
19bc17a9
RM
121{
122 if (lr->idx < n)
123 return -1;
124
125 lr->idx -= n;
126 return 0;
127}
128
129
130static inline void
131lr_ignore_rest (struct linereader *lr, int verbose)
132{
133 if (verbose)
134 {
135 while (isspace (lr->buf[lr->idx]) && lr->buf[lr->idx] != '\n'
136 && lr->buf[lr->idx] != lr->comment_char)
137 if (lr->buf[lr->idx] == '\0')
138 {
139 if (lr_next (lr) < 0)
140 return;
141 }
142 else
143 ++lr->idx;
144
145 if (lr->buf[lr->idx] != '\n' &&lr->buf[lr->idx] != lr->comment_char)
146 lr_error (lr, _("trailing garbage at end of line"));
147 }
148
149 /* Ignore continued line. */
150 while (lr->bufact > 0 && lr->buf[lr->bufact - 1] != '\n')
151 if (lr_next (lr) < 0)
152 break;
153
154 lr->idx = lr->bufact;
155}
156
157
158#endif /* linereader.h */