1/* Pattern Matchers - Common Utilities. 2 Copyright (C) 1992, 1998, 2000, 2005-2006 Free Software Foundation, Inc. 3 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 3 of the License, or 7 (at your option) any later version. 8 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. 13 14 You should have received a copy of the GNU General Public License 15 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 16 17#ifdef HAVE_CONFIG_H 18# include <config.h> 19#endif 20 21/* Specification. */ 22#include "m-common.h" 23 24#include <ctype.h> 25#include <stdlib.h> 26#include <string.h> 27 28#include "error.h" 29#include "exitfail.h" 30#include "xalloc.h" 31#include "gettext.h" 32#define _(str) gettext (str) 33 34#if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII)) 35# define IN_CTYPE_DOMAIN(c) 1 36#else 37# define IN_CTYPE_DOMAIN(c) isascii(c) 38#endif 39#define ISUPPER(C) (IN_CTYPE_DOMAIN (C) && isupper (C)) 40#define TOLOWER(C) (ISUPPER(C) ? tolower(C) : (C)) 41 42void 43kwsinit (struct compiled_kwset *ckwset, 44 bool match_icase, bool match_words, bool match_lines, char eolbyte) 45{ 46 if (match_icase) 47 { 48 int i; 49 50 ckwset->trans = XNMALLOC (NCHAR, char); 51 for (i = 0; i < NCHAR; i++) 52 ckwset->trans[i] = TOLOWER (i); 53 ckwset->kwset = kwsalloc (ckwset->trans); 54 } 55 else 56 { 57 ckwset->trans = NULL; 58 ckwset->kwset = kwsalloc (NULL); 59 } 60 if (ckwset->kwset == NULL) 61 error (exit_failure, 0, _("memory exhausted")); 62 ckwset->match_words = match_words; 63 ckwset->match_lines = match_lines; 64 ckwset->eolbyte = eolbyte; 65} 66 67#ifdef MBS_SUPPORT 68/* This function allocate the array which correspond to "buf". 69 Then this check multibyte string and mark on the positions which 70 are not singlebyte character nor the first byte of a multibyte 71 character. Caller must free the array. */ 72char* 73check_multibyte_string (const char *buf, size_t buf_size) 74{ 75 char *mb_properties = (char *) malloc (buf_size); 76 mbstate_t cur_state; 77 int i; 78 79 memset (&cur_state, 0, sizeof (mbstate_t)); 80 memset (mb_properties, 0, sizeof (char) * buf_size); 81 for (i = 0; i < buf_size ;) 82 { 83 size_t mbclen; 84 mbclen = mbrlen (buf + i, buf_size - i, &cur_state); 85 86 if (mbclen == (size_t) -1 || mbclen == (size_t) -2 || mbclen == 0) 87 { 88 /* An invalid sequence, or a truncated multibyte character. 89 We treat it as a singlebyte character. */ 90 mbclen = 1; 91 } 92 mb_properties[i] = mbclen; 93 i += mbclen; 94 } 95 96 return mb_properties; 97} 98#endif 99