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