| OLD | NEW |
| 1 /************************************************************************* | 1 /************************************************************************* |
| 2 * | 2 * |
| 3 * $Id$ | 3 * $Id$ |
| 4 * | 4 * |
| 5 * Copyright (C) 2001 Bjorn Reese and Daniel Stenberg. | 5 * Copyright (C) 2001 Bjorn Reese and Daniel Stenberg. |
| 6 * | 6 * |
| 7 * Permission to use, copy, modify, and distribute this software for any | 7 * Permission to use, copy, modify, and distribute this software for any |
| 8 * purpose with or without fee is hereby granted, provided that the above | 8 * purpose with or without fee is hereby granted, provided that the above |
| 9 * copyright notice and this permission notice appear in all copies. | 9 * copyright notice and this permission notice appear in all copies. |
| 10 * | 10 * |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 # define NIL ((char)0) | 45 # define NIL ((char)0) |
| 46 #endif | 46 #endif |
| 47 #if !defined(FALSE) | 47 #if !defined(FALSE) |
| 48 # define FALSE (1 == 0) | 48 # define FALSE (1 == 0) |
| 49 # define TRUE (! FALSE) | 49 # define TRUE (! FALSE) |
| 50 #endif | 50 #endif |
| 51 #if !defined(BOOLEAN_T) | 51 #if !defined(BOOLEAN_T) |
| 52 # define BOOLEAN_T int | 52 # define BOOLEAN_T int |
| 53 #endif | 53 #endif |
| 54 | 54 |
| 55 #if defined(TRIO_COMPILER_SUPPORTS_C99) | 55 #ifdef __VMS |
| 56 # define USE_STRTOD |
| 57 #elif defined(TRIO_COMPILER_SUPPORTS_C99) |
| 56 # define USE_STRTOD | 58 # define USE_STRTOD |
| 57 # define USE_STRTOF | 59 # define USE_STRTOF |
| 58 #elif defined(TRIO_COMPILER_MSVC) | 60 #elif defined(TRIO_COMPILER_MSVC) |
| 59 # define USE_STRTOD | 61 # define USE_STRTOD |
| 60 #endif | 62 #endif |
| 61 | 63 |
| 62 #if defined(TRIO_PLATFORM_UNIX) | 64 #if defined(TRIO_PLATFORM_UNIX) |
| 63 # define USE_STRCASECMP | 65 # define USE_STRCASECMP |
| 64 # define USE_STRNCASECMP | 66 # define USE_STRNCASECMP |
| 65 # if defined(TRIO_PLATFORM_SUNOS) | 67 # if defined(TRIO_PLATFORM_SUNOS) |
| 66 # define USE_SYS_ERRLIST | 68 # define USE_SYS_ERRLIST |
| 67 # else | 69 # else |
| 68 # define USE_STRERROR | 70 # define USE_STRERROR |
| 69 # endif | 71 # endif |
| 70 # if defined(TRIO_PLATFORM_QNX) | 72 # if defined(TRIO_PLATFORM_QNX) |
| 71 # define strcasecmp(x,y) stricmp(x,y) | 73 # define strcasecmp(x,y) stricmp(x,y) |
| 72 # define strncasecmp(x,y,n) strnicmp(x,y,n) | 74 # define strncasecmp(x,y,n) strnicmp(x,y,n) |
| 73 # endif | 75 # endif |
| 74 #elif defined(TRIO_PLATFORM_WIN32) | 76 #elif defined(TRIO_PLATFORM_WIN32) |
| 75 # define USE_STRCASECMP | 77 # define USE_STRCASECMP |
| 76 # if defined(_WIN32_WCE) | 78 # if defined(_WIN32_WCE) |
| 77 # define strcasecmp(x,y) _stricmp(x,y) | 79 # define strcasecmp(x,y) _stricmp(x,y) |
| 78 # else | 80 # else |
| 79 # define strcasecmp(x,y) strcmpi(x,y) | 81 # define strcasecmp(x,y) strcmpi(x,y) |
| 80 # endif | 82 # endif |
| 83 #elif defined(TRIO_PLATFORM_OS400) |
| 84 # define USE_STRCASECMP |
| 85 # define USE_STRNCASECMP |
| 86 # include <strings.h> |
| 81 #endif | 87 #endif |
| 82 | 88 |
| 83 #if !(defined(TRIO_PLATFORM_SUNOS)) | 89 #if !(defined(TRIO_PLATFORM_SUNOS)) |
| 84 # define USE_TOLOWER | 90 # define USE_TOLOWER |
| 85 # define USE_TOUPPER | 91 # define USE_TOUPPER |
| 86 #endif | 92 #endif |
| 87 | 93 |
| 88 /************************************************************************* | 94 /************************************************************************* |
| 89 * Structures | 95 * Structures |
| 90 */ | 96 */ |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 TRIO_ARGS1((string), | 164 TRIO_ARGS1((string), |
| 159 TRIO_CONST char *string) | 165 TRIO_CONST char *string) |
| 160 { | 166 { |
| 161 return strlen(string); | 167 return strlen(string); |
| 162 } | 168 } |
| 163 | 169 |
| 164 | 170 |
| 165 #if !defined(TRIO_MINIMAL) | 171 #if !defined(TRIO_MINIMAL) |
| 166 /** | 172 /** |
| 167 Append @p source at the end of @p target. | 173 Append @p source at the end of @p target. |
| 168 | 174 |
| 169 @param target Target string. | 175 @param target Target string. |
| 170 @param source Source string. | 176 @param source Source string. |
| 171 @return Boolean value indicating success or failure. | 177 @return Boolean value indicating success or failure. |
| 172 | 178 |
| 173 @pre @p target must point to a memory chunk with sufficient room to | 179 @pre @p target must point to a memory chunk with sufficient room to |
| 174 contain the @p target string and @p source string. | 180 contain the @p target string and @p source string. |
| 175 @pre No boundary checking is performed, so insufficient memory will | 181 @pre No boundary checking is performed, so insufficient memory will |
| 176 result in a buffer overrun. | 182 result in a buffer overrun. |
| 177 @post @p target will be zero terminated. | 183 @post @p target will be zero terminated. |
| 178 */ | 184 */ |
| 179 TRIO_STRING_PUBLIC int | 185 TRIO_STRING_PUBLIC int |
| 180 trio_append | 186 trio_append |
| 181 TRIO_ARGS2((target, source), | 187 TRIO_ARGS2((target, source), |
| 182 char *target, | 188 char *target, |
| 183 TRIO_CONST char *source) | 189 TRIO_CONST char *source) |
| 184 { | 190 { |
| 185 assert(target); | 191 assert(target); |
| 186 assert(source); | 192 assert(source); |
| 187 | 193 |
| 188 return (strcat(target, source) != NULL); | 194 return (strcat(target, source) != NULL); |
| 189 } | 195 } |
| 190 #endif /* !defined(TRIO_MINIMAL) */ | 196 #endif /* !defined(TRIO_MINIMAL) */ |
| 191 | 197 |
| 192 #if !defined(TRIO_MINIMAL) | 198 #if !defined(TRIO_MINIMAL) |
| 193 /** | 199 /** |
| 194 Append at most @p max characters from @p source to @p target. | 200 Append at most @p max characters from @p source to @p target. |
| 195 | 201 |
| 196 @param target Target string. | 202 @param target Target string. |
| 197 @param max Maximum number of characters to append. | 203 @param max Maximum number of characters to append. |
| 198 @param source Source string. | 204 @param source Source string. |
| 199 @return Boolean value indicating success or failure. | 205 @return Boolean value indicating success or failure. |
| 200 | 206 |
| 201 @pre @p target must point to a memory chuck with sufficient room to | 207 @pre @p target must point to a memory chuck with sufficient room to |
| 202 contain the @p target string and the @p source string (at most @p max | 208 contain the @p target string and the @p source string (at most @p max |
| 203 characters). | 209 characters). |
| 204 @pre No boundary checking is performed, so insufficient memory will | 210 @pre No boundary checking is performed, so insufficient memory will |
| 205 result in a buffer overrun. | 211 result in a buffer overrun. |
| 206 @post @p target will be zero terminated. | 212 @post @p target will be zero terminated. |
| 207 */ | 213 */ |
| 208 TRIO_STRING_PUBLIC int | 214 TRIO_STRING_PUBLIC int |
| 209 trio_append_max | 215 trio_append_max |
| 210 TRIO_ARGS3((target, max, source), | 216 TRIO_ARGS3((target, max, source), |
| 211 char *target, | 217 char *target, |
| 212 size_t max, | 218 size_t max, |
| 213 TRIO_CONST char *source) | 219 TRIO_CONST char *source) |
| 214 { | 220 { |
| 215 size_t length; | 221 size_t length; |
| 216 | 222 |
| 217 assert(target); | 223 assert(target); |
| 218 assert(source); | 224 assert(source); |
| 219 | 225 |
| 220 length = trio_length(target); | 226 length = trio_length(target); |
| 221 | 227 |
| 222 if (max > length) | 228 if (max > length) |
| 223 { | 229 { |
| 224 strncat(target, source, max - length - 1); | 230 strncat(target, source, max - length - 1); |
| 225 } | 231 } |
| 226 return TRUE; | 232 return TRUE; |
| 227 } | 233 } |
| 228 #endif /* !defined(TRIO_MINIMAL) */ | 234 #endif /* !defined(TRIO_MINIMAL) */ |
| 229 | 235 |
| 230 | 236 |
| 231 #if !defined(TRIO_MINIMAL) | 237 #if !defined(TRIO_MINIMAL) |
| 232 /** | 238 /** |
| 233 Determine if a string contains a substring. | 239 Determine if a string contains a substring. |
| 234 | 240 |
| 235 @param string String to be searched. | 241 @param string String to be searched. |
| 236 @param substring String to be found. | 242 @param substring String to be found. |
| 237 @return Boolean value indicating success or failure. | 243 @return Boolean value indicating success or failure. |
| 238 */ | 244 */ |
| 239 TRIO_STRING_PUBLIC int | 245 TRIO_STRING_PUBLIC int |
| 240 trio_contains | 246 trio_contains |
| 241 TRIO_ARGS2((string, substring), | 247 TRIO_ARGS2((string, substring), |
| 242 TRIO_CONST char *string, | 248 TRIO_CONST char *string, |
| 243 TRIO_CONST char *substring) | 249 TRIO_CONST char *substring) |
| 244 { | 250 { |
| 245 assert(string); | 251 assert(string); |
| 246 assert(substring); | 252 assert(substring); |
| 247 | 253 |
| 248 return (0 != strstr(string, substring)); | 254 return (0 != strstr(string, substring)); |
| 249 } | 255 } |
| 250 #endif /* !defined(TRIO_MINIMAL) */ | 256 #endif /* !defined(TRIO_MINIMAL) */ |
| 251 | 257 |
| 252 | 258 |
| 253 #if !defined(TRIO_MINIMAL) | 259 #if !defined(TRIO_MINIMAL) |
| 254 /** | 260 /** |
| 255 Copy @p source to @p target. | 261 Copy @p source to @p target. |
| 256 | 262 |
| 257 @param target Target string. | 263 @param target Target string. |
| 258 @param source Source string. | 264 @param source Source string. |
| 259 @return Boolean value indicating success or failure. | 265 @return Boolean value indicating success or failure. |
| 260 | 266 |
| 261 @pre @p target must point to a memory chunk with sufficient room to | 267 @pre @p target must point to a memory chunk with sufficient room to |
| 262 contain the @p source string. | 268 contain the @p source string. |
| 263 @pre No boundary checking is performed, so insufficient memory will | 269 @pre No boundary checking is performed, so insufficient memory will |
| 264 result in a buffer overrun. | 270 result in a buffer overrun. |
| 265 @post @p target will be zero terminated. | 271 @post @p target will be zero terminated. |
| 266 */ | 272 */ |
| 267 TRIO_STRING_PUBLIC int | 273 TRIO_STRING_PUBLIC int |
| 268 trio_copy | 274 trio_copy |
| 269 TRIO_ARGS2((target, source), | 275 TRIO_ARGS2((target, source), |
| 270 char *target, | 276 char *target, |
| 271 TRIO_CONST char *source) | 277 TRIO_CONST char *source) |
| 272 { | 278 { |
| 273 assert(target); | 279 assert(target); |
| 274 assert(source); | 280 assert(source); |
| 275 | 281 |
| 276 (void)strcpy(target, source); | 282 (void)strcpy(target, source); |
| 277 return TRUE; | 283 return TRUE; |
| 278 } | 284 } |
| 279 #endif /* !defined(TRIO_MINIMAL) */ | 285 #endif /* !defined(TRIO_MINIMAL) */ |
| 280 | 286 |
| 281 | 287 |
| 282 /** | 288 /** |
| 283 Copy at most @p max characters from @p source to @p target. | 289 Copy at most @p max characters from @p source to @p target. |
| 284 | 290 |
| 285 @param target Target string. | 291 @param target Target string. |
| 286 @param max Maximum number of characters to append. | 292 @param max Maximum number of characters to append. |
| 287 @param source Source string. | 293 @param source Source string. |
| 288 @return Boolean value indicating success or failure. | 294 @return Boolean value indicating success or failure. |
| 289 | 295 |
| 290 @pre @p target must point to a memory chunk with sufficient room to | 296 @pre @p target must point to a memory chunk with sufficient room to |
| 291 contain the @p source string (at most @p max characters). | 297 contain the @p source string (at most @p max characters). |
| 292 @pre No boundary checking is performed, so insufficient memory will | 298 @pre No boundary checking is performed, so insufficient memory will |
| 293 result in a buffer overrun. | 299 result in a buffer overrun. |
| 294 @post @p target will be zero terminated. | 300 @post @p target will be zero terminated. |
| 295 */ | 301 */ |
| 296 TRIO_STRING_PUBLIC int | 302 TRIO_STRING_PUBLIC int |
| 297 trio_copy_max | 303 trio_copy_max |
| 298 TRIO_ARGS3((target, max, source), | 304 TRIO_ARGS3((target, max, source), |
| 299 char *target, | 305 char *target, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 329 if (target) | 335 if (target) |
| 330 { | 336 { |
| 331 trio_copy_max(target, size, source); | 337 trio_copy_max(target, size, source); |
| 332 } | 338 } |
| 333 return target; | 339 return target; |
| 334 } | 340 } |
| 335 | 341 |
| 336 | 342 |
| 337 /** | 343 /** |
| 338 Duplicate @p source. | 344 Duplicate @p source. |
| 339 | 345 |
| 340 @param source Source string. | 346 @param source Source string. |
| 341 @return A copy of the @p source string. | 347 @return A copy of the @p source string. |
| 342 | 348 |
| 343 @post @p target will be zero terminated. | 349 @post @p target will be zero terminated. |
| 344 */ | 350 */ |
| 345 TRIO_STRING_PUBLIC char * | 351 TRIO_STRING_PUBLIC char * |
| 346 trio_duplicate | 352 trio_duplicate |
| 347 TRIO_ARGS1((source), | 353 TRIO_ARGS1((source), |
| 348 TRIO_CONST char *source) | 354 TRIO_CONST char *source) |
| 349 { | 355 { |
| 350 return TrioDuplicateMax(source, trio_length(source)); | 356 return TrioDuplicateMax(source, trio_length(source)); |
| 351 } | 357 } |
| 352 | 358 |
| 353 | 359 |
| 354 #if !defined(TRIO_MINIMAL) | 360 #if !defined(TRIO_MINIMAL) |
| 355 /** | 361 /** |
| 356 Duplicate at most @p max characters of @p source. | 362 Duplicate at most @p max characters of @p source. |
| 357 | 363 |
| 358 @param source Source string. | 364 @param source Source string. |
| 359 @param max Maximum number of characters to duplicate. | 365 @param max Maximum number of characters to duplicate. |
| 360 @return A copy of the @p source string. | 366 @return A copy of the @p source string. |
| 361 | 367 |
| 362 @post @p target will be zero terminated. | 368 @post @p target will be zero terminated. |
| 363 */ | 369 */ |
| 364 TRIO_STRING_PUBLIC char * | 370 TRIO_STRING_PUBLIC char * |
| 365 trio_duplicate_max TRIO_ARGS2((source, max), | 371 trio_duplicate_max TRIO_ARGS2((source, max), |
| 366 TRIO_CONST char *source, | 372 TRIO_CONST char *source, |
| 367 size_t max) | 373 size_t max) |
| 368 { | 374 { |
| 369 size_t length; | 375 size_t length; |
| 370 | 376 |
| 371 assert(source); | 377 assert(source); |
| 372 assert(max > 0); | 378 assert(max > 0); |
| 373 | 379 |
| 374 length = trio_length(source); | 380 length = trio_length(source); |
| 375 if (length > max) | 381 if (length > max) |
| 376 { | 382 { |
| 377 length = max; | 383 length = max; |
| 378 } | 384 } |
| 379 return TrioDuplicateMax(source, length); | 385 return TrioDuplicateMax(source, length); |
| 380 } | 386 } |
| 381 #endif /* !defined(TRIO_MINIMAL) */ | 387 #endif /* !defined(TRIO_MINIMAL) */ |
| 382 | 388 |
| 383 | 389 |
| 384 /** | 390 /** |
| 385 Compare if two strings are equal. | 391 Compare if two strings are equal. |
| 386 | 392 |
| 387 @param first First string. | 393 @param first First string. |
| 388 @param second Second string. | 394 @param second Second string. |
| 389 @return Boolean indicating whether the two strings are equal or not. | 395 @return Boolean indicating whether the two strings are equal or not. |
| 390 | 396 |
| 391 Case-insensitive comparison. | 397 Case-insensitive comparison. |
| 392 */ | 398 */ |
| 393 TRIO_STRING_PUBLIC int | 399 TRIO_STRING_PUBLIC int |
| 394 trio_equal | 400 trio_equal |
| 395 TRIO_ARGS2((first, second), | 401 TRIO_ARGS2((first, second), |
| 396 TRIO_CONST char *first, | 402 TRIO_CONST char *first, |
| 397 TRIO_CONST char *second) | 403 TRIO_CONST char *second) |
| 398 { | 404 { |
| 399 assert(first); | 405 assert(first); |
| 400 assert(second); | 406 assert(second); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 415 } | 421 } |
| 416 return ((*first == NIL) && (*second == NIL)); | 422 return ((*first == NIL) && (*second == NIL)); |
| 417 #endif | 423 #endif |
| 418 } | 424 } |
| 419 return FALSE; | 425 return FALSE; |
| 420 } | 426 } |
| 421 | 427 |
| 422 | 428 |
| 423 /** | 429 /** |
| 424 Compare if two strings are equal. | 430 Compare if two strings are equal. |
| 425 | 431 |
| 426 @param first First string. | 432 @param first First string. |
| 427 @param second Second string. | 433 @param second Second string. |
| 428 @return Boolean indicating whether the two strings are equal or not. | 434 @return Boolean indicating whether the two strings are equal or not. |
| 429 | 435 |
| 430 Case-sensitive comparison. | 436 Case-sensitive comparison. |
| 431 */ | 437 */ |
| 432 TRIO_STRING_PUBLIC int | 438 TRIO_STRING_PUBLIC int |
| 433 trio_equal_case | 439 trio_equal_case |
| 434 TRIO_ARGS2((first, second), | 440 TRIO_ARGS2((first, second), |
| 435 TRIO_CONST char *first, | 441 TRIO_CONST char *first, |
| 436 TRIO_CONST char *second) | 442 TRIO_CONST char *second) |
| 437 { | 443 { |
| 438 assert(first); | 444 assert(first); |
| 439 assert(second); | 445 assert(second); |
| 440 | 446 |
| 441 if ((first != NULL) && (second != NULL)) | 447 if ((first != NULL) && (second != NULL)) |
| 442 { | 448 { |
| 443 return (0 == strcmp(first, second)); | 449 return (0 == strcmp(first, second)); |
| 444 } | 450 } |
| 445 return FALSE; | 451 return FALSE; |
| 446 } | 452 } |
| 447 | 453 |
| 448 | 454 |
| 449 #if !defined(TRIO_MINIMAL) | 455 #if !defined(TRIO_MINIMAL) |
| 450 /** | 456 /** |
| 451 Compare if two strings up until the first @p max characters are equal. | 457 Compare if two strings up until the first @p max characters are equal. |
| 452 | 458 |
| 453 @param first First string. | 459 @param first First string. |
| 454 @param max Maximum number of characters to compare. | 460 @param max Maximum number of characters to compare. |
| 455 @param second Second string. | 461 @param second Second string. |
| 456 @return Boolean indicating whether the two strings are equal or not. | 462 @return Boolean indicating whether the two strings are equal or not. |
| 457 | 463 |
| 458 Case-sensitive comparison. | 464 Case-sensitive comparison. |
| 459 */ | 465 */ |
| 460 TRIO_STRING_PUBLIC int | 466 TRIO_STRING_PUBLIC int |
| 461 trio_equal_case_max | 467 trio_equal_case_max |
| 462 TRIO_ARGS3((first, max, second), | 468 TRIO_ARGS3((first, max, second), |
| 463 TRIO_CONST char *first, | 469 TRIO_CONST char *first, |
| 464 size_t max, | 470 size_t max, |
| 465 TRIO_CONST char *second) | 471 TRIO_CONST char *second) |
| 466 { | 472 { |
| 467 assert(first); | 473 assert(first); |
| 468 assert(second); | 474 assert(second); |
| 469 | 475 |
| 470 if ((first != NULL) && (second != NULL)) | 476 if ((first != NULL) && (second != NULL)) |
| 471 { | 477 { |
| 472 return (0 == strncmp(first, second, max)); | 478 return (0 == strncmp(first, second, max)); |
| 473 } | 479 } |
| 474 return FALSE; | 480 return FALSE; |
| 475 } | 481 } |
| 476 #endif /* !defined(TRIO_MINIMAL) */ | 482 #endif /* !defined(TRIO_MINIMAL) */ |
| 477 | 483 |
| 478 | 484 |
| 479 /** | 485 /** |
| 480 Compare if two strings are equal. | 486 Compare if two strings are equal. |
| 481 | 487 |
| 482 @param first First string. | 488 @param first First string. |
| 483 @param second Second string. | 489 @param second Second string. |
| 484 @return Boolean indicating whether the two strings are equal or not. | 490 @return Boolean indicating whether the two strings are equal or not. |
| 485 | 491 |
| 486 Collating characters are considered equal. | 492 Collating characters are considered equal. |
| 487 */ | 493 */ |
| 488 TRIO_STRING_PUBLIC int | 494 TRIO_STRING_PUBLIC int |
| 489 trio_equal_locale | 495 trio_equal_locale |
| 490 TRIO_ARGS2((first, second), | 496 TRIO_ARGS2((first, second), |
| 491 TRIO_CONST char *first, | 497 TRIO_CONST char *first, |
| 492 TRIO_CONST char *second) | 498 TRIO_CONST char *second) |
| 493 { | 499 { |
| 494 assert(first); | 500 assert(first); |
| 495 assert(second); | 501 assert(second); |
| 496 | 502 |
| 497 #if defined(LC_COLLATE) | 503 #if defined(LC_COLLATE) |
| 498 return (strcoll(first, second) == 0); | 504 return (strcoll(first, second) == 0); |
| 499 #else | 505 #else |
| 500 return trio_equal(first, second); | 506 return trio_equal(first, second); |
| 501 #endif | 507 #endif |
| 502 } | 508 } |
| 503 | 509 |
| 504 | 510 |
| 505 /** | 511 /** |
| 506 Compare if two strings up until the first @p max characters are equal. | 512 Compare if two strings up until the first @p max characters are equal. |
| 507 | 513 |
| 508 @param first First string. | 514 @param first First string. |
| 509 @param max Maximum number of characters to compare. | 515 @param max Maximum number of characters to compare. |
| 510 @param second Second string. | 516 @param second Second string. |
| 511 @return Boolean indicating whether the two strings are equal or not. | 517 @return Boolean indicating whether the two strings are equal or not. |
| 512 | 518 |
| 513 Case-insensitive comparison. | 519 Case-insensitive comparison. |
| 514 */ | 520 */ |
| 515 TRIO_STRING_PUBLIC int | 521 TRIO_STRING_PUBLIC int |
| 516 trio_equal_max | 522 trio_equal_max |
| 517 TRIO_ARGS3((first, max, second), | 523 TRIO_ARGS3((first, max, second), |
| 518 TRIO_CONST char *first, | 524 TRIO_CONST char *first, |
| 519 size_t max, | 525 size_t max, |
| 520 TRIO_CONST char *second) | 526 TRIO_CONST char *second) |
| 521 { | 527 { |
| 522 assert(first); | 528 assert(first); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 551 | 557 |
| 552 @param error_number Error number. | 558 @param error_number Error number. |
| 553 @return Textual description of @p error_number. | 559 @return Textual description of @p error_number. |
| 554 */ | 560 */ |
| 555 TRIO_STRING_PUBLIC TRIO_CONST char * | 561 TRIO_STRING_PUBLIC TRIO_CONST char * |
| 556 trio_error | 562 trio_error |
| 557 TRIO_ARGS1((error_number), | 563 TRIO_ARGS1((error_number), |
| 558 int error_number) | 564 int error_number) |
| 559 { | 565 { |
| 560 #if defined(USE_STRERROR) | 566 #if defined(USE_STRERROR) |
| 561 | 567 |
| 562 return strerror(error_number); | 568 return strerror(error_number); |
| 563 | 569 |
| 564 #elif defined(USE_SYS_ERRLIST) | 570 #elif defined(USE_SYS_ERRLIST) |
| 565 | 571 |
| 566 extern char *sys_errlist[]; | 572 extern char *sys_errlist[]; |
| 567 extern int sys_nerr; | 573 extern int sys_nerr; |
| 568 | 574 |
| 569 return ((error_number < 0) || (error_number >= sys_nerr)) | 575 return ((error_number < 0) || (error_number >= sys_nerr)) |
| 570 ? "unknown" | 576 ? "unknown" |
| 571 : sys_errlist[error_number]; | 577 : sys_errlist[error_number]; |
| 572 | 578 |
| 573 #else | 579 #else |
| 574 | 580 |
| 575 return "unknown"; | 581 return "unknown"; |
| 576 | 582 |
| 577 #endif | 583 #endif |
| 578 } | 584 } |
| 579 | 585 |
| 580 | 586 |
| 581 #if !defined(TRIO_MINIMAL) && !defined(_WIN32_WCE) | 587 #if !defined(TRIO_MINIMAL) && !defined(_WIN32_WCE) |
| 582 /** | 588 /** |
| 583 Format the date/time according to @p format. | 589 Format the date/time according to @p format. |
| 584 | 590 |
| 585 @param target Target string. | 591 @param target Target string. |
| 586 @param max Maximum number of characters to format. | 592 @param max Maximum number of characters to format. |
| 587 @param format Formatting string. | 593 @param format Formatting string. |
| 588 @param datetime Date/time structure. | 594 @param datetime Date/time structure. |
| 589 @return Number of formatted characters. | 595 @return Number of formatted characters. |
| 590 | 596 |
| 591 The formatting string accepts the same specifiers as the standard C | 597 The formatting string accepts the same specifiers as the standard C |
| 592 function strftime. | 598 function strftime. |
| 593 */ | 599 */ |
| 594 TRIO_STRING_PUBLIC size_t | 600 TRIO_STRING_PUBLIC size_t |
| 595 trio_format_date_max | 601 trio_format_date_max |
| 596 TRIO_ARGS4((target, max, format, datetime), | 602 TRIO_ARGS4((target, max, format, datetime), |
| 597 char *target, | 603 char *target, |
| 598 size_t max, | 604 size_t max, |
| 599 TRIO_CONST char *format, | 605 TRIO_CONST char *format, |
| 600 TRIO_CONST struct tm *datetime) | 606 TRIO_CONST struct tm *datetime) |
| 601 { | 607 { |
| 602 assert(target); | 608 assert(target); |
| 603 assert(format); | 609 assert(format); |
| 604 assert(datetime); | 610 assert(datetime); |
| 605 assert(max > 0); | 611 assert(max > 0); |
| 606 | 612 |
| 607 return strftime(target, max, format, datetime); | 613 return strftime(target, max, format, datetime); |
| 608 } | 614 } |
| 609 #endif /* !defined(TRIO_MINIMAL) */ | 615 #endif /* !defined(TRIO_MINIMAL) */ |
| 610 | 616 |
| 611 | 617 |
| 612 #if !defined(TRIO_MINIMAL) | 618 #if !defined(TRIO_MINIMAL) |
| 613 /** | 619 /** |
| 614 Calculate a hash value for a string. | 620 Calculate a hash value for a string. |
| 615 | 621 |
| 616 @param string String to be calculated on. | 622 @param string String to be calculated on. |
| 617 @param type Hash function. | 623 @param type Hash function. |
| 618 @return Calculated hash value. | 624 @return Calculated hash value. |
| 619 | 625 |
| 620 @p type can be one of the following | 626 @p type can be one of the following |
| 621 @li @c TRIO_HASH_PLAIN Plain hash function. | 627 @li @c TRIO_HASH_PLAIN Plain hash function. |
| 622 */ | 628 */ |
| 623 TRIO_STRING_PUBLIC unsigned long | 629 TRIO_STRING_PUBLIC unsigned long |
| 624 trio_hash | 630 trio_hash |
| 625 TRIO_ARGS2((string, type), | 631 TRIO_ARGS2((string, type), |
| 626 TRIO_CONST char *string, | 632 TRIO_CONST char *string, |
| 627 int type) | 633 int type) |
| 628 { | 634 { |
| 629 unsigned long value = 0L; | 635 unsigned long value = 0L; |
| 630 char ch; | 636 char ch; |
| 631 | 637 |
| 632 assert(string); | 638 assert(string); |
| 633 | 639 |
| 634 switch (type) | 640 switch (type) |
| 635 { | 641 { |
| 636 case TRIO_HASH_PLAIN: | 642 case TRIO_HASH_PLAIN: |
| 637 while ( (ch = *string++) != NIL ) | 643 while ( (ch = *string++) != NIL ) |
| 638 { | 644 { |
| 639 value *= 31; | 645 value *= 31; |
| 640 value += (unsigned long)ch; | 646 value += (unsigned long)ch; |
| 641 } | 647 } |
| 642 break; | 648 break; |
| 643 default: | 649 default: |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 712 | 718 |
| 713 #if !defined(TRIO_MINIMAL) | 719 #if !defined(TRIO_MINIMAL) |
| 714 /** | 720 /** |
| 715 Compare two strings using wildcards. | 721 Compare two strings using wildcards. |
| 716 | 722 |
| 717 @param string String to be searched. | 723 @param string String to be searched. |
| 718 @param pattern Pattern, including wildcards, to search for. | 724 @param pattern Pattern, including wildcards, to search for. |
| 719 @return Boolean value indicating success or failure. | 725 @return Boolean value indicating success or failure. |
| 720 | 726 |
| 721 Case-insensitive comparison. | 727 Case-insensitive comparison. |
| 722 | 728 |
| 723 The following wildcards can be used | 729 The following wildcards can be used |
| 724 @li @c * Match any number of characters. | 730 @li @c * Match any number of characters. |
| 725 @li @c ? Match a single character. | 731 @li @c ? Match a single character. |
| 726 */ | 732 */ |
| 727 TRIO_STRING_PUBLIC int | 733 TRIO_STRING_PUBLIC int |
| 728 trio_match | 734 trio_match |
| 729 TRIO_ARGS2((string, pattern), | 735 TRIO_ARGS2((string, pattern), |
| 730 TRIO_CONST char *string, | 736 TRIO_CONST char *string, |
| 731 TRIO_CONST char *pattern) | 737 TRIO_CONST char *pattern) |
| 732 { | 738 { |
| 733 assert(string); | 739 assert(string); |
| 734 assert(pattern); | 740 assert(pattern); |
| 735 | 741 |
| 736 for (; ('*' != *pattern); ++pattern, ++string) | 742 for (; ('*' != *pattern); ++pattern, ++string) |
| 737 { | 743 { |
| 738 if (NIL == *string) | 744 if (NIL == *string) |
| 739 { | 745 { |
| 740 return (NIL == *pattern); | 746 return (NIL == *pattern); |
| 741 } | 747 } |
| 742 if ((trio_to_upper((int)*string) != trio_to_upper((int)*pattern)) | 748 if ((trio_to_upper((int)*string) != trio_to_upper((int)*pattern)) |
| 743 && ('?' != *pattern)) | 749 && ('?' != *pattern)) |
| 744 { | 750 { |
| 745 return FALSE; | 751 return FALSE; |
| 746 } | 752 } |
| 747 } | 753 } |
| 748 /* two-line patch to prevent *too* much recursiveness: */ | 754 /* two-line patch to prevent *too* much recursiveness: */ |
| 749 while ('*' == pattern[1]) | 755 while ('*' == pattern[1]) |
| 750 pattern++; | 756 pattern++; |
| 751 | 757 |
| 752 do | 758 do |
| 753 { | 759 { |
| 754 if ( trio_match(string, &pattern[1]) ) | 760 if ( trio_match(string, &pattern[1]) ) |
| 755 { | 761 { |
| 756 return TRUE; | 762 return TRUE; |
| 757 } | 763 } |
| 758 } | 764 } |
| 759 while (*string++); | 765 while (*string++); |
| 760 | 766 |
| 761 return FALSE; | 767 return FALSE; |
| 762 } | 768 } |
| 763 #endif /* !defined(TRIO_MINIMAL) */ | 769 #endif /* !defined(TRIO_MINIMAL) */ |
| 764 | 770 |
| 765 | 771 |
| 766 #if !defined(TRIO_MINIMAL) | 772 #if !defined(TRIO_MINIMAL) |
| 767 /** | 773 /** |
| 768 Compare two strings using wildcards. | 774 Compare two strings using wildcards. |
| 769 | 775 |
| 770 @param string String to be searched. | 776 @param string String to be searched. |
| 771 @param pattern Pattern, including wildcards, to search for. | 777 @param pattern Pattern, including wildcards, to search for. |
| 772 @return Boolean value indicating success or failure. | 778 @return Boolean value indicating success or failure. |
| 773 | 779 |
| 774 Case-sensitive comparison. | 780 Case-sensitive comparison. |
| 775 | 781 |
| 776 The following wildcards can be used | 782 The following wildcards can be used |
| 777 @li @c * Match any number of characters. | 783 @li @c * Match any number of characters. |
| 778 @li @c ? Match a single character. | 784 @li @c ? Match a single character. |
| 779 */ | 785 */ |
| 780 TRIO_STRING_PUBLIC int | 786 TRIO_STRING_PUBLIC int |
| 781 trio_match_case | 787 trio_match_case |
| 782 TRIO_ARGS2((string, pattern), | 788 TRIO_ARGS2((string, pattern), |
| 783 TRIO_CONST char *string, | 789 TRIO_CONST char *string, |
| 784 TRIO_CONST char *pattern) | 790 TRIO_CONST char *pattern) |
| 785 { | 791 { |
| 786 assert(string); | 792 assert(string); |
| 787 assert(pattern); | 793 assert(pattern); |
| 788 | 794 |
| 789 for (; ('*' != *pattern); ++pattern, ++string) | 795 for (; ('*' != *pattern); ++pattern, ++string) |
| 790 { | 796 { |
| 791 if (NIL == *string) | 797 if (NIL == *string) |
| 792 { | 798 { |
| 793 return (NIL == *pattern); | 799 return (NIL == *pattern); |
| 794 } | 800 } |
| 795 if ((*string != *pattern) | 801 if ((*string != *pattern) |
| 796 && ('?' != *pattern)) | 802 && ('?' != *pattern)) |
| 797 { | 803 { |
| 798 return FALSE; | 804 return FALSE; |
| 799 } | 805 } |
| 800 } | 806 } |
| 801 /* two-line patch to prevent *too* much recursiveness: */ | 807 /* two-line patch to prevent *too* much recursiveness: */ |
| 802 while ('*' == pattern[1]) | 808 while ('*' == pattern[1]) |
| 803 pattern++; | 809 pattern++; |
| 804 | 810 |
| 805 do | 811 do |
| 806 { | 812 { |
| 807 if ( trio_match_case(string, &pattern[1]) ) | 813 if ( trio_match_case(string, &pattern[1]) ) |
| 808 { | 814 { |
| 809 return TRUE; | 815 return TRUE; |
| 810 } | 816 } |
| 811 } | 817 } |
| 812 while (*string++); | 818 while (*string++); |
| 813 | 819 |
| 814 return FALSE; | 820 return FALSE; |
| 815 } | 821 } |
| 816 #endif /* !defined(TRIO_MINIMAL) */ | 822 #endif /* !defined(TRIO_MINIMAL) */ |
| 817 | 823 |
| 818 | 824 |
| 819 #if !defined(TRIO_MINIMAL) | 825 #if !defined(TRIO_MINIMAL) |
| 820 /** | 826 /** |
| 821 Execute a function on each character in string. | 827 Execute a function on each character in string. |
| 822 | 828 |
| 823 @param target Target string. | 829 @param target Target string. |
| 824 @param source Source string. | 830 @param source Source string. |
| 825 @param Function Function to be executed. | 831 @param Function Function to be executed. |
| 826 @return Number of processed characters. | 832 @return Number of processed characters. |
| 827 */ | 833 */ |
| 828 TRIO_STRING_PUBLIC size_t | 834 TRIO_STRING_PUBLIC size_t |
| 829 trio_span_function | 835 trio_span_function |
| 830 TRIO_ARGS3((target, source, Function), | 836 TRIO_ARGS3((target, source, Function), |
| 831 char *target, | 837 char *target, |
| 832 TRIO_CONST char *source, | 838 TRIO_CONST char *source, |
| 833 int (*Function) TRIO_PROTO((int))) | 839 int (*Function) TRIO_PROTO((int))) |
| 834 { | 840 { |
| 835 size_t count = 0; | 841 size_t count = 0; |
| 836 | 842 |
| 837 assert(target); | 843 assert(target); |
| 838 assert(source); | 844 assert(source); |
| 839 assert(Function); | 845 assert(Function); |
| 840 | 846 |
| 841 while (*source != NIL) | 847 while (*source != NIL) |
| 842 { | 848 { |
| 843 *target++ = Function(*source++); | 849 *target++ = Function(*source++); |
| 844 count++; | 850 count++; |
| 845 } | 851 } |
| 846 return count; | 852 return count; |
| 847 } | 853 } |
| 848 #endif /* !defined(TRIO_MINIMAL) */ | 854 #endif /* !defined(TRIO_MINIMAL) */ |
| 849 | 855 |
| 850 | 856 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 887 TRIO_CONST char *string, | 893 TRIO_CONST char *string, |
| 888 size_t max, | 894 size_t max, |
| 889 TRIO_CONST char *substring) | 895 TRIO_CONST char *substring) |
| 890 { | 896 { |
| 891 size_t count; | 897 size_t count; |
| 892 size_t size; | 898 size_t size; |
| 893 char *result = NULL; | 899 char *result = NULL; |
| 894 | 900 |
| 895 assert(string); | 901 assert(string); |
| 896 assert(substring); | 902 assert(substring); |
| 897 | 903 |
| 898 size = trio_length(substring); | 904 size = trio_length(substring); |
| 899 if (size <= max) | 905 if (size <= max) |
| 900 { | 906 { |
| 901 for (count = 0; count <= max - size; count++) | 907 for (count = 0; count <= max - size; count++) |
| 902 { | 908 { |
| 903 if (trio_equal_max(substring, size, &string[count])) | 909 if (trio_equal_max(substring, size, &string[count])) |
| 904 { | 910 { |
| 905 result = (char *)&string[count]; | 911 result = (char *)&string[count]; |
| 906 break; | 912 break; |
| 907 } | 913 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 922 | 928 |
| 923 @warning @p string will be destroyed. | 929 @warning @p string will be destroyed. |
| 924 */ | 930 */ |
| 925 TRIO_STRING_PUBLIC char * | 931 TRIO_STRING_PUBLIC char * |
| 926 trio_tokenize | 932 trio_tokenize |
| 927 TRIO_ARGS2((string, delimiters), | 933 TRIO_ARGS2((string, delimiters), |
| 928 char *string, | 934 char *string, |
| 929 TRIO_CONST char *delimiters) | 935 TRIO_CONST char *delimiters) |
| 930 { | 936 { |
| 931 assert(delimiters); | 937 assert(delimiters); |
| 932 | 938 |
| 933 return strtok(string, delimiters); | 939 return strtok(string, delimiters); |
| 934 } | 940 } |
| 935 #endif /* !defined(TRIO_MINIMAL) */ | 941 #endif /* !defined(TRIO_MINIMAL) */ |
| 936 | 942 |
| 937 | 943 |
| 938 /** | 944 /** |
| 939 Convert string to floating-point number. | 945 Convert string to floating-point number. |
| 940 | 946 |
| 941 @param source String to be converted. | 947 @param source String to be converted. |
| 942 @param endp Pointer to end of the converted string. | 948 @param endp Pointer to end of the converted string. |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1057 if ((*source == '+') || (*source == '-')) | 1063 if ((*source == '+') || (*source == '-')) |
| 1058 source++; | 1064 source++; |
| 1059 while (isdigit((int)*source)) | 1065 while (isdigit((int)*source)) |
| 1060 { | 1066 { |
| 1061 exponent *= (int)base; | 1067 exponent *= (int)base; |
| 1062 exponent += (*source - '0'); | 1068 exponent += (*source - '0'); |
| 1063 source++; | 1069 source++; |
| 1064 } | 1070 } |
| 1065 } | 1071 } |
| 1066 } | 1072 } |
| 1067 | 1073 |
| 1068 value = integer + fraction; | 1074 value = integer + fraction; |
| 1069 if (exponent != 0) | 1075 if (exponent != 0) |
| 1070 { | 1076 { |
| 1071 if (isExponentNegative) | 1077 if (isExponentNegative) |
| 1072 value /= pow(base, (double)exponent); | 1078 value /= pow(base, (double)exponent); |
| 1073 else | 1079 else |
| 1074 value *= pow(base, (double)exponent); | 1080 value *= pow(base, (double)exponent); |
| 1075 } | 1081 } |
| 1076 if (isNegative) | 1082 if (isNegative) |
| 1077 value = -value; | 1083 value = -value; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1139 */ | 1145 */ |
| 1140 TRIO_STRING_PUBLIC long | 1146 TRIO_STRING_PUBLIC long |
| 1141 trio_to_long | 1147 trio_to_long |
| 1142 TRIO_ARGS3((string, endp, base), | 1148 TRIO_ARGS3((string, endp, base), |
| 1143 TRIO_CONST char *string, | 1149 TRIO_CONST char *string, |
| 1144 char **endp, | 1150 char **endp, |
| 1145 int base) | 1151 int base) |
| 1146 { | 1152 { |
| 1147 assert(string); | 1153 assert(string); |
| 1148 assert((base >= 2) && (base <= 36)); | 1154 assert((base >= 2) && (base <= 36)); |
| 1149 | 1155 |
| 1150 return strtol(string, endp, base); | 1156 return strtol(string, endp, base); |
| 1151 } | 1157 } |
| 1152 | 1158 |
| 1153 | 1159 |
| 1154 #if !defined(TRIO_MINIMAL) | 1160 #if !defined(TRIO_MINIMAL) |
| 1155 /** | 1161 /** |
| 1156 Convert one alphabetic letter to lower-case. | 1162 Convert one alphabetic letter to lower-case. |
| 1157 | 1163 |
| 1158 @param source The letter to be converted. | 1164 @param source The letter to be converted. |
| 1159 @return The converted letter. | 1165 @return The converted letter. |
| 1160 */ | 1166 */ |
| 1161 TRIO_STRING_PUBLIC int | 1167 TRIO_STRING_PUBLIC int |
| 1162 trio_to_lower | 1168 trio_to_lower |
| 1163 TRIO_ARGS1((source), | 1169 TRIO_ARGS1((source), |
| 1164 int source) | 1170 int source) |
| 1165 { | 1171 { |
| 1166 #if defined(USE_TOLOWER) | 1172 #if defined(USE_TOLOWER) |
| 1167 | 1173 |
| 1168 return tolower(source); | 1174 return tolower(source); |
| 1169 | 1175 |
| 1170 #else | 1176 #else |
| 1171 | 1177 |
| 1172 /* Does not handle locales or non-contiguous alphabetic characters */ | 1178 /* Does not handle locales or non-contiguous alphabetic characters */ |
| 1173 return ((source >= (int)'A') && (source <= (int)'Z')) | 1179 return ((source >= (int)'A') && (source <= (int)'Z')) |
| 1174 ? source - 'A' + 'a' | 1180 ? source - 'A' + 'a' |
| 1175 : source; | 1181 : source; |
| 1176 | 1182 |
| 1177 #endif | 1183 #endif |
| 1178 } | 1184 } |
| 1179 #endif /* !defined(TRIO_MINIMAL) */ | 1185 #endif /* !defined(TRIO_MINIMAL) */ |
| 1180 | 1186 |
| 1181 #if !defined(TRIO_MINIMAL) | 1187 #if !defined(TRIO_MINIMAL) |
| 1182 /** | 1188 /** |
| 1183 Convert string to unsigned integer. | 1189 Convert string to unsigned integer. |
| 1184 | 1190 |
| 1185 @param string String to be converted. | 1191 @param string String to be converted. |
| 1186 @param endp Pointer to end of converted string. | 1192 @param endp Pointer to end of converted string. |
| 1187 @param base Radix number of number. | 1193 @param base Radix number of number. |
| 1188 */ | 1194 */ |
| 1189 TRIO_STRING_PUBLIC unsigned long | 1195 TRIO_STRING_PUBLIC unsigned long |
| 1190 trio_to_unsigned_long | 1196 trio_to_unsigned_long |
| 1191 TRIO_ARGS3((string, endp, base), | 1197 TRIO_ARGS3((string, endp, base), |
| 1192 TRIO_CONST char *string, | 1198 TRIO_CONST char *string, |
| 1193 char **endp, | 1199 char **endp, |
| 1194 int base) | 1200 int base) |
| 1195 { | 1201 { |
| 1196 assert(string); | 1202 assert(string); |
| 1197 assert((base >= 2) && (base <= 36)); | 1203 assert((base >= 2) && (base <= 36)); |
| 1198 | 1204 |
| 1199 return strtoul(string, endp, base); | 1205 return strtoul(string, endp, base); |
| 1200 } | 1206 } |
| 1201 #endif /* !defined(TRIO_MINIMAL) */ | 1207 #endif /* !defined(TRIO_MINIMAL) */ |
| 1202 | 1208 |
| 1203 | 1209 |
| 1204 /** | 1210 /** |
| 1205 Convert one alphabetic letter to upper-case. | 1211 Convert one alphabetic letter to upper-case. |
| 1206 | 1212 |
| 1207 @param source The letter to be converted. | 1213 @param source The letter to be converted. |
| 1208 @return The converted letter. | 1214 @return The converted letter. |
| 1209 */ | 1215 */ |
| 1210 TRIO_STRING_PUBLIC int | 1216 TRIO_STRING_PUBLIC int |
| 1211 trio_to_upper | 1217 trio_to_upper |
| 1212 TRIO_ARGS1((source), | 1218 TRIO_ARGS1((source), |
| 1213 int source) | 1219 int source) |
| 1214 { | 1220 { |
| 1215 #if defined(USE_TOUPPER) | 1221 #if defined(USE_TOUPPER) |
| 1216 | 1222 |
| 1217 return toupper(source); | 1223 return toupper(source); |
| 1218 | 1224 |
| 1219 #else | 1225 #else |
| 1220 | 1226 |
| 1221 /* Does not handle locales or non-contiguous alphabetic characters */ | 1227 /* Does not handle locales or non-contiguous alphabetic characters */ |
| 1222 return ((source >= (int)'a') && (source <= (int)'z')) | 1228 return ((source >= (int)'a') && (source <= (int)'z')) |
| 1223 ? source - 'a' + 'A' | 1229 ? source - 'a' + 'A' |
| 1224 : source; | 1230 : source; |
| 1225 | 1231 |
| 1226 #endif | 1232 #endif |
| 1227 } | 1233 } |
| 1228 | 1234 |
| 1229 #if !defined(TRIO_MINIMAL) | 1235 #if !defined(TRIO_MINIMAL) |
| 1230 /** | 1236 /** |
| 1231 Convert the alphabetic letters in the string to upper-case. | 1237 Convert the alphabetic letters in the string to upper-case. |
| 1232 | 1238 |
| 1233 @param target The string to be converted. | 1239 @param target The string to be converted. |
| 1234 @return The number of processed characters (converted or not). | 1240 @return The number of processed characters (converted or not). |
| 1235 */ | 1241 */ |
| (...skipping 23 matching lines...) Expand all Loading... |
| 1259 @{ | 1265 @{ |
| 1260 */ | 1266 */ |
| 1261 | 1267 |
| 1262 /* | 1268 /* |
| 1263 * TrioStringAlloc | 1269 * TrioStringAlloc |
| 1264 */ | 1270 */ |
| 1265 TRIO_STRING_PRIVATE trio_string_t * | 1271 TRIO_STRING_PRIVATE trio_string_t * |
| 1266 TrioStringAlloc(TRIO_NOARGS) | 1272 TrioStringAlloc(TRIO_NOARGS) |
| 1267 { | 1273 { |
| 1268 trio_string_t *self; | 1274 trio_string_t *self; |
| 1269 | 1275 |
| 1270 self = (trio_string_t *)TRIO_MALLOC(sizeof(trio_string_t)); | 1276 self = (trio_string_t *)TRIO_MALLOC(sizeof(trio_string_t)); |
| 1271 if (self) | 1277 if (self) |
| 1272 { | 1278 { |
| 1273 self->content = NULL; | 1279 self->content = NULL; |
| 1274 self->length = 0; | 1280 self->length = 0; |
| 1275 self->allocated = 0; | 1281 self->allocated = 0; |
| 1276 } | 1282 } |
| 1277 return self; | 1283 return self; |
| 1278 } | 1284 } |
| 1279 | 1285 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1290 trio_string_t *self, | 1296 trio_string_t *self, |
| 1291 size_t delta) | 1297 size_t delta) |
| 1292 { | 1298 { |
| 1293 BOOLEAN_T status = FALSE; | 1299 BOOLEAN_T status = FALSE; |
| 1294 char *new_content; | 1300 char *new_content; |
| 1295 size_t new_size; | 1301 size_t new_size; |
| 1296 | 1302 |
| 1297 new_size = (delta == 0) | 1303 new_size = (delta == 0) |
| 1298 ? ( (self->allocated == 0) ? 1 : self->allocated * 2 ) | 1304 ? ( (self->allocated == 0) ? 1 : self->allocated * 2 ) |
| 1299 : self->allocated + delta; | 1305 : self->allocated + delta; |
| 1300 | 1306 |
| 1301 new_content = (char *)TRIO_REALLOC(self->content, new_size); | 1307 new_content = (char *)TRIO_REALLOC(self->content, new_size); |
| 1302 if (new_content) | 1308 if (new_content) |
| 1303 { | 1309 { |
| 1304 self->content = new_content; | 1310 self->content = new_content; |
| 1305 self->allocated = new_size; | 1311 self->allocated = new_size; |
| 1306 status = TRUE; | 1312 status = TRUE; |
| 1307 } | 1313 } |
| 1308 return status; | 1314 return status; |
| 1309 } | 1315 } |
| 1310 | 1316 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1327 return (self->allocated < length) | 1333 return (self->allocated < length) |
| 1328 ? TrioStringGrow(self, length - self->allocated) | 1334 ? TrioStringGrow(self, length - self->allocated) |
| 1329 : TRUE; | 1335 : TRUE; |
| 1330 } | 1336 } |
| 1331 #endif /* !defined(TRIO_MINIMAL) */ | 1337 #endif /* !defined(TRIO_MINIMAL) */ |
| 1332 | 1338 |
| 1333 | 1339 |
| 1334 #if !defined(TRIO_MINIMAL) | 1340 #if !defined(TRIO_MINIMAL) |
| 1335 /** | 1341 /** |
| 1336 Create a new dynamic string. | 1342 Create a new dynamic string. |
| 1337 | 1343 |
| 1338 @param initial_size Initial size of the buffer. | 1344 @param initial_size Initial size of the buffer. |
| 1339 @return Newly allocated dynamic string, or NULL if memory allocation failed. | 1345 @return Newly allocated dynamic string, or NULL if memory allocation failed. |
| 1340 */ | 1346 */ |
| 1341 TRIO_STRING_PUBLIC trio_string_t * | 1347 TRIO_STRING_PUBLIC trio_string_t * |
| 1342 trio_string_create | 1348 trio_string_create |
| 1343 TRIO_ARGS1((initial_size), | 1349 TRIO_ARGS1((initial_size), |
| 1344 int initial_size) | 1350 int initial_size) |
| 1345 { | 1351 { |
| 1346 trio_string_t *self; | 1352 trio_string_t *self; |
| 1347 | 1353 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1360 self = NULL; | 1366 self = NULL; |
| 1361 } | 1367 } |
| 1362 } | 1368 } |
| 1363 return self; | 1369 return self; |
| 1364 } | 1370 } |
| 1365 #endif /* !defined(TRIO_MINIMAL) */ | 1371 #endif /* !defined(TRIO_MINIMAL) */ |
| 1366 | 1372 |
| 1367 | 1373 |
| 1368 /** | 1374 /** |
| 1369 Deallocate the dynamic string and its contents. | 1375 Deallocate the dynamic string and its contents. |
| 1370 | 1376 |
| 1371 @param self Dynamic string | 1377 @param self Dynamic string |
| 1372 */ | 1378 */ |
| 1373 TRIO_STRING_PUBLIC void | 1379 TRIO_STRING_PUBLIC void |
| 1374 trio_string_destroy | 1380 trio_string_destroy |
| 1375 TRIO_ARGS1((self), | 1381 TRIO_ARGS1((self), |
| 1376 trio_string_t *self) | 1382 trio_string_t *self) |
| 1377 { | 1383 { |
| 1378 assert(self); | 1384 assert(self); |
| 1379 | 1385 |
| 1380 if (self) | 1386 if (self) |
| 1381 { | 1387 { |
| 1382 trio_destroy(self->content); | 1388 trio_destroy(self->content); |
| 1383 TRIO_FREE(self); | 1389 TRIO_FREE(self); |
| 1384 } | 1390 } |
| 1385 } | 1391 } |
| 1386 | 1392 |
| 1387 | 1393 |
| 1388 #if !defined(TRIO_MINIMAL) | 1394 #if !defined(TRIO_MINIMAL) |
| 1389 /** | 1395 /** |
| 1390 Get a pointer to the content. | 1396 Get a pointer to the content. |
| 1391 | 1397 |
| 1392 @param self Dynamic string. | 1398 @param self Dynamic string. |
| 1393 @param offset Offset into content. | 1399 @param offset Offset into content. |
| 1394 @return Pointer to the content. | 1400 @return Pointer to the content. |
| 1395 | 1401 |
| 1396 @p Offset can be zero, positive, or negative. If @p offset is zero, | 1402 @p Offset can be zero, positive, or negative. If @p offset is zero, |
| 1397 then the start of the content will be returned. If @p offset is positive, | 1403 then the start of the content will be returned. If @p offset is positive, |
| 1398 then a pointer to @p offset number of characters from the beginning of the | 1404 then a pointer to @p offset number of characters from the beginning of the |
| 1399 content is returned. If @p offset is negative, then a pointer to @p offset | 1405 content is returned. If @p offset is negative, then a pointer to @p offset |
| 1400 number of characters from the ending of the string, starting at the | 1406 number of characters from the ending of the string, starting at the |
| 1401 terminating zero, is returned. | 1407 terminating zero, is returned. |
| 1402 */ | 1408 */ |
| 1403 TRIO_STRING_PUBLIC char * | 1409 TRIO_STRING_PUBLIC char * |
| 1404 trio_string_get | 1410 trio_string_get |
| 1405 TRIO_ARGS2((self, offset), | 1411 TRIO_ARGS2((self, offset), |
| 1406 trio_string_t *self, | 1412 trio_string_t *self, |
| 1407 int offset) | 1413 int offset) |
| 1408 { | 1414 { |
| 1409 char *result = NULL; | 1415 char *result = NULL; |
| 1410 | 1416 |
| 1411 assert(self); | 1417 assert(self); |
| 1412 | 1418 |
| 1413 if (self->content != NULL) | 1419 if (self->content != NULL) |
| 1414 { | 1420 { |
| 1415 if (self->length == 0) | 1421 if (self->length == 0) |
| 1416 { | 1422 { |
| 1417 (void)trio_string_length(self); | 1423 (void)trio_string_length(self); |
| 1418 } | 1424 } |
| 1419 if (offset >= 0) | 1425 if (offset >= 0) |
| 1420 { | 1426 { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1433 } | 1439 } |
| 1434 result = &(self->content[offset]); | 1440 result = &(self->content[offset]); |
| 1435 } | 1441 } |
| 1436 return result; | 1442 return result; |
| 1437 } | 1443 } |
| 1438 #endif /* !defined(TRIO_MINIMAL) */ | 1444 #endif /* !defined(TRIO_MINIMAL) */ |
| 1439 | 1445 |
| 1440 | 1446 |
| 1441 /** | 1447 /** |
| 1442 Extract the content. | 1448 Extract the content. |
| 1443 | 1449 |
| 1444 @param self Dynamic String | 1450 @param self Dynamic String |
| 1445 @return Content of dynamic string. | 1451 @return Content of dynamic string. |
| 1446 | 1452 |
| 1447 The content is removed from the dynamic string. This enables destruction | 1453 The content is removed from the dynamic string. This enables destruction |
| 1448 of the dynamic string without deallocation of the content. | 1454 of the dynamic string without deallocation of the content. |
| 1449 */ | 1455 */ |
| 1450 TRIO_STRING_PUBLIC char * | 1456 TRIO_STRING_PUBLIC char * |
| 1451 trio_string_extract | 1457 trio_string_extract |
| 1452 TRIO_ARGS1((self), | 1458 TRIO_ARGS1((self), |
| 1453 trio_string_t *self) | 1459 trio_string_t *self) |
| 1454 { | 1460 { |
| 1455 char *result; | 1461 char *result; |
| 1456 | 1462 |
| 1457 assert(self); | 1463 assert(self); |
| 1458 | 1464 |
| 1459 result = self->content; | 1465 result = self->content; |
| 1460 /* FIXME: Allocate new empty buffer? */ | 1466 /* FIXME: Allocate new empty buffer? */ |
| 1461 self->content = NULL; | 1467 self->content = NULL; |
| 1462 self->length = self->allocated = 0; | 1468 self->length = self->allocated = 0; |
| 1463 return result; | 1469 return result; |
| 1464 } | 1470 } |
| 1465 | 1471 |
| 1466 | 1472 |
| 1467 #if !defined(TRIO_MINIMAL) | 1473 #if !defined(TRIO_MINIMAL) |
| 1468 /** | 1474 /** |
| 1469 Set the content of the dynamic string. | 1475 Set the content of the dynamic string. |
| 1470 | 1476 |
| 1471 @param self Dynamic String | 1477 @param self Dynamic String |
| 1472 @param buffer The new content. | 1478 @param buffer The new content. |
| 1473 | 1479 |
| 1474 Sets the content of the dynamic string to a copy @p buffer. | 1480 Sets the content of the dynamic string to a copy @p buffer. |
| 1475 An existing content will be deallocated first, if necessary. | 1481 An existing content will be deallocated first, if necessary. |
| 1476 | 1482 |
| 1477 @remark | 1483 @remark |
| 1478 This function will make a copy of @p buffer. | 1484 This function will make a copy of @p buffer. |
| 1479 You are responsible for deallocating @p buffer yourself. | 1485 You are responsible for deallocating @p buffer yourself. |
| 1480 */ | 1486 */ |
| 1481 TRIO_STRING_PUBLIC void | 1487 TRIO_STRING_PUBLIC void |
| 1482 trio_xstring_set | 1488 trio_xstring_set |
| 1483 TRIO_ARGS2((self, buffer), | 1489 TRIO_ARGS2((self, buffer), |
| 1484 trio_string_t *self, | 1490 trio_string_t *self, |
| 1485 char *buffer) | 1491 char *buffer) |
| 1486 { | 1492 { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 1514 TRIO_ARGS1((self), | 1520 TRIO_ARGS1((self), |
| 1515 trio_string_t *self) | 1521 trio_string_t *self) |
| 1516 { | 1522 { |
| 1517 trio_xstring_append_char(self, 0); | 1523 trio_xstring_append_char(self, 0); |
| 1518 } | 1524 } |
| 1519 | 1525 |
| 1520 | 1526 |
| 1521 #if !defined(TRIO_MINIMAL) | 1527 #if !defined(TRIO_MINIMAL) |
| 1522 /** | 1528 /** |
| 1523 Append the second string to the first. | 1529 Append the second string to the first. |
| 1524 | 1530 |
| 1525 @param self Dynamic string to be modified. | 1531 @param self Dynamic string to be modified. |
| 1526 @param other Dynamic string to copy from. | 1532 @param other Dynamic string to copy from. |
| 1527 @return Boolean value indicating success or failure. | 1533 @return Boolean value indicating success or failure. |
| 1528 */ | 1534 */ |
| 1529 TRIO_STRING_PUBLIC int | 1535 TRIO_STRING_PUBLIC int |
| 1530 trio_string_append | 1536 trio_string_append |
| 1531 TRIO_ARGS2((self, other), | 1537 TRIO_ARGS2((self, other), |
| 1532 trio_string_t *self, | 1538 trio_string_t *self, |
| 1533 trio_string_t *other) | 1539 trio_string_t *other) |
| 1534 { | 1540 { |
| 1535 size_t length; | 1541 size_t length; |
| 1536 | 1542 |
| 1537 assert(self); | 1543 assert(self); |
| 1538 assert(other); | 1544 assert(other); |
| 1539 | 1545 |
| 1540 length = self->length + other->length; | 1546 length = self->length + other->length; |
| 1541 if (!TrioStringGrowTo(self, length)) | 1547 if (!TrioStringGrowTo(self, length)) |
| 1542 goto error; | 1548 goto error; |
| 1543 trio_copy(&self->content[self->length], other->content); | 1549 trio_copy(&self->content[self->length], other->content); |
| 1544 self->length = length; | 1550 self->length = length; |
| 1545 return TRUE; | 1551 return TRUE; |
| 1546 | 1552 |
| 1547 error: | 1553 error: |
| 1548 return FALSE; | 1554 return FALSE; |
| 1549 } | 1555 } |
| 1550 #endif /* !defined(TRIO_MINIMAL) */ | 1556 #endif /* !defined(TRIO_MINIMAL) */ |
| 1551 | 1557 |
| 1552 | 1558 |
| 1553 #if !defined(TRIO_MINIMAL) | 1559 #if !defined(TRIO_MINIMAL) |
| 1554 /* | 1560 /* |
| 1555 * trio_xstring_append | 1561 * trio_xstring_append |
| 1556 */ | 1562 */ |
| 1557 TRIO_STRING_PUBLIC int | 1563 TRIO_STRING_PUBLIC int |
| 1558 trio_xstring_append | 1564 trio_xstring_append |
| 1559 TRIO_ARGS2((self, other), | 1565 TRIO_ARGS2((self, other), |
| 1560 trio_string_t *self, | 1566 trio_string_t *self, |
| 1561 TRIO_CONST char *other) | 1567 TRIO_CONST char *other) |
| 1562 { | 1568 { |
| 1563 size_t length; | 1569 size_t length; |
| 1564 | 1570 |
| 1565 assert(self); | 1571 assert(self); |
| 1566 assert(other); | 1572 assert(other); |
| 1567 | 1573 |
| 1568 length = self->length + trio_length(other); | 1574 length = self->length + trio_length(other); |
| 1569 if (!TrioStringGrowTo(self, length)) | 1575 if (!TrioStringGrowTo(self, length)) |
| 1570 goto error; | 1576 goto error; |
| 1571 trio_copy(&self->content[self->length], other); | 1577 trio_copy(&self->content[self->length], other); |
| 1572 self->length = length; | 1578 self->length = length; |
| 1573 return TRUE; | 1579 return TRUE; |
| 1574 | 1580 |
| 1575 error: | 1581 error: |
| 1576 return FALSE; | 1582 return FALSE; |
| 1577 } | 1583 } |
| 1578 #endif /* !defined(TRIO_MINIMAL) */ | 1584 #endif /* !defined(TRIO_MINIMAL) */ |
| 1579 | 1585 |
| 1580 | 1586 |
| 1581 /* | 1587 /* |
| 1582 * trio_xstring_append_char | 1588 * trio_xstring_append_char |
| 1583 */ | 1589 */ |
| 1584 TRIO_STRING_PUBLIC int | 1590 TRIO_STRING_PUBLIC int |
| 1585 trio_xstring_append_char | 1591 trio_xstring_append_char |
| 1586 TRIO_ARGS2((self, character), | 1592 TRIO_ARGS2((self, character), |
| 1587 trio_string_t *self, | 1593 trio_string_t *self, |
| 1588 char character) | 1594 char character) |
| 1589 { | 1595 { |
| 1590 assert(self); | 1596 assert(self); |
| 1591 | 1597 |
| 1592 if ((int)self->length >= trio_string_size(self)) | 1598 if ((int)self->length >= trio_string_size(self)) |
| 1593 { | 1599 { |
| 1594 if (!TrioStringGrow(self, 0)) | 1600 if (!TrioStringGrow(self, 0)) |
| 1595 goto error; | 1601 goto error; |
| 1596 } | 1602 } |
| 1597 self->content[self->length] = character; | 1603 self->content[self->length] = character; |
| 1598 self->length++; | 1604 self->length++; |
| 1599 return TRUE; | 1605 return TRUE; |
| 1600 | 1606 |
| 1601 error: | 1607 error: |
| 1602 return FALSE; | 1608 return FALSE; |
| 1603 } | 1609 } |
| 1604 | 1610 |
| 1605 | 1611 |
| 1606 #if !defined(TRIO_MINIMAL) | 1612 #if !defined(TRIO_MINIMAL) |
| 1607 /** | 1613 /** |
| 1608 Search for the first occurrence of second parameter in the first. | 1614 Search for the first occurrence of second parameter in the first. |
| 1609 | 1615 |
| 1610 @param self Dynamic string to be modified. | 1616 @param self Dynamic string to be modified. |
| 1611 @param other Dynamic string to copy from. | 1617 @param other Dynamic string to copy from. |
| 1612 @return Boolean value indicating success or failure. | 1618 @return Boolean value indicating success or failure. |
| 1613 */ | 1619 */ |
| 1614 TRIO_STRING_PUBLIC int | 1620 TRIO_STRING_PUBLIC int |
| 1615 trio_string_contains | 1621 trio_string_contains |
| 1616 TRIO_ARGS2((self, other), | 1622 TRIO_ARGS2((self, other), |
| 1617 trio_string_t *self, | 1623 trio_string_t *self, |
| 1618 trio_string_t *other) | 1624 trio_string_t *other) |
| 1619 { | 1625 { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1684 #if !defined(TRIO_MINIMAL) | 1690 #if !defined(TRIO_MINIMAL) |
| 1685 /* | 1691 /* |
| 1686 * trio_string_duplicate | 1692 * trio_string_duplicate |
| 1687 */ | 1693 */ |
| 1688 TRIO_STRING_PUBLIC trio_string_t * | 1694 TRIO_STRING_PUBLIC trio_string_t * |
| 1689 trio_string_duplicate | 1695 trio_string_duplicate |
| 1690 TRIO_ARGS1((other), | 1696 TRIO_ARGS1((other), |
| 1691 trio_string_t *other) | 1697 trio_string_t *other) |
| 1692 { | 1698 { |
| 1693 trio_string_t *self; | 1699 trio_string_t *self; |
| 1694 | 1700 |
| 1695 assert(other); | 1701 assert(other); |
| 1696 | 1702 |
| 1697 self = TrioStringAlloc(); | 1703 self = TrioStringAlloc(); |
| 1698 if (self) | 1704 if (self) |
| 1699 { | 1705 { |
| 1700 self->content = TrioDuplicateMax(other->content, other->length); | 1706 self->content = TrioDuplicateMax(other->content, other->length); |
| 1701 if (self->content) | 1707 if (self->content) |
| 1702 { | 1708 { |
| 1703 self->length = other->length; | 1709 self->length = other->length; |
| 1704 self->allocated = self->length + 1; | 1710 self->allocated = self->length + 1; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1715 | 1721 |
| 1716 /* | 1722 /* |
| 1717 * trio_xstring_duplicate | 1723 * trio_xstring_duplicate |
| 1718 */ | 1724 */ |
| 1719 TRIO_STRING_PUBLIC trio_string_t * | 1725 TRIO_STRING_PUBLIC trio_string_t * |
| 1720 trio_xstring_duplicate | 1726 trio_xstring_duplicate |
| 1721 TRIO_ARGS1((other), | 1727 TRIO_ARGS1((other), |
| 1722 TRIO_CONST char *other) | 1728 TRIO_CONST char *other) |
| 1723 { | 1729 { |
| 1724 trio_string_t *self; | 1730 trio_string_t *self; |
| 1725 | 1731 |
| 1726 assert(other); | 1732 assert(other); |
| 1727 | 1733 |
| 1728 self = TrioStringAlloc(); | 1734 self = TrioStringAlloc(); |
| 1729 if (self) | 1735 if (self) |
| 1730 { | 1736 { |
| 1731 self->content = TrioDuplicateMax(other, trio_length(other)); | 1737 self->content = TrioDuplicateMax(other, trio_length(other)); |
| 1732 if (self->content) | 1738 if (self->content) |
| 1733 { | 1739 { |
| 1734 self->length = trio_length(self->content); | 1740 self->length = trio_length(self->content); |
| 1735 self->allocated = self->length + 1; | 1741 self->allocated = self->length + 1; |
| (...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2097 TRIO_ARGS1((self), | 2103 TRIO_ARGS1((self), |
| 2098 trio_string_t *self) | 2104 trio_string_t *self) |
| 2099 { | 2105 { |
| 2100 assert(self); | 2106 assert(self); |
| 2101 | 2107 |
| 2102 return trio_upper(self->content); | 2108 return trio_upper(self->content); |
| 2103 } | 2109 } |
| 2104 #endif /* !defined(TRIO_MINIMAL) */ | 2110 #endif /* !defined(TRIO_MINIMAL) */ |
| 2105 | 2111 |
| 2106 /** @} End of DynamicStrings */ | 2112 /** @} End of DynamicStrings */ |
| OLD | NEW |