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 | |
Adam Tkac | 8aee1a8 | 2009-09-04 12:08:56 +0000 | [diff] [blame] | 33 | #ifdef HAVE_CONFIG_H |
| 34 | #include <config.h> |
Adam Tkac | ad1cbd9 | 2008-10-06 14:08:00 +0000 | [diff] [blame] | 35 | #endif |
| 36 | |
Pierre Ossman | 5bc20a6 | 2011-11-08 12:42:41 +0000 | [diff] [blame] | 37 | #include <sys/time.h> |
| 38 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 39 | #include <rfb/util.h> |
| 40 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 41 | namespace rfb { |
| 42 | |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 43 | char* strDup(const char* s) { |
| 44 | if (!s) return 0; |
| 45 | int l = strlen(s); |
| 46 | char* r = new char[l+1]; |
| 47 | memcpy(r, s, l+1); |
| 48 | return r; |
| 49 | }; |
| 50 | |
| 51 | void strFree(char* s) { |
| 52 | delete [] s; |
| 53 | } |
| 54 | |
| 55 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 56 | bool strSplit(const char* src, const char limiter, char** out1, char** out2, bool fromEnd) { |
| 57 | CharArray out1old, out2old; |
| 58 | if (out1) out1old.buf = *out1; |
| 59 | if (out2) out2old.buf = *out2; |
| 60 | int len = strlen(src); |
| 61 | int i=0, increment=1, limit=len; |
| 62 | if (fromEnd) { |
| 63 | i=len-1; increment = -1; limit = -1; |
| 64 | } |
| 65 | while (i!=limit) { |
| 66 | if (src[i] == limiter) { |
| 67 | if (out1) { |
| 68 | *out1 = new char[i+1]; |
| 69 | if (i) memcpy(*out1, src, i); |
| 70 | (*out1)[i] = 0; |
| 71 | } |
| 72 | if (out2) { |
| 73 | *out2 = new char[len-i]; |
| 74 | if (len-i-1) memcpy(*out2, &src[i+1], len-i-1); |
| 75 | (*out2)[len-i-1] = 0; |
| 76 | } |
| 77 | return true; |
| 78 | } |
| 79 | i+=increment; |
| 80 | } |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 81 | if (out1) *out1 = strDup(src); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 82 | if (out2) *out2 = 0; |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | bool strContains(const char* src, char c) { |
| 87 | int l=strlen(src); |
| 88 | for (int i=0; i<l; i++) |
| 89 | if (src[i] == c) return true; |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | void strCopy(char* dest, const char* src, int destlen) { |
| 94 | if (src) |
| 95 | strncpy(dest, src, destlen-1); |
| 96 | dest[src ? destlen-1 : 0] = 0; |
| 97 | } |
| 98 | |
Pierre Ossman | 5bc20a6 | 2011-11-08 12:42:41 +0000 | [diff] [blame] | 99 | unsigned msSince(const struct timeval *then) |
| 100 | { |
| 101 | struct timeval now; |
| 102 | unsigned diff; |
| 103 | |
| 104 | gettimeofday(&now, NULL); |
| 105 | |
| 106 | diff = (now.tv_sec - then->tv_sec) * 1000; |
| 107 | |
| 108 | diff += now.tv_usec / 1000; |
| 109 | diff -= then->tv_usec / 1000; |
| 110 | |
| 111 | return diff; |
| 112 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 113 | }; |