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 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2011-2019 Pierre Ossman for Cendio AB
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. //
  20. // util.h - miscellaneous useful bits
  21. //
  22. #ifndef __RFB_UTIL_H__
  23. #define __RFB_UTIL_H__
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #include <limits.h>
  28. #include <string.h>
  29. struct timeval;
  30. #ifdef __GNUC__
  31. # define __printf_attr(a, b) __attribute__((__format__ (__printf__, a, b)))
  32. #else
  33. # define __printf_attr(a, b)
  34. #endif // __GNUC__
  35. #ifndef __unused_attr
  36. # define __unused_attr __attribute((__unused__))
  37. #endif
  38. namespace rfb {
  39. // -=- Class to handle cleanup of arrays of characters
  40. class CharArray {
  41. public:
  42. CharArray() : buf(0) {}
  43. CharArray(char* str) : buf(str) {} // note: assumes ownership
  44. CharArray(int len) {
  45. buf = new char[len]();
  46. }
  47. ~CharArray() {
  48. delete [] buf;
  49. }
  50. void format(const char *fmt, ...) __printf_attr(2, 3);
  51. // Get the buffer pointer & clear it (i.e. caller takes ownership)
  52. char* takeBuf() {char* tmp = buf; buf = 0; return tmp;}
  53. void replaceBuf(char* b) {delete [] buf; buf = b;}
  54. char* buf;
  55. private:
  56. CharArray(const CharArray&);
  57. CharArray& operator=(const CharArray&);
  58. };
  59. char* strDup(const char* s);
  60. void strFree(char* s);
  61. void strFree(wchar_t* s);
  62. // Returns true if split successful. Returns false otherwise.
  63. // ALWAYS *copies* first part of string to out1 buffer.
  64. // If limiter not found, leaves out2 alone (null) and just copies to out1.
  65. // If out1 or out2 non-zero, calls strFree and zeroes them.
  66. // If fromEnd is true, splits at end of string rather than beginning.
  67. // Either out1 or out2 may be null, in which case the split will not return
  68. // that part of the string. Obviously, setting both to 0 is not useful...
  69. bool strSplit(const char* src, const char limiter, char** out1, char** out2, bool fromEnd=false);
  70. // Returns true if src contains c
  71. bool strContains(const char* src, char c);
  72. // Copies src to dest, up to specified length-1, and guarantees termination
  73. void strCopy(char* dest, const char* src, int destlen);
  74. // Makes sure line endings are in a certain format
  75. char* convertLF(const char* src, size_t bytes = (size_t)-1);
  76. char* convertCRLF(const char* src, size_t bytes = (size_t)-1);
  77. // Convertions between various Unicode formats. The returned strings are
  78. // always null terminated and must be freed using strFree().
  79. size_t ucs4ToUTF8(unsigned src, char* dst);
  80. size_t utf8ToUCS4(const char* src, size_t max, unsigned* dst);
  81. size_t ucs4ToUTF16(unsigned src, wchar_t* dst);
  82. size_t utf16ToUCS4(const wchar_t* src, size_t max, unsigned* dst);
  83. char* latin1ToUTF8(const char* src, size_t bytes = (size_t)-1);
  84. char* utf8ToLatin1(const char* src, size_t bytes = (size_t)-1);
  85. char* utf16ToUTF8(const wchar_t* src, size_t units = (size_t)-1);
  86. wchar_t* utf8ToUTF16(const char* src, size_t bytes = (size_t)-1);
  87. // HELPER functions for timeout handling
  88. // soonestTimeout() is a function to help work out the soonest of several
  89. // timeouts.
  90. inline void soonestTimeout(int* timeout, int newTimeout) {
  91. if (newTimeout && (!*timeout || newTimeout < *timeout))
  92. *timeout = newTimeout;
  93. }
  94. // secsToMillis() turns seconds into milliseconds, capping the value so it
  95. // can't wrap round and become -ve
  96. inline int secsToMillis(int secs) {
  97. return (secs < 0 || secs > (INT_MAX/1000) ? INT_MAX : secs * 1000);
  98. }
  99. // Returns time elapsed between two moments in milliseconds.
  100. unsigned msBetween(const struct timeval *first,
  101. const struct timeval *second);
  102. // Returns time elapsed since given moment in milliseconds.
  103. unsigned msSince(const struct timeval *then);
  104. // Returns true if first happened before seconds
  105. bool isBefore(const struct timeval *first,
  106. const struct timeval *second);
  107. size_t siPrefix(long long value, const char *unit,
  108. char *buffer, size_t maxlen, int precision=6);
  109. size_t iecPrefix(long long value, const char *unit,
  110. char *buffer, size_t maxlen, int precision=6);
  111. }
  112. // Some platforms (e.g. Windows) include max() and min() macros in their
  113. // standard headers, but they are also standard C++ template functions, so some
  114. // C++ headers will undefine them. So we steer clear of the names min and max
  115. // and define __rfbmin and __rfbmax instead.
  116. #ifndef __rfbmax
  117. #define __rfbmax(a,b) (((a) > (b)) ? (a) : (b))
  118. #endif
  119. #ifndef __rfbmin
  120. #define __rfbmin(a,b) (((a) < (b)) ? (a) : (b))
  121. #endif
  122. #endif