1 /* Pattern Matchers - Common Utilities.
2 Copyright (C) 1992, 1998, 2000, 2005 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
33 #define _(str) gettext (str)
35 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
36 # define IN_CTYPE_DOMAIN(c) 1
38 # define IN_CTYPE_DOMAIN(c) isascii(c)
40 #define ISUPPER(C) (IN_CTYPE_DOMAIN (C) && isupper (C))
41 #define TOLOWER(C) (ISUPPER(C) ? tolower(C) : (C))
44 kwsinit (struct compiled_kwset
*ckwset
,
45 bool match_icase
, bool match_words
, bool match_lines
, char eolbyte
)
51 ckwset
->trans
= (char *) xmalloc (NCHAR
* sizeof (char));
52 for (i
= 0; i
< NCHAR
; i
++)
53 ckwset
->trans
[i
] = TOLOWER (i
);
54 ckwset
->kwset
= kwsalloc (ckwset
->trans
);
59 ckwset
->kwset
= kwsalloc (NULL
);
61 if (ckwset
->kwset
== NULL
)
62 error (exit_failure
, 0, _("memory exhausted"));
63 ckwset
->match_words
= match_words
;
64 ckwset
->match_lines
= match_lines
;
65 ckwset
->eolbyte
= eolbyte
;
69 /* This function allocate the array which correspond to "buf".
70 Then this check multibyte string and mark on the positions which
71 are not singlebyte character nor the first byte of a multibyte
72 character. Caller must free the array. */
74 check_multibyte_string (const char *buf
, size_t buf_size
)
76 char *mb_properties
= (char *) malloc (buf_size
);
80 memset (&cur_state
, 0, sizeof (mbstate_t));
81 memset (mb_properties
, 0, sizeof (char) * buf_size
);
82 for (i
= 0; i
< buf_size
;)
85 mbclen
= mbrlen (buf
+ i
, buf_size
- i
, &cur_state
);
87 if (mbclen
== (size_t) -1 || mbclen
== (size_t) -2 || mbclen
== 0)
89 /* An invalid sequence, or a truncated multibyte character.
90 We treat it as a singlebyte character. */
93 mb_properties
[i
] = mbclen
;