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