1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #define FORBIDDEN_SYMBOL_EXCEPTION_ctype_h
24
25 #include "common/util.h"
26 #include "common/debug.h"
27
28 namespace Common {
29
30 //
31 // Print hexdump of the data passed in
32 //
hexdump(const byte * data,int len,int bytesPerLine,int startOffset)33 void hexdump(const byte *data, int len, int bytesPerLine, int startOffset) {
34 assert(1 <= bytesPerLine && bytesPerLine <= 32);
35 int i;
36 byte c;
37 int offset = startOffset;
38 while (len >= bytesPerLine) {
39 debugN("%06x: ", offset);
40 for (i = 0; i < bytesPerLine; i++) {
41 debugN("%02x ", data[i]);
42 if (i % 4 == 3)
43 debugN(" ");
44 }
45 debugN(" |");
46 for (i = 0; i < bytesPerLine; i++) {
47 c = data[i];
48 if (c < 32 || c >= 127)
49 c = '.';
50 debugN("%c", c);
51 }
52 debugN("|\n");
53 data += bytesPerLine;
54 len -= bytesPerLine;
55 offset += bytesPerLine;
56 }
57
58 if (len <= 0)
59 return;
60
61 debugN("%06x: ", offset);
62 for (i = 0; i < bytesPerLine; i++) {
63 if (i < len)
64 debugN("%02x ", data[i]);
65 else
66 debugN(" ");
67 if (i % 4 == 3)
68 debugN(" ");
69 }
70 debugN(" |");
71 for (i = 0; i < len; i++) {
72 c = data[i];
73 if (c < 32 || c >= 127)
74 c = '.';
75 debugN("%c", c);
76 }
77 for (; i < bytesPerLine; i++)
78 debugN(" ");
79 debugN("|\n");
80 }
81
82
83 #pragma mark -
84
85
parseBool(const String & val,bool & valAsBool)86 bool parseBool(const String &val, bool &valAsBool) {
87 if (val.equalsIgnoreCase("true") ||
88 val.equalsIgnoreCase("yes") ||
89 val.equals("1")) {
90 valAsBool = true;
91 return true;
92 }
93 if (val.equalsIgnoreCase("false") ||
94 val.equalsIgnoreCase("no") ||
95 val.equals("0") ||
96 val.empty()) {
97 valAsBool = false;
98 return true;
99 }
100
101 return false;
102 }
103
104
105 #pragma mark -
106
107
108 #define ENSURE_ASCII_CHAR(c) \
109 if (c < 0 || c > 127) \
110 return false
111
isAlnum(int c)112 bool isAlnum(int c) {
113 ENSURE_ASCII_CHAR(c);
114 return isalnum((byte)c);
115 }
116
isAlpha(int c)117 bool isAlpha(int c) {
118 ENSURE_ASCII_CHAR(c);
119 return isalpha((byte)c);
120 }
121
isDigit(int c)122 bool isDigit(int c) {
123 ENSURE_ASCII_CHAR(c);
124 return isdigit((byte)c);
125 }
126
isXDigit(int c)127 bool isXDigit(int c) {
128 ENSURE_ASCII_CHAR(c);
129 return isxdigit((byte)c);
130 }
131
isLower(int c)132 bool isLower(int c) {
133 ENSURE_ASCII_CHAR(c);
134 return islower((byte)c);
135 }
136
isSpace(int c)137 bool isSpace(int c) {
138 ENSURE_ASCII_CHAR(c);
139 return isspace((byte)c);
140 }
141
isUpper(int c)142 bool isUpper(int c) {
143 ENSURE_ASCII_CHAR(c);
144 return isupper((byte)c);
145 }
146
isPrint(int c)147 bool isPrint(int c) {
148 ENSURE_ASCII_CHAR(c);
149 return isprint((byte)c);
150 }
151
isPunct(int c)152 bool isPunct(int c) {
153 ENSURE_ASCII_CHAR(c);
154 return ispunct((byte)c);
155 }
156
isCntrl(int c)157 bool isCntrl(int c) {
158 ENSURE_ASCII_CHAR(c);
159 return iscntrl((byte)c);
160 }
161
isGraph(int c)162 bool isGraph(int c) {
163 ENSURE_ASCII_CHAR(c);
164 return isgraph((byte)c);
165 }
166
isBlank(int c)167 bool isBlank(int c) {
168 return c == ' ' || c == '\t';
169 }
170
171
172 #pragma mark -
173
174
getHumanReadableBytes(uint64 bytes,Common::String & unitsOut)175 Common::String getHumanReadableBytes(uint64 bytes, Common::String &unitsOut) {
176 if (bytes < 1024) {
177 unitsOut = "B";
178 return Common::String::format("%lu", (unsigned long int)bytes);
179 }
180
181 double floating = bytes / 1024.0;
182 unitsOut = "KB";
183
184 if (floating >= 1024) {
185 floating /= 1024.0;
186 unitsOut = "MB";
187 }
188
189 if (floating >= 1024) {
190 floating /= 1024.0;
191 unitsOut = "GB";
192 }
193
194 if (floating >= 1024) { // woah
195 floating /= 1024.0;
196 unitsOut = "TB";
197 }
198
199 // print one digit after floating point
200 return Common::String::format("%.1f", floating);
201 }
202
203 } // End of namespace Common
204