|
Lines 414-419
Link Here
|
| 414 |
} |
414 |
} |
| 415 |
|
415 |
|
| 416 |
/* |
416 |
/* |
|
|
417 |
* Format a URL into a string. |
| 418 |
* |
| 419 |
* This function formats the information from urlptr into buf |
| 420 |
* according to format. No more than size characters will be copied to |
| 421 |
* buf. If the total number of characters including the terminating |
| 422 |
* NUL is less is less than size, then the number of characters copied |
| 423 |
* not counting the terminating NUL is returned. Otherwise, zero is |
| 424 |
* returned and the contents of buf are indeterminate. |
| 425 |
* |
| 426 |
* The format string consists of ordinary characters and zero or more |
| 427 |
* conversion specifications. A conversion specification is a ``%'' |
| 428 |
* sign and one character. Conversion specifications are replaced by |
| 429 |
* elements from the url. The url is parsed into the following parts, |
| 430 |
* as defined in RFC1738: |
| 431 |
* |
| 432 |
* scheme://user:password@host:port/file |
| 433 |
* |
| 434 |
* Not all parts are present in all URLs; those missing will generate |
| 435 |
* the null string. The non-alphabetic characters separate the parts, |
| 436 |
* with the exception of the last ``/'', which is the first character |
| 437 |
* in the file part. |
| 438 |
* |
| 439 |
* The conversion specifications map to the parts like so: %s: scheme, |
| 440 |
* %u: user, %w: password, %h: host, %p: port, %f: file. |
| 441 |
* |
| 442 |
* Any other character after the ``%'' in a conversion specification |
| 443 |
* will replace the conversion specification, so that ``%%'' becomes |
| 444 |
* ``%''. Using this mechanism for any character other than ``%'' and |
| 445 |
* the documented specifications could lead to problems with later |
| 446 |
* version of strfurl. |
| 447 |
* |
| 448 |
* Note for future expansion: capital letters for conversion to imply |
| 449 |
* smarts, so that %P will become the default port for the scheme if |
| 450 |
* none is provided? |
| 451 |
*/ |
| 452 |
size_t |
| 453 |
fetchStrfURL(char *const buf, size_t size, const char *format, |
| 454 |
struct url *urlptr) |
| 455 |
{ |
| 456 |
char *pt, *ptlim ; |
| 457 |
|
| 458 |
for (pt = buf, ptlim = pt + size; pt < ptlim && *format; format += 1) |
| 459 |
if (*format != '%') *pt++ = *format ; |
| 460 |
else { |
| 461 |
format += 1 ; |
| 462 |
switch (*format) { |
| 463 |
case 'd': |
| 464 |
pt += snprintf(pt, ptlim - pt, "%s", urlptr->doc) ; |
| 465 |
break ; |
| 466 |
case 'h': |
| 467 |
pt += snprintf(pt, ptlim - pt, "%s", urlptr->host) ; |
| 468 |
break ; |
| 469 |
case 'p': |
| 470 |
pt += snprintf(pt, ptlim - pt, "%d", urlptr->port) ; |
| 471 |
break ; |
| 472 |
case 's': |
| 473 |
pt += snprintf(pt, ptlim - pt, "%s", urlptr->scheme) ; |
| 474 |
break ; |
| 475 |
case 'u': |
| 476 |
pt += snprintf(pt, ptlim - pt, "%s", urlptr->user) ; |
| 477 |
break ; |
| 478 |
case 'w': |
| 479 |
pt += snprintf(pt, ptlim - pt, "%s", urlptr->pwd) ; |
| 480 |
break ; |
| 481 |
default: |
| 482 |
*pt++ = *format ; |
| 483 |
break ; |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
if (pt >= ptlim) return 0 ; |
| 488 |
*pt = '\0' ; |
| 489 |
return pt - buf ; |
| 490 |
} |
| 491 |
|
| 492 |
/* |
| 417 |
* Free a URL |
493 |
* Free a URL |
| 418 |
*/ |
494 |
*/ |
| 419 |
void |
495 |
void |