… | |
… | |
21 | * |
21 | * |
22 | * The authors can be reached via e-mail to <support@deliantra.net> |
22 | * The authors can be reached via e-mail to <support@deliantra.net> |
23 | */ |
23 | */ |
24 | |
24 | |
25 | /* |
25 | /* |
26 | * General convenience functions for crossfire. |
26 | * General convenience functions for deliantra. |
27 | */ |
27 | */ |
28 | |
28 | |
29 | #include <cstdlib> |
29 | #include <cstdlib> |
30 | #include <sys/types.h> |
30 | #include <sys/types.h> |
31 | #include <unistd.h> |
31 | #include <unistd.h> |
… | |
… | |
410 | } |
410 | } |
411 | } |
411 | } |
412 | result[resultlen] = '\0'; |
412 | result[resultlen] = '\0'; |
413 | } |
413 | } |
414 | |
414 | |
415 | /** |
|
|
416 | * Taking a string as an argument, mutate it into a string that looks like a list. |
|
|
417 | * a 'list' for the purposes here, is a string of items, seperated by commas, except |
|
|
418 | * for the last entry, which has an 'and' before it, and a full stop (period) after it. |
|
|
419 | * This function will also strip all trailing non alphanumeric characters. |
|
|
420 | * It does not insert an oxford comma. |
|
|
421 | */ |
|
|
422 | void |
|
|
423 | make_list_like (char *input) |
|
|
424 | { |
|
|
425 | char *p, tmp[MAX_BUF]; |
|
|
426 | int i; |
|
|
427 | |
|
|
428 | if (!input || strlen (input) > MAX_BUF - 5) |
|
|
429 | return; |
|
|
430 | /* bad stuff would happen if we continued here, the -5 is to make space for ' and ' */ |
|
|
431 | |
|
|
432 | strncpy (tmp, input, MAX_BUF - 5); |
|
|
433 | /*trim all trailing commas, spaces etc. */ |
|
|
434 | for (i = strlen (tmp); !isalnum (tmp[i]) && i >= 0; i--) |
|
|
435 | tmp[i] = '\0'; |
|
|
436 | |
|
|
437 | strcat (tmp, "."); |
|
|
438 | |
|
|
439 | p = strrchr (tmp, ','); |
|
|
440 | if (p) |
|
|
441 | { |
|
|
442 | *p = '\0'; |
|
|
443 | strcpy (input, tmp); |
|
|
444 | p++; |
|
|
445 | strcat (input, " and"); |
|
|
446 | strcat (input, p); |
|
|
447 | } |
|
|
448 | else |
|
|
449 | strcpy (input, tmp); |
|
|
450 | |
|
|
451 | return; |
|
|
452 | } |
|
|
453 | |
|
|
454 | /******************************************************************************/ |
415 | /******************************************************************************/ |
455 | |
416 | |
456 | /* Checks a player-provided string which will become the msg property of |
417 | /* Checks a player-provided string which will become the msg property of |
457 | * an object for dangerous input. |
418 | * an object for dangerous input. |
458 | */ |
419 | */ |
… | |
… | |
612 | memcpy (dst, src, ++len); |
573 | memcpy (dst, src, ++len); |
613 | |
574 | |
614 | return len; |
575 | return len; |
615 | } |
576 | } |
616 | |
577 | |
617 | const char * |
578 | char * |
|
|
579 | vformat (const char *format, va_list ap) |
|
|
580 | { |
|
|
581 | static dynbuf_text buf; buf.clear (); |
|
|
582 | buf.vprintf (format, ap); |
|
|
583 | return buf; |
|
|
584 | } |
|
|
585 | |
|
|
586 | char * |
618 | format (const char *format, ...) |
587 | format (const char *format, ...) |
619 | { |
588 | { |
620 | static dynbuf_text buf; |
|
|
621 | |
|
|
622 | buf.clear (); |
|
|
623 | |
|
|
624 | va_list ap; |
589 | va_list ap; |
625 | va_start (ap, format); |
590 | va_start (ap, format); |
626 | buf.vprintf (format, ap); |
591 | char *buf = vformat (format, ap); |
627 | va_end (ap); |
592 | va_end (ap); |
628 | |
593 | |
629 | return buf; |
594 | return buf; |
630 | } |
595 | } |
631 | |
596 | |