Changeset 245 for trunk/src/helpers/stringh.c
- Timestamp:
- Feb 2, 2003, 9:22:17 PM (23 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/helpers/stringh.c
r242 r245 409 409 410 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 */ 411 514 } 412 515 … … 1160 1263 1161 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; 1162 1304 } 1163 1305 … … 2032 2174 } 2033 2175 } 2176 2034 2177 return NULL; // Found nothing 2035 2178 } 2036 2179 2180
Note:
See TracChangeset
for help on using the changeset viewer.