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.cxx 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <stdarg.h>
  22. #include <stdio.h>
  23. #include <sys/time.h>
  24. #include <rfb/util.h>
  25. namespace rfb {
  26. void CharArray::format(const char *fmt, ...) {
  27. va_list ap;
  28. int len;
  29. va_start(ap, fmt);
  30. len = vsnprintf(NULL, 0, fmt, ap);
  31. va_end(ap);
  32. delete [] buf;
  33. if (len < 0) {
  34. buf = new char[1];
  35. buf[0] = '\0';
  36. return;
  37. }
  38. buf = new char[len+1];
  39. va_start(ap, fmt);
  40. vsnprintf(buf, len+1, fmt, ap);
  41. va_end(ap);
  42. }
  43. char* strDup(const char* s) {
  44. if (!s) return 0;
  45. int l = strlen(s);
  46. char* r = new char[l+1];
  47. memcpy(r, s, l+1);
  48. return r;
  49. };
  50. void strFree(char* s) {
  51. delete [] s;
  52. }
  53. bool strSplit(const char* src, const char limiter, char** out1, char** out2, bool fromEnd) {
  54. CharArray out1old, out2old;
  55. if (out1) out1old.buf = *out1;
  56. if (out2) out2old.buf = *out2;
  57. int len = strlen(src);
  58. int i=0, increment=1, limit=len;
  59. if (fromEnd) {
  60. i=len-1; increment = -1; limit = -1;
  61. }
  62. while (i!=limit) {
  63. if (src[i] == limiter) {
  64. if (out1) {
  65. *out1 = new char[i+1];
  66. if (i) memcpy(*out1, src, i);
  67. (*out1)[i] = 0;
  68. }
  69. if (out2) {
  70. *out2 = new char[len-i];
  71. if (len-i-1) memcpy(*out2, &src[i+1], len-i-1);
  72. (*out2)[len-i-1] = 0;
  73. }
  74. return true;
  75. }
  76. i+=increment;
  77. }
  78. if (out1) *out1 = strDup(src);
  79. if (out2) *out2 = 0;
  80. return false;
  81. }
  82. bool strContains(const char* src, char c) {
  83. int l=strlen(src);
  84. for (int i=0; i<l; i++)
  85. if (src[i] == c) return true;
  86. return false;
  87. }
  88. void strCopy(char* dest, const char* src, int destlen) {
  89. if (src)
  90. strncpy(dest, src, destlen-1);
  91. dest[src ? destlen-1 : 0] = 0;
  92. }
  93. unsigned msBetween(const struct timeval *first,
  94. const struct timeval *second)
  95. {
  96. unsigned diff;
  97. diff = (second->tv_sec - first->tv_sec) * 1000;
  98. diff += second->tv_usec / 1000;
  99. diff -= first->tv_usec / 1000;
  100. return diff;
  101. }
  102. unsigned msSince(const struct timeval *then)
  103. {
  104. struct timeval now;
  105. gettimeofday(&now, NULL);
  106. return msBetween(then, &now);
  107. }
  108. bool isBefore(const struct timeval *first,
  109. const struct timeval *second)
  110. {
  111. if (first->tv_sec < second->tv_sec)
  112. return true;
  113. if (first->tv_sec > second->tv_sec)
  114. return false;
  115. if (first->tv_usec < second->tv_usec)
  116. return true;
  117. return false;
  118. }
  119. static size_t doPrefix(long long value, const char *unit,
  120. char *buffer, size_t maxlen,
  121. unsigned divisor, const char **prefixes,
  122. size_t prefixCount, int precision) {
  123. double newValue;
  124. size_t prefix, len;
  125. newValue = value;
  126. prefix = 0;
  127. while (newValue >= divisor) {
  128. if (prefix >= prefixCount)
  129. break;
  130. newValue /= divisor;
  131. prefix++;
  132. }
  133. len = snprintf(buffer, maxlen, "%.*g %s%s", precision, newValue,
  134. (prefix == 0) ? "" : prefixes[prefix-1], unit);
  135. buffer[maxlen-1] = '\0';
  136. return len;
  137. }
  138. static const char *siPrefixes[] =
  139. { "k", "M", "G", "T", "P", "E", "Z", "Y" };
  140. static const char *iecPrefixes[] =
  141. { "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" };
  142. size_t siPrefix(long long value, const char *unit,
  143. char *buffer, size_t maxlen, int precision) {
  144. return doPrefix(value, unit, buffer, maxlen, 1000, siPrefixes,
  145. sizeof(siPrefixes)/sizeof(*siPrefixes),
  146. precision);
  147. }
  148. size_t iecPrefix(long long value, const char *unit,
  149. char *buffer, size_t maxlen, int precision) {
  150. return doPrefix(value, unit, buffer, maxlen, 1024, iecPrefixes,
  151. sizeof(iecPrefixes)/sizeof(*iecPrefixes),
  152. precision);
  153. }
  154. };