| 1 |
|
|---|
| 2 | /*
|
|---|
| 3 | *@@sourcefile stringh.c:
|
|---|
| 4 | * contains string/text helper functions. These are good for
|
|---|
| 5 | * parsing/splitting strings and other stuff used throughout
|
|---|
| 6 | * XWorkplace.
|
|---|
| 7 | *
|
|---|
| 8 | * Note that these functions are really a bunch of very mixed
|
|---|
| 9 | * up string helpers, which you may or may not find helpful.
|
|---|
| 10 | * If you're looking for string functions with memory
|
|---|
| 11 | * management, look at xstring.c instead.
|
|---|
| 12 | *
|
|---|
| 13 | * Usage: All OS/2 programs.
|
|---|
| 14 | *
|
|---|
| 15 | * Function prefixes (new with V0.81):
|
|---|
| 16 | * -- strh* string helper functions.
|
|---|
| 17 | *
|
|---|
| 18 | * Note: Version numbering in this file relates to XWorkplace version
|
|---|
| 19 | * numbering.
|
|---|
| 20 | *
|
|---|
| 21 | *@@header "helpers\stringh.h"
|
|---|
| 22 | */
|
|---|
| 23 |
|
|---|
| 24 | /*
|
|---|
| 25 | * Copyright (C) 1997-2002 Ulrich Mller.
|
|---|
| 26 | * Parts Copyright (C) 1991-1999 iMatix Corporation.
|
|---|
| 27 | * This file is part of the "XWorkplace helpers" source package.
|
|---|
| 28 | * This is free software; you can redistribute it and/or modify
|
|---|
| 29 | * it under the terms of the GNU General Public License as published
|
|---|
| 30 | * by the Free Software Foundation, in version 2 as it comes in the
|
|---|
| 31 | * "COPYING" file of the XWorkplace main distribution.
|
|---|
| 32 | * This program is distributed in the hope that it will be useful,
|
|---|
| 33 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 34 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 35 | * GNU General Public License for more details.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #define OS2EMX_PLAIN_CHAR
|
|---|
| 39 | // this is needed for "os2emx.h"; if this is defined,
|
|---|
| 40 | // emx will define PSZ as _signed_ char, otherwise
|
|---|
| 41 | // as unsigned char
|
|---|
| 42 |
|
|---|
| 43 | #define INCL_WINSHELLDATA
|
|---|
| 44 | #define INCL_DOSERRORS
|
|---|
| 45 | #include <os2.h>
|
|---|
| 46 |
|
|---|
| 47 | #include <stdlib.h>
|
|---|
| 48 | #include <stdio.h>
|
|---|
| 49 | #include <string.h>
|
|---|
| 50 | #include <ctype.h>
|
|---|
| 51 | #include <math.h>
|
|---|
| 52 |
|
|---|
| 53 | #include "setup.h" // code generation and debugging options
|
|---|
| 54 |
|
|---|
| 55 | #define DONT_REPLACE_STRINGH_MALLOC
|
|---|
| 56 | #include "helpers\stringh.h"
|
|---|
| 57 | #include "helpers\xstring.h" // extended string helpers
|
|---|
| 58 |
|
|---|
| 59 | #pragma hdrstop
|
|---|
| 60 |
|
|---|
| 61 | /*
|
|---|
| 62 | *@@category: Helpers\C helpers\String management
|
|---|
| 63 | * See stringh.c and xstring.c.
|
|---|
| 64 | */
|
|---|
| 65 |
|
|---|
| 66 | /*
|
|---|
| 67 | *@@category: Helpers\C helpers\String management\C string helpers
|
|---|
| 68 | * See stringh.c.
|
|---|
| 69 | */
|
|---|
| 70 |
|
|---|
| 71 | #ifdef __DEBUG_MALLOC_ENABLED__
|
|---|
| 72 |
|
|---|
| 73 | /*
|
|---|
| 74 | *@@ strhStoreDebug:
|
|---|
| 75 | * memory debug version of strhStore.
|
|---|
| 76 | *
|
|---|
| 77 | *@@added V0.9.16 (2001-12-08) [umoeller]
|
|---|
| 78 | */
|
|---|
| 79 |
|
|---|
| 80 | APIRET (strhStoreDebug)(PSZ *ppszTarget,
|
|---|
| 81 | PCSZ pcszSource,
|
|---|
| 82 | PULONG pulLength, // out: length of new string (ptr can be NULL)
|
|---|
| 83 | PCSZ pcszSourceFile,
|
|---|
| 84 | unsigned long ulLine,
|
|---|
| 85 | PCSZ pcszFunction)
|
|---|
| 86 | {
|
|---|
| 87 | ULONG ulLength = 0;
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 | if (ppszTarget)
|
|---|
| 92 | {
|
|---|
| 93 | if (*ppszTarget)
|
|---|
| 94 | free(*ppszTarget);
|
|---|
| 95 |
|
|---|
| 96 | if ( (pcszSource)
|
|---|
| 97 | && (ulLength = strlen(pcszSource))
|
|---|
| 98 | )
|
|---|
| 99 | {
|
|---|
| 100 | if (*ppszTarget = (PSZ)memdMalloc(ulLength + 1,
|
|---|
| 101 | pcszSourceFile,
|
|---|
| 102 | ulLine,
|
|---|
| 103 | pcszFunction))
|
|---|
| 104 | memcpy(*ppszTarget, pcszSource, ulLength + 1);
|
|---|
| 105 | else
|
|---|
| 106 | return ERROR_NOT_ENOUGH_MEMORY;
|
|---|
| 107 | }
|
|---|
| 108 | else
|
|---|
| 109 | *ppszTarget = NULL;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | if (pulLength)
|
|---|
| 113 | *pulLength = ulLength;
|
|---|
| 114 |
|
|---|
| 115 | return NO_ERROR;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | #endif
|
|---|
| 119 |
|
|---|
| 120 | /*
|
|---|
| 121 | *@@ strhStore:
|
|---|
| 122 | * stores a copy of the given string in the specified
|
|---|
| 123 | * buffer. Uses strdup internally.
|
|---|
| 124 | *
|
|---|
| 125 | * If *ppszTarget != NULL, the previous string is freed
|
|---|
| 126 | * and set to NULL.
|
|---|
| 127 | * If pcszSource != NULL, a copy of it is stored in the
|
|---|
| 128 | * buffer.
|
|---|
| 129 | *
|
|---|
| 130 | *@@added V0.9.16 (2001-12-06) [umoeller]
|
|---|
| 131 | */
|
|---|
| 132 |
|
|---|
| 133 | APIRET strhStore(PSZ *ppszTarget,
|
|---|
| 134 | PCSZ pcszSource,
|
|---|
| 135 | PULONG pulLength) // out: length of new string (ptr can be NULL)
|
|---|
| 136 | {
|
|---|
| 137 | ULONG ulLength = 0;
|
|---|
| 138 |
|
|---|
| 139 | if (ppszTarget)
|
|---|
| 140 | {
|
|---|
| 141 | if (*ppszTarget)
|
|---|
| 142 | free(*ppszTarget);
|
|---|
| 143 |
|
|---|
| 144 | if ( (pcszSource)
|
|---|
| 145 | && (ulLength = strlen(pcszSource))
|
|---|
| 146 | )
|
|---|
| 147 | {
|
|---|
| 148 | if (*ppszTarget = (PSZ)malloc(ulLength + 1))
|
|---|
| 149 | memcpy(*ppszTarget, pcszSource, ulLength + 1);
|
|---|
| 150 | else
|
|---|
| 151 | return ERROR_NOT_ENOUGH_MEMORY;
|
|---|
| 152 | }
|
|---|
| 153 | else
|
|---|
| 154 | *ppszTarget = NULL;
|
|---|
| 155 | }
|
|---|
| 156 | else
|
|---|
| 157 | return ERROR_INVALID_PARAMETER;
|
|---|
| 158 |
|
|---|
| 159 | if (pulLength)
|
|---|
| 160 | *pulLength = ulLength;
|
|---|
| 161 |
|
|---|
| 162 | return NO_ERROR;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /*
|
|---|
| 166 | *@@ strhcpy:
|
|---|
| 167 | * like strdup, but this one doesn't crash if string2 is NULL,
|
|---|
| 168 | * but sets the first byte in string1 to \0 instead.
|
|---|
| 169 | *
|
|---|
| 170 | *@@added V0.9.14 (2001-08-01) [umoeller]
|
|---|
| 171 | */
|
|---|
| 172 |
|
|---|
| 173 | PSZ strhcpy(PSZ string1, PCSZ string2)
|
|---|
| 174 | {
|
|---|
| 175 | if (string2)
|
|---|
| 176 | return strcpy(string1, string2);
|
|---|
| 177 |
|
|---|
| 178 | *string1 = '\0';
|
|---|
| 179 | return string1;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /*
|
|---|
| 183 | *@@ strhCopyBuf:
|
|---|
| 184 | * copies pcszSource to pszTarget, taking
|
|---|
| 185 | * its length into account.
|
|---|
| 186 | *
|
|---|
| 187 | * Returns:
|
|---|
| 188 | *
|
|---|
| 189 | * -- NO_ERROR
|
|---|
| 190 | *
|
|---|
| 191 | * -- ERROR_INVALID_PARAMETER: pcszSource is
|
|---|
| 192 | * null or points to a null byte.
|
|---|
| 193 | *
|
|---|
| 194 | * -- ERROR_FILENAME_EXCED_RANGE: pcszSource
|
|---|
| 195 | * is too long to fit into pszTarget.
|
|---|
| 196 | *
|
|---|
| 197 | *@@added V1.0.1 (2003-01-05) [umoeller]
|
|---|
| 198 | */
|
|---|
| 199 |
|
|---|
| 200 | APIRET strhCopyBuf(PSZ pszTarget,
|
|---|
| 201 | PCSZ pcszSource,
|
|---|
| 202 | ULONG cbTarget)
|
|---|
| 203 | {
|
|---|
| 204 | ULONG cb;
|
|---|
| 205 | if (!pcszSource || !*pcszSource)
|
|---|
| 206 | return ERROR_INVALID_PARAMETER;
|
|---|
| 207 | cb = strlen(pcszSource) + 1;
|
|---|
| 208 | if (cb > cbTarget)
|
|---|
| 209 | return ERROR_FILENAME_EXCED_RANGE;
|
|---|
| 210 |
|
|---|
| 211 | memcpy(pszTarget,
|
|---|
| 212 | pcszSource,
|
|---|
| 213 | cb);
|
|---|
| 214 | return NO_ERROR;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | #ifdef __DEBUG_MALLOC_ENABLED__
|
|---|
| 218 |
|
|---|
| 219 | /*
|
|---|
| 220 | *@@ strhdupDebug:
|
|---|
| 221 | * memory debug version of strhdup.
|
|---|
| 222 | *
|
|---|
| 223 | *@@added V0.9.0 [umoeller]
|
|---|
| 224 | */
|
|---|
| 225 |
|
|---|
| 226 | PSZ strhdupDebug(PCSZ pcszSource,
|
|---|
| 227 | unsigned long *pulLength,
|
|---|
| 228 | PCSZ pcszSourceFile,
|
|---|
| 229 | unsigned long ulLine,
|
|---|
| 230 | PCSZ pcszFunction)
|
|---|
| 231 | {
|
|---|
| 232 | PSZ pszReturn = NULL;
|
|---|
| 233 | ULONG ulLength = 0;
|
|---|
| 234 |
|
|---|
| 235 | if ( (pcszSource)
|
|---|
| 236 | && (ulLength = strlen(pcszSource))
|
|---|
| 237 | )
|
|---|
| 238 | {
|
|---|
| 239 | if (pszReturn = (PSZ)memdMalloc(ulLength + 1,
|
|---|
| 240 | pcszSourceFile, // fixed V0.9.16 (2001-12-08) [umoeller]
|
|---|
| 241 | ulLine,
|
|---|
| 242 | pcszFunction))
|
|---|
| 243 | memcpy(pszReturn, pcszSource, ulLength + 1);
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | if (pulLength)
|
|---|
| 247 | *pulLength = ulLength;
|
|---|
| 248 |
|
|---|
| 249 | return pszReturn;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | #endif // __DEBUG_MALLOC_ENABLED__
|
|---|
| 253 |
|
|---|
| 254 | /*
|
|---|
| 255 | *@@ strhdup:
|
|---|
| 256 | * like strdup, but this one doesn't crash if pszSource
|
|---|
| 257 | * is NULL. Instead, this returns NULL if pcszSource is
|
|---|
| 258 | * NULL or points to a null byte. In addition, this
|
|---|
| 259 | * can report the length of the string (V0.9.16).
|
|---|
| 260 | *
|
|---|
| 261 | *@@added V0.9.0 [umoeller]
|
|---|
| 262 | *@@changed V0.9.16 (2001-10-25) [umoeller]: added pulLength
|
|---|
| 263 | */
|
|---|
| 264 |
|
|---|
| 265 | PSZ strhdup(PCSZ pcszSource,
|
|---|
| 266 | unsigned long *pulLength) // out: length of string excl. null terminator (ptr can be NULL)
|
|---|
| 267 | {
|
|---|
| 268 | PSZ pszReturn = NULL;
|
|---|
| 269 | ULONG ulLength = 0;
|
|---|
| 270 |
|
|---|
| 271 | if ( (pcszSource)
|
|---|
| 272 | && (ulLength = strlen(pcszSource))
|
|---|
| 273 | )
|
|---|
| 274 | {
|
|---|
| 275 | if (pszReturn = (PSZ)malloc(ulLength + 1))
|
|---|
| 276 | memcpy(pszReturn, pcszSource, ulLength + 1);
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | if (pulLength)
|
|---|
| 280 | *pulLength = ulLength;
|
|---|
| 281 |
|
|---|
| 282 | return pszReturn;
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | /*
|
|---|
| 286 | *@@ strhcmp:
|
|---|
| 287 | * better strcmp. This doesn't crash if any of the
|
|---|
| 288 | * string pointers are NULL, but returns a proper
|
|---|
| 289 | * value then.
|
|---|
| 290 | *
|
|---|
| 291 | * Besides, this is guaranteed to only return -1, 0,
|
|---|
| 292 | * or +1, while strcmp can return any positive or
|
|---|
| 293 | * negative value. This is useful for tree comparison
|
|---|
| 294 | * funcs.
|
|---|
| 295 | *
|
|---|
| 296 | *@@added V0.9.9 (2001-02-16) [umoeller]
|
|---|
| 297 | */
|
|---|
| 298 |
|
|---|
| 299 | int strhcmp(PCSZ p1, PCSZ p2)
|
|---|
| 300 | {
|
|---|
| 301 | if (p1 && p2)
|
|---|
| 302 | {
|
|---|
| 303 | int i = strcmp(p1, p2);
|
|---|
| 304 | if (i < 0) return -1;
|
|---|
| 305 | if (i > 0) return +1;
|
|---|
| 306 | }
|
|---|
| 307 | else if (p1)
|
|---|
| 308 | // but p2 is NULL: p1 greater than p2 then
|
|---|
| 309 | return +1;
|
|---|
| 310 | else if (p2)
|
|---|
| 311 | // but p1 is NULL: p1 less than p2 then
|
|---|
| 312 | return -1;
|
|---|
| 313 |
|
|---|
| 314 | // return 0 if strcmp returned 0 above or both strings are NULL
|
|---|
| 315 | return 0;
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | /*
|
|---|
| 319 | *@@ strhicmp:
|
|---|
| 320 | * like strhcmp, but compares without respect
|
|---|
| 321 | * to case.
|
|---|
| 322 | *
|
|---|
| 323 | *@@added V0.9.9 (2001-04-07) [umoeller]
|
|---|
| 324 | */
|
|---|
| 325 |
|
|---|
| 326 | int strhicmp(PCSZ p1, PCSZ p2)
|
|---|
| 327 | {
|
|---|
| 328 | if (p1 && p2)
|
|---|
| 329 | {
|
|---|
| 330 | int i = stricmp(p1, p2);
|
|---|
| 331 | if (i < 0) return -1;
|
|---|
| 332 | if (i > 0) return +1;
|
|---|
| 333 | }
|
|---|
| 334 | else if (p1)
|
|---|
| 335 | // but p2 is NULL: p1 greater than p2 then
|
|---|
| 336 | return +1;
|
|---|
| 337 | else if (p2)
|
|---|
| 338 | // but p1 is NULL: p1 less than p2 then
|
|---|
| 339 | return -1;
|
|---|
| 340 |
|
|---|
| 341 | // return 0 if strcmp returned 0 above or both strings are NULL
|
|---|
| 342 | return 0;
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 | /*
|
|---|
| 346 | *@@ strhistr:
|
|---|
| 347 | * like strstr, but case-insensitive.
|
|---|
| 348 | *
|
|---|
| 349 | *@@changed V0.9.0 [umoeller]: crashed if null pointers were passed, thanks Rdiger Ihle
|
|---|
| 350 | */
|
|---|
| 351 |
|
|---|
| 352 | PSZ strhistr(PCSZ string1, PCSZ string2)
|
|---|
| 353 | {
|
|---|
| 354 | PSZ prc = NULL;
|
|---|
| 355 |
|
|---|
| 356 | if ((string1) && (string2))
|
|---|
| 357 | {
|
|---|
| 358 | PSZ pszSrchIn = strdup(string1);
|
|---|
| 359 | PSZ pszSrchFor = strdup(string2);
|
|---|
| 360 |
|
|---|
| 361 | if ((pszSrchIn) && (pszSrchFor))
|
|---|
| 362 | {
|
|---|
| 363 | strupr(pszSrchIn);
|
|---|
| 364 | strupr(pszSrchFor);
|
|---|
| 365 |
|
|---|
| 366 | if (prc = strstr(pszSrchIn, pszSrchFor))
|
|---|
| 367 | {
|
|---|
| 368 | // prc now has the first occurence of the string,
|
|---|
| 369 | // but in pszSrchIn; we need to map this
|
|---|
| 370 | // return value to the original string
|
|---|
| 371 | prc = (prc-pszSrchIn) // offset in pszSrchIn
|
|---|
| 372 | + (PSZ)string1;
|
|---|
| 373 | }
|
|---|
| 374 | }
|
|---|
| 375 | if (pszSrchFor)
|
|---|
| 376 | free(pszSrchFor);
|
|---|
| 377 | if (pszSrchIn)
|
|---|
| 378 | free(pszSrchIn);
|
|---|
| 379 | }
|
|---|
| 380 |
|
|---|
| 381 | return prc;
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| 384 | /*
|
|---|
| 385 | *@@ strhncpy0:
|
|---|
| 386 | * like strncpy, but always appends a 0 character.
|
|---|
| 387 | *
|
|---|
| 388 | *@@changed V0.9.16 (2002-01-09) [umoeller]: fixed crash on null pszSource
|
|---|
| 389 | */
|
|---|
| 390 |
|
|---|
| 391 | ULONG strhncpy0(PSZ pszTarget,
|
|---|
| 392 | PCSZ pszSource,
|
|---|
| 393 | ULONG cbSource)
|
|---|
| 394 | {
|
|---|
| 395 | ULONG ul = 0;
|
|---|
| 396 | PSZ pTarget = pszTarget,
|
|---|
| 397 | pSource;
|
|---|
| 398 |
|
|---|
| 399 | if (pSource = (PSZ)pszSource) // V0.9.16 (2002-01-09) [umoeller]
|
|---|
| 400 | {
|
|---|
| 401 | for (ul = 0; ul < cbSource; ul++)
|
|---|
| 402 | if (*pSource)
|
|---|
| 403 | *pTarget++ = *pSource++;
|
|---|
| 404 | else
|
|---|
| 405 | break;
|
|---|
| 406 | }
|
|---|
| 407 |
|
|---|
| 408 | *pTarget = 0;
|
|---|
| 409 |
|
|---|
| 410 | return ul;
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | /*
|
|---|
| 414 | *@@ strlcpy:
|
|---|
| 415 | * copies src to string dst of size siz. At most siz-1 characters
|
|---|
| 416 | * will be copied. Always NUL terminates, unless siz == 0.
|
|---|
| 417 | *
|
|---|
| 418 | * Returns strlen(src); if retval >= siz, truncation occurred.
|
|---|
| 419 | *
|
|---|
| 420 | * Taken from the OpenBSD sources at
|
|---|
| 421 | *
|
|---|
| 422 | + ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.c
|
|---|
| 423 | *
|
|---|
| 424 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
|
|---|
| 425 | * All rights reserved.
|
|---|
| 426 | *
|
|---|
| 427 | * OpenBSD licence applies (see top of that file).
|
|---|
| 428 | *
|
|---|
| 429 | *@@added V1.0.1 (2003-01-29) [umoeller]
|
|---|
| 430 | */
|
|---|
| 431 |
|
|---|
| 432 | size_t strlcpy(char *dst,
|
|---|
| 433 | const char *src,
|
|---|
| 434 | size_t siz)
|
|---|
| 435 | {
|
|---|
| 436 | register char *d = dst;
|
|---|
| 437 | register const char *s = src;
|
|---|
| 438 | register size_t n = siz;
|
|---|
| 439 |
|
|---|
| 440 | /* Copy as many bytes as will fit */
|
|---|
| 441 | if (n != 0 && --n != 0)
|
|---|
| 442 | {
|
|---|
| 443 | do
|
|---|
| 444 | {
|
|---|
| 445 | if ((*d++ = *s++) == 0)
|
|---|
| 446 | break;
|
|---|
| 447 | } while (--n != 0);
|
|---|
| 448 | }
|
|---|
| 449 |
|
|---|
| 450 | /* Not enough room in dst, add NUL and traverse rest of src */
|
|---|
| 451 | if (n == 0)
|
|---|
| 452 | {
|
|---|
| 453 | if (siz != 0)
|
|---|
| 454 | *d = '\0'; /* NUL-terminate dst */
|
|---|
| 455 | while (*s++)
|
|---|
| 456 | ;
|
|---|
| 457 | }
|
|---|
| 458 |
|
|---|
| 459 | return (s - src - 1); /* count does not include NUL */
|
|---|
| 460 | }
|
|---|
| 461 |
|
|---|
| 462 | /*
|
|---|
| 463 | *@@ strlcat:
|
|---|
| 464 | * appends src to string dst of size siz. Unlike strncat,
|
|---|
| 465 | * siz is the full size of dst, not space left. At most
|
|---|
| 466 | * siz-1 characters will be copied. Always NUL terminates,
|
|---|
| 467 | * unless siz <= strlen(dst).
|
|---|
| 468 | *
|
|---|
| 469 | * Returns strlen(src) + MIN(siz, strlen(initial dst)),
|
|---|
| 470 | * in other words, strlen(dst) after the concatenation.
|
|---|
| 471 | * If retval >= siz, truncation occurred.
|
|---|
| 472 | *
|
|---|
| 473 | * Taken from the OpenBSD sources at
|
|---|
| 474 | *
|
|---|
| 475 | + ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcat.c
|
|---|
| 476 | *
|
|---|
| 477 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
|
|---|
| 478 | * All rights reserved.
|
|---|
| 479 | *
|
|---|
| 480 | * OpenBSD licence applies (see top of that file).
|
|---|
| 481 | *
|
|---|
| 482 | *@@added V1.0.1 (2003-01-29) [umoeller]
|
|---|
| 483 | */
|
|---|
| 484 |
|
|---|
| 485 | size_t strlcat(char *dst,
|
|---|
| 486 | const char *src,
|
|---|
| 487 | size_t siz)
|
|---|
| 488 | {
|
|---|
| 489 | register char *d = dst;
|
|---|
| 490 | register const char *s = src;
|
|---|
| 491 | register size_t n = siz;
|
|---|
| 492 | size_t dlen;
|
|---|
| 493 |
|
|---|
| 494 | /* Find the end of dst and adjust bytes left but don't go past end */
|
|---|
| 495 | while (n-- != 0 && *d != '\0')
|
|---|
| 496 | d++;
|
|---|
| 497 | dlen = d - dst;
|
|---|
| 498 | n = siz - dlen;
|
|---|
| 499 |
|
|---|
| 500 | if (n == 0)
|
|---|
| 501 | return(dlen + strlen(s));
|
|---|
| 502 | while (*s != '\0')
|
|---|
| 503 | {
|
|---|
| 504 | if (n != 1)
|
|---|
| 505 | {
|
|---|
| 506 | *d++ = *s;
|
|---|
| 507 | n--;
|
|---|
| 508 | }
|
|---|
| 509 | s++;
|
|---|
| 510 | }
|
|---|
| 511 | *d = '\0';
|
|---|
| 512 |
|
|---|
| 513 | return (dlen + (s - src)); /* count does not include NUL */
|
|---|
| 514 | }
|
|---|
| 515 |
|
|---|
| 516 | /*
|
|---|
| 517 | *@@ strhlen:
|
|---|
| 518 | * like strlen, but doesn't crash on
|
|---|
| 519 | * null strings, but returns 0 also.
|
|---|
| 520 | *
|
|---|
| 521 | *@@added V0.9.19 (2002-04-02) [umoeller]
|
|---|
| 522 | */
|
|---|
| 523 |
|
|---|
| 524 | ULONG strhlen(PCSZ pcsz)
|
|---|
| 525 | {
|
|---|
| 526 | if (pcsz)
|
|---|
| 527 | return strlen(pcsz);
|
|---|
| 528 |
|
|---|
| 529 | return 0;
|
|---|
| 530 | }
|
|---|
| 531 |
|
|---|
| 532 | /*
|
|---|
| 533 | *@@ strhSize:
|
|---|
| 534 | * returns the size of the given string, which
|
|---|
| 535 | * is the memory required to allocate a copy,
|
|---|
| 536 | * including the null terminator.
|
|---|
| 537 | *
|
|---|
| 538 | * Returns 0 only if pcsz is NULL. If pcsz
|
|---|
| 539 | * points to a null character, this returns 1.
|
|---|
| 540 | *
|
|---|
| 541 | *@@added V0.9.18 (2002-02-13) [umoeller]
|
|---|
| 542 | *@@changed V0.9.18 (2002-03-27) [umoeller]: now returning 1 for ptr to null byte
|
|---|
| 543 | */
|
|---|
| 544 |
|
|---|
| 545 | ULONG strhSize(PCSZ pcsz)
|
|---|
| 546 | {
|
|---|
| 547 | if (pcsz) // && *pcsz) // V0.9.18 (2002-03-27) [umoeller]
|
|---|
| 548 | return strlen(pcsz) + 1;
|
|---|
| 549 |
|
|---|
| 550 | return 0;
|
|---|
| 551 | }
|
|---|
| 552 |
|
|---|
| 553 | /*
|
|---|
| 554 | * strhCount:
|
|---|
| 555 | * this counts the occurences of c in pszSearch.
|
|---|
| 556 | */
|
|---|
| 557 |
|
|---|
| 558 | ULONG strhCount(PCSZ pszSearch,
|
|---|
| 559 | CHAR c)
|
|---|
| 560 | {
|
|---|
| 561 | PCSZ p = pszSearch;
|
|---|
| 562 | ULONG ulCount = 0;
|
|---|
| 563 | while (TRUE)
|
|---|
| 564 | {
|
|---|
| 565 | if (!(p = strchr(p, c)))
|
|---|
| 566 | return ulCount;
|
|---|
| 567 |
|
|---|
| 568 | ulCount++;
|
|---|
| 569 | p++;
|
|---|
| 570 | }
|
|---|
| 571 | }
|
|---|
| 572 |
|
|---|
| 573 | /*
|
|---|
| 574 | *@@ strhIsDecimal:
|
|---|
| 575 | * returns TRUE if psz consists of decimal digits only.
|
|---|
| 576 | */
|
|---|
| 577 |
|
|---|
| 578 | BOOL strhIsDecimal(PSZ psz)
|
|---|
| 579 | {
|
|---|
| 580 | PSZ p = psz;
|
|---|
| 581 | while (*p != 0)
|
|---|
| 582 | {
|
|---|
| 583 | if (isdigit(*p) == 0)
|
|---|
| 584 | return FALSE;
|
|---|
| 585 | p++;
|
|---|
| 586 | }
|
|---|
| 587 |
|
|---|
| 588 | return TRUE;
|
|---|
| 589 | }
|
|---|
| 590 |
|
|---|
| 591 | #ifdef __DEBUG_MALLOC_ENABLED__
|
|---|
| 592 |
|
|---|
| 593 | /*
|
|---|
| 594 | *@@ strhSubstrDebug:
|
|---|
| 595 | * memory debug version of strhSubstr.
|
|---|
| 596 | *
|
|---|
| 597 | *@@added V0.9.14 (2001-08-01) [umoeller]
|
|---|
| 598 | */
|
|---|
| 599 |
|
|---|
| 600 | PSZ strhSubstrDebug(PCSZ pBegin, // in: first char
|
|---|
| 601 | PCSZ pEnd, // in: last char (not included)
|
|---|
| 602 | PCSZ pcszSourceFile,
|
|---|
| 603 | unsigned long ulLine,
|
|---|
| 604 | PCSZ pcszFunction)
|
|---|
| 605 | {
|
|---|
| 606 | PSZ pszSubstr = NULL;
|
|---|
| 607 |
|
|---|
| 608 | if (pEnd > pBegin) // V0.9.9 (2001-04-04) [umoeller]
|
|---|
| 609 | {
|
|---|
| 610 | ULONG cbSubstr = (pEnd - pBegin);
|
|---|
| 611 | if (pszSubstr = (PSZ)memdMalloc(cbSubstr + 1,
|
|---|
| 612 | pcszSourceFile,
|
|---|
| 613 | ulLine,
|
|---|
| 614 | pcszFunction))
|
|---|
| 615 | {
|
|---|
| 616 | // strhncpy0(pszSubstr, pBegin, cbSubstr);
|
|---|
| 617 | memcpy(pszSubstr, pBegin, cbSubstr); // V0.9.9 (2001-04-04) [umoeller]
|
|---|
| 618 | *(pszSubstr + cbSubstr) = '\0';
|
|---|
| 619 | }
|
|---|
| 620 | }
|
|---|
| 621 |
|
|---|
| 622 | return pszSubstr;
|
|---|
| 623 | }
|
|---|
| 624 |
|
|---|
| 625 | #endif // __DEBUG_MALLOC_ENABLED__
|
|---|
| 626 |
|
|---|
| 627 | /*
|
|---|
| 628 | *@@ strhSubstr:
|
|---|
| 629 | * this creates a new PSZ containing the string
|
|---|
| 630 | * from pBegin to pEnd, excluding the pEnd character.
|
|---|
| 631 | * The new string is null-terminated. The caller
|
|---|
| 632 | * must free() the new string after use.
|
|---|
| 633 | *
|
|---|
| 634 | * Example:
|
|---|
| 635 | + "1234567890"
|
|---|
| 636 | + ^ ^
|
|---|
| 637 | + p1 p2
|
|---|
| 638 | + strhSubstr(p1, p2)
|
|---|
| 639 | * would return a new string containing "2345678".
|
|---|
| 640 | *
|
|---|
| 641 | *@@changed V0.9.9 (2001-04-04) [umoeller]: fixed crashes with invalid pointers
|
|---|
| 642 | *@@changed V0.9.9 (2001-04-04) [umoeller]: now using memcpy for speed
|
|---|
| 643 | */
|
|---|
| 644 |
|
|---|
| 645 | PSZ strhSubstr(PCSZ pBegin, // in: first char
|
|---|
| 646 | PCSZ pEnd) // in: last char (not included)
|
|---|
| 647 | {
|
|---|
| 648 | PSZ pszSubstr = NULL;
|
|---|
| 649 |
|
|---|
| 650 | if (pEnd > pBegin) // V0.9.9 (2001-04-04) [umoeller]
|
|---|
| 651 | {
|
|---|
| 652 | ULONG cbSubstr = (pEnd - pBegin);
|
|---|
| 653 | if (pszSubstr = (PSZ)malloc(cbSubstr + 1))
|
|---|
| 654 | {
|
|---|
| 655 | memcpy(pszSubstr, pBegin, cbSubstr); // V0.9.9 (2001-04-04) [umoeller]
|
|---|
| 656 | *(pszSubstr + cbSubstr) = '\0';
|
|---|
| 657 | }
|
|---|
| 658 | }
|
|---|
| 659 |
|
|---|
| 660 | return pszSubstr;
|
|---|
| 661 | }
|
|---|
| 662 |
|
|---|
| 663 | /*
|
|---|
| 664 | *@@ strhExtract:
|
|---|
| 665 | * searches pszBuf for the cOpen character and returns
|
|---|
| 666 | * the data in between cOpen and cClose, excluding
|
|---|
| 667 | * those two characters, in a newly allocated buffer
|
|---|
| 668 | * which you must free() afterwards.
|
|---|
| 669 | *
|
|---|
| 670 | * Spaces and newlines/linefeeds are skipped.
|
|---|
| 671 | *
|
|---|
| 672 | * If the search was successful, the new buffer
|
|---|
| 673 | * is returned and, if (ppEnd != NULL), *ppEnd points
|
|---|
| 674 | * to the first character after the cClose character
|
|---|
| 675 | * found in the buffer.
|
|---|
| 676 | *
|
|---|
| 677 | * If the search was not successful, NULL is
|
|---|
| 678 | * returned, and *ppEnd is unchanged.
|
|---|
| 679 | *
|
|---|
| 680 | * If another cOpen character is found before
|
|---|
| 681 | * cClose, matching cClose characters will be skipped.
|
|---|
| 682 | * You can therefore nest the cOpen and cClose
|
|---|
| 683 | * characters.
|
|---|
| 684 | *
|
|---|
| 685 | * This function ignores cOpen and cClose characters
|
|---|
| 686 | * in C-style comments and strings surrounded by
|
|---|
| 687 | * double quotes.
|
|---|
| 688 | *
|
|---|
| 689 | * Example:
|
|---|
| 690 | *
|
|---|
| 691 | + PSZ pszBuf = "KEYWORD { --blah-- } next",
|
|---|
| 692 | + pEnd;
|
|---|
| 693 | + strhExtract(pszBuf,
|
|---|
| 694 | + '{', '}',
|
|---|
| 695 | + &pEnd)
|
|---|
| 696 | *
|
|---|
| 697 | * would return a new buffer containing " --blah-- ",
|
|---|
| 698 | * and ppEnd would afterwards point to the space
|
|---|
| 699 | * before "next" in the static buffer.
|
|---|
| 700 | *
|
|---|
| 701 | *@@added V0.9.0 [umoeller]
|
|---|
| 702 | */
|
|---|
| 703 |
|
|---|
| 704 | PSZ strhExtract(PCSZ pszBuf, // in: search buffer
|
|---|
| 705 | CHAR cOpen, // in: opening char
|
|---|
| 706 | CHAR cClose, // in: closing char
|
|---|
| 707 | PCSZ *ppEnd) // out: if != NULL, receives first character after closing char
|
|---|
| 708 | {
|
|---|
| 709 | PSZ pszReturn = NULL;
|
|---|
| 710 | PCSZ pOpen;
|
|---|
| 711 | if ( (pszBuf)
|
|---|
| 712 | && (pOpen = strchr(pszBuf, cOpen))
|
|---|
| 713 | )
|
|---|
| 714 | {
|
|---|
| 715 | // opening char found:
|
|---|
| 716 | // now go thru the whole rest of the buffer
|
|---|
| 717 | PCSZ p = pOpen + 1;
|
|---|
| 718 | LONG lLevel = 1; // if this goes 0, we're done
|
|---|
| 719 | while (*p)
|
|---|
| 720 | {
|
|---|
| 721 | if (*p == cOpen)
|
|---|
| 722 | lLevel++;
|
|---|
| 723 | else if (*p == cClose)
|
|---|
| 724 | {
|
|---|
| 725 | lLevel--;
|
|---|
| 726 | if (lLevel <= 0)
|
|---|
| 727 | {
|
|---|
| 728 | // matching closing bracket found:
|
|---|
| 729 | // extract string
|
|---|
| 730 | pszReturn = strhSubstr(pOpen + 1, // after cOpen
|
|---|
| 731 | p); // excluding cClose
|
|---|
| 732 | if (ppEnd)
|
|---|
| 733 | *ppEnd = p + 1;
|
|---|
| 734 | break; // while (*p)
|
|---|
| 735 | }
|
|---|
| 736 | }
|
|---|
| 737 | else if (*p == '\"')
|
|---|
| 738 | {
|
|---|
| 739 | // beginning of string:
|
|---|
| 740 | PCSZ p2 = p+1;
|
|---|
| 741 | // find end of string
|
|---|
| 742 | while ((*p2) && (*p2 != '\"'))
|
|---|
| 743 | p2++;
|
|---|
| 744 |
|
|---|
| 745 | if (*p2 == '\"')
|
|---|
| 746 | // closing quote found:
|
|---|
| 747 | // search on after that
|
|---|
| 748 | p = p2; // raised below
|
|---|
| 749 | else
|
|---|
| 750 | break; // while (*p)
|
|---|
| 751 | }
|
|---|
| 752 |
|
|---|
| 753 | p++;
|
|---|
| 754 | }
|
|---|
| 755 | }
|
|---|
| 756 |
|
|---|
| 757 | return pszReturn;
|
|---|
| 758 | }
|
|---|
| 759 |
|
|---|
| 760 | /*
|
|---|
| 761 | *@@ strhQuote:
|
|---|
| 762 | * similar to strhExtract, except that
|
|---|
| 763 | * opening and closing chars are the same,
|
|---|
| 764 | * and therefore no nesting is possible.
|
|---|
| 765 | * Useful for extracting stuff between
|
|---|
| 766 | * quotes.
|
|---|
| 767 | *
|
|---|
| 768 | *@@added V0.9.0 [umoeller]
|
|---|
| 769 | */
|
|---|
| 770 |
|
|---|
| 771 | PSZ strhQuote(PSZ pszBuf,
|
|---|
| 772 | CHAR cQuote,
|
|---|
| 773 | PSZ *ppEnd)
|
|---|
| 774 | {
|
|---|
| 775 | PSZ pszReturn = NULL,
|
|---|
| 776 | p1 = NULL;
|
|---|
| 777 | if ((p1 = strchr(pszBuf, cQuote)))
|
|---|
| 778 | {
|
|---|
| 779 | PSZ p2;
|
|---|
| 780 | if (p2 = strchr(p1+1, cQuote))
|
|---|
| 781 | {
|
|---|
| 782 | pszReturn = strhSubstr(p1+1, p2);
|
|---|
| 783 | if (ppEnd)
|
|---|
| 784 | // store closing char
|
|---|
| 785 | *ppEnd = p2 + 1;
|
|---|
| 786 | }
|
|---|
| 787 | }
|
|---|
| 788 |
|
|---|
| 789 | return pszReturn;
|
|---|
| 790 | }
|
|---|
| 791 |
|
|---|
| 792 | /*
|
|---|
| 793 | *@@ strhStrip:
|
|---|
| 794 | * removes all double spaces.
|
|---|
| 795 | * This copies within the "psz" buffer.
|
|---|
| 796 | * If any double spaces are found, the
|
|---|
| 797 | * string will be shorter than before,
|
|---|
| 798 | * but the buffer is _not_ reallocated,
|
|---|
| 799 | * so there will be unused bytes at the
|
|---|
| 800 | * end.
|
|---|
| 801 | *
|
|---|
| 802 | * Returns the number of spaces removed.
|
|---|
| 803 | *
|
|---|
| 804 | *@@added V0.9.0 [umoeller]
|
|---|
| 805 | */
|
|---|
| 806 |
|
|---|
| 807 | ULONG strhStrip(PSZ psz) // in/out: string
|
|---|
| 808 | {
|
|---|
| 809 | PSZ p;
|
|---|
| 810 | ULONG cb = strlen(psz),
|
|---|
| 811 | ulrc = 0;
|
|---|
| 812 |
|
|---|
| 813 | for (p = psz; p < psz+cb; p++)
|
|---|
| 814 | {
|
|---|
| 815 | if ((*p == ' ') && (*(p+1) == ' '))
|
|---|
| 816 | {
|
|---|
| 817 | PSZ p2 = p;
|
|---|
| 818 | while (*p2)
|
|---|
| 819 | {
|
|---|
| 820 | *p2 = *(p2+1);
|
|---|
| 821 | p2++;
|
|---|
| 822 | }
|
|---|
| 823 | cb--;
|
|---|
| 824 | p--;
|
|---|
| 825 | ulrc++;
|
|---|
| 826 | }
|
|---|
| 827 | }
|
|---|
| 828 | return ulrc;
|
|---|
| 829 | }
|
|---|
| 830 |
|
|---|
| 831 | /*
|
|---|
| 832 | *@@ strhins:
|
|---|
| 833 | * this inserts one string into another.
|
|---|
| 834 | *
|
|---|
| 835 | * pszInsert is inserted into pszBuffer at offset
|
|---|
| 836 | * ulInsertOfs (which counts from 0).
|
|---|
| 837 | *
|
|---|
| 838 | * A newly allocated string is returned. pszBuffer is
|
|---|
| 839 | * not changed. The new string should be free()'d after
|
|---|
| 840 | * use.
|
|---|
| 841 | *
|
|---|
| 842 | * Upon errors, NULL is returned.
|
|---|
| 843 | *
|
|---|
| 844 | *@@changed V0.9.0 [umoeller]: completely rewritten.
|
|---|
| 845 | */
|
|---|
| 846 |
|
|---|
| 847 | PSZ strhins(PCSZ pcszBuffer,
|
|---|
| 848 | ULONG ulInsertOfs,
|
|---|
| 849 | PCSZ pcszInsert)
|
|---|
| 850 | {
|
|---|
| 851 | PSZ pszNew = NULL;
|
|---|
| 852 |
|
|---|
| 853 | if ((pcszBuffer) && (pcszInsert))
|
|---|
| 854 | {
|
|---|
| 855 | do {
|
|---|
| 856 | ULONG cbBuffer = strlen(pcszBuffer);
|
|---|
| 857 | ULONG cbInsert = strlen(pcszInsert);
|
|---|
| 858 |
|
|---|
| 859 | // check string length
|
|---|
| 860 | if (ulInsertOfs > cbBuffer + 1)
|
|---|
| 861 | break; // do
|
|---|
| 862 |
|
|---|
| 863 | // OK, let's go.
|
|---|
| 864 | pszNew = (PSZ)malloc(cbBuffer + cbInsert + 1); // additional null terminator
|
|---|
| 865 |
|
|---|
| 866 | // copy stuff before pInsertPos
|
|---|
| 867 | memcpy(pszNew,
|
|---|
| 868 | pcszBuffer,
|
|---|
| 869 | ulInsertOfs);
|
|---|
| 870 | // copy string to be inserted
|
|---|
| 871 | memcpy(pszNew + ulInsertOfs,
|
|---|
| 872 | pcszInsert,
|
|---|
| 873 | cbInsert);
|
|---|
| 874 | // copy stuff after pInsertPos
|
|---|
| 875 | strcpy(pszNew + ulInsertOfs + cbInsert,
|
|---|
| 876 | pcszBuffer + ulInsertOfs);
|
|---|
| 877 | } while (FALSE);
|
|---|
| 878 | }
|
|---|
| 879 |
|
|---|
| 880 | return pszNew;
|
|---|
| 881 | }
|
|---|
| 882 |
|
|---|
| 883 | /*
|
|---|
| 884 | *@@ strhFindReplace:
|
|---|
| 885 | * wrapper around xstrFindReplace to work with C strings.
|
|---|
| 886 | * Note that *ppszBuf can get reallocated and must
|
|---|
| 887 | * be free()'able.
|
|---|
| 888 | *
|
|---|
| 889 | * Repetitive use of this wrapper is not recommended
|
|---|
| 890 | * because it is considerably slower than xstrFindReplace.
|
|---|
| 891 | *
|
|---|
| 892 | *@@added V0.9.6 (2000-11-01) [umoeller]
|
|---|
| 893 | *@@changed V0.9.7 (2001-01-15) [umoeller]: renamed from strhrpl
|
|---|
| 894 | */
|
|---|
| 895 |
|
|---|
| 896 | ULONG strhFindReplace(PSZ *ppszBuf, // in/out: string
|
|---|
| 897 | PULONG pulOfs, // in: where to begin search (0 = start);
|
|---|
| 898 | // out: ofs of first char after replacement string
|
|---|
| 899 | PCSZ pcszSearch, // in: search string; cannot be NULL
|
|---|
| 900 | PCSZ pcszReplace) // in: replacement string; cannot be NULL
|
|---|
| 901 | {
|
|---|
| 902 | ULONG ulrc = 0;
|
|---|
| 903 | XSTRING xstrBuf,
|
|---|
| 904 | xstrFind,
|
|---|
| 905 | xstrReplace;
|
|---|
| 906 | size_t ShiftTable[256];
|
|---|
| 907 | BOOL fRepeat = FALSE;
|
|---|
| 908 | xstrInitSet(&xstrBuf, *ppszBuf);
|
|---|
| 909 | // reallocated and returned, so we're safe
|
|---|
| 910 | xstrInitSet(&xstrFind, (PSZ)pcszSearch);
|
|---|
| 911 | xstrInitSet(&xstrReplace, (PSZ)pcszReplace);
|
|---|
| 912 | // these two are never freed, so we're safe too
|
|---|
| 913 |
|
|---|
| 914 | if ((ulrc = xstrFindReplace(&xstrBuf,
|
|---|
| 915 | pulOfs,
|
|---|
| 916 | &xstrFind,
|
|---|
| 917 | &xstrReplace,
|
|---|
| 918 | ShiftTable,
|
|---|
| 919 | &fRepeat)))
|
|---|
| 920 | // replaced:
|
|---|
| 921 | *ppszBuf = xstrBuf.psz;
|
|---|
| 922 |
|
|---|
| 923 | return ulrc;
|
|---|
| 924 | }
|
|---|
| 925 |
|
|---|
| 926 | /*
|
|---|
| 927 | * strhWords:
|
|---|
| 928 | * returns the no. of words in "psz".
|
|---|
| 929 | * A string is considered a "word" if
|
|---|
| 930 | * it is surrounded by spaces only.
|
|---|
| 931 | *
|
|---|
| 932 | *@@added V0.9.0 [umoeller]
|
|---|
| 933 | */
|
|---|
| 934 |
|
|---|
| 935 | ULONG strhWords(PSZ psz)
|
|---|
| 936 | {
|
|---|
| 937 | PSZ p;
|
|---|
| 938 | ULONG cb = strlen(psz),
|
|---|
| 939 | ulWords = 0;
|
|---|
| 940 | if (cb > 1)
|
|---|
| 941 | {
|
|---|
| 942 | ulWords = 1;
|
|---|
| 943 | for (p = psz; p < psz+cb; p++)
|
|---|
| 944 | if (*p == ' ')
|
|---|
| 945 | ulWords++;
|
|---|
| 946 | }
|
|---|
| 947 | return ulWords;
|
|---|
| 948 | }
|
|---|
| 949 |
|
|---|
| 950 | /*
|
|---|
| 951 | *@@ strhGetWord:
|
|---|
| 952 | * finds word boundaries.
|
|---|
| 953 | *
|
|---|
| 954 | * *ppszStart is used as the beginning of the
|
|---|
| 955 | * search.
|
|---|
| 956 | *
|
|---|
| 957 | * If a word is found, *ppszStart is set to
|
|---|
| 958 | * the first character of the word which was
|
|---|
| 959 | * found and *ppszEnd receives the address
|
|---|
| 960 | * of the first character _after_ the word,
|
|---|
| 961 | * which is probably a space or a \n or \r char.
|
|---|
| 962 | * We then return TRUE.
|
|---|
| 963 | *
|
|---|
| 964 | * The search is stopped if a null character
|
|---|
| 965 | * is found or pLimit is reached. In that case,
|
|---|
| 966 | * FALSE is returned.
|
|---|
| 967 | *
|
|---|
| 968 | *@@added V0.9.1 (2000-02-13) [umoeller]
|
|---|
| 969 | */
|
|---|
| 970 |
|
|---|
| 971 | BOOL strhGetWord(PSZ *ppszStart, // in: start of search,
|
|---|
| 972 | // out: start of word (if TRUE is returned)
|
|---|
| 973 | PCSZ pLimit, // in: ptr to last char after *ppszStart to be
|
|---|
| 974 | // searched; if the word does not end before
|
|---|
| 975 | // or with this char, FALSE is returned
|
|---|
| 976 | PCSZ pcszBeginChars, // stringh.h defines STRH_BEGIN_CHARS
|
|---|
| 977 | PCSZ pcszEndChars, // stringh.h defines STRH_END_CHARS
|
|---|
| 978 | PSZ *ppszEnd) // out: first char _after_ word
|
|---|
| 979 | // (if TRUE is returned)
|
|---|
| 980 | {
|
|---|
| 981 | // characters after which a word can be started
|
|---|
| 982 | // PCSZ pcszBeginChars = "\x0d\x0a ";
|
|---|
| 983 | // PCSZ pcszEndChars = "\x0d\x0a /-";
|
|---|
| 984 |
|
|---|
| 985 | PSZ pStart = *ppszStart;
|
|---|
| 986 |
|
|---|
| 987 | // find start of word
|
|---|
| 988 | while ( (pStart < (PSZ)pLimit)
|
|---|
| 989 | && (strchr(pcszBeginChars, *pStart))
|
|---|
| 990 | )
|
|---|
| 991 | // if char is a "before word" char: go for next
|
|---|
| 992 | pStart++;
|
|---|
| 993 |
|
|---|
| 994 | if (pStart < (PSZ)pLimit)
|
|---|
| 995 | {
|
|---|
| 996 | // found a valid "word start" character
|
|---|
| 997 | // (which is not in pcszBeginChars):
|
|---|
| 998 |
|
|---|
| 999 | // find end of word
|
|---|
| 1000 | PSZ pEndOfWord = pStart;
|
|---|
| 1001 | while ( (pEndOfWord <= (PSZ)pLimit)
|
|---|
| 1002 | && (strchr(pcszEndChars, *pEndOfWord) == 0)
|
|---|
| 1003 | )
|
|---|
| 1004 | // if char is not an "end word" char: go for next
|
|---|
| 1005 | pEndOfWord++;
|
|---|
| 1006 |
|
|---|
| 1007 | if (pEndOfWord <= (PSZ)pLimit)
|
|---|
| 1008 | {
|
|---|
| 1009 | // whoa, got a word:
|
|---|
| 1010 | *ppszStart = pStart;
|
|---|
| 1011 | *ppszEnd = pEndOfWord;
|
|---|
| 1012 | return TRUE;
|
|---|
| 1013 | }
|
|---|
| 1014 | }
|
|---|
| 1015 |
|
|---|
| 1016 | return FALSE;
|
|---|
| 1017 | }
|
|---|
| 1018 |
|
|---|
| 1019 | /*
|
|---|
| 1020 | *@@ strhIsWord:
|
|---|
| 1021 | * returns TRUE if p points to a "word"
|
|---|
| 1022 | * in pcszBuf.
|
|---|
| 1023 | *
|
|---|
| 1024 | * p is considered a word if the character _before_
|
|---|
| 1025 | * it is in pcszBeginChars and the char _after_
|
|---|
| 1026 | * it (i.e. *(p+cbSearch)) is in pcszEndChars.
|
|---|
| 1027 | *
|
|---|
| 1028 | *@@added V0.9.6 (2000-11-12) [umoeller]
|
|---|
| 1029 | *@@changed V0.9.18 (2002-02-23) [umoeller]: fixed end char check
|
|---|
| 1030 | */
|
|---|
| 1031 |
|
|---|
| 1032 | BOOL strhIsWord(PCSZ pcszBuf,
|
|---|
| 1033 | PCSZ p, // in: start of word
|
|---|
| 1034 | ULONG cbSearch, // in: length of word
|
|---|
| 1035 | PCSZ pcszBeginChars, // suggestion: "\x0d\x0a ()/\\-,."
|
|---|
| 1036 | PCSZ pcszEndChars) // suggestion: "\x0d\x0a ()/\\-,.:;"
|
|---|
| 1037 | {
|
|---|
| 1038 | // check previous char
|
|---|
| 1039 | if ( (p == pcszBuf)
|
|---|
| 1040 | || (strchr(pcszBeginChars, *(p-1)))
|
|---|
| 1041 | )
|
|---|
| 1042 | {
|
|---|
| 1043 | // OK, valid begin char:
|
|---|
| 1044 | // check end char
|
|---|
| 1045 | CHAR cNextChar;
|
|---|
| 1046 | if (!(cNextChar = p[cbSearch]))
|
|---|
| 1047 | // null terminator:
|
|---|
| 1048 | return TRUE;
|
|---|
| 1049 | else
|
|---|
| 1050 | {
|
|---|
| 1051 | // not null terminator: check if char is
|
|---|
| 1052 | // in the list of valid end chars
|
|---|
| 1053 | if (strchr(pcszEndChars, cNextChar))
|
|---|
| 1054 | {
|
|---|
| 1055 | // OK, is end char: avoid doubles of that char,
|
|---|
| 1056 | // but allow spaces
|
|---|
| 1057 | // fixed V0.9.18 (2002-02-23) [umoeller]
|
|---|
| 1058 | CHAR cNextNext = p[cbSearch + 1];
|
|---|
| 1059 | if ( (cNextNext != cNextChar)
|
|---|
| 1060 | || (cNextNext == ' ')
|
|---|
| 1061 | || (cNextNext == 0)
|
|---|
| 1062 | )
|
|---|
| 1063 | return TRUE;
|
|---|
| 1064 | }
|
|---|
| 1065 | }
|
|---|
| 1066 | }
|
|---|
| 1067 |
|
|---|
| 1068 | return FALSE;
|
|---|
| 1069 | }
|
|---|
| 1070 |
|
|---|
| 1071 | /*
|
|---|
| 1072 | *@@ strhFindWord:
|
|---|
| 1073 | * searches for pszSearch in pszBuf, which is
|
|---|
| 1074 | * returned if found (or NULL if not).
|
|---|
| 1075 | *
|
|---|
| 1076 | * As opposed to strstr, this finds pszSearch
|
|---|
| 1077 | * only if it is a "word". A search string is
|
|---|
| 1078 | * considered a word if the character _before_
|
|---|
| 1079 | * it is in pcszBeginChars and the char _after_
|
|---|
| 1080 | * it is in pcszEndChars.
|
|---|
| 1081 | *
|
|---|
| 1082 | * Example:
|
|---|
| 1083 | + strhFindWord("This is an example.", "is");
|
|---|
| 1084 | + returns ...........^ this, but not the "is" in "This".
|
|---|
| 1085 | *
|
|---|
| 1086 | * The algorithm here uses strstr to find pszSearch in pszBuf
|
|---|
| 1087 | * and performs additional "is-word" checks for each item found
|
|---|
| 1088 | * (by calling strhIsWord).
|
|---|
| 1089 | *
|
|---|
| 1090 | * Note that this function is fairly slow compared to xstrFindWord.
|
|---|
| 1091 | *
|
|---|
| 1092 | *@@added V0.9.0 (99-11-08) [umoeller]
|
|---|
| 1093 | *@@changed V0.9.0 (99-11-10) [umoeller]: tried second algorithm, reverted to original...
|
|---|
| 1094 | */
|
|---|
| 1095 |
|
|---|
| 1096 | PSZ strhFindWord(PCSZ pszBuf,
|
|---|
| 1097 | PCSZ pszSearch,
|
|---|
| 1098 | PCSZ pcszBeginChars, // suggestion: "\x0d\x0a ()/\\-,."
|
|---|
| 1099 | PCSZ pcszEndChars) // suggestion: "\x0d\x0a ()/\\-,.:;"
|
|---|
| 1100 | {
|
|---|
| 1101 | PSZ pszReturn = 0;
|
|---|
| 1102 | ULONG cbBuf = strlen(pszBuf),
|
|---|
| 1103 | cbSearch = strlen(pszSearch);
|
|---|
| 1104 |
|
|---|
| 1105 | if ((cbBuf) && (cbSearch))
|
|---|
| 1106 | {
|
|---|
| 1107 | PCSZ p = pszBuf;
|
|---|
| 1108 |
|
|---|
| 1109 | do // while p
|
|---|
| 1110 | {
|
|---|
| 1111 | p = strstr(p, pszSearch);
|
|---|
| 1112 | if (p)
|
|---|
| 1113 | {
|
|---|
| 1114 | // string found:
|
|---|
| 1115 | // check if that's a word
|
|---|
| 1116 |
|
|---|
| 1117 | if (strhIsWord(pszBuf,
|
|---|
| 1118 | p,
|
|---|
| 1119 | cbSearch,
|
|---|
| 1120 | pcszBeginChars,
|
|---|
| 1121 | pcszEndChars))
|
|---|
| 1122 | {
|
|---|
| 1123 | // valid end char:
|
|---|
| 1124 | pszReturn = (PSZ)p;
|
|---|
| 1125 | break;
|
|---|
| 1126 | }
|
|---|
| 1127 |
|
|---|
| 1128 | p += cbSearch;
|
|---|
| 1129 | }
|
|---|
| 1130 | } while (p);
|
|---|
| 1131 |
|
|---|
| 1132 | }
|
|---|
| 1133 | return pszReturn;
|
|---|
| 1134 | }
|
|---|
| 1135 |
|
|---|
| 1136 | /*
|
|---|
| 1137 | *@@ strhFindEOL:
|
|---|
| 1138 | * returns a pointer to the next \r, \n or null character
|
|---|
| 1139 | * following pszSearchIn. Stores the offset in *pulOffset.
|
|---|
| 1140 | *
|
|---|
| 1141 | * This should never return NULL because at some point,
|
|---|
| 1142 | * there will be a null byte in your string.
|
|---|
| 1143 | *
|
|---|
| 1144 | *@@added V0.9.4 (2000-07-01) [umoeller]
|
|---|
| 1145 | */
|
|---|
| 1146 |
|
|---|
| 1147 | PSZ strhFindEOL(PCSZ pcszSearchIn, // in: where to search
|
|---|
| 1148 | PULONG pulOffset) // out: offset (ptr can be NULL)
|
|---|
| 1149 | {
|
|---|
| 1150 | PCSZ p = pcszSearchIn,
|
|---|
| 1151 | prc = 0;
|
|---|
| 1152 | while (TRUE)
|
|---|
| 1153 | {
|
|---|
| 1154 | if ( (*p == '\r') || (*p == '\n') || (*p == 0) )
|
|---|
| 1155 | {
|
|---|
| 1156 | prc = p;
|
|---|
| 1157 | break;
|
|---|
| 1158 | }
|
|---|
| 1159 | p++;
|
|---|
| 1160 | }
|
|---|
| 1161 |
|
|---|
| 1162 | if ((pulOffset) && (prc))
|
|---|
| 1163 | *pulOffset = prc - pcszSearchIn;
|
|---|
| 1164 |
|
|---|
| 1165 | return (PSZ)prc;
|
|---|
| 1166 | }
|
|---|
| 1167 |
|
|---|
| 1168 | /*
|
|---|
| 1169 | *@@ strhFindNextLine:
|
|---|
| 1170 | * like strhFindEOL, but this returns the character
|
|---|
| 1171 | * _after_ \r or \n. Note that this might return
|
|---|
| 1172 | * a pointer to terminating NULL character also.
|
|---|
| 1173 | */
|
|---|
| 1174 |
|
|---|
| 1175 | PSZ strhFindNextLine(PSZ pszSearchIn, PULONG pulOffset)
|
|---|
| 1176 | {
|
|---|
| 1177 | PSZ pEOL = strhFindEOL(pszSearchIn, NULL);
|
|---|
| 1178 | // pEOL now points to the \r char or the terminating 0 byte;
|
|---|
| 1179 | // if not null byte, advance pointer
|
|---|
| 1180 | PSZ pNextLine = pEOL;
|
|---|
| 1181 | if (*pNextLine == '\r')
|
|---|
| 1182 | pNextLine++;
|
|---|
| 1183 | if (*pNextLine == '\n')
|
|---|
| 1184 | pNextLine++;
|
|---|
| 1185 | if (pulOffset)
|
|---|
| 1186 | *pulOffset = pNextLine - pszSearchIn;
|
|---|
| 1187 | return pNextLine;
|
|---|
| 1188 | }
|
|---|
| 1189 |
|
|---|
| 1190 | /*
|
|---|
| 1191 | *@@ strhBeautifyTitle:
|
|---|
| 1192 | * replaces all line breaks (0xd, 0xa) with spaces.
|
|---|
| 1193 | * Returns the new length of the string or 0 on
|
|---|
| 1194 | * errors.
|
|---|
| 1195 | *
|
|---|
| 1196 | *@@changed V0.9.12 (2001-05-17) [pr]: multiple line break chars. end up as only 1 space
|
|---|
| 1197 | *@@changed V0.9.19 (2002-06-18) [umoeller]: now returning length
|
|---|
| 1198 | */
|
|---|
| 1199 |
|
|---|
| 1200 | ULONG strhBeautifyTitle(PSZ psz)
|
|---|
| 1201 | {
|
|---|
| 1202 | ULONG ulrc;
|
|---|
| 1203 | PSZ p = psz;
|
|---|
| 1204 |
|
|---|
| 1205 | while (*p)
|
|---|
| 1206 | {
|
|---|
| 1207 | if ( (*p == '\r')
|
|---|
| 1208 | || (*p == '\n')
|
|---|
| 1209 | )
|
|---|
| 1210 | {
|
|---|
| 1211 | if ( (p != psz)
|
|---|
| 1212 | && (p[-1] == ' ')
|
|---|
| 1213 | )
|
|---|
| 1214 | memmove(p, p + 1, strlen(p));
|
|---|
| 1215 | else
|
|---|
| 1216 | *p++ = ' ';
|
|---|
| 1217 | }
|
|---|
| 1218 | else
|
|---|
| 1219 | p++;
|
|---|
| 1220 | }
|
|---|
| 1221 |
|
|---|
| 1222 | return (p - psz);
|
|---|
| 1223 | }
|
|---|
| 1224 |
|
|---|
| 1225 | /*
|
|---|
| 1226 | *@@ strhBeautifyTitle:
|
|---|
| 1227 | * like strhBeautifyTitle, but copies into
|
|---|
| 1228 | * a new buffer. More efficient.
|
|---|
| 1229 | *
|
|---|
| 1230 | *@@added V0.9.19 (2002-06-18) [umoeller]
|
|---|
| 1231 | */
|
|---|
| 1232 |
|
|---|
| 1233 | ULONG strhBeautifyTitle2(PSZ pszTarget, // out: beautified string
|
|---|
| 1234 | PCSZ pcszSource) // in: string to be beautified (can be NULL)
|
|---|
| 1235 | {
|
|---|
| 1236 | ULONG ulrc;
|
|---|
| 1237 | PCSZ pSource = pcszSource;
|
|---|
| 1238 | PSZ pTarget = pszTarget;
|
|---|
| 1239 | CHAR c;
|
|---|
| 1240 | if (!pcszSource)
|
|---|
| 1241 | {
|
|---|
| 1242 | *pszTarget = '\0';
|
|---|
| 1243 | return 0;
|
|---|
| 1244 | }
|
|---|
| 1245 |
|
|---|
| 1246 | while (c = *pSource++)
|
|---|
| 1247 | {
|
|---|
| 1248 | if ( (c == '\r')
|
|---|
| 1249 | || (c == '\n')
|
|---|
| 1250 | )
|
|---|
| 1251 | {
|
|---|
| 1252 | if ( (pTarget == pszTarget)
|
|---|
| 1253 | || (pTarget[-1] != ' ')
|
|---|
| 1254 | )
|
|---|
| 1255 | *pTarget++ = ' ';
|
|---|
| 1256 | }
|
|---|
| 1257 | else
|
|---|
| 1258 | *pTarget++ = c;
|
|---|
| 1259 | }
|
|---|
| 1260 |
|
|---|
| 1261 | // null-terminate
|
|---|
| 1262 | *pTarget = '\0';
|
|---|
| 1263 |
|
|---|
| 1264 | return (pTarget - pszTarget);
|
|---|
| 1265 | }
|
|---|
| 1266 |
|
|---|
| 1267 | /*
|
|---|
| 1268 | *@@ strhKillChar:
|
|---|
| 1269 | * removes the first occurence of c in psz
|
|---|
| 1270 | * by overwriting it with the following characters.
|
|---|
| 1271 | *
|
|---|
| 1272 | * For this to work, you _must_ pass in strlen(psz)
|
|---|
| 1273 | * in the ULONG pointed to by ulLength. If
|
|---|
| 1274 | *
|
|---|
| 1275 | * Returns TRUE only if c was actually found. In
|
|---|
| 1276 | * that case, *pulLength is decremented.
|
|---|
| 1277 | *
|
|---|
| 1278 | *@@added V1.0.1 (2003-01-30) [umoeller]
|
|---|
| 1279 | */
|
|---|
| 1280 |
|
|---|
| 1281 | BOOL strhKillChar(PSZ psz,
|
|---|
| 1282 | CHAR c,
|
|---|
| 1283 | PULONG pulLength)
|
|---|
| 1284 | {
|
|---|
| 1285 | PSZ p;
|
|---|
| 1286 | if (p = strchr(psz, c))
|
|---|
| 1287 | {
|
|---|
| 1288 | // "string~rest"
|
|---|
| 1289 | // ÀÄÄÄÄÄÙ 6 chars (p - pszBuf)
|
|---|
| 1290 | // ÀÄÄÄÄÄÄÄÄÄÄÙ 11 chars (ulLen)
|
|---|
| 1291 | // ^ p (pszBuf + 6)
|
|---|
| 1292 | // ^ pszBuf
|
|---|
| 1293 | memmove(p, // pszBuf + 6
|
|---|
| 1294 | p + 1, // pszBuf + 7
|
|---|
| 1295 | // include null byte
|
|---|
| 1296 | *pulLength // 11
|
|---|
| 1297 | - (p - psz)); // - 6 = 5
|
|---|
| 1298 | --(*pulLength);
|
|---|
| 1299 |
|
|---|
| 1300 | return TRUE;
|
|---|
| 1301 | }
|
|---|
| 1302 |
|
|---|
| 1303 | return FALSE;
|
|---|
| 1304 | }
|
|---|
| 1305 |
|
|---|
| 1306 | /*
|
|---|
| 1307 | * strhFindAttribValue:
|
|---|
| 1308 | * searches for pszAttrib in pszSearchIn; if found,
|
|---|
| 1309 | * returns the first character after the "=" char.
|
|---|
| 1310 | * If "=" is not found, a space, \r, and \n are
|
|---|
| 1311 | * also accepted. This function searches without
|
|---|
| 1312 | * respecting case.
|
|---|
| 1313 | *
|
|---|
| 1314 | * <B>Example:</B>
|
|---|
| 1315 | + strhFindAttribValue("<PAGE BLAH=\"data\">", "BLAH")
|
|---|
| 1316 | +
|
|---|
| 1317 | + returns ....................... ^ this address.
|
|---|
| 1318 | *
|
|---|
| 1319 | *@@added V0.9.0 [umoeller]
|
|---|
| 1320 | *@@changed V0.9.3 (2000-05-19) [umoeller]: some speed optimizations
|
|---|
| 1321 | *@@changed V0.9.12 (2001-05-22) [umoeller]: fixed space bug, thanks Yuri Dario
|
|---|
| 1322 | */
|
|---|
| 1323 |
|
|---|
| 1324 | PSZ strhFindAttribValue(const char *pszSearchIn, const char *pszAttrib)
|
|---|
| 1325 | {
|
|---|
| 1326 | PSZ prc = 0;
|
|---|
| 1327 | PSZ pszSearchIn2, p;
|
|---|
| 1328 | ULONG cbAttrib = strlen(pszAttrib),
|
|---|
| 1329 | ulLength = strlen(pszSearchIn);
|
|---|
| 1330 |
|
|---|
| 1331 | // use alloca(), so memory is freed on function exit
|
|---|
| 1332 | pszSearchIn2 = (PSZ)alloca(ulLength + 1);
|
|---|
| 1333 | memcpy(pszSearchIn2, pszSearchIn, ulLength + 1);
|
|---|
| 1334 |
|
|---|
| 1335 | // 1) find token, (space char, \n, \r, \t)
|
|---|
| 1336 | p = strtok(pszSearchIn2, " \n\r\t");
|
|---|
| 1337 | while (p)
|
|---|
| 1338 | {
|
|---|
| 1339 | CHAR c2;
|
|---|
| 1340 | PSZ pOrig;
|
|---|
| 1341 |
|
|---|
| 1342 | // check tag name
|
|---|
| 1343 | if (!strnicmp(p, pszAttrib, cbAttrib))
|
|---|
| 1344 | {
|
|---|
| 1345 | // position in original string
|
|---|
| 1346 | pOrig = (PSZ)pszSearchIn + (p - pszSearchIn2);
|
|---|
| 1347 |
|
|---|
| 1348 | // yes:
|
|---|
| 1349 | prc = pOrig + cbAttrib;
|
|---|
| 1350 | c2 = *prc;
|
|---|
| 1351 | while ( ( (c2 == ' ')
|
|---|
| 1352 | || (c2 == '=')
|
|---|
| 1353 | || (c2 == '\n')
|
|---|
| 1354 | || (c2 == '\r')
|
|---|
| 1355 | )
|
|---|
| 1356 | && (c2 != 0)
|
|---|
| 1357 | )
|
|---|
| 1358 | c2 = *++prc;
|
|---|
| 1359 |
|
|---|
| 1360 | break;
|
|---|
| 1361 | }
|
|---|
| 1362 |
|
|---|
| 1363 | p = strtok(NULL, " \n\r\t");
|
|---|
| 1364 | }
|
|---|
| 1365 |
|
|---|
| 1366 | return prc;
|
|---|
| 1367 | }
|
|---|
| 1368 |
|
|---|
| 1369 | /* PSZ strhFindAttribValue(const char *pszSearchIn, const char *pszAttrib)
|
|---|
| 1370 | {
|
|---|
| 1371 | PSZ prc = 0;
|
|---|
| 1372 | PSZ pszSearchIn2 = (PSZ)pszSearchIn,
|
|---|
| 1373 | p,
|
|---|
| 1374 | p2;
|
|---|
| 1375 | ULONG cbAttrib = strlen(pszAttrib);
|
|---|
| 1376 |
|
|---|
| 1377 | // 1) find space char
|
|---|
| 1378 | while ((p = strchr(pszSearchIn2, ' ')))
|
|---|
| 1379 | {
|
|---|
| 1380 | CHAR c;
|
|---|
| 1381 | p++;
|
|---|
| 1382 | if (strlen(p) >= cbAttrib) // V0.9.9 (2001-03-27) [umoeller]
|
|---|
| 1383 | {
|
|---|
| 1384 | c = *(p+cbAttrib); // V0.9.3 (2000-05-19) [umoeller]
|
|---|
| 1385 | // now check whether the p+strlen(pszAttrib)
|
|---|
| 1386 | // is a valid end-of-tag character
|
|---|
| 1387 | if ( (memicmp(p, (PVOID)pszAttrib, cbAttrib) == 0)
|
|---|
| 1388 | && ( (c == ' ')
|
|---|
| 1389 | || (c == '>')
|
|---|
| 1390 | || (c == '=')
|
|---|
| 1391 | || (c == '\r')
|
|---|
| 1392 | || (c == '\n')
|
|---|
| 1393 | || (c == 0)
|
|---|
| 1394 | )
|
|---|
| 1395 | )
|
|---|
| 1396 | {
|
|---|
| 1397 | // yes:
|
|---|
| 1398 | CHAR c2;
|
|---|
| 1399 | p2 = p + cbAttrib;
|
|---|
| 1400 | c2 = *p2;
|
|---|
| 1401 | while ( ( (c2 == ' ')
|
|---|
| 1402 | || (c2 == '=')
|
|---|
| 1403 | || (c2 == '\n')
|
|---|
| 1404 | || (c2 == '\r')
|
|---|
| 1405 | )
|
|---|
| 1406 | && (c2 != 0)
|
|---|
| 1407 | )
|
|---|
| 1408 | c2 = *++p2;
|
|---|
| 1409 |
|
|---|
| 1410 | prc = p2;
|
|---|
| 1411 | break; // first while
|
|---|
| 1412 | }
|
|---|
| 1413 | }
|
|---|
| 1414 | else
|
|---|
| 1415 | break;
|
|---|
| 1416 |
|
|---|
| 1417 | pszSearchIn2++;
|
|---|
| 1418 | }
|
|---|
| 1419 | return prc;
|
|---|
| 1420 | } */
|
|---|
| 1421 |
|
|---|
| 1422 | /*
|
|---|
| 1423 | * strhGetNumAttribValue:
|
|---|
| 1424 | * stores the numerical parameter value of an HTML-style
|
|---|
| 1425 | * tag in *pl.
|
|---|
| 1426 | *
|
|---|
| 1427 | * Returns the address of the tag parameter in the
|
|---|
| 1428 | * search buffer, if found, or NULL.
|
|---|
| 1429 | *
|
|---|
| 1430 | * <B>Example:</B>
|
|---|
| 1431 | + strhGetNumAttribValue("<PAGE BLAH=123>, "BLAH", &l);
|
|---|
| 1432 | *
|
|---|
| 1433 | * stores 123 in the "l" variable.
|
|---|
| 1434 | *
|
|---|
| 1435 | *@@added V0.9.0 [umoeller]
|
|---|
| 1436 | *@@changed V0.9.9 (2001-04-04) [umoeller]: this failed on "123" strings in quotes, fixed
|
|---|
| 1437 | */
|
|---|
| 1438 |
|
|---|
| 1439 | PSZ strhGetNumAttribValue(const char *pszSearchIn, // in: where to search
|
|---|
| 1440 | const char *pszTag, // e.g. "INDEX"
|
|---|
| 1441 | PLONG pl) // out: numerical value
|
|---|
| 1442 | {
|
|---|
| 1443 | PSZ pParam;
|
|---|
| 1444 | if ((pParam = strhFindAttribValue(pszSearchIn, pszTag)))
|
|---|
| 1445 | {
|
|---|
| 1446 | if ( (*pParam == '\"')
|
|---|
| 1447 | || (*pParam == '\'')
|
|---|
| 1448 | )
|
|---|
| 1449 | pParam++; // V0.9.9 (2001-04-04) [umoeller]
|
|---|
| 1450 |
|
|---|
| 1451 | sscanf(pParam, "%ld", pl);
|
|---|
| 1452 | }
|
|---|
| 1453 |
|
|---|
| 1454 | return pParam;
|
|---|
| 1455 | }
|
|---|
| 1456 |
|
|---|
| 1457 | /*
|
|---|
| 1458 | * strhGetTextAttr:
|
|---|
| 1459 | * retrieves the attribute value of a textual HTML-style tag
|
|---|
| 1460 | * in a newly allocated buffer, which is returned,
|
|---|
| 1461 | * or NULL if attribute not found.
|
|---|
| 1462 | * If an attribute value is to contain spaces, it
|
|---|
| 1463 | * must be enclosed in quotes.
|
|---|
| 1464 | *
|
|---|
| 1465 | * The offset of the attribute data in pszSearchIn is
|
|---|
| 1466 | * returned in *pulOffset so that you can do multiple
|
|---|
| 1467 | * searches.
|
|---|
| 1468 | *
|
|---|
| 1469 | * This returns a new buffer, which should be free()'d after use.
|
|---|
| 1470 | *
|
|---|
| 1471 | * <B>Example:</B>
|
|---|
| 1472 | + ULONG ulOfs = 0;
|
|---|
| 1473 | + strhGetTextAttr("<PAGE BLAH="blublub">, "BLAH", &ulOfs)
|
|---|
| 1474 | + ............^ ulOfs
|
|---|
| 1475 | *
|
|---|
| 1476 | * returns a new string with the value "blublub" (without
|
|---|
| 1477 | * quotes) and sets ulOfs to 12.
|
|---|
| 1478 | *
|
|---|
| 1479 | *@@added V0.9.0 [umoeller]
|
|---|
| 1480 | */
|
|---|
| 1481 |
|
|---|
| 1482 | PSZ strhGetTextAttr(const char *pszSearchIn,
|
|---|
| 1483 | const char *pszTag,
|
|---|
| 1484 | PULONG pulOffset) // out: offset where found
|
|---|
| 1485 | {
|
|---|
| 1486 | PSZ pParam,
|
|---|
| 1487 | pParam2,
|
|---|
| 1488 | prc = NULL;
|
|---|
| 1489 | ULONG ulCount = 0;
|
|---|
| 1490 | LONG lNestingLevel = 0;
|
|---|
| 1491 |
|
|---|
| 1492 | if ((pParam = strhFindAttribValue(pszSearchIn, pszTag)))
|
|---|
| 1493 | {
|
|---|
| 1494 | // determine end character to search for: a space
|
|---|
| 1495 | CHAR cEnd = ' ';
|
|---|
| 1496 | if (*pParam == '\"')
|
|---|
| 1497 | {
|
|---|
| 1498 | // or, if the data is enclosed in quotes, a quote
|
|---|
| 1499 | cEnd = '\"';
|
|---|
| 1500 | pParam++;
|
|---|
| 1501 | }
|
|---|
| 1502 |
|
|---|
| 1503 | // V1.0.3 (2004-11-10) [pr]: @@fixes 461
|
|---|
| 1504 | if (*pParam == '\'')
|
|---|
| 1505 | {
|
|---|
| 1506 | // or, if the data is enclosed in single quotes, a single quote
|
|---|
| 1507 | cEnd = '\'';
|
|---|
| 1508 | pParam++;
|
|---|
| 1509 | }
|
|---|
| 1510 |
|
|---|
| 1511 | if (pulOffset)
|
|---|
| 1512 | // store the offset
|
|---|
| 1513 | (*pulOffset) = pParam - (PSZ)pszSearchIn;
|
|---|
| 1514 |
|
|---|
| 1515 | // now find end of attribute
|
|---|
| 1516 | pParam2 = pParam;
|
|---|
| 1517 | while (*pParam)
|
|---|
| 1518 | {
|
|---|
| 1519 | if (*pParam == cEnd)
|
|---|
| 1520 | // end character found
|
|---|
| 1521 | break;
|
|---|
| 1522 | else if (*pParam == '<')
|
|---|
| 1523 | // yet another opening tag found:
|
|---|
| 1524 | // this is probably some "<" in the attributes
|
|---|
| 1525 | lNestingLevel++;
|
|---|
| 1526 | else if (*pParam == '>')
|
|---|
| 1527 | {
|
|---|
| 1528 | lNestingLevel--;
|
|---|
| 1529 | if (lNestingLevel < 0)
|
|---|
| 1530 | // end of tag found:
|
|---|
| 1531 | break;
|
|---|
| 1532 | }
|
|---|
| 1533 | ulCount++;
|
|---|
| 1534 | pParam++;
|
|---|
| 1535 | }
|
|---|
| 1536 |
|
|---|
| 1537 | // copy attribute to new buffer
|
|---|
| 1538 | if (ulCount)
|
|---|
| 1539 | {
|
|---|
| 1540 | prc = (PSZ)malloc(ulCount+1);
|
|---|
| 1541 | memcpy(prc, pParam2, ulCount);
|
|---|
| 1542 | *(prc+ulCount) = 0;
|
|---|
| 1543 | }
|
|---|
| 1544 | }
|
|---|
| 1545 | return prc;
|
|---|
| 1546 | }
|
|---|
| 1547 |
|
|---|
| 1548 | /*
|
|---|
| 1549 | * strhFindEndOfTag:
|
|---|
| 1550 | * returns a pointer to the ">" char
|
|---|
| 1551 | * which seems to terminate the tag beginning
|
|---|
| 1552 | * after pszBeginOfTag.
|
|---|
| 1553 | *
|
|---|
| 1554 | * If additional "<" chars are found, we look
|
|---|
| 1555 | * for additional ">" characters too.
|
|---|
| 1556 | *
|
|---|
| 1557 | * Note: You must pass the address of the opening
|
|---|
| 1558 | * '<' character to this function.
|
|---|
| 1559 | *
|
|---|
| 1560 | * Example:
|
|---|
| 1561 | + PSZ pszTest = "<BODY ATTR=\"<BODY>\">";
|
|---|
| 1562 | + strhFindEndOfTag(pszTest)
|
|---|
| 1563 | + returns.................................^ this.
|
|---|
| 1564 | *
|
|---|
| 1565 | *@@added V0.9.0 [umoeller]
|
|---|
| 1566 | */
|
|---|
| 1567 |
|
|---|
| 1568 | PSZ strhFindEndOfTag(const char *pszBeginOfTag)
|
|---|
| 1569 | {
|
|---|
| 1570 | PSZ p = (PSZ)pszBeginOfTag,
|
|---|
| 1571 | prc = NULL;
|
|---|
| 1572 | LONG lNestingLevel = 0;
|
|---|
| 1573 |
|
|---|
| 1574 | while (*p)
|
|---|
| 1575 | {
|
|---|
| 1576 | if (*p == '<')
|
|---|
| 1577 | // another opening tag found:
|
|---|
| 1578 | lNestingLevel++;
|
|---|
| 1579 | else if (*p == '>')
|
|---|
| 1580 | {
|
|---|
| 1581 | // closing tag found:
|
|---|
| 1582 | lNestingLevel--;
|
|---|
| 1583 | if (lNestingLevel < 1)
|
|---|
| 1584 | {
|
|---|
| 1585 | // corresponding: return this
|
|---|
| 1586 | prc = p;
|
|---|
| 1587 | break;
|
|---|
| 1588 | }
|
|---|
| 1589 | }
|
|---|
| 1590 | p++;
|
|---|
| 1591 | }
|
|---|
| 1592 |
|
|---|
| 1593 | return prc;
|
|---|
| 1594 | }
|
|---|
| 1595 |
|
|---|
| 1596 | /*
|
|---|
| 1597 | * strhGetBlock:
|
|---|
| 1598 | * this complex function searches the given string
|
|---|
| 1599 | * for a pair of opening/closing HTML-style tags.
|
|---|
| 1600 | *
|
|---|
| 1601 | * If found, this routine returns TRUE and does
|
|---|
| 1602 | * the following:
|
|---|
| 1603 | *
|
|---|
| 1604 | * 1) allocate a new buffer, copy the text
|
|---|
| 1605 | * enclosed by the opening/closing tags
|
|---|
| 1606 | * into it and set *ppszBlock to that
|
|---|
| 1607 | * buffer;
|
|---|
| 1608 | *
|
|---|
| 1609 | * 2) if the opening tag has any attributes,
|
|---|
| 1610 | * allocate another buffer, copy the
|
|---|
| 1611 | * attributes into it and set *ppszAttrs
|
|---|
| 1612 | * to that buffer; if no attributes are
|
|---|
| 1613 | * found, *ppszAttrs will be NULL;
|
|---|
| 1614 | *
|
|---|
| 1615 | * 3) set *pulOffset to the offset from the
|
|---|
| 1616 | * beginning of *ppszSearchIn where the
|
|---|
| 1617 | * opening tag was found;
|
|---|
| 1618 | *
|
|---|
| 1619 | * 4) advance *ppszSearchIn to after the
|
|---|
| 1620 | * closing tag, so that you can do
|
|---|
| 1621 | * multiple searches without finding the
|
|---|
| 1622 | * same tags twice.
|
|---|
| 1623 | *
|
|---|
| 1624 | * All buffers should be freed using free().
|
|---|
| 1625 | *
|
|---|
| 1626 | * This returns the following:
|
|---|
| 1627 | * -- 0: no error
|
|---|
| 1628 | * -- 1: tag not found at all (doesn't have to be an error)
|
|---|
| 1629 | * -- 2: begin tag found, but no corresponding end tag found. This
|
|---|
| 1630 | * is a real error.
|
|---|
| 1631 | * -- 3: begin tag is not terminated by ">" (e.g. "<BEGINTAG whatever")
|
|---|
| 1632 | *
|
|---|
| 1633 | * <B>Example:</B>
|
|---|
| 1634 | + PSZ pSearch = "<PAGE INDEX=1>This is page 1.</PAGE>More text."
|
|---|
| 1635 | + PSZ pszBlock, pszAttrs;
|
|---|
| 1636 | + ULONG ulOfs;
|
|---|
| 1637 | + strhGetBlock(&pSearch, "PAGE", &pszBlock, &pszAttrs, &ulOfs)
|
|---|
| 1638 | *
|
|---|
| 1639 | * would do the following:
|
|---|
| 1640 | *
|
|---|
| 1641 | * 1) set pszBlock to a new string containing "This is page 1."
|
|---|
| 1642 | * without quotes;
|
|---|
| 1643 | *
|
|---|
| 1644 | * 2) set pszAttrs to a new string containing "<PAGE INDEX=1>";
|
|---|
| 1645 | *
|
|---|
| 1646 | * 3) set ulOfs to 0, because "<PAGE" was found at the beginning;
|
|---|
| 1647 | *
|
|---|
| 1648 | * 4) pSearch would be advanced to point to the "More text"
|
|---|
| 1649 | * string in the original buffer.
|
|---|
| 1650 | *
|
|---|
| 1651 | * Hey-hey. A one-shot function, fairly complicated, but indispensable
|
|---|
| 1652 | * for HTML parsing.
|
|---|
| 1653 | *
|
|---|
| 1654 | *@@added V0.9.0 [umoeller]
|
|---|
| 1655 | *@@changed V0.9.1 (2000-01-03) [umoeller]: fixed heap overwrites (thanks to string debugging)
|
|---|
| 1656 | *@@changed V0.9.1 (2000-01-06) [umoeller]: changed prototype
|
|---|
| 1657 | *@@changed V0.9.3 (2000-05-06) [umoeller]: NULL string check was missing
|
|---|
| 1658 | */
|
|---|
| 1659 |
|
|---|
| 1660 | ULONG strhGetBlock(const char *pszSearchIn, // in: buffer to search
|
|---|
| 1661 | PULONG pulSearchOffset, // in/out: offset where to start search (0 for beginning)
|
|---|
| 1662 | const char *pszTag,
|
|---|
| 1663 | PSZ *ppszBlock, // out: block enclosed by the tags
|
|---|
| 1664 | PSZ *ppszAttribs, // out: attributes of the opening tag
|
|---|
| 1665 | PULONG pulOfsBeginTag, // out: offset from pszSearchIn where opening tag was found
|
|---|
| 1666 | PULONG pulOfsBeginBlock) // out: offset from pszSearchIn where beginning of block was found
|
|---|
| 1667 | {
|
|---|
| 1668 | ULONG ulrc = 1;
|
|---|
| 1669 | PSZ pszBeginTag = (PSZ)pszSearchIn + *pulSearchOffset,
|
|---|
| 1670 | pszSearch2 = pszBeginTag,
|
|---|
| 1671 | pszClosingTag;
|
|---|
| 1672 | ULONG cbTag = strlen(pszTag);
|
|---|
| 1673 |
|
|---|
| 1674 | // go thru the block and check all tags if it's the
|
|---|
| 1675 | // begin tag we're looking for
|
|---|
| 1676 | while ((pszBeginTag = strchr(pszBeginTag, '<')))
|
|---|
| 1677 | {
|
|---|
| 1678 | if (memicmp(pszBeginTag+1, (void*)pszTag, strlen(pszTag)) == 0)
|
|---|
| 1679 | // yes: stop
|
|---|
| 1680 | break;
|
|---|
| 1681 | else
|
|---|
| 1682 | pszBeginTag++;
|
|---|
| 1683 | }
|
|---|
| 1684 |
|
|---|
| 1685 | if (pszBeginTag)
|
|---|
| 1686 | {
|
|---|
| 1687 | // we found <TAG>:
|
|---|
| 1688 | ULONG ulNestingLevel = 0;
|
|---|
| 1689 |
|
|---|
| 1690 | PSZ pszEndOfBeginTag = strhFindEndOfTag(pszBeginTag);
|
|---|
| 1691 | // strchr(pszBeginTag, '>');
|
|---|
| 1692 | if (pszEndOfBeginTag)
|
|---|
| 1693 | {
|
|---|
| 1694 | // does the caller want the attributes?
|
|---|
| 1695 | if (ppszAttribs)
|
|---|
| 1696 | {
|
|---|
| 1697 | // yes: then copy them
|
|---|
| 1698 | ULONG ulAttrLen = pszEndOfBeginTag - pszBeginTag;
|
|---|
| 1699 | PSZ pszAttrs = (PSZ)malloc(ulAttrLen + 1);
|
|---|
| 1700 | strncpy(pszAttrs, pszBeginTag, ulAttrLen);
|
|---|
| 1701 | // add terminating 0
|
|---|
| 1702 | *(pszAttrs + ulAttrLen) = 0;
|
|---|
| 1703 |
|
|---|
| 1704 | *ppszAttribs = pszAttrs;
|
|---|
| 1705 | }
|
|---|
| 1706 |
|
|---|
| 1707 | // output offset of where we found the begin tag
|
|---|
| 1708 | if (pulOfsBeginTag)
|
|---|
| 1709 | *pulOfsBeginTag = pszBeginTag - (PSZ)pszSearchIn;
|
|---|
| 1710 |
|
|---|
| 1711 | // now find corresponding closing tag (e.g. "</BODY>"
|
|---|
| 1712 | pszBeginTag = pszEndOfBeginTag+1;
|
|---|
| 1713 | // now we're behind the '>' char of the opening tag
|
|---|
| 1714 | // increase offset of that too
|
|---|
| 1715 | if (pulOfsBeginBlock)
|
|---|
| 1716 | *pulOfsBeginBlock = pszBeginTag - (PSZ)pszSearchIn;
|
|---|
| 1717 |
|
|---|
| 1718 | // find next closing tag;
|
|---|
| 1719 | // for the first run, pszSearch2 points to right
|
|---|
| 1720 | // after the '>' char of the opening tag
|
|---|
| 1721 | pszSearch2 = pszBeginTag;
|
|---|
| 1722 | while ( (pszSearch2) // fixed V0.9.3 (2000-05-06) [umoeller]
|
|---|
| 1723 | && (pszClosingTag = strstr(pszSearch2, "<"))
|
|---|
| 1724 | )
|
|---|
| 1725 | {
|
|---|
| 1726 | // if we have another opening tag before our closing
|
|---|
| 1727 | // tag, we need to have several closing tags before
|
|---|
| 1728 | // we're done
|
|---|
| 1729 | if (memicmp(pszClosingTag+1, (void*)pszTag, cbTag) == 0)
|
|---|
| 1730 | ulNestingLevel++;
|
|---|
| 1731 | else
|
|---|
| 1732 | {
|
|---|
| 1733 | // is this ours?
|
|---|
| 1734 | if ( (*(pszClosingTag+1) == '/')
|
|---|
| 1735 | && (memicmp(pszClosingTag+2, (void*)pszTag, cbTag) == 0)
|
|---|
| 1736 | )
|
|---|
| 1737 | {
|
|---|
| 1738 | // we've found a matching closing tag; is
|
|---|
| 1739 | // it ours?
|
|---|
| 1740 | if (ulNestingLevel == 0)
|
|---|
| 1741 | {
|
|---|
| 1742 | // our closing tag found:
|
|---|
| 1743 | // allocate mem for a new buffer
|
|---|
| 1744 | // and extract all the text between
|
|---|
| 1745 | // open and closing tags to it
|
|---|
| 1746 | ULONG ulLen = pszClosingTag - pszBeginTag;
|
|---|
| 1747 | if (ppszBlock)
|
|---|
| 1748 | {
|
|---|
| 1749 | PSZ pNew = (PSZ)malloc(ulLen + 1);
|
|---|
| 1750 | strhncpy0(pNew, pszBeginTag, ulLen);
|
|---|
| 1751 | *ppszBlock = pNew;
|
|---|
| 1752 | }
|
|---|
| 1753 |
|
|---|
| 1754 | // raise search offset to after the closing tag
|
|---|
| 1755 | *pulSearchOffset = (pszClosingTag + cbTag + 1) - (PSZ)pszSearchIn;
|
|---|
| 1756 |
|
|---|
| 1757 | ulrc = 0;
|
|---|
| 1758 |
|
|---|
| 1759 | break;
|
|---|
| 1760 | } else
|
|---|
| 1761 | // not our closing tag:
|
|---|
| 1762 | ulNestingLevel--;
|
|---|
| 1763 | }
|
|---|
| 1764 | }
|
|---|
| 1765 | // no matching closing tag: search on after that
|
|---|
| 1766 | pszSearch2 = strhFindEndOfTag(pszClosingTag);
|
|---|
| 1767 | } // end while (pszClosingTag = strstr(pszSearch2, "<"))
|
|---|
| 1768 |
|
|---|
| 1769 | if (!pszClosingTag)
|
|---|
| 1770 | // no matching closing tag found:
|
|---|
| 1771 | // return 2 (closing tag not found)
|
|---|
| 1772 | ulrc = 2;
|
|---|
| 1773 | } // end if (pszBeginTag)
|
|---|
| 1774 | else
|
|---|
| 1775 | // no matching ">" for opening tag found:
|
|---|
| 1776 | ulrc = 3;
|
|---|
| 1777 | }
|
|---|
| 1778 |
|
|---|
| 1779 | return ulrc;
|
|---|
| 1780 | }
|
|---|
| 1781 |
|
|---|
| 1782 | /* ******************************************************************
|
|---|
| 1783 | *
|
|---|
| 1784 | * Miscellaneous
|
|---|
| 1785 | *
|
|---|
| 1786 | ********************************************************************/
|
|---|
| 1787 |
|
|---|
| 1788 | /*
|
|---|
| 1789 | *@@ strhArrayAppend:
|
|---|
| 1790 | * this appends a string to a "string array".
|
|---|
| 1791 | *
|
|---|
| 1792 | * A string array is considered a sequence of
|
|---|
| 1793 | * zero-terminated strings in memory. That is,
|
|---|
| 1794 | * after each string's null-byte, the next
|
|---|
| 1795 | * string comes up.
|
|---|
| 1796 | *
|
|---|
| 1797 | * This is useful for composing a single block
|
|---|
| 1798 | * of memory from, say, list box entries, which
|
|---|
| 1799 | * can then be written to OS2.INI in one flush.
|
|---|
| 1800 | *
|
|---|
| 1801 | * To append strings to such an array, call this
|
|---|
| 1802 | * function for each string you wish to append.
|
|---|
| 1803 | * This will re-allocate *ppszRoot with each call,
|
|---|
| 1804 | * and update *pcbRoot, which then contains the
|
|---|
| 1805 | * total size of all strings (including all null
|
|---|
| 1806 | * terminators).
|
|---|
| 1807 | *
|
|---|
| 1808 | * Pass *pcbRoot to PrfSaveProfileData to have the
|
|---|
| 1809 | * block saved.
|
|---|
| 1810 | *
|
|---|
| 1811 | * Note: On the first call, *ppszRoot and *pcbRoot
|
|---|
| 1812 | * _must_ be both NULL, or this crashes.
|
|---|
| 1813 | *
|
|---|
| 1814 | *@@changed V0.9.13 (2001-06-21) [umoeller]: added cbNew
|
|---|
| 1815 | */
|
|---|
| 1816 |
|
|---|
| 1817 | VOID strhArrayAppend(PSZ *ppszRoot, // in: root of array
|
|---|
| 1818 | const char *pcszNew, // in: string to append
|
|---|
| 1819 | ULONG cbNew, // in: size of that string or 0 to run strlen() here
|
|---|
| 1820 | PULONG pcbRoot) // in/out: size of array
|
|---|
| 1821 | {
|
|---|
| 1822 | PSZ pszTemp;
|
|---|
| 1823 |
|
|---|
| 1824 | if (!cbNew) // V0.9.13 (2001-06-21) [umoeller]
|
|---|
| 1825 | cbNew = strlen(pcszNew);
|
|---|
| 1826 |
|
|---|
| 1827 | pszTemp = (PSZ)malloc(*pcbRoot
|
|---|
| 1828 | + cbNew
|
|---|
| 1829 | + 1); // two null bytes
|
|---|
| 1830 | if (*ppszRoot)
|
|---|
| 1831 | {
|
|---|
| 1832 | // not first loop: copy old stuff
|
|---|
| 1833 | memcpy(pszTemp,
|
|---|
| 1834 | *ppszRoot,
|
|---|
| 1835 | *pcbRoot);
|
|---|
| 1836 | free(*ppszRoot);
|
|---|
| 1837 | }
|
|---|
| 1838 | // append new string
|
|---|
| 1839 | strcpy(pszTemp + *pcbRoot,
|
|---|
| 1840 | pcszNew);
|
|---|
| 1841 | // update root
|
|---|
| 1842 | *ppszRoot = pszTemp;
|
|---|
| 1843 | // update length
|
|---|
| 1844 | *pcbRoot += cbNew + 1;
|
|---|
| 1845 | }
|
|---|
| 1846 |
|
|---|
| 1847 | /*
|
|---|
| 1848 | *@@ strhCreateDump:
|
|---|
| 1849 | * this dumps a memory block into a string
|
|---|
| 1850 | * and returns that string in a new buffer.
|
|---|
| 1851 | *
|
|---|
| 1852 | * You must free() the returned PSZ after use.
|
|---|
| 1853 | *
|
|---|
| 1854 | * The output looks like the following:
|
|---|
| 1855 | *
|
|---|
| 1856 | + 0000: FE FF 0E 02 90 00 00 00 ........
|
|---|
| 1857 | + 0008: FD 01 00 00 57 50 46 6F ....WPFo
|
|---|
| 1858 | + 0010: 6C 64 65 72 00 78 01 34 lder.x.4
|
|---|
| 1859 | *
|
|---|
| 1860 | * Each line is terminated with a newline (\n)
|
|---|
| 1861 | * character only.
|
|---|
| 1862 | *
|
|---|
| 1863 | *@@added V0.9.1 (2000-01-22) [umoeller]
|
|---|
| 1864 | */
|
|---|
| 1865 |
|
|---|
| 1866 | PSZ strhCreateDump(PBYTE pb, // in: start address of buffer
|
|---|
| 1867 | ULONG ulSize, // in: size of buffer
|
|---|
| 1868 | ULONG ulIndent) // in: indentation of every line
|
|---|
| 1869 | {
|
|---|
| 1870 | PSZ pszReturn = 0;
|
|---|
| 1871 | XSTRING strReturn;
|
|---|
| 1872 | CHAR szTemp[1000];
|
|---|
| 1873 |
|
|---|
| 1874 | PBYTE pbCurrent = pb; // current byte
|
|---|
| 1875 | ULONG ulCount = 0,
|
|---|
| 1876 | ulCharsInLine = 0; // if this grows > 7, a new line is started
|
|---|
| 1877 | CHAR szLine[400] = "",
|
|---|
| 1878 | szAscii[30] = " "; // ASCII representation; filled for every line
|
|---|
| 1879 | PSZ pszLine = szLine,
|
|---|
| 1880 | pszAscii = szAscii;
|
|---|
| 1881 |
|
|---|
| 1882 | xstrInit(&strReturn, (ulSize * 30) + ulIndent);
|
|---|
| 1883 |
|
|---|
| 1884 | for (pbCurrent = pb;
|
|---|
| 1885 | ulCount < ulSize;
|
|---|
| 1886 | pbCurrent++, ulCount++)
|
|---|
| 1887 | {
|
|---|
| 1888 | if (ulCharsInLine == 0)
|
|---|
| 1889 | {
|
|---|
| 1890 | memset(szLine, ' ', ulIndent);
|
|---|
| 1891 | pszLine += ulIndent;
|
|---|
| 1892 | }
|
|---|
| 1893 | pszLine += sprintf(pszLine, "%02lX ", (ULONG)*pbCurrent);
|
|---|
| 1894 |
|
|---|
| 1895 | if ( (*pbCurrent > 31) && (*pbCurrent < 127) )
|
|---|
| 1896 | // printable character:
|
|---|
| 1897 | *pszAscii = *pbCurrent;
|
|---|
| 1898 | else
|
|---|
| 1899 | *pszAscii = '.';
|
|---|
| 1900 | pszAscii++;
|
|---|
| 1901 |
|
|---|
| 1902 | ulCharsInLine++;
|
|---|
| 1903 | if ( (ulCharsInLine > 7) // 8 bytes added?
|
|---|
| 1904 | || (ulCount == ulSize-1) // end of buffer reached?
|
|---|
| 1905 | )
|
|---|
| 1906 | {
|
|---|
| 1907 | // if we haven't had eight bytes yet,
|
|---|
| 1908 | // fill buffer up to eight bytes with spaces
|
|---|
| 1909 | ULONG ul2;
|
|---|
| 1910 | for (ul2 = ulCharsInLine;
|
|---|
| 1911 | ul2 < 8;
|
|---|
| 1912 | ul2++)
|
|---|
| 1913 | pszLine += sprintf(pszLine, " ");
|
|---|
| 1914 |
|
|---|
| 1915 | sprintf(szTemp, "%04lX: %s %s\n",
|
|---|
| 1916 | (ulCount & 0xFFFFFFF8), // offset in hex
|
|---|
| 1917 | szLine, // bytes string
|
|---|
| 1918 | szAscii); // ASCII string
|
|---|
| 1919 | xstrcat(&strReturn, szTemp, 0);
|
|---|
| 1920 |
|
|---|
| 1921 | // restart line buffer
|
|---|
| 1922 | pszLine = szLine;
|
|---|
| 1923 |
|
|---|
| 1924 | // clear ASCII buffer
|
|---|
| 1925 | strcpy(szAscii, " ");
|
|---|
| 1926 | pszAscii = szAscii;
|
|---|
| 1927 |
|
|---|
| 1928 | // reset line counter
|
|---|
| 1929 | ulCharsInLine = 0;
|
|---|
| 1930 | }
|
|---|
| 1931 | }
|
|---|
| 1932 |
|
|---|
| 1933 | if (strReturn.cbAllocated)
|
|---|
| 1934 | pszReturn = strReturn.psz;
|
|---|
| 1935 |
|
|---|
| 1936 | return pszReturn;
|
|---|
| 1937 | }
|
|---|
| 1938 |
|
|---|
| 1939 | /* ******************************************************************
|
|---|
| 1940 | *
|
|---|
| 1941 | * Fast string searches
|
|---|
| 1942 | *
|
|---|
| 1943 | ********************************************************************/
|
|---|
| 1944 |
|
|---|
| 1945 | #define ASSERT(a)
|
|---|
| 1946 |
|
|---|
| 1947 | /*
|
|---|
| 1948 | * The following code has been taken from the "Standard
|
|---|
| 1949 | * Function Library", file sflfind.c, and only slightly
|
|---|
| 1950 | * modified to conform to the rest of this file.
|
|---|
| 1951 | *
|
|---|
| 1952 | * Written: 96/04/24 iMatix SFL project team <sfl@imatix.com>
|
|---|
| 1953 | * Revised: 98/05/04
|
|---|
| 1954 | *
|
|---|
| 1955 | * Copyright: Copyright (c) 1991-99 iMatix Corporation.
|
|---|
| 1956 | *
|
|---|
| 1957 | * The SFL Licence allows incorporating SFL code into other
|
|---|
| 1958 | * programs, as long as the copyright is reprinted and the
|
|---|
| 1959 | * code is marked as modified, so this is what we do.
|
|---|
| 1960 | */
|
|---|
| 1961 |
|
|---|
| 1962 | /*
|
|---|
| 1963 | *@@ strhmemfind:
|
|---|
| 1964 | * searches for a pattern in a block of memory using the
|
|---|
| 1965 | * Boyer-Moore-Horspool-Sunday algorithm.
|
|---|
| 1966 | *
|
|---|
| 1967 | * The block and pattern may contain any values; you must
|
|---|
| 1968 | * explicitly provide their lengths. If you search for strings,
|
|---|
| 1969 | * use strlen() on the buffers.
|
|---|
| 1970 | *
|
|---|
| 1971 | * Returns a pointer to the pattern if found within the block,
|
|---|
| 1972 | * or NULL if the pattern was not found.
|
|---|
| 1973 | *
|
|---|
| 1974 | * This algorithm needs a "shift table" to cache data for the
|
|---|
| 1975 | * search pattern. This table can be reused when performing
|
|---|
| 1976 | * several searches with the same pattern.
|
|---|
| 1977 | *
|
|---|
| 1978 | * "shift" must point to an array big enough to hold 256 (8**2)
|
|---|
| 1979 | * "size_t" values.
|
|---|
| 1980 | *
|
|---|
| 1981 | * If (*repeat_find == FALSE), the shift table is initialized.
|
|---|
| 1982 | * So on the first search with a given pattern, *repeat_find
|
|---|
| 1983 | * should be FALSE. This function sets it to TRUE after the
|
|---|
| 1984 | * shift table is initialised, allowing the initialisation
|
|---|
| 1985 | * phase to be skipped on subsequent searches.
|
|---|
| 1986 | *
|
|---|
| 1987 | * This function is most effective when repeated searches are
|
|---|
| 1988 | * made for the same pattern in one or more large buffers.
|
|---|
| 1989 | *
|
|---|
| 1990 | * Example:
|
|---|
| 1991 | *
|
|---|
| 1992 | + PSZ pszHaystack = "This is a sample string.",
|
|---|
| 1993 | + pszNeedle = "string";
|
|---|
| 1994 | + size_t shift[256];
|
|---|
| 1995 | + BOOL fRepeat = FALSE;
|
|---|
| 1996 | +
|
|---|
| 1997 | + PSZ pFound = strhmemfind(pszHaystack,
|
|---|
| 1998 | + strlen(pszHaystack), // block size
|
|---|
| 1999 | + pszNeedle,
|
|---|
| 2000 | + strlen(pszNeedle), // pattern size
|
|---|
| 2001 | + shift,
|
|---|
| 2002 | + &fRepeat);
|
|---|
| 2003 | *
|
|---|
| 2004 | * Taken from the "Standard Function Library", file sflfind.c.
|
|---|
| 2005 | * Copyright: Copyright (c) 1991-99 iMatix Corporation.
|
|---|
| 2006 | * Slightly modified by umoeller.
|
|---|
| 2007 | *
|
|---|
| 2008 | *@@added V0.9.3 (2000-05-08) [umoeller]
|
|---|
| 2009 | */
|
|---|
| 2010 |
|
|---|
| 2011 | void* strhmemfind(const void *in_block, // in: block containing data
|
|---|
| 2012 | size_t block_size, // in: size of block in bytes
|
|---|
| 2013 | const void *in_pattern, // in: pattern to search for
|
|---|
| 2014 | size_t pattern_size, // in: size of pattern block
|
|---|
| 2015 | size_t *shift, // in/out: shift table (search buffer)
|
|---|
| 2016 | BOOL *repeat_find) // in/out: if TRUE, *shift is already initialized
|
|---|
| 2017 | {
|
|---|
| 2018 | size_t byte_nbr, // Distance through block
|
|---|
| 2019 | match_size; // Size of matched part
|
|---|
| 2020 | const unsigned char
|
|---|
| 2021 | *match_base = NULL, // Base of match of pattern
|
|---|
| 2022 | *match_ptr = NULL, // Point within current match
|
|---|
| 2023 | *limit = NULL; // Last potiental match point
|
|---|
| 2024 | const unsigned char
|
|---|
| 2025 | *block = (unsigned char *) in_block, // Concrete pointer to block data
|
|---|
| 2026 | *pattern = (unsigned char *) in_pattern; // Concrete pointer to search value
|
|---|
| 2027 |
|
|---|
| 2028 | if ( (block == NULL)
|
|---|
| 2029 | || (pattern == NULL)
|
|---|
| 2030 | || (shift == NULL)
|
|---|
| 2031 | )
|
|---|
| 2032 | return NULL;
|
|---|
| 2033 |
|
|---|
| 2034 | // Pattern must be smaller or equal in size to string
|
|---|
| 2035 | if (block_size < pattern_size)
|
|---|
| 2036 | return NULL; // Otherwise it's not found
|
|---|
| 2037 |
|
|---|
| 2038 | if (pattern_size == 0) // Empty patterns match at start
|
|---|
| 2039 | return (void*)block;
|
|---|
| 2040 |
|
|---|
| 2041 | // Build the shift table unless we're continuing a previous search
|
|---|
| 2042 |
|
|---|
| 2043 | // The shift table determines how far to shift before trying to match
|
|---|
| 2044 | // again, if a match at this point fails. If the byte after where the
|
|---|
| 2045 | // end of our pattern falls is not in our pattern, then we start to
|
|---|
| 2046 | // match again after that byte; otherwise we line up the last occurence
|
|---|
| 2047 | // of that byte in our pattern under that byte, and try match again.
|
|---|
| 2048 |
|
|---|
| 2049 | if (!repeat_find || !*repeat_find)
|
|---|
| 2050 | {
|
|---|
| 2051 | for (byte_nbr = 0;
|
|---|
| 2052 | byte_nbr < 256;
|
|---|
| 2053 | byte_nbr++)
|
|---|
| 2054 | shift[byte_nbr] = pattern_size + 1;
|
|---|
| 2055 | for (byte_nbr = 0;
|
|---|
| 2056 | byte_nbr < pattern_size;
|
|---|
| 2057 | byte_nbr++)
|
|---|
| 2058 | shift[(unsigned char)pattern[byte_nbr]] = pattern_size - byte_nbr;
|
|---|
| 2059 |
|
|---|
| 2060 | if (repeat_find)
|
|---|
| 2061 | *repeat_find = TRUE;
|
|---|
| 2062 | }
|
|---|
| 2063 |
|
|---|
| 2064 | // Search for the block, each time jumping up by the amount
|
|---|
| 2065 | // computed in the shift table
|
|---|
| 2066 |
|
|---|
| 2067 | limit = block + (block_size - pattern_size + 1);
|
|---|
| 2068 | ASSERT (limit > block);
|
|---|
| 2069 |
|
|---|
| 2070 | for (match_base = block;
|
|---|
| 2071 | match_base < limit;
|
|---|
| 2072 | match_base += shift[*(match_base + pattern_size)])
|
|---|
| 2073 | {
|
|---|
| 2074 | match_ptr = match_base;
|
|---|
| 2075 | match_size = 0;
|
|---|
| 2076 |
|
|---|
| 2077 | // Compare pattern until it all matches, or we find a difference
|
|---|
| 2078 | while (*match_ptr++ == pattern[match_size++])
|
|---|
| 2079 | {
|
|---|
| 2080 | ASSERT (match_size <= pattern_size &&
|
|---|
| 2081 | match_ptr == (match_base + match_size));
|
|---|
| 2082 |
|
|---|
| 2083 | // If we found a match, return the start address
|
|---|
| 2084 | if (match_size >= pattern_size)
|
|---|
| 2085 | return (void*)match_base;
|
|---|
| 2086 |
|
|---|
| 2087 | }
|
|---|
| 2088 | }
|
|---|
| 2089 | return NULL; // Found nothing
|
|---|
| 2090 | }
|
|---|
| 2091 |
|
|---|
| 2092 | /*
|
|---|
| 2093 | *@@ strhtxtfind:
|
|---|
| 2094 | * searches for a case-insensitive text pattern in a string
|
|---|
| 2095 | * using the Boyer-Moore-Horspool-Sunday algorithm. The string and
|
|---|
| 2096 | * pattern are null-terminated strings. Returns a pointer to the pattern
|
|---|
| 2097 | * if found within the string, or NULL if the pattern was not found.
|
|---|
| 2098 | * Will match strings irrespective of case. To match exact strings, use
|
|---|
| 2099 | * strhfind(). Will not work on multibyte characters.
|
|---|
| 2100 | *
|
|---|
| 2101 | * Examples:
|
|---|
| 2102 | + char *result;
|
|---|
| 2103 | +
|
|---|
| 2104 | + result = strhtxtfind ("AbracaDabra", "cad");
|
|---|
| 2105 | + if (result)
|
|---|
| 2106 | + puts (result);
|
|---|
| 2107 | +
|
|---|
| 2108 | * Taken from the "Standard Function Library", file sflfind.c.
|
|---|
| 2109 | * Copyright: Copyright (c) 1991-99 iMatix Corporation.
|
|---|
| 2110 | * Slightly modified.
|
|---|
| 2111 | *
|
|---|
| 2112 | *@@added V0.9.3 (2000-05-08) [umoeller]
|
|---|
| 2113 | */
|
|---|
| 2114 |
|
|---|
| 2115 | char* strhtxtfind (const char *string, // String containing data
|
|---|
| 2116 | const char *pattern) // Pattern to search for
|
|---|
| 2117 | {
|
|---|
| 2118 | size_t
|
|---|
| 2119 | shift [256]; // Shift distance for each value
|
|---|
| 2120 | size_t
|
|---|
| 2121 | string_size,
|
|---|
| 2122 | pattern_size,
|
|---|
| 2123 | byte_nbr, // Index into byte array
|
|---|
| 2124 | match_size; // Size of matched part
|
|---|
| 2125 | const char
|
|---|
| 2126 | *match_base = NULL, // Base of match of pattern
|
|---|
| 2127 | *match_ptr = NULL, // Point within current match
|
|---|
| 2128 | *limit = NULL; // Last potiental match point
|
|---|
| 2129 |
|
|---|
| 2130 | ASSERT (string); // Expect non-NULL pointers, but
|
|---|
| 2131 | ASSERT (pattern); // fail gracefully if not debugging
|
|---|
| 2132 | if (string == NULL || pattern == NULL)
|
|---|
| 2133 | return NULL;
|
|---|
| 2134 |
|
|---|
| 2135 | string_size = strlen (string);
|
|---|
| 2136 | pattern_size = strlen (pattern);
|
|---|
| 2137 |
|
|---|
| 2138 | // Pattern must be smaller or equal in size to string
|
|---|
| 2139 | if (string_size < pattern_size)
|
|---|
| 2140 | return NULL; // Otherwise it cannot be found
|
|---|
| 2141 |
|
|---|
| 2142 | if (pattern_size == 0) // Empty string matches at start
|
|---|
| 2143 | return (char*)string;
|
|---|
| 2144 |
|
|---|
| 2145 | // Build the shift table
|
|---|
| 2146 |
|
|---|
| 2147 | // The shift table determines how far to shift before trying to match
|
|---|
| 2148 | // again, if a match at this point fails. If the byte after where the
|
|---|
| 2149 | // end of our pattern falls is not in our pattern, then we start to
|
|---|
| 2150 | // match again after that byte; otherwise we line up the last occurence
|
|---|
| 2151 | // of that byte in our pattern under that byte, and try match again.
|
|---|
| 2152 |
|
|---|
| 2153 | for (byte_nbr = 0; byte_nbr < 256; byte_nbr++)
|
|---|
| 2154 | shift [byte_nbr] = pattern_size + 1;
|
|---|
| 2155 |
|
|---|
| 2156 | for (byte_nbr = 0; byte_nbr < pattern_size; byte_nbr++)
|
|---|
| 2157 | shift [(unsigned char) tolower (pattern [byte_nbr])] = pattern_size - byte_nbr;
|
|---|
| 2158 |
|
|---|
| 2159 | // Search for the string. If we don't find a match, move up by the
|
|---|
| 2160 | // amount we computed in the shift table above, to find location of
|
|---|
| 2161 | // the next potiental match.
|
|---|
| 2162 |
|
|---|
| 2163 | limit = string + (string_size - pattern_size + 1);
|
|---|
| 2164 | ASSERT (limit > string);
|
|---|
| 2165 |
|
|---|
| 2166 | for (match_base = string;
|
|---|
| 2167 | match_base < limit;
|
|---|
| 2168 | match_base += shift [(unsigned char) tolower (*(match_base + pattern_size))])
|
|---|
| 2169 | {
|
|---|
| 2170 | match_ptr = match_base;
|
|---|
| 2171 | match_size = 0;
|
|---|
| 2172 |
|
|---|
| 2173 | // Compare pattern until it all matches, or we find a difference
|
|---|
| 2174 | while (tolower (*match_ptr++) == tolower (pattern [match_size++]))
|
|---|
| 2175 | {
|
|---|
| 2176 | ASSERT (match_size <= pattern_size &&
|
|---|
| 2177 | match_ptr == (match_base + match_size));
|
|---|
| 2178 |
|
|---|
| 2179 | // If we found a match, return the start address
|
|---|
| 2180 | if (match_size >= pattern_size)
|
|---|
| 2181 | return (char*)match_base;
|
|---|
| 2182 | }
|
|---|
| 2183 | }
|
|---|
| 2184 |
|
|---|
| 2185 | return NULL; // Found nothing
|
|---|
| 2186 | }
|
|---|
| 2187 |
|
|---|
| 2188 |
|
|---|