]> git.ipfire.org Git - thirdparty/glibc.git/blob - locale/programs/linereader.h
Update.
[thirdparty/glibc.git] / locale / programs / linereader.h
1 /* Copyright (C) 1996-2001, 2002, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper, <drepper@gnu.org>.
4
5 The GNU C Library is free software; you can redistribute it and/or
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.
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 Lesser General Public License for more details.
14
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. */
19
20 #ifndef _LINEREADER_H
21 #define _LINEREADER_H 1
22
23 #include <ctype.h>
24 #include <libintl.h>
25 #include <stdint.h>
26 #include <stdio.h>
27
28 #include "charmap.h"
29 #include "error.h"
30 #include "locfile-token.h"
31 #include "repertoire.h"
32
33
34 typedef const struct keyword_t *(*kw_hash_fct_t) (const char *, unsigned int);
35 struct charset_t;
36 struct localedef_t;
37
38 struct token
39 {
40 enum token_t tok;
41 union
42 {
43 struct
44 {
45 char *startmb;
46 size_t lenmb;
47 uint32_t *startwc;
48 size_t lenwc;
49 } str;
50 unsigned long int num;
51 struct
52 {
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];
56 int nbytes;
57 } charcode;
58 uint32_t ucs4;
59 } val;
60 };
61
62
63 struct 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;
80 int return_widestr;
81
82 kw_hash_fct_t hash_fct;
83 };
84
85
86 /* Functions defined in linereader.c. */
87 extern struct linereader *lr_open (const char *fname, kw_hash_fct_t hf);
88 extern struct linereader *lr_create (FILE *fp, const char *fname,
89 kw_hash_fct_t hf);
90 extern int lr_eof (struct linereader *lr);
91 extern void lr_close (struct linereader *lr);
92 extern int lr_next (struct linereader *lr);
93 extern struct token *lr_token (struct linereader *lr,
94 const struct charmap_t *charmap,
95 struct localedef_t *locale,
96 const struct repertoire_t *repertoire,
97 int verbose);
98 extern void lr_ignore_rest (struct linereader *lr, int verbose);
99
100
101 #define lr_error(lr, fmt, args...) \
102 WITH_CUR_LOCALE (error_at_line (0, 0, lr->fname, lr->lineno, fmt, ## args))
103
104
105
106 static inline int
107 __attribute ((always_inline))
108 lr_getc (struct linereader *lr)
109 {
110 if (lr->idx == lr->bufact)
111 {
112 if (lr->bufact != 0)
113 if (lr_next (lr) < 0)
114 return EOF;
115
116 if (lr->bufact == 0)
117 return EOF;
118 }
119
120 return lr->buf[lr->idx] == '\32' ? EOF : lr->buf[lr->idx++];
121 }
122
123
124 static inline int
125 __attribute ((always_inline))
126 lr_ungetc (struct linereader *lr, int ch)
127 {
128 if (lr->idx == 0)
129 return -1;
130
131 if (ch != EOF)
132 lr->buf[--lr->idx] = ch;
133 return 0;
134 }
135
136
137 static inline int
138 lr_ungetn (struct linereader *lr, size_t n)
139 {
140 if (lr->idx < n)
141 return -1;
142
143 lr->idx -= n;
144 return 0;
145 }
146
147
148 #endif /* linereader.h */