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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. * The following applies to stcasecmp and strncasecmp implementations:
  20. *
  21. * Copyright (c) 1987 Regents of the University of California.
  22. * All rights reserved.
  23. *
  24. * Redistribution and use in source and binary forms are permitted
  25. * provided that this notice is preserved and that due credit is given
  26. * to the University of California at Berkeley. The name of the University
  27. * may not be used to endorse or promote products derived from this
  28. * software without specific written prior permission. This software
  29. * is provided ``as is'' without express or implied warranty.
  30. */
  31. #ifdef HAVE_CONFIG_H
  32. #include <config.h>
  33. #endif
  34. #include <stdarg.h>
  35. #include <stdio.h>
  36. #include <sys/time.h>
  37. #include <rfb/util.h>
  38. namespace rfb {
  39. void CharArray::format(const char *fmt, ...) {
  40. va_list ap;
  41. size_t len;
  42. va_start(ap, fmt);
  43. len = vsnprintf(NULL, 0, fmt, ap);
  44. va_end(ap);
  45. delete [] buf;
  46. if (len < 0) {
  47. buf = new char[1];
  48. buf[0] = '\0';
  49. return;
  50. }
  51. buf = new char[len+1];
  52. va_start(ap, fmt);
  53. vsnprintf(buf, len+1, fmt, ap);
  54. va_end(ap);
  55. }
  56. char* strDup(const char* s) {
  57. if (!s) return 0;
  58. int l = strlen(s);
  59. char* r = new char[l+1];
  60. memcpy(r, s, l+1);
  61. return r;
  62. };
  63. void strFree(char* s) {
  64. delete [] s;
  65. }
  66. bool strSplit(const char* src, const char limiter, char** out1, char** out2, bool fromEnd) {
  67. CharArray out1old, out2old;
  68. if (out1) out1old.buf = *out1;
  69. if (out2) out2old.buf = *out2;
  70. int len = strlen(src);
  71. int i=0, increment=1, limit=len;
  72. if (fromEnd) {
  73. i=len-1; increment = -1; limit = -1;
  74. }
  75. while (i!=limit) {
  76. if (src[i] == limiter) {
  77. if (out1) {
  78. *out1 = new char[i+1];
  79. if (i) memcpy(*out1, src, i);
  80. (*out1)[i] = 0;
  81. }
  82. if (out2) {
  83. *out2 = new char[len-i];
  84. if (len-i-1) memcpy(*out2, &src[i+1], len-i-1);
  85. (*out2)[len-i-1] = 0;
  86. }
  87. return true;
  88. }
  89. i+=increment;
  90. }
  91. if (out1) *out1 = strDup(src);
  92. if (out2) *out2 = 0;
  93. return false;
  94. }
  95. bool strContains(const char* src, char c) {
  96. int l=strlen(src);
  97. for (int i=0; i<l; i++)
  98. if (src[i] == c) return true;
  99. return false;
  100. }
  101. void strCopy(char* dest, const char* src, int destlen) {
  102. if (src)
  103. strncpy(dest, src, destlen-1);
  104. dest[src ? destlen-1 : 0] = 0;
  105. }
  106. unsigned msSince(const struct timeval *then)
  107. {
  108. struct timeval now;
  109. unsigned diff;
  110. gettimeofday(&now, NULL);
  111. diff = (now.tv_sec - then->tv_sec) * 1000;
  112. diff += now.tv_usec / 1000;
  113. diff -= then->tv_usec / 1000;
  114. return diff;
  115. }
  116. static size_t doPrefix(long long value, const char *unit,
  117. char *buffer, size_t maxlen,
  118. unsigned divisor, const char **prefixes,
  119. size_t prefixCount) {
  120. double newValue;
  121. size_t prefix, len;
  122. newValue = value;
  123. prefix = 0;
  124. while (newValue >= divisor) {
  125. if (prefix >= prefixCount)
  126. break;
  127. newValue /= divisor;
  128. prefix++;
  129. }
  130. len = snprintf(buffer, maxlen, "%g %s%s", newValue,
  131. (prefix == 0) ? "" : prefixes[prefix-1], unit);
  132. buffer[maxlen-1] = '\0';
  133. return len;
  134. }
  135. static const char *siPrefixes[] =
  136. { "k", "M", "G", "T", "P", "E", "Z", "Y" };
  137. static const char *iecPrefixes[] =
  138. { "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" };
  139. size_t siPrefix(long long value, const char *unit,
  140. char *buffer, size_t maxlen) {
  141. return doPrefix(value, unit, buffer, maxlen, 1000, siPrefixes,
  142. sizeof(siPrefixes)/sizeof(*siPrefixes));
  143. }
  144. size_t iecPrefix(long long value, const char *unit,
  145. char *buffer, size_t maxlen) {
  146. return doPrefix(value, unit, buffer, maxlen, 1024, iecPrefixes,
  147. sizeof(iecPrefixes)/sizeof(*iecPrefixes));
  148. }
  149. };