You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.h 3.6KB

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