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