Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 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 | /* |
| 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 Tkac | 8aee1a8 | 2009-09-04 12:08:56 +0000 | [diff] [blame] | 33 | #ifdef HAVE_CONFIG_H |
| 34 | #include <config.h> |
Adam Tkac | ad1cbd9 | 2008-10-06 14:08:00 +0000 | [diff] [blame] | 35 | #endif |
| 36 | |
Pierre Ossman | ba6fbfe | 2015-03-03 16:41:29 +0100 | [diff] [blame] | 37 | #include <stdarg.h> |
Pierre Ossman | 6462434 | 2015-03-03 16:30:13 +0100 | [diff] [blame] | 38 | #include <stdio.h> |
Pierre Ossman | 5bc20a6 | 2011-11-08 12:42:41 +0000 | [diff] [blame] | 39 | #include <sys/time.h> |
| 40 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 41 | #include <rfb/util.h> |
| 42 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 43 | namespace rfb { |
| 44 | |
Pierre Ossman | ba6fbfe | 2015-03-03 16:41:29 +0100 | [diff] [blame] | 45 | void CharArray::format(const char *fmt, ...) { |
| 46 | va_list ap; |
| 47 | size_t len; |
| 48 | |
| 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 Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 68 | 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 Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 81 | 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 Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 106 | if (out1) *out1 = strDup(src); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 107 | 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 Ossman | a99d14d | 2015-12-13 15:43:46 +0100 | [diff] [blame^] | 124 | unsigned msBetween(const struct timeval *first, |
| 125 | const struct timeval *second) |
| 126 | { |
| 127 | unsigned diff; |
| 128 | |
| 129 | diff = (second->tv_sec - first->tv_sec) * 1000; |
| 130 | |
| 131 | diff += second->tv_usec / 1000; |
| 132 | diff -= first->tv_usec / 1000; |
| 133 | |
| 134 | return diff; |
| 135 | } |
| 136 | |
Pierre Ossman | 5bc20a6 | 2011-11-08 12:42:41 +0000 | [diff] [blame] | 137 | unsigned msSince(const struct timeval *then) |
| 138 | { |
| 139 | struct timeval now; |
Pierre Ossman | 5bc20a6 | 2011-11-08 12:42:41 +0000 | [diff] [blame] | 140 | |
| 141 | gettimeofday(&now, NULL); |
| 142 | |
Pierre Ossman | a99d14d | 2015-12-13 15:43:46 +0100 | [diff] [blame^] | 143 | return msBetween(then, &now); |
| 144 | } |
Pierre Ossman | 5bc20a6 | 2011-11-08 12:42:41 +0000 | [diff] [blame] | 145 | |
Pierre Ossman | a99d14d | 2015-12-13 15:43:46 +0100 | [diff] [blame^] | 146 | bool isBefore(const struct timeval *first, |
| 147 | const struct timeval *second) |
| 148 | { |
| 149 | if (first->tv_sec < second->tv_sec) |
| 150 | return true; |
| 151 | if (first->tv_sec > second->tv_sec) |
| 152 | return false; |
| 153 | if (first->tv_usec < second->tv_usec) |
| 154 | return true; |
| 155 | return false; |
Pierre Ossman | 5bc20a6 | 2011-11-08 12:42:41 +0000 | [diff] [blame] | 156 | } |
Pierre Ossman | 6462434 | 2015-03-03 16:30:13 +0100 | [diff] [blame] | 157 | |
| 158 | static size_t doPrefix(long long value, const char *unit, |
| 159 | char *buffer, size_t maxlen, |
| 160 | unsigned divisor, const char **prefixes, |
Pierre Ossman | 921f6c8 | 2017-02-24 12:33:09 +0100 | [diff] [blame] | 161 | size_t prefixCount, int precision) { |
Pierre Ossman | 6462434 | 2015-03-03 16:30:13 +0100 | [diff] [blame] | 162 | double newValue; |
| 163 | size_t prefix, len; |
| 164 | |
| 165 | newValue = value; |
| 166 | prefix = 0; |
| 167 | while (newValue >= divisor) { |
| 168 | if (prefix >= prefixCount) |
| 169 | break; |
| 170 | newValue /= divisor; |
| 171 | prefix++; |
| 172 | } |
| 173 | |
Pierre Ossman | 921f6c8 | 2017-02-24 12:33:09 +0100 | [diff] [blame] | 174 | len = snprintf(buffer, maxlen, "%.*g %s%s", precision, newValue, |
Pierre Ossman | 6462434 | 2015-03-03 16:30:13 +0100 | [diff] [blame] | 175 | (prefix == 0) ? "" : prefixes[prefix-1], unit); |
| 176 | buffer[maxlen-1] = '\0'; |
| 177 | |
| 178 | return len; |
| 179 | } |
| 180 | |
| 181 | static const char *siPrefixes[] = |
| 182 | { "k", "M", "G", "T", "P", "E", "Z", "Y" }; |
| 183 | static const char *iecPrefixes[] = |
| 184 | { "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" }; |
| 185 | |
| 186 | size_t siPrefix(long long value, const char *unit, |
Pierre Ossman | 921f6c8 | 2017-02-24 12:33:09 +0100 | [diff] [blame] | 187 | char *buffer, size_t maxlen, int precision) { |
Pierre Ossman | 6462434 | 2015-03-03 16:30:13 +0100 | [diff] [blame] | 188 | return doPrefix(value, unit, buffer, maxlen, 1000, siPrefixes, |
Pierre Ossman | 921f6c8 | 2017-02-24 12:33:09 +0100 | [diff] [blame] | 189 | sizeof(siPrefixes)/sizeof(*siPrefixes), |
| 190 | precision); |
Pierre Ossman | 6462434 | 2015-03-03 16:30:13 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | size_t iecPrefix(long long value, const char *unit, |
Pierre Ossman | 921f6c8 | 2017-02-24 12:33:09 +0100 | [diff] [blame] | 194 | char *buffer, size_t maxlen, int precision) { |
Pierre Ossman | 6462434 | 2015-03-03 16:30:13 +0100 | [diff] [blame] | 195 | return doPrefix(value, unit, buffer, maxlen, 1024, iecPrefixes, |
Pierre Ossman | 921f6c8 | 2017-02-24 12:33:09 +0100 | [diff] [blame] | 196 | sizeof(iecPrefixes)/sizeof(*iecPrefixes), |
| 197 | precision); |
Pierre Ossman | 6462434 | 2015-03-03 16:30:13 +0100 | [diff] [blame] | 198 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 199 | }; |