blob: cfec2ef32eb50fbe254bfd67a4ec8aba2a38693b [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* 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/*
20 * The following applies to stcasecmp and strncasecmp implementations:
21 *
22 * Copyright (c) 1987 Regents of the University of California.
23 * All rights reserved.
24 *
25 * Redistribution and use in source and binary forms are permitted
26 * provided that this notice is preserved and that due credit is given
27 * to the University of California at Berkeley. The name of the University
28 * may not be used to endorse or promote products derived from this
29 * software without specific written prior permission. This software
30 * is provided ``as is'' without express or implied warranty.
31 */
32
Adam Tkac8aee1a82009-09-04 12:08:56 +000033#ifdef HAVE_CONFIG_H
34#include <config.h>
Adam Tkacad1cbd92008-10-06 14:08:00 +000035#endif
36
Pierre Ossmanba6fbfe2015-03-03 16:41:29 +010037#include <stdarg.h>
Pierre Ossman64624342015-03-03 16:30:13 +010038#include <stdio.h>
Pierre Ossman5bc20a62011-11-08 12:42:41 +000039#include <sys/time.h>
40
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000041#include <rfb/util.h>
42
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000043namespace rfb {
44
Pierre Ossmanba6fbfe2015-03-03 16:41:29 +010045 void CharArray::format(const char *fmt, ...) {
46 va_list ap;
Steve Kondikb3c9f7b2017-07-08 02:08:43 -070047 int len;
Pierre Ossmanba6fbfe2015-03-03 16:41:29 +010048
49 va_start(ap, fmt);
50 len = vsnprintf(NULL, 0, fmt, ap);
51 va_end(ap);
52
53 delete [] buf;
54
55 if (len < 0) {
56 buf = new char[1];
57 buf[0] = '\0';
58 return;
59 }
60
61 buf = new char[len+1];
62
63 va_start(ap, fmt);
64 vsnprintf(buf, len+1, fmt, ap);
65 va_end(ap);
66 }
67
Adam Tkacd36b6262009-09-04 10:57:20 +000068 char* strDup(const char* s) {
69 if (!s) return 0;
70 int l = strlen(s);
71 char* r = new char[l+1];
72 memcpy(r, s, l+1);
73 return r;
74 };
75
76 void strFree(char* s) {
77 delete [] s;
78 }
79
80
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000081 bool strSplit(const char* src, const char limiter, char** out1, char** out2, bool fromEnd) {
82 CharArray out1old, out2old;
83 if (out1) out1old.buf = *out1;
84 if (out2) out2old.buf = *out2;
85 int len = strlen(src);
86 int i=0, increment=1, limit=len;
87 if (fromEnd) {
88 i=len-1; increment = -1; limit = -1;
89 }
90 while (i!=limit) {
91 if (src[i] == limiter) {
92 if (out1) {
93 *out1 = new char[i+1];
94 if (i) memcpy(*out1, src, i);
95 (*out1)[i] = 0;
96 }
97 if (out2) {
98 *out2 = new char[len-i];
99 if (len-i-1) memcpy(*out2, &src[i+1], len-i-1);
100 (*out2)[len-i-1] = 0;
101 }
102 return true;
103 }
104 i+=increment;
105 }
Adam Tkacd36b6262009-09-04 10:57:20 +0000106 if (out1) *out1 = strDup(src);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000107 if (out2) *out2 = 0;
108 return false;
109 }
110
111 bool strContains(const char* src, char c) {
112 int l=strlen(src);
113 for (int i=0; i<l; i++)
114 if (src[i] == c) return true;
115 return false;
116 }
117
118 void strCopy(char* dest, const char* src, int destlen) {
119 if (src)
120 strncpy(dest, src, destlen-1);
121 dest[src ? destlen-1 : 0] = 0;
122 }
123
Pierre Ossman5bc20a62011-11-08 12:42:41 +0000124 unsigned msSince(const struct timeval *then)
125 {
126 struct timeval now;
127 unsigned diff;
128
129 gettimeofday(&now, NULL);
130
131 diff = (now.tv_sec - then->tv_sec) * 1000;
132
133 diff += now.tv_usec / 1000;
134 diff -= then->tv_usec / 1000;
135
136 return diff;
137 }
Pierre Ossman64624342015-03-03 16:30:13 +0100138
139 static size_t doPrefix(long long value, const char *unit,
140 char *buffer, size_t maxlen,
141 unsigned divisor, const char **prefixes,
Pierre Ossman921f6c82017-02-24 12:33:09 +0100142 size_t prefixCount, int precision) {
Pierre Ossman64624342015-03-03 16:30:13 +0100143 double newValue;
144 size_t prefix, len;
145
146 newValue = value;
147 prefix = 0;
148 while (newValue >= divisor) {
149 if (prefix >= prefixCount)
150 break;
151 newValue /= divisor;
152 prefix++;
153 }
154
Pierre Ossman921f6c82017-02-24 12:33:09 +0100155 len = snprintf(buffer, maxlen, "%.*g %s%s", precision, newValue,
Pierre Ossman64624342015-03-03 16:30:13 +0100156 (prefix == 0) ? "" : prefixes[prefix-1], unit);
157 buffer[maxlen-1] = '\0';
158
159 return len;
160 }
161
162 static const char *siPrefixes[] =
163 { "k", "M", "G", "T", "P", "E", "Z", "Y" };
164 static const char *iecPrefixes[] =
165 { "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" };
166
167 size_t siPrefix(long long value, const char *unit,
Pierre Ossman921f6c82017-02-24 12:33:09 +0100168 char *buffer, size_t maxlen, int precision) {
Pierre Ossman64624342015-03-03 16:30:13 +0100169 return doPrefix(value, unit, buffer, maxlen, 1000, siPrefixes,
Pierre Ossman921f6c82017-02-24 12:33:09 +0100170 sizeof(siPrefixes)/sizeof(*siPrefixes),
171 precision);
Pierre Ossman64624342015-03-03 16:30:13 +0100172 }
173
174 size_t iecPrefix(long long value, const char *unit,
Pierre Ossman921f6c82017-02-24 12:33:09 +0100175 char *buffer, size_t maxlen, int precision) {
Pierre Ossman64624342015-03-03 16:30:13 +0100176 return doPrefix(value, unit, buffer, maxlen, 1024, iecPrefixes,
Pierre Ossman921f6c82017-02-24 12:33:09 +0100177 sizeof(iecPrefixes)/sizeof(*iecPrefixes),
178 precision);
Pierre Ossman64624342015-03-03 16:30:13 +0100179 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000180};