| 1 | /* |
| 2 | * Copyright (c) 1991, 2002 Michael J. Roberts. All Rights Reserved. |
| 3 | * |
| 4 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 5 | * on using and copying this software. |
| 6 | */ |
| 7 | /* |
| 8 | Name |
| 9 | t3std.h - standard definitions |
| 10 | Function |
| 11 | Various standard definitions |
| 12 | Notes |
| 13 | None |
| 14 | Modified |
| 15 | 10/17/98 MJRoberts - creation (from TADS 2 lib.h) |
| 16 | */ |
| 17 | |
| 18 | #ifndef T3_STD_INCLUDED |
| 19 | #define T3_STD_INCLUDED |
| 20 | |
| 21 | #include <stddef.h> |
| 22 | #include <string.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <stdarg.h> |
| 25 | #include <ctype.h> |
| 26 | #include <limits.h> |
| 27 | |
| 28 | #include "os.h" |
| 29 | |
| 30 | |
| 31 | /* ------------------------------------------------------------------------ */ |
| 32 | /* |
| 33 | * err_throw() Return Handling |
| 34 | * |
| 35 | * Some compilers (such as MSVC 2007) are capable of doing global |
| 36 | * optimizations that can detect functions that never return. err_throw() |
| 37 | * is one such function: it uses longjmp() to jump out, so it never returns |
| 38 | * to its caller. |
| 39 | * |
| 40 | * Most compilers can't detect this automatically, and C++ doesn't have a |
| 41 | * standard way to declare a function that never returns. So on most |
| 42 | * compilers, the compiler will assume that err_throw() returns, and thus |
| 43 | * will generate a warning if err_throw() isn't followed by some proper |
| 44 | * control flow statement. For example, in a function with a return value, |
| 45 | * a code branch containing an err_throw() would still need a 'return |
| 46 | * <val>' statement - without such a statement, the compiler would generate |
| 47 | * an error about a branch without a value return. |
| 48 | * |
| 49 | * This creates a porting dilemma. On compilers that can detect that |
| 50 | * err_throw() never returns, the presence of any statement in a code |
| 51 | * branch after an err_throw() will cause an "unreachable code" error. For |
| 52 | * all other compilers, the *absence* of such code will often cause a |
| 53 | * different error ("missing return", etc). |
| 54 | * |
| 55 | * The only way I can see to deal with this is to use a compile-time |
| 56 | * #define to select which type of compiler we're using, and use this to |
| 57 | * insert or delete the proper dummy control flow statement after an |
| 58 | * err_throw(). So: |
| 59 | * |
| 60 | * --- INSTRUCTIONS TO BASE CODE DEVELOPERS --- |
| 61 | * |
| 62 | * - after each err_throw() call, if the code branch needs some kind of |
| 63 | * explicit termination (such as a "return val;" statement), code it with |
| 64 | * the AFTER_ERR_THROW() macro. Since err_throw() never *actually* |
| 65 | * returns, these will be dummy statements that will never be reached, but |
| 66 | * the compiler might require their presence anyway because it doesn't know |
| 67 | * better. |
| 68 | * |
| 69 | * --- INSTRUCTIONS TO PORTERS --- |
| 70 | * |
| 71 | * - if your compiler CAN detect that err_throw() never returns, define |
| 72 | * COMPILER_DETECTS_THROW_NORETURN in your compiler command-line options; |
| 73 | * |
| 74 | * - otherwise, leave the symbol undefined. |
| 75 | */ |
| 76 | #ifdef COMPILER_DETECTS_THROW_NORETURN |
| 77 | #define COMPILER_DETECTS_THROW_NORETURN_IN_VMERR |
| 78 | #define AFTER_ERR_THROW(code) |
| 79 | #else |
| 80 | #define AFTER_ERR_THROW(code) code |
| 81 | #endif |
| 82 | |
| 83 | /* |
| 84 | * And the same idea as above, but for the special case where the compiler |
| 85 | * detects this within the vmerr.cpp module (for static functions in the |
| 86 | * same module), but not across modules. |
| 87 | */ |
| 88 | #ifdef COMPILER_DETECTS_THROW_NORETURN_IN_VMERR |
| 89 | #define VMERR_AFTER_ERR_THROW(code) |
| 90 | #else |
| 91 | #define VMERR_AFTER_ERR_THROW(code) code |
| 92 | #endif |
| 93 | |
| 94 | /* |
| 95 | * os_term() return handling. This is similar to the longjmp() issue |
| 96 | * above. Some compilers perform global optimizations that detect that |
| 97 | * exit(), and functions that unconditionally call exit(), such as |
| 98 | * os_term(), do not return. Since these compilers know that anything |
| 99 | * following a call to exit() is unreachable, some will complain about if |
| 100 | * anything follows an exit(). Compilers that *don't* do such |
| 101 | * optimizations will complain if there *isn't* a 'return' after an exit() |
| 102 | * if the containing function requires a return value, since as far as |
| 103 | * they're concerned the function is falling off the end without returning |
| 104 | * a value. So there's no solution at the C++ level; it's a compiler |
| 105 | * variation that we have to address per compiler. If you get 'unreachable |
| 106 | * code' errors in the generic code after calls to os_term(), define this |
| 107 | * macro. Most platforms can leave it undefined. |
| 108 | */ |
| 109 | #ifdef COMPILER_DETECTS_OS_TERM_NORETURN |
| 110 | #define AFTER_OS_TERM(code) |
| 111 | #else |
| 112 | #define AFTER_OS_TERM(code) code |
| 113 | #endif |
| 114 | |
| 115 | /* |
| 116 | * Some compilers (notably gcc >= 4.7.1) require that overloads for the |
| 117 | * basic 'new' and 'delete' operators be declared with 'throw' clauses to |
| 118 | * match the standard C++ library (that is, 'new' must be declared to throw |
| 119 | * std::bad_alloc, and 'delete' must be declared to throw nothing). |
| 120 | * Naturally, some other compilers (notably MSVC 2003) don't want the |
| 121 | * 'throw' clauses. If your compiler wants the 'throw' declarations, |
| 122 | * define NEW_DELETE_NEED_THROW in your makefile, otherwise omit it. Note |
| 123 | * that some compilers (notably gcc < 4.7.1) don't care one way or the |
| 124 | * other, so if your compiler doesn't complain, you probably don't need to |
| 125 | * worry about this setting. |
| 126 | */ |
| 127 | #ifndef SYSTHROW |
| 128 | #ifdef NEW_DELETE_NEED_THROW |
| 129 | #define SYSTHROW(exc) exc |
| 130 | #else |
| 131 | #define SYSTHROW(exc) |
| 132 | #endif |
| 133 | #endif |
| 134 | |
| 135 | |
| 136 | /* ------------------------------------------------------------------------ */ |
| 137 | /* |
| 138 | * T3 OS interface extensions. These are portable interfaces to |
| 139 | * OS-dependent functionality, along the same lines as the functions |
| 140 | * defined in tads2/osifc.h but specific to TADS 3. |
| 141 | */ |
| 142 | |
| 143 | /* |
| 144 | * Initialize the UI after loading the image file. The T3 image loader |
| 145 | * calls this after it's finished loading a .t3 image file, but before |
| 146 | * executing any code in the new game. This lets the local platform UI |
| 147 | * perform any extra initialization that depends on inspecting the contents |
| 148 | * of the loaded game. |
| 149 | * |
| 150 | * This isn't required to do anything; a valid implementation is an empty |
| 151 | * stub routine. The purpose of this routine is to give the UI a chance to |
| 152 | * customize the UI according to the details of the loaded game, before the |
| 153 | * game starts running. In particular, the UI configuration might vary |
| 154 | * according to which intrinsic classes or function sets are linked by the |
| 155 | * game. For example, one set of windows might be used for a traditional |
| 156 | * console game, while another is used for a Web UI game, the latter being |
| 157 | * recognizable by linking the tads-net function set. |
| 158 | */ |
| 159 | void os_init_ui_after_load(class CVmBifTable *bif_table, |
| 160 | class CVmMetaTable *meta_table); |
| 161 | |
| 162 | /* ------------------------------------------------------------------------ */ |
| 163 | /* |
| 164 | * Memory debugging |
| 165 | */ |
| 166 | |
| 167 | /* in debug builds, we override operator new, which requires including <new> */ |
| 168 | #ifdef T3_DEBUG |
| 169 | #include <new> |
| 170 | #endif |
| 171 | |
| 172 | /* for Windows debug builds, add stack trace info to allocation blocks */ |
| 173 | #if defined(T3_DEBUG) && defined(T_WIN32) |
| 174 | # define OS_MEM_PREFIX \ |
| 175 | struct { \ |
| 176 | DWORD return_addr; \ |
| 177 | } stk[6]; |
| 178 | void os_mem_prefix_set(struct mem_prefix_t *mem); |
| 179 | |
| 180 | # define OS_MEM_PREFIX_FMT ", return=(%lx, %lx, %lx, %lx, %lx, %lx)" |
| 181 | # define OS_MEM_PREFIX_FMT_VARS(mem) \ |
| 182 | , (mem)->stk[0].return_addr, \ |
| 183 | (mem)->stk[1].return_addr, \ |
| 184 | (mem)->stk[2].return_addr, \ |
| 185 | (mem)->stk[3].return_addr, \ |
| 186 | (mem)->stk[4].return_addr, \ |
| 187 | (mem)->stk[5].return_addr |
| 188 | #endif |
| 189 | |
| 190 | /* provide empty default definitions for system memory header add-ons */ |
| 191 | #ifndef OS_MEM_PREFIX |
| 192 | # define OS_MEM_PREFIX |
| 193 | # define os_mem_prefix_set(mem) |
| 194 | # define OS_MEM_PREFIX_FMT |
| 195 | # define OS_MEM_PREFIX_FMT_VARS(mem) |
| 196 | #endif |
| 197 | |
| 198 | /* ------------------------------------------------------------------------ */ |
| 199 | /* |
| 200 | * Types |
| 201 | */ |
| 202 | |
| 203 | /* short-hand for various types */ |
| 204 | #ifndef OS_UCHAR_DEFINED |
| 205 | typedef unsigned char uchar; |
| 206 | #endif |
| 207 | |
| 208 | #ifndef OS_USHORT_DEFINED |
| 209 | typedef unsigned short ushort; |
| 210 | #endif |
| 211 | |
| 212 | #ifndef OS_UINT_DEFINED |
| 213 | typedef unsigned int uint; |
| 214 | #endif |
| 215 | |
| 216 | #ifndef OS_ULONG_DEFINED |
| 217 | typedef unsigned long ulong; |
| 218 | #endif |
| 219 | |
| 220 | /* sizeof() extension macros */ |
| 221 | #ifndef countof |
| 222 | #define countof(array) (sizeof(array)/sizeof((array)[0])) |
| 223 | #endif |
| 224 | #define sizeof_field(struct_name, field) sizeof(((struct_name *)0)->field) |
| 225 | |
| 226 | /* |
| 227 | * The types int16_t, uint16_t, int32_t, and uint32_t are defined by ANSI |
| 228 | * C99 as EXACTLY the specified number of bits (e.g., int16 is a signed |
| 229 | * 16-bit integer; uint32 is an unsigned 32-bit integer). |
| 230 | * |
| 231 | * Many modern compilers provide definitions for these types in <stdint.h>. |
| 232 | * When <stdint.h> isn't available, ports must provide suitable typedefs in |
| 233 | * the osxxx.h header - see tads2/osifc.h. |
| 234 | * |
| 235 | * Because these types have exact sizes, their limits are fixed, so we can |
| 236 | * provide portable definitions here. |
| 237 | */ |
| 238 | #define INT16MAXVAL 32767 |
| 239 | #define INT16MINVAL (-32768) |
| 240 | #define UINT16MAXVAL 65535 |
| 241 | #define INT32MAXVAL 2147483647L |
| 242 | #define INT32MINVAL (-2147483647L-1) |
| 243 | #define UINT32MAXVAL 4294967295U |
| 244 | |
| 245 | |
| 246 | /* |
| 247 | * Text character. We use ASCII and UTF-8 for most character string |
| 248 | * representations, so our basic character type is 'char', which is defined |
| 249 | * univerally as one byte. (UTF-8 uses varying numbers of bytes per |
| 250 | * character, but its basic storage unit is bytes.) |
| 251 | */ |
| 252 | typedef char textchar_t; |
| 253 | |
| 254 | |
| 255 | /* ------------------------------------------------------------------------ */ |
| 256 | /* |
| 257 | * Logical and Arithmetic right shifts. C99 deliberately leaves it up to |
| 258 | * the implementation to define what happens to the high bits for a right |
| 259 | * shift of a negative signed integer: they could be filled with 0s or 1s, |
| 260 | * depending on the implementation. C99 is clear on the result for |
| 261 | * unsigned values (the high bits are filled with 0s), however it's still |
| 262 | * possible that we'll encounter an older implementation that doesn't |
| 263 | * conform. |
| 264 | * |
| 265 | * There's one case where we care about ASHR vs RSHR being well defined, |
| 266 | * and that's the VM's implementation of the OPC_LSHR and OPC_ASHR |
| 267 | * instructions. We want these to be predictable on all platforms - we |
| 268 | * insist on breaking the cycle of ambiguity; we don't want to pass along |
| 269 | * this headache to our own users writing TADS programs. |
| 270 | * |
| 271 | * Nearly all modern compilers treat right shifts of signed operands as |
| 272 | * arithmetic, and right shifts of unsigned operands as logical. So our |
| 273 | * default implementation will take advantage of this to produce highly |
| 274 | * efficient code. For platforms where the signed/unsigned behavior |
| 275 | * doesn't work this way, we provide portable bit-twiddly versions that |
| 276 | * will work, but are somewhat more overhead to implement. |
| 277 | * |
| 278 | * If you're on a platform that DOES provide the "modern" signed==ASHR / |
| 279 | * unsigned==LSHR behavior, you don't have to do anything special - that's |
| 280 | * the default. If your platform doesn't use the modern behavior, AND your |
| 281 | * compiler's preprocessor does >> calculations the same way as generated |
| 282 | * code, you also don't have to do anything, since our #if's below will |
| 283 | * detect the situation and generate our portable explicit ASHR/LSHR code. |
| 284 | * If your compiler doesn't use the "modern" behavior AND its preprocessor |
| 285 | * uses different rules from generated code for >>, then you'll have to |
| 286 | * define the preprocessor symbols OS_CUSTOM_ASHR and/or OS_CUSTOM_LSHR in |
| 287 | * your makefile. |
| 288 | * |
| 289 | * You can test that your configuration is correct by compiling and running |
| 290 | * the shr.t from the test suite (tads3/test/data). To further test the |
| 291 | * detection conditions and custom macros, use test/test_shr.cpp. |
| 292 | */ |
| 293 | #if defined(OS_CUSTOM_ASHR) || ((-1 >> 1) != -1) |
| 294 | /* signed a >> signed b != a ASHR b, so implement with bit masking */ |
| 295 | inline int32_t t3_ashr(int32_t a, int32_t b) { |
| 296 | int32_t mask = (~0 << (sizeof(int32_t)*CHAR_BIT - b)); |
| 297 | return ((a >> b) | ((a & mask) ? mask : 0)); |
| 298 | }; |
| 299 | #else |
| 300 | /* signed a >> signed b == a ASHR b, so we can use >> */ |
| 301 | inline int32_t t3_ashr(int32_t a, int32_t b) { return a >> b; } |
| 302 | #endif |
| 303 | |
| 304 | #if defined(OS_CUSTOM_LSHR) || ((ULONG_MAX >> 1UL) != ULONG_MAX/2) |
| 305 | /* unsigned a >> unsigned b != a LSHR b, so implement with bit masking */ |
| 306 | inline int32_t t3_lshr(int32_t a, int32_t b) { |
| 307 | return ((a >> b) & ~(~0 << (sizeof(int32_t)*CHAR_BIT - b))); |
| 308 | } |
| 309 | #else |
| 310 | /* |
| 311 | * unsigned a >> unsigned b == a LSHR b, so we can use >>; note that we |
| 312 | * explicit mask the left operand to 32 bits in case we're on a 64-bit or |
| 313 | * larger platform, as the T3 VM itself is defined as a 32-bit machine |
| 314 | */ |
| 315 | inline int32_t t3_lshr(int32_t a, int32_t b) { |
| 316 | return (int32_t)((uint32_t)a >> (uint32_t)b); |
| 317 | } |
| 318 | #endif |
| 319 | |
| 320 | |
| 321 | /* ------------------------------------------------------------------------ */ |
| 322 | /* |
| 323 | * General portable utility macros |
| 324 | */ |
| 325 | |
| 326 | /* clear a struture */ |
| 327 | #define CLRSTRUCT(x) memset(&(x), 0, (size_t)sizeof(x)) |
| 328 | #define CPSTRUCT(dst,src) memcpy(&(dst), &(src), (size_t)sizeof(dst)) |
| 329 | |
| 330 | |
| 331 | /* TRUE and FALSE */ |
| 332 | #ifndef TRUE |
| 333 | # define TRUE 1 |
| 334 | #endif /* TRUE */ |
| 335 | #ifndef FALSE |
| 336 | # define FALSE 0 |
| 337 | #endif /* FALSE */ |
| 338 | |
| 339 | |
| 340 | /* bitwise operations */ |
| 341 | #define bit(va, bt) ((va) & (bt)) |
| 342 | #define bis(va, bt) ((va) |= (bt)) |
| 343 | #define bic(va, bt) ((va) &= ~(bt)) |
| 344 | |
| 345 | |
| 346 | /* conditionally compile code if debugging is enabled */ |
| 347 | #ifdef DEBUG |
| 348 | # define IF_DEBUG(x) x |
| 349 | #else /* DEBUG */ |
| 350 | # define IF_DEBUG(x) |
| 351 | #endif /* DEBUG */ |
| 352 | |
| 353 | /* offset within a structure of a member of the structure */ |
| 354 | #ifndef offsetof |
| 355 | # define offsetof(s_name, m_name) (size_t)&(((s_name *)0)->m_name) |
| 356 | #endif /* offsetof */ |
| 357 | |
| 358 | |
| 359 | /* |
| 360 | * Read an unsigned 32-bit value from the portable external-file |
| 361 | * representation. This is parallel to osrp4(), but explicitly reads an |
| 362 | * unsigned value. The important thing is that we mask the result to 32 |
| 363 | * bits, to prevent unwarranted sign extension on architectures with word |
| 364 | * sizes greater than 32 bits (at the moment, this basically means 64-bit |
| 365 | * machines, but it would apply to any >32-bit architecture). |
| 366 | * |
| 367 | * NB: as of TADS 3.1, osrp4() *should* be doing this automatically. It |
| 368 | * should be reading a 32 bit value and interpreting it as unsigned, doing |
| 369 | * any necessary zero extension to larger 'int' sizes. However, we're |
| 370 | * keeping this for now to be sure. |
| 371 | */ |
| 372 | #define t3rp4u(p) ((ulong)(osrp4(p) & 0xFFFFFFFFU)) |
| 373 | |
| 374 | |
| 375 | /* ------------------------------------------------------------------------ */ |
| 376 | /* |
| 377 | * Allocate space for a null-terminated string and save a copy of the |
| 378 | * string |
| 379 | */ |
| 380 | char *lib_copy_str(const char *str); |
| 381 | char *lib_copy_str(const char *str, size_t len); |
| 382 | |
| 383 | /* |
| 384 | * allocate space for a string of a given length; we'll add in space for |
| 385 | * a null terminator |
| 386 | */ |
| 387 | char *lib_alloc_str(size_t len); |
| 388 | |
| 389 | /* |
| 390 | * Free a string previously allocated with lib_copy_str() or |
| 391 | * lib_alloc_str() |
| 392 | */ |
| 393 | void lib_free_str(char *buf); |
| 394 | |
| 395 | /* ------------------------------------------------------------------------ */ |
| 396 | /* |
| 397 | * Safe strcpy with an explicit source length. Truncates the string to the |
| 398 | * buffer size, and always null-terminates. Note that the source string is |
| 399 | * NOT null-terminated - we copy the explicit length given, up to the |
| 400 | * output size limit. |
| 401 | */ |
| 402 | inline void lib_strcpy(char *dst, size_t dstsiz, |
| 403 | const char *src, size_t srclen) |
| 404 | { |
| 405 | if (dstsiz > 0) |
| 406 | { |
| 407 | size_t copylen = srclen; |
| 408 | if (copylen > dstsiz - 1) |
| 409 | copylen = dstsiz - 1; |
| 410 | memcpy(dst, src, copylen); |
| 411 | dst[copylen] = '\0'; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | /* |
| 416 | * Safe strcpy - checks the output buffer size and truncates the string if |
| 417 | * necessary; always null-terminates the result. |
| 418 | */ |
| 419 | inline void lib_strcpy(char *dst, size_t dstsiz, const char *src) |
| 420 | { |
| 421 | lib_strcpy(dst, dstsiz, src, strlen(src)); |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * Compare two counted-length strings, with or without case sensitivity. |
| 426 | */ |
| 427 | inline int lib_strcmp(const char *str1, size_t len1, |
| 428 | const char *str2, size_t len2) |
| 429 | { |
| 430 | int bylen = len1 - len2, bymem; |
| 431 | if (bylen == 0) |
| 432 | return memcmp(str1, str2, len1); |
| 433 | else if (bylen < 0) |
| 434 | bymem = memcmp(str1, str2, len1); |
| 435 | else |
| 436 | bymem = memcmp(str1, str2, len2); |
| 437 | return (bymem != 0 ? bymem : bylen); |
| 438 | } |
| 439 | inline int lib_stricmp(const char *str1, size_t len1, |
| 440 | const char *str2, size_t len2) |
| 441 | { |
| 442 | int bylen = len1 - len2, bymem; |
| 443 | if (bylen == 0) |
| 444 | return memicmp(str1, str2, len1); |
| 445 | else if (bylen < 0) |
| 446 | bymem = memicmp(str1, str2, len1); |
| 447 | else |
| 448 | bymem = memicmp(str1, str2, len2); |
| 449 | return (bymem != 0 ? bymem : bylen); |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * Compare a counted-length string to a regular C string, with or without |
| 454 | * case sensitivity. |
| 455 | */ |
| 456 | inline int lib_strcmp(const char *str1, size_t len1, const char *str2) |
| 457 | { |
| 458 | return lib_strcmp(str1, len1, str2, strlen(str2)); |
| 459 | } |
| 460 | inline int lib_stricmp(const char *str1, size_t len1, const char *str2) |
| 461 | { |
| 462 | return lib_stricmp(str1, len1, str2, strlen(str2)); |
| 463 | } |
| 464 | |
| 465 | |
| 466 | /* ------------------------------------------------------------------------ */ |
| 467 | /* |
| 468 | * Limited-length strchr. Searches within the given string for the given |
| 469 | * character; stops if we exhaust the length limit 'len' bytes or reach a |
| 470 | * null character in the string. |
| 471 | */ |
| 472 | inline char *lib_strnchr(const char *src, size_t len, int ch) |
| 473 | { |
| 474 | /* search until we exhaust the length limit or reach a null byte */ |
| 475 | for ( ; len != 0 && *src != '\0' ; --len, ++src) |
| 476 | { |
| 477 | /* if this is the character we're looking for, return the pointer */ |
| 478 | if (*src == ch) |
| 479 | return (char *)src; |
| 480 | } |
| 481 | |
| 482 | /* didn't find it */ |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | /* ------------------------------------------------------------------------ */ |
| 487 | /* |
| 488 | * Limited-length atoi |
| 489 | */ |
| 490 | int lib_atoi(const char *str, size_t len); |
| 491 | |
| 492 | /* |
| 493 | * Limited-length atoi, with auto-advance of the string |
| 494 | */ |
| 495 | int lib_atoi_adv(const char *&str, size_t &len); |
| 496 | |
| 497 | /* ------------------------------------------------------------------------ */ |
| 498 | /* |
| 499 | * Compare two strings, ignoring differences in whitespace between the |
| 500 | * strings. Returns true if the strings are equal (other than |
| 501 | * whitespace, false if not. |
| 502 | * |
| 503 | * Note that we do not ignore the *presence* of whitespace; we only |
| 504 | * ignore differences in the amount of whitespace. For example, "login" |
| 505 | * does not equal "log_in" (underscore = whitespace for these examples |
| 506 | * only, to emphasize the spacing), because the first lacks whitespace |
| 507 | * where the second has it; but "log_in" equals "log___in", because both |
| 508 | * strings have whitespace, albeit in different amounts, in the same |
| 509 | * place, and are otherwise the same. |
| 510 | */ |
| 511 | int lib_strequal_collapse_spaces(const char *a, size_t a_len, |
| 512 | const char *b, size_t b_len); |
| 513 | |
| 514 | |
| 515 | /* ------------------------------------------------------------------------ */ |
| 516 | /* |
| 517 | * Utility routine - compare UTF-8 strings with full Unicode case folding. |
| 518 | * Returns <0 if a<b, 0 if a==b, >0 if a>b. |
| 519 | * |
| 520 | * If 'bmatchlen' is non-null, string 'a' can match as a leading substring |
| 521 | * of string 'b'. If 'b' is identical to 'a' or contains 'a' as a leading |
| 522 | * substring, we'll return 0 to indicate a match, and fill in '*bmatchlen' |
| 523 | * with the number of bytes matched. |
| 524 | * |
| 525 | * If 'bmatchlen' is null, we'll do a straightforward string comparison, so |
| 526 | * a 0 return means the two strings match exactly. |
| 527 | */ |
| 528 | int t3_compare_case_fold( |
| 529 | const char *a, size_t alen, |
| 530 | const char *b, size_t blen, size_t *bmatchlen); |
| 531 | |
| 532 | /* compare a UTF-8 string against a wchar_t* string */ |
| 533 | int t3_compare_case_fold( |
| 534 | const wchar_t *a, size_t alen, |
| 535 | const char *b, size_t blen, size_t *bmatchlen); |
| 536 | |
| 537 | /* |
| 538 | * Compare the minimum portions of two UTF-8 strings with case folding. |
| 539 | * This compares the folded version of the first character of each string |
| 540 | * to the other. If they match, we advance the pointers and lengths past |
| 541 | * the matched text and return 0; otherwise we return < 0 if the first |
| 542 | * string sorts before the second, > 0 if the first sorts after the second. |
| 543 | * |
| 544 | * In the simplest case, this matches one character from each string. |
| 545 | * However, there are situations where the folded version of one character |
| 546 | * can correspond to two characters of source text in the other string, |
| 547 | * such as the German ess-zed: this will match "ss" in the source text in |
| 548 | * the other string, consuming only one character (the ess-zed) in one |
| 549 | * string but two ("ss") in the other. |
| 550 | */ |
| 551 | int t3_compare_case_fold_min(class utf8_ptr &a, size_t &alen, |
| 552 | class utf8_ptr &b, size_t &blen); |
| 553 | |
| 554 | int t3_compare_case_fold_min(class utf8_ptr &a, size_t &alen, |
| 555 | const wchar_t* &b, size_t &blen); |
| 556 | |
| 557 | int t3_compare_case_fold_min(const wchar_t* &a, size_t &alen, |
| 558 | const wchar_t* &b, size_t &blen); |
| 559 | |
| 560 | |
| 561 | /* ------------------------------------------------------------------------ */ |
| 562 | /* |
| 563 | * Find a version suffix in an identifier string. A version suffix |
| 564 | * starts with the given character. If we don't find the character, |
| 565 | * we'll return the default version suffix. In any case, we'll set |
| 566 | * name_len to the length of the name portion, excluding the version |
| 567 | * suffix and its leading separator. |
| 568 | * |
| 569 | * For example, with a '/' suffix, a versioned name string would look |
| 570 | * like "tads-gen/030000" - the name is "tads_gen" and the version is |
| 571 | * "030000". |
| 572 | */ |
| 573 | const char *lib_find_vsn_suffix(const char *name_string, char suffix_char, |
| 574 | const char *default_vsn, size_t *name_len); |
| 575 | |
| 576 | |
| 577 | /* ------------------------------------------------------------------------ */ |
| 578 | /* |
| 579 | * Unicode-compatible character classification functions. These |
| 580 | * functions accept any Unicode character, but classify all non-ASCII |
| 581 | * characters in the Unicode character set as unknown; hence, |
| 582 | * is_digit(ch) will always return false for any non-ASCII character, |
| 583 | * even if the character is considered a digit in the Unicode character |
| 584 | * set, and to_upper(ch) will return ch for any non-ASCII character, |
| 585 | * even if the character has a case conversion defined in the Unicode |
| 586 | * set. |
| 587 | * |
| 588 | * Use the t3_is_xxx() and t3_to_xxx() functions defined vmuni.h for |
| 589 | * classifications and conversions that operate over the entire Unicode |
| 590 | * character set. |
| 591 | */ |
| 592 | |
| 593 | /* determine if a character is an ASCII character */ |
| 594 | inline int is_ascii(wchar_t c) { return (((unsigned int)c) <= 127); } |
| 595 | |
| 596 | /* determine if a character is an ASCII space */ |
| 597 | inline int is_space(wchar_t c) { return (is_ascii(c) && isspace((char)c)); } |
| 598 | |
| 599 | /* determine if a character is an ASCII alphabetic character */ |
| 600 | inline int is_alpha(wchar_t c) { return (is_ascii(c) && isalpha((char)c)); } |
| 601 | |
| 602 | /* determine if a character is an ASCII numeric character */ |
| 603 | inline int is_digit(wchar_t c) { return (is_ascii(c) && isdigit((char)c)); } |
| 604 | |
| 605 | /* determine if a character is an ASCII octal numeric character */ |
| 606 | inline int is_odigit(wchar_t c) |
| 607 | { return (is_ascii(c) && isdigit((char)c) && c <= '7'); } |
| 608 | |
| 609 | /* determine if a character is an ASCII hex numeric character */ |
| 610 | inline int is_xdigit(wchar_t c) |
| 611 | { |
| 612 | return (is_ascii(c) |
| 613 | && (isdigit((char)c) |
| 614 | || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))); |
| 615 | } |
| 616 | |
| 617 | /* get the numeric value of a decimal digit character */ |
| 618 | inline int value_of_digit(wchar_t c) |
| 619 | { |
| 620 | return (int)(c - '0'); |
| 621 | } |
| 622 | |
| 623 | /* get the numeric value of an octal numeric character */ |
| 624 | inline int value_of_odigit(wchar_t c) |
| 625 | { |
| 626 | return (int)(c - '0'); |
| 627 | } |
| 628 | |
| 629 | /* get the numeric value of a hex numeric character */ |
| 630 | inline int value_of_xdigit(wchar_t c) |
| 631 | { |
| 632 | /* |
| 633 | * since our internal characters are always in unicode, we can take |
| 634 | * advantage of the order of unicode characters to reduce the number |
| 635 | * of comparisons we must make here |
| 636 | */ |
| 637 | return (int)(c >= 'a' |
| 638 | ? c - 'a' + 10 |
| 639 | : c >= 'A' ? c - 'A' + 10 |
| 640 | : c - '0'); |
| 641 | } |
| 642 | |
| 643 | /* convert a number 0-15 to a hex digit */ |
| 644 | inline char int_to_xdigit(int i) |
| 645 | { |
| 646 | return ((i >= 0 && i < 10) ? '0' + i : |
| 647 | (i >= 10 && i < 16 ) ? 'A' + i - 10 : |
| 648 | '?'); |
| 649 | } |
| 650 | |
| 651 | /* convert a byte to a pair of hex digits */ |
| 652 | inline void byte_to_xdigits(char *buf, unsigned char b) |
| 653 | { |
| 654 | buf[0] = int_to_xdigit((b >> 4) & 0x0F); |
| 655 | buf[1] = int_to_xdigit(b & 0x0F); |
| 656 | } |
| 657 | |
| 658 | /* determine if a character is a symbol initial character */ |
| 659 | inline int is_syminit(wchar_t c) |
| 660 | { |
| 661 | /* underscores and alphabetic characters can start symbols */ |
| 662 | return (is_ascii(c) && (c == '_' || isalpha((char)c))); |
| 663 | } |
| 664 | |
| 665 | /* determine if a character is a symbol non-initial character */ |
| 666 | inline int is_sym(wchar_t c) |
| 667 | { |
| 668 | /* underscores, alphabetics, and digits can be in symbols */ |
| 669 | return (is_ascii(c) && (c == '_' || isalpha((char)c) |
| 670 | || isdigit((char)c))); |
| 671 | } |
| 672 | |
| 673 | /* determine if a character is ASCII lower-case */ |
| 674 | inline int is_lower(wchar_t c) { return (is_ascii(c) && islower((char)c)); } |
| 675 | |
| 676 | /* convert ASCII lower-case to upper-case */ |
| 677 | inline wchar_t to_upper(wchar_t c) |
| 678 | { |
| 679 | return (is_ascii(c) ? toupper((char)c) : c); |
| 680 | } |
| 681 | |
| 682 | inline wchar_t to_lower(wchar_t c) |
| 683 | { |
| 684 | return (is_ascii(c) ? tolower((char)c) : c); |
| 685 | } |
| 686 | |
| 687 | /* convert a string to lower case */ |
| 688 | void t3strlwr(char *p); |
| 689 | |
| 690 | /* ------------------------------------------------------------------------ */ |
| 691 | /* |
| 692 | * sprintf and vsprintf replacements. These versions provide subsets of |
| 693 | * the full 'printf' format capabilities, but check for buffer overflow, |
| 694 | * which the standard library's sprintf functions do not. |
| 695 | * |
| 696 | * NB: the 'args' parameter is effectively const, even though it's not |
| 697 | * declared as such. That is, you can safely call t3vsprintf multiple |
| 698 | * times with the same 'args' parameter without worrying that the contents |
| 699 | * will be changed on platforms where va_list is a reference type. (It's |
| 700 | * not declared const due to an implementation detail, specifically that |
| 701 | * the routine internally needs to make a private copy with va_copy(), |
| 702 | * which doesn't accept a const source value.) |
| 703 | */ |
| 704 | size_t t3sprintf(char *buf, size_t buflen, const char *fmt, ...); |
| 705 | size_t t3vsprintf(char *buf, size_t buflen, const char *fmt, va_list args); |
| 706 | |
| 707 | /* |
| 708 | * Automatic memory allocation versions of sprintf and vsprintf: we'll |
| 709 | * measure the actual space needed, allocate a buffer, format the message |
| 710 | * into the buffer, and return the allocated buffer pointer. The caller is |
| 711 | * responsible for freeing the returned buffer via t3free(). |
| 712 | */ |
| 713 | char *t3sprintf_alloc(const char *fmt, ...); |
| 714 | char *t3vsprintf_alloc(const char *fmt, va_list args); |
| 715 | |
| 716 | |
| 717 | /* ------------------------------------------------------------------------ */ |
| 718 | /* |
| 719 | * Basic heap allocation functions. We don't call malloc and free |
| 720 | * directly, but use our own cover functions; when we compile the system |
| 721 | * for debugging, we use diagnostic memory allocators so that we can more |
| 722 | * easily find memory mismanagement errors (such as leaks, multiple |
| 723 | * deletes, and use after deletion). |
| 724 | */ |
| 725 | |
| 726 | #define T3MALLOC_TYPE_MALLOC 1 |
| 727 | #define T3MALLOC_TYPE_NEW 2 |
| 728 | #define T3MALLOC_TYPE_NEWARR 3 |
| 729 | |
| 730 | #ifdef T3_DEBUG |
| 731 | |
| 732 | /* |
| 733 | * Compiling in debug mode - use our diagnostic heap functions. |
| 734 | * Override C++ operators new, new[], delete, and delete[] as well, so |
| 735 | * that we can handle those allocations through our diagnostic heap |
| 736 | * manager, too. |
| 737 | */ |
| 738 | |
| 739 | void *t3malloc(size_t siz, int alloc_type); |
| 740 | void *t3realloc(void *oldptr, size_t siz); |
| 741 | void t3free(void *ptr, int alloc_type); |
| 742 | |
| 743 | inline void *t3malloc(size_t siz) |
| 744 | { return t3malloc(siz, T3MALLOC_TYPE_MALLOC); } |
| 745 | inline void *t3mallocnew(size_t siz) |
| 746 | { return t3malloc(siz, T3MALLOC_TYPE_NEW); } |
| 747 | inline void t3free(void *ptr) |
| 748 | { t3free(ptr, T3MALLOC_TYPE_MALLOC); } |
| 749 | |
| 750 | void *operator new(size_t siz) SYSTHROW(throw (std::bad_alloc)); |
| 751 | void *operator new[](size_t siz) SYSTHROW(throw (std::bad_alloc)); |
| 752 | void operator delete(void *ptr) SYSTHROW(throw ()); |
| 753 | void operator delete[](void *ptr) SYSTHROW(throw ()); |
| 754 | |
| 755 | /* |
| 756 | * List all allocated memory blocks - displays heap information on stdout. |
| 757 | * This can be called at program termination to detect un-freed memory |
| 758 | * blocks, the existence of which could indicate a memory leak. |
| 759 | * |
| 760 | * If cb is provided, we'll display output through the given callback |
| 761 | * function; otherwise we'll display the output directly on stderr. |
| 762 | */ |
| 763 | void t3_list_memory_blocks(void (*cb)(const char *msg)); |
| 764 | |
| 765 | #else /* T3_DEBUG */ |
| 766 | |
| 767 | /* |
| 768 | * Compiling in production mode - use the system memory allocators |
| 769 | * directly. Note that we go through the osmalloc() et. al. functions |
| 770 | * rather than calling malloc() directly, so that individual ports can |
| 771 | * use customized memory management where necessary or desirable. |
| 772 | */ |
| 773 | #define t3malloc(siz) (::osmalloc(siz)) |
| 774 | #define t3mallocnew(siz) (::osmalloc(siz)) |
| 775 | #define t3realloc(ptr, siz) (::osrealloc(ptr, siz)) |
| 776 | #define t3free(ptr) (::osfree(ptr)) |
| 777 | |
| 778 | #define t3_list_memory_blocks(cb) |
| 779 | |
| 780 | #endif /* T3_DEBUG */ |
| 781 | |
| 782 | |
| 783 | /* ------------------------------------------------------------------------ */ |
| 784 | /* |
| 785 | * A simple array list type. We keep an underlying array of elements, |
| 786 | * automatically expanding the underlying array as needed to accomodate new |
| 787 | * elements. |
| 788 | */ |
| 789 | |
| 790 | /* array list element type codes */ |
| 791 | #define ARRAY_LIST_ELE_INT 1 |
| 792 | #define ARRAY_LIST_ELE_LONG 2 |
| 793 | #define ARRAY_LIST_ELE_PTR 3 |
| 794 | |
| 795 | /* array list element - we can store various types here */ |
| 796 | union array_list_ele_t |
| 797 | { |
| 798 | array_list_ele_t(int i) { intval = i; } |
| 799 | array_list_ele_t(long l) { longval = l; } |
| 800 | array_list_ele_t(void *p) { ptrval = p; } |
| 801 | |
| 802 | int intval; |
| 803 | long longval; |
| 804 | void *ptrval; |
| 805 | |
| 806 | /* compare to a given value for equality */ |
| 807 | int equals(array_list_ele_t other, int typ) |
| 808 | { |
| 809 | return ((typ == ARRAY_LIST_ELE_INT && intval == other.intval) |
| 810 | || (typ == ARRAY_LIST_ELE_LONG && longval == other.longval) |
| 811 | || (typ == ARRAY_LIST_ELE_PTR && ptrval == other.ptrval)); |
| 812 | } |
| 813 | }; |
| 814 | |
| 815 | /* |
| 816 | * The array list type |
| 817 | */ |
| 818 | class CArrayList |
| 819 | { |
| 820 | public: |
| 821 | CArrayList() |
| 822 | { |
| 823 | /* we have nothing allocated yet */ |
| 824 | arr_ = 0; |
| 825 | cnt_ = 0; |
| 826 | |
| 827 | /* use default initial size and increment */ |
| 828 | alloc_ = 16; |
| 829 | inc_siz_ = 16; |
| 830 | } |
| 831 | CArrayList(size_t init_cnt, size_t inc_siz) |
| 832 | { |
| 833 | /* we have nothing allocated yet */ |
| 834 | arr_ = 0; |
| 835 | cnt_ = 0; |
| 836 | |
| 837 | /* remember the initial size and increment */ |
| 838 | alloc_ = init_cnt; |
| 839 | inc_siz_ = inc_siz; |
| 840 | } |
| 841 | |
| 842 | virtual ~CArrayList() |
| 843 | { |
| 844 | /* delete our underlying array */ |
| 845 | free_mem(arr_); |
| 846 | } |
| 847 | |
| 848 | /* get the number of elements in the array */ |
| 849 | size_t get_count() const { return cnt_; } |
| 850 | |
| 851 | /* get the element at the given index (no error checking) */ |
| 852 | int get_ele_int(size_t idx) const { return arr_[idx].intval; } |
| 853 | long get_ele_long(size_t idx) const { return arr_[idx].longval; } |
| 854 | void *get_ele_ptr(size_t idx) const { return arr_[idx].ptrval; } |
| 855 | |
| 856 | /* find an element's index; returns -1 if not found */ |
| 857 | int find_ele(int i) const |
| 858 | { return find_ele(array_list_ele_t(i), ARRAY_LIST_ELE_INT); } |
| 859 | int find_ele(long l) const |
| 860 | { return find_ele(array_list_ele_t(l), ARRAY_LIST_ELE_LONG); } |
| 861 | int find_ele(void *p) const |
| 862 | { return find_ele(array_list_ele_t(p), ARRAY_LIST_ELE_PTR); } |
| 863 | |
| 864 | /* find an element's index; returns -1 if not found */ |
| 865 | int find_ele(array_list_ele_t ele, int typ) const |
| 866 | { |
| 867 | size_t i; |
| 868 | array_list_ele_t *p; |
| 869 | |
| 870 | /* scan for the element */ |
| 871 | for (i = 0, p = arr_ ; i < cnt_ ; ++i, ++p) |
| 872 | { |
| 873 | /* if this is the element, return the index */ |
| 874 | if (p->equals(ele, typ)) |
| 875 | return (int)i; |
| 876 | } |
| 877 | |
| 878 | /* didn't find it */ |
| 879 | return -1; |
| 880 | } |
| 881 | |
| 882 | /* add a new element */ |
| 883 | void add_ele(int i) { add_ele(array_list_ele_t(i)); } |
| 884 | void add_ele(long l) { add_ele(array_list_ele_t(l)); } |
| 885 | void add_ele(void *p) { add_ele(array_list_ele_t(p)); } |
| 886 | |
| 887 | /* add a new element */ |
| 888 | void add_ele(array_list_ele_t ele) |
| 889 | { |
| 890 | /* expand the array if necessary */ |
| 891 | if (arr_ == 0) |
| 892 | { |
| 893 | /* we don't have an array yet, so allocate at the initial size */ |
| 894 | init(); |
| 895 | } |
| 896 | if (cnt_ >= alloc_) |
| 897 | { |
| 898 | /* allocate at the new size */ |
| 899 | arr_ = (array_list_ele_t *) |
| 900 | realloc_mem(arr_, alloc_ * sizeof(arr_[0]), |
| 901 | (alloc_ + inc_siz_) * sizeof(arr_[0])); |
| 902 | |
| 903 | /* remember the new size */ |
| 904 | alloc_ += inc_siz_; |
| 905 | } |
| 906 | |
| 907 | /* add the new element */ |
| 908 | arr_[cnt_++] = ele; |
| 909 | } |
| 910 | |
| 911 | /* remove one element by value; returns true if found, false if not */ |
| 912 | void remove_ele(int i) |
| 913 | { remove_ele(array_list_ele_t(i), ARRAY_LIST_ELE_INT); } |
| 914 | void remove_ele(long l) |
| 915 | { remove_ele(array_list_ele_t(l), ARRAY_LIST_ELE_LONG); } |
| 916 | void remove_ele(void *p) |
| 917 | { remove_ele(array_list_ele_t(p), ARRAY_LIST_ELE_PTR); } |
| 918 | |
| 919 | /* remove one element by value; returns true if found, false if not */ |
| 920 | int remove_ele(array_list_ele_t ele, int typ) |
| 921 | { |
| 922 | size_t i; |
| 923 | array_list_ele_t *p; |
| 924 | |
| 925 | /* scan for the element */ |
| 926 | for (i = 0, p = arr_ ; i < cnt_ ; ++i, ++p) |
| 927 | { |
| 928 | /* if this is the element, remove it */ |
| 929 | if (p->equals(ele, typ)) |
| 930 | { |
| 931 | /* remove the element at this index */ |
| 932 | remove_ele(i); |
| 933 | |
| 934 | /* indicate that we found the element */ |
| 935 | return TRUE; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | /* we didn't find the element */ |
| 940 | return FALSE; |
| 941 | } |
| 942 | |
| 943 | /* remove the element at the given index */ |
| 944 | void remove_ele(size_t idx) |
| 945 | { |
| 946 | array_list_ele_t *p; |
| 947 | |
| 948 | /* move each following element down one slot */ |
| 949 | for (p = arr_ + idx, ++idx ; idx < cnt_ ; ++idx, ++p) |
| 950 | *p = *(p + 1); |
| 951 | |
| 952 | /* reduce the in-use count */ |
| 953 | --cnt_; |
| 954 | } |
| 955 | |
| 956 | /* clear the entire list */ |
| 957 | void clear() { cnt_ = 0; } |
| 958 | |
| 959 | protected: |
| 960 | /* |
| 961 | * Initialize. This is called to set up the array at the initial size, |
| 962 | * stored in alloc_, when we first need memory. Note that we defer |
| 963 | * this until we actually need the memory for two reasons. First, we |
| 964 | * can't call it from the constructor, because the vtable won't be |
| 965 | * built at construction, and we need to call the virtual alloc_mem(). |
| 966 | * Second, by waiting, we ensure that we won't allocate any memory if |
| 967 | * our list is never actually needed. |
| 968 | */ |
| 969 | void init() |
| 970 | { |
| 971 | /* allocate the array */ |
| 972 | arr_ = (array_list_ele_t *)alloc_mem(alloc_ * sizeof(arr_[0])); |
| 973 | } |
| 974 | |
| 975 | /* memory management */ |
| 976 | virtual void *alloc_mem(size_t siz) |
| 977 | { return t3malloc(siz); } |
| 978 | virtual void *realloc_mem(void *p, size_t oldsiz, size_t newsiz) |
| 979 | { return t3realloc(p, newsiz); } |
| 980 | virtual void free_mem(void *p) |
| 981 | { t3free(p); } |
| 982 | |
| 983 | /* our array of elements */ |
| 984 | array_list_ele_t *arr_; |
| 985 | |
| 986 | /* number of elements allocated */ |
| 987 | size_t alloc_; |
| 988 | |
| 989 | /* number of elements currently in use */ |
| 990 | size_t cnt_; |
| 991 | |
| 992 | /* increment size */ |
| 993 | size_t inc_siz_; |
| 994 | }; |
| 995 | |
| 996 | #endif /* T3_STD_INCLUDED */ |
| 997 |