blob: a50ea58628713021fd696be3a5acdc166c80ba05 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19/*
20 * The following applies to stcasecmp and strncasecmp implementations:
21 *
22 * Copyright (c) 1987 Regents of the University of California.
23 * All rights reserved.
24 *
25 * Redistribution and use in source and binary forms are permitted
26 * provided that this notice is preserved and that due credit is given
27 * to the University of California at Berkeley. The name of the University
28 * may not be used to endorse or promote products derived from this
29 * software without specific written prior permission. This software
30 * is provided ``as is'' without express or implied warranty.
31 */
32
Adam Tkac3a549222008-10-08 13:40:28 +000033#ifdef HAVE_COMMON_CONFIG_H
Adam Tkacad1cbd92008-10-06 14:08:00 +000034#include <common-config.h>
35#endif
36
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000037#include <rfb/util.h>
38
39// Provide strcasecmp() and/or strncasecmp() if absent on this system.
40
41#ifndef WIN32
42#if !defined(HAVE_STRCASECMP) || !defined(HAVE_STRNCASECMP)
43
44extern "C" {
45
46/*
47 * This array is designed for mapping upper and lower case letter
48 * together for a case independent comparison. The mappings are
49 * based upon ascii character sequences.
50 */
51static unsigned char s_charmap[] = {
52 '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
53 '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
54 '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
55 '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
56 '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
57 '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
58 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
59 '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
60 '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
61 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
62 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
63 '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
64 '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
65 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
66 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
67 '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
68 '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
69 '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
70 '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
71 '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
72 '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
73 '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
74 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
75 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
76 '\300', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
77 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
78 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
79 '\370', '\371', '\372', '\333', '\334', '\335', '\336', '\337',
80 '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
81 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
82 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
83 '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
84};
85
86#ifndef HAVE_STRCASECMP
87int
88strcasecmp(const char *s1, const char *s2)
89{
90 unsigned char u1, u2;
91
92 for (;;) {
93 u1 = (unsigned char) *s1++;
94 u2 = (unsigned char) *s2++;
95 if (s_charmap[u1] != s_charmap[u2]) {
96 return s_charmap[u1] - s_charmap[u2];
97 }
98 if (u1 == '\0') {
99 return 0;
100 }
101 }
102}
103#endif // !defined(HAVE_STRCASECMP)
104
105#ifndef HAVE_STRNCASECMP
106int
107strncasecmp(const char *s1, const char *s2, size_t n)
108{
109 unsigned char u1, u2;
110
111 for (; n != 0; --n) {
112 u1 = (unsigned char) *s1++;
113 u2 = (unsigned char) *s2++;
114 if (s_charmap[u1] != s_charmap[u2]) {
115 return s_charmap[u1] - s_charmap[u2];
116 }
117 if (u1 == '\0') {
118 return 0;
119 }
120 }
121 return 0;
122}
123#endif // !defined(HAVE_STRNCASECMP)
124
125} // extern "C"
126
127#endif // !defined(HAVE_STRCASECMP) || !defined(HAVE_STRNCASECMP)
128#endif // defined(WIN32)
129
130namespace rfb {
131
132 char* strDup(const char* s) {
133 if (!s) return 0;
134 int l = strlen(s);
135 char* r = new char[l+1];
136 memcpy(r, s, l+1);
137 return r;
138 };
139
140 void strFree(char* s) {
141 delete [] s;
142 }
143
144
145 bool strSplit(const char* src, const char limiter, char** out1, char** out2, bool fromEnd) {
146 CharArray out1old, out2old;
147 if (out1) out1old.buf = *out1;
148 if (out2) out2old.buf = *out2;
149 int len = strlen(src);
150 int i=0, increment=1, limit=len;
151 if (fromEnd) {
152 i=len-1; increment = -1; limit = -1;
153 }
154 while (i!=limit) {
155 if (src[i] == limiter) {
156 if (out1) {
157 *out1 = new char[i+1];
158 if (i) memcpy(*out1, src, i);
159 (*out1)[i] = 0;
160 }
161 if (out2) {
162 *out2 = new char[len-i];
163 if (len-i-1) memcpy(*out2, &src[i+1], len-i-1);
164 (*out2)[len-i-1] = 0;
165 }
166 return true;
167 }
168 i+=increment;
169 }
170 if (out1) *out1 = strDup(src);
171 if (out2) *out2 = 0;
172 return false;
173 }
174
175 bool strContains(const char* src, char c) {
176 int l=strlen(src);
177 for (int i=0; i<l; i++)
178 if (src[i] == c) return true;
179 return false;
180 }
181
182 void strCopy(char* dest, const char* src, int destlen) {
183 if (src)
184 strncpy(dest, src, destlen-1);
185 dest[src ? destlen-1 : 0] = 0;
186 }
187
188};