blob: 2b3c9f40f4efbfb05e6ae470a5aeaa35b1765d7e [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 Ossman64624342015-03-03 16:30:13 +010037#include <stdio.h>
Pierre Ossman5bc20a62011-11-08 12:42:41 +000038#include <sys/time.h>
39
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000040#include <rfb/util.h>
41
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000042namespace rfb {
43
Adam Tkacd36b6262009-09-04 10:57:20 +000044 char* strDup(const char* s) {
45 if (!s) return 0;
46 int l = strlen(s);
47 char* r = new char[l+1];
48 memcpy(r, s, l+1);
49 return r;
50 };
51
52 void strFree(char* s) {
53 delete [] s;
54 }
55
56
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000057 bool strSplit(const char* src, const char limiter, char** out1, char** out2, bool fromEnd) {
58 CharArray out1old, out2old;
59 if (out1) out1old.buf = *out1;
60 if (out2) out2old.buf = *out2;
61 int len = strlen(src);
62 int i=0, increment=1, limit=len;
63 if (fromEnd) {
64 i=len-1; increment = -1; limit = -1;
65 }
66 while (i!=limit) {
67 if (src[i] == limiter) {
68 if (out1) {
69 *out1 = new char[i+1];
70 if (i) memcpy(*out1, src, i);
71 (*out1)[i] = 0;
72 }
73 if (out2) {
74 *out2 = new char[len-i];
75 if (len-i-1) memcpy(*out2, &src[i+1], len-i-1);
76 (*out2)[len-i-1] = 0;
77 }
78 return true;
79 }
80 i+=increment;
81 }
Adam Tkacd36b6262009-09-04 10:57:20 +000082 if (out1) *out1 = strDup(src);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000083 if (out2) *out2 = 0;
84 return false;
85 }
86
87 bool strContains(const char* src, char c) {
88 int l=strlen(src);
89 for (int i=0; i<l; i++)
90 if (src[i] == c) return true;
91 return false;
92 }
93
94 void strCopy(char* dest, const char* src, int destlen) {
95 if (src)
96 strncpy(dest, src, destlen-1);
97 dest[src ? destlen-1 : 0] = 0;
98 }
99
Pierre Ossman5bc20a62011-11-08 12:42:41 +0000100 unsigned msSince(const struct timeval *then)
101 {
102 struct timeval now;
103 unsigned diff;
104
105 gettimeofday(&now, NULL);
106
107 diff = (now.tv_sec - then->tv_sec) * 1000;
108
109 diff += now.tv_usec / 1000;
110 diff -= then->tv_usec / 1000;
111
112 return diff;
113 }
Pierre Ossman64624342015-03-03 16:30:13 +0100114
115 static size_t doPrefix(long long value, const char *unit,
116 char *buffer, size_t maxlen,
117 unsigned divisor, const char **prefixes,
118 size_t prefixCount) {
119 double newValue;
120 size_t prefix, len;
121
122 newValue = value;
123 prefix = 0;
124 while (newValue >= divisor) {
125 if (prefix >= prefixCount)
126 break;
127 newValue /= divisor;
128 prefix++;
129 }
130
131 len = snprintf(buffer, maxlen, "%g %s%s", newValue,
132 (prefix == 0) ? "" : prefixes[prefix-1], unit);
133 buffer[maxlen-1] = '\0';
134
135 return len;
136 }
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
143 size_t siPrefix(long long value, const char *unit,
144 char *buffer, size_t maxlen) {
145 return doPrefix(value, unit, buffer, maxlen, 1000, siPrefixes,
146 sizeof(siPrefixes)/sizeof(*siPrefixes));
147 }
148
149 size_t iecPrefix(long long value, const char *unit,
150 char *buffer, size_t maxlen) {
151 return doPrefix(value, unit, buffer, maxlen, 1024, iecPrefixes,
152 sizeof(iecPrefixes)/sizeof(*iecPrefixes));
153 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000154};