File: str.c

package info (click to toggle)
dictd 1.12.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,932 kB
  • ctags: 1,925
  • sloc: ansic: 13,044; sh: 4,041; yacc: 497; makefile: 444; cpp: 277; lex: 255; perl: 175; awk: 12
file content (151 lines) | stat: -rw-r--r-- 3,127 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* str.c -- 
 * Created: Fri Aug  8 15:42:10 2003 by vle@gmx.net
 * Copyright 2003 Aleksey Cheusov <vle@gmx.net>
 * This program comes with ABSOLUTELY NO WARRANTY.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 1, or (at your option) any
 * later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "dictP.h"
#include "str.h"

#include <errno.h>

/*
  Copy alphanumeric and space characters converting them to lower case.
  Strings are encoded using 8-bit character set.
*/
static int tolower_alnumspace_8bit (
   const char *src, char *dest,
   int allchars_mode,
   int cs_mode)
{
   int c;

   for (; *src; ++src) {
      c = * (const unsigned char *) src;

      if (isspace (c)) {
         *dest++ = ' ';
      }else if (allchars_mode || isalnum (c)){
	 if (!cs_mode)
	    c = tolower (c);

	 *dest++ = c;
      }
   }

   *dest = '\0';
   return 0;
}

#if HAVE_UTF8
/*
  Copies alphanumeric and space characters converting them to lower case.
  Strings are UTF-8 encoded.
*/
static int tolower_alnumspace_utf8 (
   const char *src, char *dest,
   int allchars_mode,
   int cs_mode)
{
   wchar_t      ucs4_char;
   size_t len;
   int    len2;

   mbstate_t ps;
   mbstate_t ps2;

   memset (&ps,  0, sizeof (ps));
   memset (&ps2, 0, sizeof (ps2));

   while (src && src [0]){
      len = mbrtowc__ (&ucs4_char, src, MB_CUR_MAX__, &ps);
      if ((int) len < 0)
	 return errno;

      if (iswspace__ (ucs4_char)){
	 *dest++ = ' ';
      }else if (allchars_mode || iswalnum__ (ucs4_char)){
	 if (!cs_mode)
	    ucs4_char = towlower__ (ucs4_char);

	 len2 = wcrtomb__ (dest, ucs4_char, &ps2);
	 if (len2 < 0)
	    return errno;

	 dest += len2;
      }

      src += len;
   }

   *dest = 0;

   assert (strlen (src) == strlen (dest));

   return (src == NULL);
}
#endif

/* returns 0 if failed */
int tolower_alnumspace (
   const char *src, char *dest,
   int allchars_mode,
   int cs_mode,
   int utf8_mode)
{
#if HAVE_UTF8
   if (utf8_mode){
      return tolower_alnumspace_utf8 (src, dest, allchars_mode, cs_mode);
   }else{
#endif
      return tolower_alnumspace_8bit (src, dest, allchars_mode, cs_mode);
#if HAVE_UTF8
   }
#endif
}

char *strlwr_8bit (char *str)
{
   char *p;
   for (p = str; *p; ++p){
      *p = tolower ((unsigned char) *p);
   }

   return str;
}

#if HAVE_UTF8
char *copy_utf8_string (
   const char *src,
   char *dest,
   size_t len)
{
   size_t i;
   const char *p;

   for (i=0; i < len; ++i){
      p = src + i * (MB_CUR_MAX__ + 1);

      while (*p){
	 *dest++ = *p++;
      }
   }

   *dest = 0;
   return dest;
}
#endif