Back to index

glibc  2.9
linereader.h
Go to the documentation of this file.
00001 /* Copyright (C) 1996-2001, 2002, 2003, 2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper, <drepper@gnu.org>.
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published
00007    by the Free Software Foundation; version 2 of the License, or
00008    (at your option) any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program; if not, write to the Free Software Foundation,
00017    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00018 
00019 #ifndef _LINEREADER_H
00020 #define _LINEREADER_H 1
00021 
00022 #include <ctype.h>
00023 #include <libintl.h>
00024 #include <stdint.h>
00025 #include <stdio.h>
00026 
00027 #include "charmap.h"
00028 #include "error.h"
00029 #include "locfile-token.h"
00030 #include "repertoire.h"
00031 
00032 
00033 typedef const struct keyword_t *(*kw_hash_fct_t) (const char *, unsigned int);
00034 struct charset_t;
00035 struct localedef_t;
00036 
00037 struct token
00038 {
00039   enum token_t tok;
00040   union
00041   {
00042     struct
00043     {
00044       char *startmb;
00045       size_t lenmb;
00046       uint32_t *startwc;
00047       size_t lenwc;
00048     } str;
00049     unsigned long int num;
00050     struct
00051     {
00052       /* This element is sized on the safe expectation that no single
00053         character in any character set uses more then 16 bytes.  */
00054       unsigned char bytes[16];
00055       int nbytes;
00056     } charcode;
00057     uint32_t ucs4;
00058   } val;
00059 };
00060 
00061 
00062 struct linereader
00063 {
00064   FILE *fp;
00065   const char *fname;
00066   char *buf;
00067   size_t bufsize;
00068   size_t bufact;
00069   size_t lineno;
00070 
00071   size_t idx;
00072 
00073   char comment_char;
00074   char escape_char;
00075 
00076   struct token token;
00077 
00078   int translate_strings;
00079   int return_widestr;
00080 
00081   kw_hash_fct_t hash_fct;
00082 };
00083 
00084 
00085 /* Functions defined in linereader.c.  */
00086 extern struct linereader *lr_open (const char *fname, kw_hash_fct_t hf);
00087 extern struct linereader *lr_create (FILE *fp, const char *fname,
00088                                  kw_hash_fct_t hf);
00089 extern int lr_eof (struct linereader *lr);
00090 extern void lr_close (struct linereader *lr);
00091 extern int lr_next (struct linereader *lr);
00092 extern struct token *lr_token (struct linereader *lr,
00093                             const struct charmap_t *charmap,
00094                             struct localedef_t *locale,
00095                             const struct repertoire_t *repertoire,
00096                             int verbose);
00097 extern void lr_ignore_rest (struct linereader *lr, int verbose);
00098 
00099 
00100 #define lr_error(lr, fmt, args...) \
00101   WITH_CUR_LOCALE (error_at_line (0, 0, lr->fname, lr->lineno, fmt, ## args))
00102 
00103 
00104 
00105 static inline int
00106 __attribute ((always_inline))
00107 lr_getc (struct linereader *lr)
00108 {
00109   if (lr->idx == lr->bufact)
00110     {
00111       if (lr->bufact != 0)
00112        if (lr_next (lr) < 0)
00113          return EOF;
00114 
00115       if (lr->bufact == 0)
00116        return EOF;
00117     }
00118 
00119   return lr->buf[lr->idx] == '\32' ? EOF : lr->buf[lr->idx++];
00120 }
00121 
00122 
00123 static inline int
00124 __attribute ((always_inline))
00125 lr_ungetc (struct linereader *lr, int ch)
00126 {
00127   if (lr->idx == 0)
00128     return -1;
00129 
00130   if (ch != EOF)
00131     lr->buf[--lr->idx] = ch;
00132   return 0;
00133 }
00134 
00135 
00136 static inline int
00137 lr_ungetn (struct linereader *lr, size_t n)
00138 {
00139   if (lr->idx < n)
00140     return -1;
00141 
00142   lr->idx -= n;
00143   return 0;
00144 }
00145 
00146 
00147 #endif /* linereader.h */