blob: 4124769a2f31aaaaa913883b7f2a9383a2dff4de [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
33namespace rfb {
34
35 // -=- Class to handle cleanup of arrays of characters
36 class CharArray {
37 public:
38 CharArray() : buf(0) {}
39 CharArray(char* str) : buf(str) {} // note: assumes ownership
40 CharArray(int len) {
41 buf = new char[len];
42 }
43 ~CharArray() {
44 delete [] buf;
45 }
46 // Get the buffer pointer & clear it (i.e. caller takes ownership)
47 char* takeBuf() {char* tmp = buf; buf = 0; return tmp;}
48 void replaceBuf(char* b) {delete [] buf; buf = b;}
49 char* buf;
50 private:
51 CharArray(const CharArray&);
52 CharArray& operator=(const CharArray&);
53 };
54
Adam Tkacd36b6262009-09-04 10:57:20 +000055 char* strDup(const char* s);
56 void strFree(char* s);
57
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000058 // Returns true if split successful. Returns false otherwise.
59 // ALWAYS *copies* first part of string to out1 buffer.
60 // If limiter not found, leaves out2 alone (null) and just copies to out1.
61 // If out1 or out2 non-zero, calls strFree and zeroes them.
62 // If fromEnd is true, splits at end of string rather than beginning.
63 // Either out1 or out2 may be null, in which case the split will not return
64 // that part of the string. Obviously, setting both to 0 is not useful...
65 bool strSplit(const char* src, const char limiter, char** out1, char** out2, bool fromEnd=false);
66
67 // Returns true if src contains c
68 bool strContains(const char* src, char c);
69
70 // Copies src to dest, up to specified length-1, and guarantees termination
71 void strCopy(char* dest, const char* src, int destlen);
72
73
74 // HELPER functions for timeout handling
75
76 // soonestTimeout() is a function to help work out the soonest of several
77 // timeouts.
78 inline void soonestTimeout(int* timeout, int newTimeout) {
79 if (newTimeout && (!*timeout || newTimeout < *timeout))
80 *timeout = newTimeout;
81 }
82
83 // secsToMillis() turns seconds into milliseconds, capping the value so it
84 // can't wrap round and become -ve
85 inline int secsToMillis(int secs) {
86 return (secs < 0 || secs > (INT_MAX/1000) ? INT_MAX : secs * 1000);
87 }
88}
89
90// Some platforms (e.g. Windows) include max() and min() macros in their
91// standard headers, but they are also standard C++ template functions, so some
92// C++ headers will undefine them. So we steer clear of the names min and max
93// and define __rfbmin and __rfbmax instead.
94
95#ifndef __rfbmax
96#define __rfbmax(a,b) (((a) > (b)) ? (a) : (b))
97#endif
98#ifndef __rfbmin
99#define __rfbmin(a,b) (((a) < (b)) ? (a) : (b))
100#endif
101
102// Declare strcasecmp() and/or strncasecmp() if absent on this system.
103
104#if !defined(WIN32) && !defined(HAVE_STRCASECMP)
105extern "C" {
106 int strcasecmp(const char *s1, const char *s2);
107}
108#endif
109#if !defined(WIN32) && !defined(HAVE_STRNCASECMP)
110extern "C" {
111 int strncasecmp(const char *s1, const char *s2, size_t n);
112}
113#endif
114
115#endif