blob: 9ad177207f79160cb6ba4d290cfd4989b691fd92 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* 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 Tkac8aee1a82009-09-04 12:08:56 +000026#ifdef HAVE_CONFIG_H
27#include <config.h>
Adam Tkacad1cbd92008-10-06 14:08:00 +000028#endif
29
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000030#include <limits.h>
31#include <string.h>
32
Pierre Ossman5bc20a62011-11-08 12:42:41 +000033struct timeval;
34
Pierre Ossmanba6fbfe2015-03-03 16:41:29 +010035#ifdef __GNUC__
36# define __printf_attr(a, b) __attribute__((__format__ (__printf__, a, b)))
37#else
38# define __printf_attr(a, b)
39#endif // __GNUC__
40
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000041namespace rfb {
42
43 // -=- Class to handle cleanup of arrays of characters
44 class CharArray {
45 public:
46 CharArray() : buf(0) {}
47 CharArray(char* str) : buf(str) {} // note: assumes ownership
48 CharArray(int len) {
49 buf = new char[len];
50 }
51 ~CharArray() {
52 delete [] buf;
53 }
Pierre Ossmanba6fbfe2015-03-03 16:41:29 +010054 void format(const char *fmt, ...) __printf_attr(2, 3);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000055 // Get the buffer pointer & clear it (i.e. caller takes ownership)
56 char* takeBuf() {char* tmp = buf; buf = 0; return tmp;}
57 void replaceBuf(char* b) {delete [] buf; buf = b;}
58 char* buf;
59 private:
60 CharArray(const CharArray&);
61 CharArray& operator=(const CharArray&);
62 };
63
Adam Tkacd36b6262009-09-04 10:57:20 +000064 char* strDup(const char* s);
65 void strFree(char* s);
66
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000067 // Returns true if split successful. Returns false otherwise.
68 // ALWAYS *copies* first part of string to out1 buffer.
69 // If limiter not found, leaves out2 alone (null) and just copies to out1.
70 // If out1 or out2 non-zero, calls strFree and zeroes them.
71 // If fromEnd is true, splits at end of string rather than beginning.
72 // Either out1 or out2 may be null, in which case the split will not return
73 // that part of the string. Obviously, setting both to 0 is not useful...
74 bool strSplit(const char* src, const char limiter, char** out1, char** out2, bool fromEnd=false);
75
76 // Returns true if src contains c
77 bool strContains(const char* src, char c);
78
79 // Copies src to dest, up to specified length-1, and guarantees termination
80 void strCopy(char* dest, const char* src, int destlen);
81
82
83 // HELPER functions for timeout handling
84
85 // soonestTimeout() is a function to help work out the soonest of several
86 // timeouts.
87 inline void soonestTimeout(int* timeout, int newTimeout) {
88 if (newTimeout && (!*timeout || newTimeout < *timeout))
89 *timeout = newTimeout;
90 }
91
92 // secsToMillis() turns seconds into milliseconds, capping the value so it
93 // can't wrap round and become -ve
94 inline int secsToMillis(int secs) {
95 return (secs < 0 || secs > (INT_MAX/1000) ? INT_MAX : secs * 1000);
96 }
Pierre Ossman5bc20a62011-11-08 12:42:41 +000097
98 // Returns time elapsed since given moment in milliseconds.
99 unsigned msSince(const struct timeval *then);
Pierre Ossman64624342015-03-03 16:30:13 +0100100
101 size_t siPrefix(long long value, const char *unit,
102 char *buffer, size_t maxlen);
103 size_t iecPrefix(long long value, const char *unit,
104 char *buffer, size_t maxlen);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000105}
106
107// Some platforms (e.g. Windows) include max() and min() macros in their
108// standard headers, but they are also standard C++ template functions, so some
109// C++ headers will undefine them. So we steer clear of the names min and max
110// and define __rfbmin and __rfbmax instead.
111
112#ifndef __rfbmax
113#define __rfbmax(a,b) (((a) > (b)) ? (a) : (b))
114#endif
115#ifndef __rfbmin
116#define __rfbmin(a,b) (((a) < (b)) ? (a) : (b))
117#endif
118
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000119#endif