diff --git a/src/report.c b/src/report.c index 55dc99ed7..e62adb7e8 100644 --- a/src/report.c +++ b/src/report.c @@ -120,7 +120,14 @@ void newline(stream *out) { sputs("", out); } -static const char *spaces = " "; +void write_spaces(stream *out, size_t num) { + static const char spaces[REPORTWIDTH] = " "; + while (num > 0) { + size_t bytes = (num > REPORTWIDTH) ? REPORTWIDTH : num; + swrite(spaces, sizeof(char), bytes, out); + num -= bytes; + } +} static void centre(stream *out, const char *s, bool breaking) { @@ -140,9 +147,8 @@ static void centre(stream *out, const char *s, bool breaking) freestrlist(T); } else { - swrite(spaces, sizeof(char), (REPORTWIDTH - strlen(s) + 1) / 2, out); + write_spaces(out, (REPORTWIDTH - strlen(s) + 1) / 2); sputs(s, out); - newline(out); } } @@ -176,16 +182,16 @@ char marker) const char *last_space = begin; if (mark && indent >= 2) { - swrite(spaces, sizeof(char), indent - 2, out); + write_spaces(out, indent - 2); swrite(mark, sizeof(char), 1, out); - swrite(spaces, sizeof(char), 1, out); + write_spaces(out, 1); mark = 0; } else if (begin == str) { - swrite(spaces, sizeof(char), indent, out); + write_spaces(out, indent); } else { - swrite(spaces, sizeof(char), indent + hanging_indent, out); + write_spaces(out, indent + hanging_indent); } while (*end && end <= begin + length - indent) { if (*end == ' ') { diff --git a/src/report.h b/src/report.h index 5f4da17a9..189a38f0f 100644 --- a/src/report.h +++ b/src/report.h @@ -15,8 +15,10 @@ extern "C" { #endif + struct stream; void register_nr(void); void report_cleanup(void); + void write_spaces(struct stream *out, size_t num); #ifdef __cplusplus } diff --git a/src/reports.test.c b/src/reports.test.c index eb772782f..29f7ae5aa 100644 --- a/src/reports.test.c +++ b/src/reports.test.c @@ -1,6 +1,7 @@ #include #include #include "reports.h" +#include "report.h" #include #include @@ -10,6 +11,8 @@ #include #include +#include +#include #include #include @@ -98,11 +101,43 @@ static void test_seen_faction(CuTest *tc) { CuAssertTrue(tc, f1->no < f2->no); } +static void test_write_spaces(CuTest *tc) { + stream out = { 0 }; + char buf[1024]; + size_t len; + + mstream_init(&out); + write_spaces(&out, 4); + out.api->rewind(out.handle); + len = out.api->read(out.handle, buf, sizeof(buf)); + buf[len] = '\0'; + CuAssertStrEquals(tc, " ", buf); + CuAssertIntEquals(tc, ' ', buf[3]); + mstream_done(&out); +} + +static void test_write_many_spaces(CuTest *tc) { + stream out = { 0 }; + char buf[1024]; + size_t len; + + mstream_init(&out); + write_spaces(&out, 100); + out.api->rewind(out.handle); + len = out.api->read(out.handle, buf, sizeof(buf)); + buf[len] = '\0'; + CuAssertIntEquals(tc, 100, (int)len); + CuAssertIntEquals(tc, ' ', buf[99]); + mstream_done(&out); +} + CuSuite *get_reports_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_reorder_units); SUITE_ADD_TEST(suite, test_seen_faction); SUITE_ADD_TEST(suite, test_regionid); + SUITE_ADD_TEST(suite, test_write_spaces); + SUITE_ADD_TEST(suite, test_write_many_spaces); return suite; }