diff options
author | irungentoo <irungentoo@gmail.com> | 2013-11-15 16:33:21 -0800 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-11-15 16:33:21 -0800 |
commit | da21a8f84483602dcaed09be615ce97c878d847b (patch) | |
tree | 925cf23baeda7a36bc961560cc7e1b30c969718a | |
parent | 706b6ec11c5e3944a87579f9af5119f631afe85d (diff) | |
parent | ca621bd44089e196b867f63d003bf974d5bd6d83 (diff) |
Merge pull request #651 from FullName/ntox-wrap-basic-with-continuation
Basic wrap(): Embed continuation markers if there's enough space.
-rw-r--r-- | testing/nTox.c | 109 |
1 files changed, 83 insertions, 26 deletions
diff --git a/testing/nTox.c b/testing/nTox.c index 35841bbe..d9206415 100644 --- a/testing/nTox.c +++ b/testing/nTox.c | |||
@@ -59,7 +59,9 @@ char input_line[STRING_LENGTH]; | |||
59 | const size_t wrap_cont_len = 3; | 59 | const size_t wrap_cont_len = 3; |
60 | const char wrap_cont_str[] = "\n+ "; | 60 | const char wrap_cont_str[] = "\n+ "; |
61 | 61 | ||
62 | /* documented: fdmnlsahxgiztq */ | 62 | #define STRING_LENGTH_WRAPPED (STRING_LENGTH + 16 * (wrap_cont_len + 1)) |
63 | |||
64 | /* documented: fdmnlsahxgiztq(c[rfg]) */ | ||
63 | /* undocumented: d (tox_do()) */ | 65 | /* undocumented: d (tox_do()) */ |
64 | 66 | ||
65 | /* 249 characters */ | 67 | /* 249 characters */ |
@@ -593,29 +595,82 @@ void line_eval(Tox *m, char *line) | |||
593 | } | 595 | } |
594 | } | 596 | } |
595 | 597 | ||
596 | /* basic wrap, ignores embedded '\t', '\n' or '|' */ | 598 | /* basic wrap, ignores embedded '\t', '\n' or '|' |
597 | void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) | 599 | * inserts continuation markers if there's enough space left, |
600 | * otherwise turns spaces into newlines if possible */ | ||
601 | void wrap(char output[STRING_LENGTH_WRAPPED], char input[STRING_LENGTH], int line_width) | ||
598 | { | 602 | { |
599 | strcpy(output, input); | 603 | size_t i, k, m, len = strlen(input); |
600 | size_t i, k, m, len = strlen(output); | 604 | |
605 | if ((line_width < 4) || (len < (size_t)line_width)) { | ||
606 | /* if line_width ridiculously tiny, it's not worth the effort */ | ||
607 | strcpy(output, input); | ||
608 | return; | ||
609 | } | ||
610 | |||
611 | /* how much can we shift? */ | ||
612 | size_t delta_is = 0, delta_remain = STRING_LENGTH_WRAPPED - len - 1; | ||
613 | |||
614 | /* if the line is very very short, don't insert continuation markers, | ||
615 | * as they would use up too much of the line */ | ||
616 | if ((size_t)line_width < 2 * wrap_cont_len) | ||
617 | delta_remain = 0; | ||
601 | 618 | ||
602 | for (i = line_width; i < len; i = i + line_width) { | 619 | for (i = line_width; i < len; i += line_width) { |
603 | /* look backward for a space to turn into a new line */ | 620 | /* look backward for a space to expand/turn into a new line */ |
604 | k = i; | 621 | k = i; |
605 | m = i - line_width; | 622 | m = i - line_width; |
606 | 623 | ||
607 | while (output[k] != ' ' && k > m) { | 624 | while (input[k] != ' ' && k > m) { |
608 | k--; | 625 | k--; |
609 | } | 626 | } |
610 | 627 | ||
611 | if (k > m) { | 628 | if (k > m) { |
612 | /* replace and set as new line start */ | 629 | if (delta_remain > wrap_cont_len) { |
613 | output[k] = '\n'; | 630 | /* replace space with continuation, then |
614 | i = k + 1; | 631 | * set the pos. after the space as new line start |
615 | } | 632 | * (i.e. space is being "eaten") */ |
633 | memcpy(output + m + delta_is, input + m, k - m); | ||
634 | strcpy(output + k + delta_is, wrap_cont_str); | ||
635 | |||
636 | delta_remain -= wrap_cont_len - 1; | ||
637 | delta_is += wrap_cont_len - 1; | ||
638 | i = k + 1; | ||
639 | } else { | ||
640 | /* no more space to push forward: replace the space, | ||
641 | * use its pos. + 1 as starting point for the next line */ | ||
642 | memcpy(output + m + delta_is, input + m, k - m); | ||
643 | output[k + delta_is] = '\n'; | ||
644 | i = k + 1; | ||
645 | } | ||
646 | } else { | ||
647 | /* string ends right here: | ||
648 | * don't add a continuation marker with nothing following */ | ||
649 | if (i == len - 1) | ||
650 | break; | ||
651 | |||
652 | /* nothing found backwards */ | ||
653 | if (delta_remain > wrap_cont_len) { | ||
654 | /* break at the end of the line, | ||
655 | * i.e. in the middle of the word at the border */ | ||
656 | memcpy(output + m + delta_is, input + m, line_width); | ||
657 | strcpy(output + i + delta_is, wrap_cont_str); | ||
616 | 658 | ||
617 | /* else: nothing to do now, TODO shift string */ | 659 | delta_remain -= wrap_cont_len; |
660 | delta_is += wrap_cont_len; | ||
661 | } else { | ||
662 | /* no more space to push, no space to convert: | ||
663 | * just copy the whole line and move on; | ||
664 | * means the line count calc'ed will be off */ | ||
665 | memcpy(output + m + delta_is, input + m, line_width); | ||
666 | } | ||
667 | } | ||
618 | } | 668 | } |
669 | |||
670 | i -= line_width; | ||
671 | memcpy(output + i + delta_is, input + i, len - i); | ||
672 | |||
673 | output[len + delta_is] = 0; | ||
619 | } | 674 | } |
620 | 675 | ||
621 | /* | 676 | /* |
@@ -623,7 +678,7 @@ void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) | |||
623 | * marks wrapped lines with "+ " in front, which does expand output | 678 | * marks wrapped lines with "+ " in front, which does expand output |
624 | * does NOT honor '\t': would require a lot more work (and tab width isn't always 8) | 679 | * does NOT honor '\t': would require a lot more work (and tab width isn't always 8) |
625 | */ | 680 | */ |
626 | void wrap_bars(char output[STRING_LENGTH], char input[STRING_LENGTH], size_t line_width) | 681 | void wrap_bars(char output[STRING_LENGTH_WRAPPED], char input[STRING_LENGTH], size_t line_width) |
627 | { | 682 | { |
628 | size_t len = strlen(input); | 683 | size_t len = strlen(input); |
629 | size_t ipos, opos = 0; | 684 | size_t ipos, opos = 0; |
@@ -639,8 +694,8 @@ void wrap_bars(char output[STRING_LENGTH], char input[STRING_LENGTH], size_t lin | |||
639 | 694 | ||
640 | output[opos++] = input[ipos]; | 695 | output[opos++] = input[ipos]; |
641 | 696 | ||
642 | if (opos >= STRING_LENGTH) { | 697 | if (opos >= STRING_LENGTH_WRAPPED) { |
643 | opos = STRING_LENGTH - 1; | 698 | opos = STRING_LENGTH_WRAPPED - 1; |
644 | break; | 699 | break; |
645 | } | 700 | } |
646 | 701 | ||
@@ -648,8 +703,8 @@ void wrap_bars(char output[STRING_LENGTH], char input[STRING_LENGTH], size_t lin | |||
648 | output[opos - 1] = ' '; | 703 | output[opos - 1] = ' '; |
649 | bar_avail = opos; | 704 | bar_avail = opos; |
650 | 705 | ||
651 | if (opos + 2 >= STRING_LENGTH) { | 706 | if (opos + 2 >= STRING_LENGTH_WRAPPED) { |
652 | opos = STRING_LENGTH - 1; | 707 | opos = STRING_LENGTH_WRAPPED - 1; |
653 | break; | 708 | break; |
654 | } | 709 | } |
655 | 710 | ||
@@ -673,8 +728,8 @@ void wrap_bars(char output[STRING_LENGTH], char input[STRING_LENGTH], size_t lin | |||
673 | } | 728 | } |
674 | 729 | ||
675 | if (space_avail > nl_got) { | 730 | if (space_avail > nl_got) { |
676 | if (opos + wrap_cont_len - 1 >= STRING_LENGTH) { | 731 | if (opos + wrap_cont_len - 1 >= STRING_LENGTH_WRAPPED) { |
677 | opos = STRING_LENGTH - 1; | 732 | opos = STRING_LENGTH_WRAPPED - 1; |
678 | break; | 733 | break; |
679 | } | 734 | } |
680 | 735 | ||
@@ -691,8 +746,8 @@ void wrap_bars(char output[STRING_LENGTH], char input[STRING_LENGTH], size_t lin | |||
691 | char c = input[ipos]; | 746 | char c = input[ipos]; |
692 | 747 | ||
693 | if ((c == '|') || (c == ' ') || (c == '\n')) { | 748 | if ((c == '|') || (c == ' ') || (c == '\n')) { |
694 | if (opos + wrap_cont_len >= STRING_LENGTH) { | 749 | if (opos + wrap_cont_len >= STRING_LENGTH_WRAPPED) { |
695 | opos = STRING_LENGTH - 1; | 750 | opos = STRING_LENGTH_WRAPPED - 1; |
696 | break; | 751 | break; |
697 | } | 752 | } |
698 | 753 | ||
@@ -704,8 +759,8 @@ void wrap_bars(char output[STRING_LENGTH], char input[STRING_LENGTH], size_t lin | |||
704 | 759 | ||
705 | output[opos++] = input[ipos]; | 760 | output[opos++] = input[ipos]; |
706 | 761 | ||
707 | if (opos >= STRING_LENGTH) { | 762 | if (opos >= STRING_LENGTH_WRAPPED) { |
708 | opos = STRING_LENGTH - 1; | 763 | opos = STRING_LENGTH_WRAPPED - 1; |
709 | break; | 764 | break; |
710 | } | 765 | } |
711 | 766 | ||
@@ -713,6 +768,9 @@ void wrap_bars(char output[STRING_LENGTH], char input[STRING_LENGTH], size_t lin | |||
713 | } | 768 | } |
714 | } | 769 | } |
715 | 770 | ||
771 | if (opos >= STRING_LENGTH_WRAPPED) | ||
772 | opos = STRING_LENGTH_WRAPPED - 1; | ||
773 | |||
716 | output[opos] = 0; | 774 | output[opos] = 0; |
717 | } | 775 | } |
718 | 776 | ||
@@ -744,7 +802,7 @@ char *appender(char *str, const char c) | |||
744 | void do_refresh() | 802 | void do_refresh() |
745 | { | 803 | { |
746 | int count = 0; | 804 | int count = 0; |
747 | char wrap_output[STRING_LENGTH]; | 805 | char wrap_output[STRING_LENGTH_WRAPPED]; |
748 | int L; | 806 | int L; |
749 | int i; | 807 | int i; |
750 | 808 | ||
@@ -983,7 +1041,6 @@ void write_file(Tox *m, int friendnumber, uint8_t filenumber, uint8_t *data, uin | |||
983 | fclose(pFile); | 1041 | fclose(pFile); |
984 | } | 1042 | } |
985 | 1043 | ||
986 | |||
987 | int main(int argc, char *argv[]) | 1044 | int main(int argc, char *argv[]) |
988 | { | 1045 | { |
989 | /* minimalistic locale support (i.e. when printing dates) */ | 1046 | /* minimalistic locale support (i.e. when printing dates) */ |