Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | 546b2ad | 2019-05-02 12:32:03 +0200 | [diff] [blame] | 2 | * Copyright 2011-2019 Pierre Ossman for Cendio AB |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 3 | * |
| 4 | * This is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This software is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this software; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 17 | * USA. |
| 18 | */ |
| 19 | |
| 20 | // |
| 21 | // util.h - miscellaneous useful bits |
| 22 | // |
| 23 | |
| 24 | #ifndef __RFB_UTIL_H__ |
| 25 | #define __RFB_UTIL_H__ |
| 26 | |
Adam Tkac | 8aee1a8 | 2009-09-04 12:08:56 +0000 | [diff] [blame] | 27 | #ifdef HAVE_CONFIG_H |
| 28 | #include <config.h> |
Adam Tkac | ad1cbd9 | 2008-10-06 14:08:00 +0000 | [diff] [blame] | 29 | #endif |
| 30 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 31 | #include <limits.h> |
| 32 | #include <string.h> |
| 33 | |
Pierre Ossman | 5bc20a6 | 2011-11-08 12:42:41 +0000 | [diff] [blame] | 34 | struct timeval; |
| 35 | |
Pierre Ossman | ba6fbfe | 2015-03-03 16:41:29 +0100 | [diff] [blame] | 36 | #ifdef __GNUC__ |
| 37 | # define __printf_attr(a, b) __attribute__((__format__ (__printf__, a, b))) |
| 38 | #else |
| 39 | # define __printf_attr(a, b) |
| 40 | #endif // __GNUC__ |
| 41 | |
Steve Kondik | 0c81bbb | 2017-07-10 08:56:00 -0700 | [diff] [blame] | 42 | #ifndef __unused_attr |
| 43 | # define __unused_attr __attribute((__unused__)) |
| 44 | #endif |
| 45 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 46 | namespace rfb { |
| 47 | |
| 48 | // -=- Class to handle cleanup of arrays of characters |
| 49 | class CharArray { |
| 50 | public: |
| 51 | CharArray() : buf(0) {} |
| 52 | CharArray(char* str) : buf(str) {} // note: assumes ownership |
| 53 | CharArray(int len) { |
Jan Grulich | a752d8f | 2018-10-01 10:17:20 +0200 | [diff] [blame] | 54 | buf = new char[len](); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 55 | } |
| 56 | ~CharArray() { |
| 57 | delete [] buf; |
| 58 | } |
Pierre Ossman | ba6fbfe | 2015-03-03 16:41:29 +0100 | [diff] [blame] | 59 | void format(const char *fmt, ...) __printf_attr(2, 3); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 60 | // Get the buffer pointer & clear it (i.e. caller takes ownership) |
| 61 | char* takeBuf() {char* tmp = buf; buf = 0; return tmp;} |
| 62 | void replaceBuf(char* b) {delete [] buf; buf = b;} |
| 63 | char* buf; |
| 64 | private: |
| 65 | CharArray(const CharArray&); |
| 66 | CharArray& operator=(const CharArray&); |
| 67 | }; |
| 68 | |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 69 | char* strDup(const char* s); |
| 70 | void strFree(char* s); |
| 71 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 72 | // Returns true if split successful. Returns false otherwise. |
| 73 | // ALWAYS *copies* first part of string to out1 buffer. |
| 74 | // If limiter not found, leaves out2 alone (null) and just copies to out1. |
| 75 | // If out1 or out2 non-zero, calls strFree and zeroes them. |
| 76 | // If fromEnd is true, splits at end of string rather than beginning. |
| 77 | // Either out1 or out2 may be null, in which case the split will not return |
| 78 | // that part of the string. Obviously, setting both to 0 is not useful... |
| 79 | bool strSplit(const char* src, const char limiter, char** out1, char** out2, bool fromEnd=false); |
| 80 | |
| 81 | // Returns true if src contains c |
| 82 | bool strContains(const char* src, char c); |
| 83 | |
| 84 | // Copies src to dest, up to specified length-1, and guarantees termination |
| 85 | void strCopy(char* dest, const char* src, int destlen); |
| 86 | |
Pierre Ossman | 546b2ad | 2019-05-02 12:32:03 +0200 | [diff] [blame] | 87 | // Makes sure line endings are in a certain format |
| 88 | |
| 89 | char* convertLF(const char* src, size_t bytes = (size_t)-1); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 90 | |
Pierre Ossman | 56fa782 | 2016-01-22 16:40:59 +0100 | [diff] [blame^] | 91 | // Convertions between various Unicode formats. The returned strings are |
| 92 | // always null terminated and must be freed using strFree(). |
| 93 | |
| 94 | size_t ucs4ToUTF8(unsigned src, char* dst); |
| 95 | size_t utf8ToUCS4(const char* src, size_t max, unsigned* dst); |
| 96 | |
| 97 | char* latin1ToUTF8(const char* src, size_t bytes = (size_t)-1); |
| 98 | char* utf8ToLatin1(const char* src, size_t bytes = (size_t)-1); |
| 99 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 100 | // HELPER functions for timeout handling |
| 101 | |
| 102 | // soonestTimeout() is a function to help work out the soonest of several |
| 103 | // timeouts. |
| 104 | inline void soonestTimeout(int* timeout, int newTimeout) { |
| 105 | if (newTimeout && (!*timeout || newTimeout < *timeout)) |
| 106 | *timeout = newTimeout; |
| 107 | } |
| 108 | |
| 109 | // secsToMillis() turns seconds into milliseconds, capping the value so it |
| 110 | // can't wrap round and become -ve |
| 111 | inline int secsToMillis(int secs) { |
| 112 | return (secs < 0 || secs > (INT_MAX/1000) ? INT_MAX : secs * 1000); |
| 113 | } |
Pierre Ossman | 5bc20a6 | 2011-11-08 12:42:41 +0000 | [diff] [blame] | 114 | |
Pierre Ossman | a99d14d | 2015-12-13 15:43:46 +0100 | [diff] [blame] | 115 | // Returns time elapsed between two moments in milliseconds. |
| 116 | unsigned msBetween(const struct timeval *first, |
| 117 | const struct timeval *second); |
| 118 | |
Pierre Ossman | 5bc20a6 | 2011-11-08 12:42:41 +0000 | [diff] [blame] | 119 | // Returns time elapsed since given moment in milliseconds. |
| 120 | unsigned msSince(const struct timeval *then); |
Pierre Ossman | 6462434 | 2015-03-03 16:30:13 +0100 | [diff] [blame] | 121 | |
Pierre Ossman | a99d14d | 2015-12-13 15:43:46 +0100 | [diff] [blame] | 122 | // Returns true if first happened before seconds |
| 123 | bool isBefore(const struct timeval *first, |
| 124 | const struct timeval *second); |
| 125 | |
Pierre Ossman | 6462434 | 2015-03-03 16:30:13 +0100 | [diff] [blame] | 126 | size_t siPrefix(long long value, const char *unit, |
Pierre Ossman | 921f6c8 | 2017-02-24 12:33:09 +0100 | [diff] [blame] | 127 | char *buffer, size_t maxlen, int precision=6); |
Pierre Ossman | 6462434 | 2015-03-03 16:30:13 +0100 | [diff] [blame] | 128 | size_t iecPrefix(long long value, const char *unit, |
Pierre Ossman | 921f6c8 | 2017-02-24 12:33:09 +0100 | [diff] [blame] | 129 | char *buffer, size_t maxlen, int precision=6); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | // Some platforms (e.g. Windows) include max() and min() macros in their |
| 133 | // standard headers, but they are also standard C++ template functions, so some |
| 134 | // C++ headers will undefine them. So we steer clear of the names min and max |
| 135 | // and define __rfbmin and __rfbmax instead. |
| 136 | |
| 137 | #ifndef __rfbmax |
| 138 | #define __rfbmax(a,b) (((a) > (b)) ? (a) : (b)) |
| 139 | #endif |
| 140 | #ifndef __rfbmin |
| 141 | #define __rfbmin(a,b) (((a) < (b)) ? (a) : (b)) |
| 142 | #endif |
| 143 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 144 | #endif |