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 | // util.h - miscellaneous useful bits |
| 21 | // |
| 22 | |
| 23 | #ifndef __RFB_UTIL_H__ |
| 24 | #define __RFB_UTIL_H__ |
| 25 | |
Adam Tkac | 8aee1a8 | 2009-09-04 12:08:56 +0000 | [diff] [blame] | 26 | #ifdef HAVE_CONFIG_H |
| 27 | #include <config.h> |
Adam Tkac | ad1cbd9 | 2008-10-06 14:08:00 +0000 | [diff] [blame] | 28 | #endif |
| 29 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 30 | #include <limits.h> |
| 31 | #include <string.h> |
| 32 | |
Pierre Ossman | 5bc20a6 | 2011-11-08 12:42:41 +0000 | [diff] [blame] | 33 | struct timeval; |
| 34 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 35 | namespace rfb { |
| 36 | |
| 37 | // -=- Class to handle cleanup of arrays of characters |
| 38 | class CharArray { |
| 39 | public: |
| 40 | CharArray() : buf(0) {} |
| 41 | CharArray(char* str) : buf(str) {} // note: assumes ownership |
| 42 | CharArray(int len) { |
| 43 | buf = new char[len]; |
| 44 | } |
| 45 | ~CharArray() { |
| 46 | delete [] buf; |
| 47 | } |
| 48 | // Get the buffer pointer & clear it (i.e. caller takes ownership) |
| 49 | char* takeBuf() {char* tmp = buf; buf = 0; return tmp;} |
| 50 | void replaceBuf(char* b) {delete [] buf; buf = b;} |
| 51 | char* buf; |
| 52 | private: |
| 53 | CharArray(const CharArray&); |
| 54 | CharArray& operator=(const CharArray&); |
| 55 | }; |
| 56 | |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 57 | char* strDup(const char* s); |
| 58 | void strFree(char* s); |
| 59 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 60 | // Returns true if split successful. Returns false otherwise. |
| 61 | // ALWAYS *copies* first part of string to out1 buffer. |
| 62 | // If limiter not found, leaves out2 alone (null) and just copies to out1. |
| 63 | // If out1 or out2 non-zero, calls strFree and zeroes them. |
| 64 | // If fromEnd is true, splits at end of string rather than beginning. |
| 65 | // Either out1 or out2 may be null, in which case the split will not return |
| 66 | // that part of the string. Obviously, setting both to 0 is not useful... |
| 67 | bool strSplit(const char* src, const char limiter, char** out1, char** out2, bool fromEnd=false); |
| 68 | |
| 69 | // Returns true if src contains c |
| 70 | bool strContains(const char* src, char c); |
| 71 | |
| 72 | // Copies src to dest, up to specified length-1, and guarantees termination |
| 73 | void strCopy(char* dest, const char* src, int destlen); |
| 74 | |
| 75 | |
| 76 | // HELPER functions for timeout handling |
| 77 | |
| 78 | // soonestTimeout() is a function to help work out the soonest of several |
| 79 | // timeouts. |
| 80 | inline void soonestTimeout(int* timeout, int newTimeout) { |
| 81 | if (newTimeout && (!*timeout || newTimeout < *timeout)) |
| 82 | *timeout = newTimeout; |
| 83 | } |
| 84 | |
| 85 | // secsToMillis() turns seconds into milliseconds, capping the value so it |
| 86 | // can't wrap round and become -ve |
| 87 | inline int secsToMillis(int secs) { |
| 88 | return (secs < 0 || secs > (INT_MAX/1000) ? INT_MAX : secs * 1000); |
| 89 | } |
Pierre Ossman | 5bc20a6 | 2011-11-08 12:42:41 +0000 | [diff] [blame] | 90 | |
| 91 | // Returns time elapsed since given moment in milliseconds. |
| 92 | unsigned msSince(const struct timeval *then); |
Pierre Ossman | 6462434 | 2015-03-03 16:30:13 +0100 | [diff] [blame] | 93 | |
| 94 | size_t siPrefix(long long value, const char *unit, |
| 95 | char *buffer, size_t maxlen); |
| 96 | size_t iecPrefix(long long value, const char *unit, |
| 97 | char *buffer, size_t maxlen); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // Some platforms (e.g. Windows) include max() and min() macros in their |
| 101 | // standard headers, but they are also standard C++ template functions, so some |
| 102 | // C++ headers will undefine them. So we steer clear of the names min and max |
| 103 | // and define __rfbmin and __rfbmax instead. |
| 104 | |
| 105 | #ifndef __rfbmax |
| 106 | #define __rfbmax(a,b) (((a) > (b)) ? (a) : (b)) |
| 107 | #endif |
| 108 | #ifndef __rfbmin |
| 109 | #define __rfbmin(a,b) (((a) < (b)) ? (a) : (b)) |
| 110 | #endif |
| 111 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 112 | #endif |