blob: 2709f2cce5f2db9548898cc8c27f4544763f9099 [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 Tkac8aee1a82009-09-04 12:08:56 +000033#ifdef HAVE_CONFIG_H
34#include <config.h>
Adam Tkacad1cbd92008-10-06 14:08:00 +000035#endif
36
Pierre Ossman5bc20a62011-11-08 12:42:41 +000037#include <sys/time.h>
38
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000039#include <rfb/util.h>
40
41// Provide strcasecmp() and/or strncasecmp() if absent on this system.
42
43#ifndef WIN32
44#if !defined(HAVE_STRCASECMP) || !defined(HAVE_STRNCASECMP)
45
46extern "C" {
47
48/*
49 * This array is designed for mapping upper and lower case letter
50 * together for a case independent comparison. The mappings are
51 * based upon ascii character sequences.
52 */
53static unsigned char s_charmap[] = {
54 '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
55 '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
56 '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
57 '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
58 '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
59 '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
60 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
61 '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
62 '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
63 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
64 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
65 '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
66 '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
67 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
68 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
69 '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
70 '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
71 '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
72 '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
73 '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
74 '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
75 '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
76 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
77 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
78 '\300', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
79 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
80 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
81 '\370', '\371', '\372', '\333', '\334', '\335', '\336', '\337',
82 '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
83 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
84 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
85 '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
86};
87
88#ifndef HAVE_STRCASECMP
89int
90strcasecmp(const char *s1, const char *s2)
91{
92 unsigned char u1, u2;
93
94 for (;;) {
95 u1 = (unsigned char) *s1++;
96 u2 = (unsigned char) *s2++;
97 if (s_charmap[u1] != s_charmap[u2]) {
98 return s_charmap[u1] - s_charmap[u2];
99 }
100 if (u1 == '\0') {
101 return 0;
102 }
103 }
104}
105#endif // !defined(HAVE_STRCASECMP)
106
107#ifndef HAVE_STRNCASECMP
108int
109strncasecmp(const char *s1, const char *s2, size_t n)
110{
111 unsigned char u1, u2;
112
113 for (; n != 0; --n) {
114 u1 = (unsigned char) *s1++;
115 u2 = (unsigned char) *s2++;
116 if (s_charmap[u1] != s_charmap[u2]) {
117 return s_charmap[u1] - s_charmap[u2];
118 }
119 if (u1 == '\0') {
120 return 0;
121 }
122 }
123 return 0;
124}
125#endif // !defined(HAVE_STRNCASECMP)
126
127} // extern "C"
128
129#endif // !defined(HAVE_STRCASECMP) || !defined(HAVE_STRNCASECMP)
130#endif // defined(WIN32)
131
132namespace rfb {
133
Adam Tkacd36b6262009-09-04 10:57:20 +0000134 char* strDup(const char* s) {
135 if (!s) return 0;
136 int l = strlen(s);
137 char* r = new char[l+1];
138 memcpy(r, s, l+1);
139 return r;
140 };
141
142 void strFree(char* s) {
143 delete [] s;
144 }
145
146
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000147 bool strSplit(const char* src, const char limiter, char** out1, char** out2, bool fromEnd) {
148 CharArray out1old, out2old;
149 if (out1) out1old.buf = *out1;
150 if (out2) out2old.buf = *out2;
151 int len = strlen(src);
152 int i=0, increment=1, limit=len;
153 if (fromEnd) {
154 i=len-1; increment = -1; limit = -1;
155 }
156 while (i!=limit) {
157 if (src[i] == limiter) {
158 if (out1) {
159 *out1 = new char[i+1];
160 if (i) memcpy(*out1, src, i);
161 (*out1)[i] = 0;
162 }
163 if (out2) {
164 *out2 = new char[len-i];
165 if (len-i-1) memcpy(*out2, &src[i+1], len-i-1);
166 (*out2)[len-i-1] = 0;
167 }
168 return true;
169 }
170 i+=increment;
171 }
Adam Tkacd36b6262009-09-04 10:57:20 +0000172 if (out1) *out1 = strDup(src);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000173 if (out2) *out2 = 0;
174 return false;
175 }
176
177 bool strContains(const char* src, char c) {
178 int l=strlen(src);
179 for (int i=0; i<l; i++)
180 if (src[i] == c) return true;
181 return false;
182 }
183
184 void strCopy(char* dest, const char* src, int destlen) {
185 if (src)
186 strncpy(dest, src, destlen-1);
187 dest[src ? destlen-1 : 0] = 0;
188 }
189
Pierre Ossman5bc20a62011-11-08 12:42:41 +0000190 unsigned msSince(const struct timeval *then)
191 {
192 struct timeval now;
193 unsigned diff;
194
195 gettimeofday(&now, NULL);
196
197 diff = (now.tv_sec - then->tv_sec) * 1000;
198
199 diff += now.tv_usec / 1000;
200 diff -= then->tv_usec / 1000;
201
202 return diff;
203 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000204};