1114402Sru// -*- C++ -*-
2151503Sru/* Copyright (C) 1989, 1990, 1991, 1992, 2004 Free Software Foundation, Inc.
3114402Sru     Written by James Clark (jjc@jclark.com)
4114402Sru
5114402SruThis file is part of groff.
6114402Sru
7114402Srugroff is free software; you can redistribute it and/or modify it under
8114402Sruthe terms of the GNU General Public License as published by the Free
9114402SruSoftware Foundation; either version 2, or (at your option) any later
10114402Sruversion.
11114402Sru
12114402Srugroff is distributed in the hope that it will be useful, but WITHOUT ANY
13114402SruWARRANTY; without even the implied warranty of MERCHANTABILITY or
14114402SruFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15114402Srufor more details.
16114402Sru
17114402SruYou should have received a copy of the GNU General Public License along
18114402Sruwith groff; see the file COPYING.  If not, write to the Free Software
19151503SruFoundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
20114402Sru
21114411Sru/* $FreeBSD$ */
22114411Sru
23114402Sru#include <ctype.h>
24114411Sru#ifdef __FreeBSD__
25114411Sru#include <locale.h>
26114411Sru#endif
27151503Sru
28151503Sru#include "lib.h"
29114402Sru#include "cset.h"
30114402Sru
31114402Srucset csalpha(CSET_BUILTIN);
32114402Srucset csupper(CSET_BUILTIN);
33114402Srucset cslower(CSET_BUILTIN);
34114402Srucset csdigit(CSET_BUILTIN);
35114402Srucset csxdigit(CSET_BUILTIN);
36114402Srucset csspace(CSET_BUILTIN);
37114402Srucset cspunct(CSET_BUILTIN);
38114402Srucset csalnum(CSET_BUILTIN);
39114402Srucset csprint(CSET_BUILTIN);
40114402Srucset csgraph(CSET_BUILTIN);
41114402Srucset cscntrl(CSET_BUILTIN);
42114402Sru
43114411Sru#if defined(isascii) && !defined(__FreeBSD__)
44114402Sru#define ISASCII(c) isascii(c)
45114402Sru#else
46114402Sru#define ISASCII(c) (1)
47114402Sru#endif
48114402Sru
49114402Sruvoid cset::clear()
50114402Sru{
51114402Sru  char *p = v;
52114402Sru  for (int i = 0; i <= UCHAR_MAX; i++)
53114402Sru    p[i] = 0;
54114402Sru}
55114402Sru
56114402Srucset::cset()
57114402Sru{
58114402Sru  clear();
59114402Sru}
60114402Sru
61114402Srucset::cset(const char *s)
62114402Sru{
63114402Sru  clear();
64114402Sru  while (*s)
65114402Sru    v[(unsigned char)*s++] = 1;
66114402Sru}
67114402Sru
68114402Srucset::cset(const unsigned char *s)
69114402Sru{
70114402Sru  clear();
71114402Sru  while (*s)
72114402Sru    v[*s++] = 1;
73114402Sru}
74114402Sru
75114402Srucset::cset(cset_builtin)
76114402Sru{
77114402Sru  // these are initialised by cset_init::cset_init()
78114402Sru}
79114402Sru
80114402Srucset &cset::operator|=(const cset &cs)
81114402Sru{
82114402Sru  for (int i = 0; i <= UCHAR_MAX; i++)
83114402Sru    if (cs.v[i])
84114402Sru      v[i] = 1;
85114402Sru  return *this;
86114402Sru}
87114402Sru
88114402Sru
89114402Sruint cset_init::initialised = 0;
90114402Sru
91114402Srucset_init::cset_init()
92114402Sru{
93114402Sru  if (initialised)
94114402Sru    return;
95114402Sru  initialised = 1;
96114411Sru#ifdef __FreeBSD__
97114411Sru  (void) setlocale(LC_CTYPE, "");
98114411Sru#endif
99114402Sru  for (int i = 0; i <= UCHAR_MAX; i++) {
100114402Sru    csalpha.v[i] = ISASCII(i) && isalpha(i);
101114402Sru    csupper.v[i] = ISASCII(i) && isupper(i);
102114402Sru    cslower.v[i] = ISASCII(i) && islower(i);
103114402Sru    csdigit.v[i] = ISASCII(i) && isdigit(i);
104114402Sru    csxdigit.v[i] = ISASCII(i) && isxdigit(i);
105114402Sru    csspace.v[i] = ISASCII(i) && isspace(i);
106114402Sru    cspunct.v[i] = ISASCII(i) && ispunct(i);
107114402Sru    csalnum.v[i] = ISASCII(i) && isalnum(i);
108114402Sru    csprint.v[i] = ISASCII(i) && isprint(i);
109114402Sru    csgraph.v[i] = ISASCII(i) && isgraph(i);
110114402Sru    cscntrl.v[i] = ISASCII(i) && iscntrl(i);
111114402Sru  }
112114402Sru}
113