1// -*- C++ -*-
2/* Copyright (C) 1989, 1990, 1991, 1992, 2004 Free Software Foundation, Inc.
3     Written by James Clark (jjc@jclark.com)
4
5This file is part of groff.
6
7groff is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
11
12groff is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License along
18with groff; see the file COPYING.  If not, write to the Free Software
19Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
20
21/* $FreeBSD$ */
22
23#include <ctype.h>
24#ifdef __FreeBSD__
25#include <locale.h>
26#endif
27
28#include "lib.h"
29#include "cset.h"
30
31cset csalpha(CSET_BUILTIN);
32cset csupper(CSET_BUILTIN);
33cset cslower(CSET_BUILTIN);
34cset csdigit(CSET_BUILTIN);
35cset csxdigit(CSET_BUILTIN);
36cset csspace(CSET_BUILTIN);
37cset cspunct(CSET_BUILTIN);
38cset csalnum(CSET_BUILTIN);
39cset csprint(CSET_BUILTIN);
40cset csgraph(CSET_BUILTIN);
41cset cscntrl(CSET_BUILTIN);
42
43#if defined(isascii) && !defined(__FreeBSD__)
44#define ISASCII(c) isascii(c)
45#else
46#define ISASCII(c) (1)
47#endif
48
49void cset::clear()
50{
51  char *p = v;
52  for (int i = 0; i <= UCHAR_MAX; i++)
53    p[i] = 0;
54}
55
56cset::cset()
57{
58  clear();
59}
60
61cset::cset(const char *s)
62{
63  clear();
64  while (*s)
65    v[(unsigned char)*s++] = 1;
66}
67
68cset::cset(const unsigned char *s)
69{
70  clear();
71  while (*s)
72    v[*s++] = 1;
73}
74
75cset::cset(cset_builtin)
76{
77  // these are initialised by cset_init::cset_init()
78}
79
80cset &cset::operator|=(const cset &cs)
81{
82  for (int i = 0; i <= UCHAR_MAX; i++)
83    if (cs.v[i])
84      v[i] = 1;
85  return *this;
86}
87
88
89int cset_init::initialised = 0;
90
91cset_init::cset_init()
92{
93  if (initialised)
94    return;
95  initialised = 1;
96#ifdef __FreeBSD__
97  (void) setlocale(LC_CTYPE, "");
98#endif
99  for (int i = 0; i <= UCHAR_MAX; i++) {
100    csalpha.v[i] = ISASCII(i) && isalpha(i);
101    csupper.v[i] = ISASCII(i) && isupper(i);
102    cslower.v[i] = ISASCII(i) && islower(i);
103    csdigit.v[i] = ISASCII(i) && isdigit(i);
104    csxdigit.v[i] = ISASCII(i) && isxdigit(i);
105    csspace.v[i] = ISASCII(i) && isspace(i);
106    cspunct.v[i] = ISASCII(i) && ispunct(i);
107    csalnum.v[i] = ISASCII(i) && isalnum(i);
108    csprint.v[i] = ISASCII(i) && isprint(i);
109    csgraph.v[i] = ISASCII(i) && isgraph(i);
110    cscntrl.v[i] = ISASCII(i) && iscntrl(i);
111  }
112}
113