Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame^] | 1 | /* Copyright (C) 2002-2003 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 | #include <rfb/util.h> |
| 19 | |
| 20 | namespace rfb { |
| 21 | |
| 22 | char* strDup(const char* s) { |
| 23 | if (!s) return 0; |
| 24 | int l = strlen(s); |
| 25 | char* r = new char[l+1]; |
| 26 | memcpy(r, s, l+1); |
| 27 | return r; |
| 28 | }; |
| 29 | |
| 30 | void strFree(char* s) { |
| 31 | delete [] s; |
| 32 | } |
| 33 | |
| 34 | |
| 35 | bool strSplit(const char* src, const char limiter, char** out1, char** out2, bool fromEnd) { |
| 36 | CharArray out1old, out2old; |
| 37 | if (out1) out1old.buf = *out1; |
| 38 | if (out2) out2old.buf = *out2; |
| 39 | int len = strlen(src); |
| 40 | int i=0, increment=1, limit=len; |
| 41 | if (fromEnd) { |
| 42 | i=len-1; increment = -1; limit = -1; |
| 43 | } |
| 44 | while (i!=limit) { |
| 45 | if (src[i] == limiter) { |
| 46 | if (out1) { |
| 47 | *out1 = new char[i+1]; |
| 48 | if (i) memcpy(*out1, src, i); |
| 49 | (*out1)[i] = 0; |
| 50 | } |
| 51 | if (out2) { |
| 52 | *out2 = new char[len-i]; |
| 53 | if (len-i-1) memcpy(*out2, &src[i+1], len-i-1); |
| 54 | (*out2)[len-i-1] = 0; |
| 55 | } |
| 56 | return true; |
| 57 | } |
| 58 | i+=increment; |
| 59 | } |
| 60 | if (out1) *out1 = strDup(src); |
| 61 | if (out2) *out2 = 0; |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | bool strContains(const char* src, char c) { |
| 66 | int l=strlen(src); |
| 67 | for (int i=0; i<l; i++) |
| 68 | if (src[i] == c) return true; |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | void strCopy(char* dest, const char* src, int destlen) { |
| 73 | if (src) |
| 74 | strncpy(dest, src, destlen-1); |
| 75 | dest[src ? destlen-1 : 0] = 0; |
| 76 | } |
| 77 | |
| 78 | }; |