| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file util.cpp |
| 5 | * |
| 6 | * Part of the OpenJazz project |
| 7 | * |
| 8 | * @par History: |
| 9 | * - 23rd August 2005: Created main.c |
| 10 | * - 22nd July 2008: Created util.c from parts of main.c |
| 11 | * - 3rd February 2009: Renamed util.c to util.cpp |
| 12 | * - 3rd February 2009: Created file.cpp from parts of util.cpp |
| 13 | * - 4th February 2009: Created palette.cpp from parts of main.cpp and util.cpp |
| 14 | * - 13th July 2009: Created graphics.cpp from parts of util.cpp |
| 15 | * |
| 16 | * @par Licence: |
| 17 | * Copyright (c) 2005-2012 AJ Thomson |
| 18 | * |
| 19 | * OpenJazz is distributed under the terms of |
| 20 | * the GNU General Public License, version 2.0 |
| 21 | * |
| 22 | * @par Description: |
| 23 | * Contains core utility functions. |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | |
| 28 | #include "util.h" |
| 29 | #include "io/file.h" |
| 30 | #include "io/log.h" |
| 31 | |
| 32 | #include <string.h> |
| 33 | #include <cstdio> |
| 34 | #include <cctype> |
| 35 | |
| 36 | /** |
| 37 | * Check if a file exists. |
| 38 | * |
| 39 | * @param fileName The file to check |
| 40 | * @param pathType Kind of directory |
| 41 | * |
| 42 | * @return Whether or not the file exists |
| 43 | */ |
| 44 | bool fileExists (const char * fileName, int pathType) { |
| 45 | |
| 46 | File *file; |
| 47 | |
| 48 | #ifdef VERBOSE |
| 49 | printf("Check: "); |
| 50 | #endif |
| 51 | |
| 52 | try { |
| 53 | |
| 54 | file = new File(fileName, pathType); |
| 55 | |
| 56 | } catch (int e) { |
| 57 | |
| 58 | return false; |
| 59 | |
| 60 | } |
| 61 | |
| 62 | delete file; |
| 63 | |
| 64 | return true; |
| 65 | |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * Create a short based on the little-endian contents of the first two bytes in |
| 71 | * the given memory location. |
| 72 | * |
| 73 | * @param data Pointer to the memory location |
| 74 | * |
| 75 | * @return The generated short |
| 76 | */ |
| 77 | unsigned short int createShort (const unsigned char* data) { |
| 78 | |
| 79 | unsigned short int val; |
| 80 | |
| 81 | val = data[0] + (data[1] << 8); |
| 82 | |
| 83 | return val; |
| 84 | |
| 85 | } |
| 86 | |
| 87 | |
| 88 | /** |
| 89 | * Create an int based on the little-endian contents of the first two bytes in |
| 90 | * the given memory location. |
| 91 | * |
| 92 | * @param data Pointer to the memory location |
| 93 | * |
| 94 | * @return The generated int |
| 95 | */ |
| 96 | int createInt (const unsigned char* data) { |
| 97 | |
| 98 | unsigned int val; |
| 99 | |
| 100 | val = data[0] + (data[1] << 8) + (data[2] << 16) + (data[3] << 24); |
| 101 | |
| 102 | return *((int *)&val); |
| 103 | |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /** |
| 108 | * Create a new string from the contents of an existing string. |
| 109 | * |
| 110 | * @param string The existing string |
| 111 | * |
| 112 | * @return The new string |
| 113 | */ |
| 114 | char * createString (const char *string) { |
| 115 | |
| 116 | char *cloned; |
| 117 | |
| 118 | cloned = new char[strlen(string) + 1]; |
| 119 | strcpy(cloned, string); |
| 120 | |
| 121 | return cloned; |
| 122 | |
| 123 | } |
| 124 | |
| 125 | |
| 126 | /** |
| 127 | * Create a new string from the concatenation of two existing strings. |
| 128 | * |
| 129 | * @param first The existing string to form the start of the new string |
| 130 | * @param second The exisitng string to form the end of the new string |
| 131 | * |
| 132 | * @return The new string |
| 133 | */ |
| 134 | char * createString (const char *first, const char *second) { |
| 135 | |
| 136 | char *concatenated; |
| 137 | |
| 138 | concatenated = new char[strlen(first) + strlen(second) + 1]; |
| 139 | strcpy(concatenated, first); |
| 140 | strcat(concatenated, second); |
| 141 | |
| 142 | return concatenated; |
| 143 | |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /** |
| 148 | * Create a new file name string with a 3-digit numerical extension. |
| 149 | * |
| 150 | * @param type The pre-dot file name |
| 151 | * @param extension The number to constitute the extension |
| 152 | * |
| 153 | * @return The new file name string |
| 154 | */ |
| 155 | char * createFileName (const char *type, int extension) { |
| 156 | |
| 157 | char *fileName; |
| 158 | int pos; |
| 159 | |
| 160 | pos = strlen(type); |
| 161 | fileName = new char[pos + 5]; |
| 162 | strcpy(fileName, type); |
| 163 | fileName[pos++] = '.'; |
| 164 | fileName[pos++] = '0' + ((extension / 100) % 10); |
| 165 | fileName[pos++] = '0' + ((extension / 10) % 10); |
| 166 | fileName[pos++] = '0' + (extension % 10); |
| 167 | fileName[pos] = 0; |
| 168 | |
| 169 | return fileName; |
| 170 | |
| 171 | } |
| 172 | |
| 173 | |
| 174 | /** |
| 175 | * Create a new file name string with the given extension. |
| 176 | * |
| 177 | * @param type The pre-dot file name |
| 178 | * @param extension The extension |
| 179 | * |
| 180 | * @return The new file name string |
| 181 | */ |
| 182 | char * createFileName (const char *type, const char *extension) { |
| 183 | |
| 184 | char *fileName; |
| 185 | int pos; |
| 186 | |
| 187 | pos = strlen(type); |
| 188 | fileName = new char[pos + strlen(extension) + 2]; |
| 189 | strcpy(fileName, type); |
| 190 | fileName[pos++] = '.'; |
| 191 | strcpy(fileName + pos, extension); |
| 192 | |
| 193 | return fileName; |
| 194 | |
| 195 | } |
| 196 | |
| 197 | |
| 198 | /** |
| 199 | * Create a new file name string with a 1-digit numerical suffix and a 3-digit |
| 200 | * numerical extension. |
| 201 | * |
| 202 | * @param type The pre-dot file name |
| 203 | * @param level The number to constitute the suffix |
| 204 | * @param extension The number to constitute the extension |
| 205 | * |
| 206 | * @return The new file name string |
| 207 | */ |
| 208 | char * createFileName (const char *type, int level, int extension) { |
| 209 | |
| 210 | char *fileName; |
| 211 | int pos; |
| 212 | |
| 213 | pos = strlen(type); |
| 214 | fileName = new char[pos + 6]; |
| 215 | strcpy(fileName, type); |
| 216 | fileName[pos++] = '0' + (level % 10); |
| 217 | fileName[pos++] = '.'; |
| 218 | fileName[pos++] = '0' + ((extension / 100) % 10); |
| 219 | fileName[pos++] = '0' + ((extension / 10) % 10); |
| 220 | fileName[pos++] = '0' + (extension % 10); |
| 221 | fileName[pos] = 0; |
| 222 | |
| 223 | return fileName; |
| 224 | |
| 225 | } |
| 226 | |
| 227 | |
| 228 | /** |
| 229 | * Create a new variable-length string from the contents of an existing string. |
| 230 | * |
| 231 | * @param string The existing string |
| 232 | * |
| 233 | * @return The new string |
| 234 | */ |
| 235 | char * createEditableString (const char *string) { |
| 236 | |
| 237 | char *cloned; |
| 238 | |
| 239 | cloned = new char[STRING_LENGTH + 1]; |
| 240 | strcpy(cloned, string); |
| 241 | |
| 242 | return cloned; |
| 243 | |
| 244 | } |
| 245 | |
| 246 | void lowercaseString (char *string) { |
| 247 | for (int i = 0; string[i]; i++) { |
| 248 | if ((string[i] >= 65) && (string[i] <= 90)) |
| 249 | string[i] += 32; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | void uppercaseString (char *string) { |
| 254 | for (int i = 0; string[i]; i++) { |
| 255 | if ((string[i] >= 97) && (string[i] <= 122)) |
| 256 | string[i] -= 32; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | void camelcaseString (char *string) { |
| 261 | bool isFirst = true; |
| 262 | |
| 263 | for (int i = 0; string[i]; i++) { |
| 264 | if(isFirst) { |
| 265 | if ((string[i] >= 97) && (string[i] <= 122)) |
| 266 | string[i] -= 32; |
| 267 | isFirst = false; |
| 268 | } else { |
| 269 | if ((string[i] >= 65) && (string[i] <= 90)) |
| 270 | string[i] += 32; |
| 271 | } |
| 272 | if(string[i] == 32) |
| 273 | isFirst = true; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | |
| 278 | /** |
| 279 | * Get the sine of the given angle |
| 280 | * |
| 281 | * @param angle The given angle (where 1024 represents a full circle) |
| 282 | * |
| 283 | * @return The sine of the angle |
| 284 | */ |
| 285 | fixed fSin (fixed angle) { |
| 286 | |
| 287 | return sinLut[angle & 1023]; |
| 288 | |
| 289 | } |
| 290 | |
| 291 | |
| 292 | /** |
| 293 | * Get the cosine of the given angle |
| 294 | * |
| 295 | * @param angle The given angle (where 1024 represetns a full circle) |
| 296 | * |
| 297 | * @return The cosine of the angle |
| 298 | */ |
| 299 | fixed fCos (fixed angle) { |
| 300 | |
| 301 | return sinLut[(angle + 256) & 1023]; |
| 302 | |
| 303 | } |
| 304 | |
| 305 | |
| 306 | /** |
| 307 | * Unpack a block of RLE compressed data. |
| 308 | * |
| 309 | * @param data Buffer containing compressed data |
| 310 | * @param size The length of the compressed block |
| 311 | * @param outSize The length of the uncompressed block |
| 312 | * |
| 313 | * @return Buffer containing the uncompressed data |
| 314 | */ |
| 315 | unsigned char* unpackRLE (unsigned char* data, unsigned int size, unsigned int outSize) { |
| 316 | unsigned char* buffer = new unsigned char[outSize]; |
| 317 | |
| 318 | unsigned int posIn = 0, posOut = 0; |
| 319 | while (posIn < size) { |
| 320 | unsigned char code = data[posIn]; |
| 321 | unsigned char amount = code & 127; |
| 322 | |
| 323 | if (code & 128) { // repeat |
| 324 | unsigned char value = data[posIn+1]; |
| 325 | |
| 326 | if (posOut + amount >= outSize) { |
| 327 | LOG_WARN("Exceeding write buffer while RLE unpacking."); |
| 328 | break; |
| 329 | } |
| 330 | |
| 331 | memset(buffer + posOut, value, amount); |
| 332 | |
| 333 | posIn += 2; |
| 334 | posOut += amount; |
| 335 | } else if (amount) { // copy |
| 336 | |
| 337 | if (posOut + amount >= outSize) { |
| 338 | LOG_WARN("Exceeding write buffer while RLE unpacking."); |
| 339 | break; |
| 340 | } |
| 341 | |
| 342 | memcpy(buffer + posOut, data + posIn + 1, amount); |
| 343 | |
| 344 | posIn += amount + 1; |
| 345 | posOut += amount; |
| 346 | } else { // end marker |
| 347 | buffer[posOut++] = data[posIn+1]; |
| 348 | posIn += 2; |
| 349 | |
| 350 | //LOG_MAX("End marker found while RLE unpacking."); |
| 351 | break; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | if(size != posIn || outSize != posOut) { |
| 356 | LOG_DEBUG("RLE block has incorrect size: in %d/%d out %d/%d", size, posIn, outSize, posOut); |
| 357 | } |
| 358 | |
| 359 | delete[] data; |
| 360 | return buffer; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Make a hex dump of data |
| 365 | * |
| 366 | * @param desc if non-NULL, printed as a description before hex dump |
| 367 | * @param addr Address to start dumping from |
| 368 | * @param size Number of bytes to dump |
| 369 | * @param perLine Number of bytes on each output line |
| 370 | */ |
| 371 | void hexDump (const char * desc, const void * addr, const int size, int perLine) { |
| 372 | // Length check |
| 373 | if (!(size > 0)) { |
| 374 | LOG_WARN("Cannot hexdump"); |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | if (desc != nullptr) |
| 379 | printf("\x1b[34m%s:\x1b[0m\n", desc); |
| 380 | |
| 381 | unsigned char buffer[perLine + 1]; |
| 382 | const unsigned char * pc = (const unsigned char *)addr; |
| 383 | |
| 384 | // Process every byte in the data. |
| 385 | int i; |
| 386 | for (i = 0; i < size; i++) { |
| 387 | // Multiple of perLine means new or first line (with line offset). |
| 388 | if ((i % perLine) == 0) { |
| 389 | // Only print previous-line ASCII buffer for lines beyond first. |
| 390 | if (i != 0) |
| 391 | printf(" \x1b[32m%s\n", buffer); |
| 392 | |
| 393 | // Output the offset of current line. |
| 394 | printf("\x1b[90m %04x \x1b[35m", i); |
| 395 | } |
| 396 | |
| 397 | // Now the hex code for the specific character. |
| 398 | printf(" %02x", pc[i]); |
| 399 | |
| 400 | // And buffer a printable ASCII character for later. |
| 401 | buffer[i % perLine] = isprint(pc[i]) ? pc[i] : '.'; |
| 402 | buffer[(i % perLine) + 1] = '\0'; |
| 403 | } |
| 404 | |
| 405 | // Pad out last line if not exactly perLine characters. |
| 406 | while ((i % perLine) != 0) { |
| 407 | printf(" "); |
| 408 | i++; |
| 409 | } |
| 410 | |
| 411 | // And print the final ASCII buffer. |
| 412 | printf(" \x1b[32m%s\x1b[0m\n", buffer); |
| 413 | } |