Blame locale/programs/linereader.h

Packit 6c4009
/* Copyright (C) 1996-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper, <drepper@gnu.org>.
Packit 6c4009
Packit 6c4009
   This program is free software; you can redistribute it and/or modify
Packit 6c4009
   it under the terms of the GNU General Public License as published
Packit 6c4009
   by the Free Software Foundation; version 2 of the License, or
Packit 6c4009
   (at your option) any later version.
Packit 6c4009
Packit 6c4009
   This program is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 6c4009
   GNU General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU General Public License
Packit 6c4009
   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#ifndef _LINEREADER_H
Packit 6c4009
#define _LINEREADER_H 1
Packit 6c4009
Packit 6c4009
#include <ctype.h>
Packit 6c4009
#include <libintl.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
Packit 6c4009
#include "charmap.h"
Packit 6c4009
#include "error.h"
Packit 6c4009
#include "locfile-token.h"
Packit 6c4009
#include "repertoire.h"
Packit 6c4009
#include "record-status.h"
Packit 6c4009
Packit 6c4009
typedef const struct keyword_t *(*kw_hash_fct_t) (const char *, unsigned int);
Packit 6c4009
struct charset_t;
Packit 6c4009
struct localedef_t;
Packit 6c4009
Packit 6c4009
struct token
Packit 6c4009
{
Packit 6c4009
  enum token_t tok;
Packit 6c4009
  union
Packit 6c4009
  {
Packit 6c4009
    struct
Packit 6c4009
    {
Packit 6c4009
      char *startmb;
Packit 6c4009
      size_t lenmb;
Packit 6c4009
      uint32_t *startwc;
Packit 6c4009
      size_t lenwc;
Packit 6c4009
    } str;
Packit 6c4009
    unsigned long int num;
Packit 6c4009
    struct
Packit 6c4009
    {
Packit 6c4009
      /* This element is sized on the safe expectation that no single
Packit 6c4009
	 character in any character set uses more than 16 bytes.  */
Packit 6c4009
      unsigned char bytes[16];
Packit 6c4009
      int nbytes;
Packit 6c4009
    } charcode;
Packit 6c4009
    uint32_t ucs4;
Packit 6c4009
  } val;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
struct linereader
Packit 6c4009
{
Packit 6c4009
  FILE *fp;
Packit 6c4009
  const char *fname;
Packit 6c4009
  char *buf;
Packit 6c4009
  size_t bufsize;
Packit 6c4009
  size_t bufact;
Packit 6c4009
  size_t lineno;
Packit 6c4009
Packit 6c4009
  size_t idx;
Packit 6c4009
Packit 6c4009
  char comment_char;
Packit 6c4009
  char escape_char;
Packit 6c4009
Packit 6c4009
  struct token token;
Packit 6c4009
Packit 6c4009
  int translate_strings;
Packit 6c4009
  int return_widestr;
Packit 6c4009
Packit 6c4009
  kw_hash_fct_t hash_fct;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Functions defined in linereader.c.  */
Packit 6c4009
extern struct linereader *lr_open (const char *fname, kw_hash_fct_t hf);
Packit 6c4009
extern struct linereader *lr_create (FILE *fp, const char *fname,
Packit 6c4009
				     kw_hash_fct_t hf);
Packit 6c4009
extern int lr_eof (struct linereader *lr);
Packit 6c4009
extern void lr_close (struct linereader *lr);
Packit 6c4009
extern int lr_next (struct linereader *lr);
Packit 6c4009
extern struct token *lr_token (struct linereader *lr,
Packit 6c4009
			       const struct charmap_t *charmap,
Packit 6c4009
			       struct localedef_t *locale,
Packit 6c4009
			       const struct repertoire_t *repertoire,
Packit 6c4009
			       int verbose);
Packit 6c4009
extern void lr_ignore_rest (struct linereader *lr, int verbose);
Packit 6c4009
Packit 6c4009
Packit 6c4009
static inline void
Packit 6c4009
__attribute__ ((__format__ (__printf__, 2, 3), nonnull (1, 2)))
Packit 6c4009
lr_error (struct linereader *lr, const char *fmt, ...)
Packit 6c4009
{
Packit 6c4009
  char *str;
Packit 6c4009
  va_list arg;
Packit 6c4009
  struct locale_state ls;
Packit 6c4009
  int ret;
Packit 6c4009
Packit 6c4009
  va_start (arg, fmt);
Packit 6c4009
  ls = push_locale ();
Packit 6c4009
Packit 6c4009
  ret = vasprintf (&str, fmt, arg);
Packit 6c4009
  if (ret == -1)
Packit 6c4009
    abort ();
Packit 6c4009
Packit 6c4009
  pop_locale (ls);
Packit 6c4009
  va_end (arg);
Packit 6c4009
Packit 6c4009
  error_at_line (0, 0, lr->fname, lr->lineno, "%s", str);
Packit 6c4009
Packit 6c4009
  free (str);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static inline int
Packit 6c4009
__attribute ((always_inline))
Packit 6c4009
lr_getc (struct linereader *lr)
Packit 6c4009
{
Packit 6c4009
  if (lr->idx == lr->bufact)
Packit 6c4009
    {
Packit 6c4009
      if (lr->bufact != 0)
Packit 6c4009
	if (lr_next (lr) < 0)
Packit 6c4009
	  return EOF;
Packit 6c4009
Packit 6c4009
      if (lr->bufact == 0)
Packit 6c4009
	return EOF;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return lr->buf[lr->idx] == '\32' ? EOF : lr->buf[lr->idx++];
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static inline int
Packit 6c4009
__attribute ((always_inline))
Packit 6c4009
lr_ungetc (struct linereader *lr, int ch)
Packit 6c4009
{
Packit 6c4009
  if (lr->idx == 0)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  if (ch != EOF)
Packit 6c4009
    lr->buf[--lr->idx] = ch;
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static inline int
Packit 6c4009
lr_ungetn (struct linereader *lr, size_t n)
Packit 6c4009
{
Packit 6c4009
  if (lr->idx < n)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  lr->idx -= n;
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
#endif /* linereader.h */