Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame^] | 1 | /* Copyright (C) 2002-2004 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 <string.h> |
| 27 | |
| 28 | namespace rfb { |
| 29 | |
| 30 | // -=- Class to handle cleanup of arrays of characters |
| 31 | class CharArray { |
| 32 | public: |
| 33 | CharArray() : buf(0) {} |
| 34 | CharArray(char* str) : buf(str) {} // note: assumes ownership |
| 35 | CharArray(int len) { |
| 36 | buf = new char[len]; |
| 37 | } |
| 38 | ~CharArray() { |
| 39 | delete [] buf; |
| 40 | } |
| 41 | // Get the buffer pointer & clear it (i.e. caller takes ownership) |
| 42 | char* takeBuf() {char* tmp = buf; buf = 0; return tmp;} |
| 43 | void replaceBuf(char* b) {delete [] buf; buf = b;} |
| 44 | char* buf; |
| 45 | private: |
| 46 | CharArray(const CharArray&); |
| 47 | CharArray& operator=(const CharArray&); |
| 48 | }; |
| 49 | |
| 50 | char* strDup(const char* s); |
| 51 | void strFree(char* s); |
| 52 | |
| 53 | // Returns true if split successful. Returns false otherwise. |
| 54 | // ALWAYS *copies* first part of string to out1 buffer. |
| 55 | // If limiter not found, leaves out2 alone (null) and just copies to out1. |
| 56 | // If out1 or out2 non-zero, calls strFree and zeroes them. |
| 57 | // If fromEnd is true, splits at end of string rather than beginning. |
| 58 | // Either out1 or out2 may be null, in which case the split will not return |
| 59 | // that part of the string. Obviously, setting both to 0 is not useful... |
| 60 | bool strSplit(const char* src, const char limiter, char** out1, char** out2, bool fromEnd=false); |
| 61 | |
| 62 | // Returns true if src contains c |
| 63 | bool strContains(const char* src, char c); |
| 64 | |
| 65 | // Copies src to dest, up to specified length-1, and guarantees termination |
| 66 | void strCopy(char* dest, const char* src, int destlen); |
| 67 | } |
| 68 | #endif |
| 69 | |
| 70 | // Some platforms (e.g. Windows) include max() and min() macros in their |
| 71 | // standard headers, so we define them only when not already defined. Note |
| 72 | // also that max() & min() are standard C++ template functions, so some C++ |
| 73 | // headers will undefine them. We place our definitions outside the #ifndef |
| 74 | // __RFB_UTIL_H__, so that you can always guarantee they will be defined if |
| 75 | // this file is the last #include before you use them. |
| 76 | |
| 77 | #ifndef max |
| 78 | #define max(a,b) (((a) > (b)) ? (a) : (b)) |
| 79 | #endif |
| 80 | |
| 81 | #ifndef min |
| 82 | #define min(a,b) (((a) < (b)) ? (a) : (b)) |
| 83 | #endif |
| 84 | |
| 85 | |
| 86 | // -=- PLATFORM SPECIFIC UTILITY FUNCTIONS/IMPLEMENTATIONS |
| 87 | #ifdef WIN32 |
| 88 | #include "win32/util_win32.h" |
| 89 | #endif |
| 90 | |