Cleanup: simplify BKE_report_prependf

Format the prefix once, then prepend it using a function shared between
BKE_report_prependf & BKE_report_prepend.
Replace BLI_dynstr with BLI_vsprintfN for string formatting.
This commit is contained in:
Campbell Barton
2023-07-03 14:13:08 +10:00
parent 33a7b80a6f
commit c63fd11095

View File

@@ -138,55 +138,44 @@ void BKE_reportf(ReportList *reports, eReportType type, const char *_format, ...
}
}
void BKE_reports_prepend(ReportList *reports, const char *_prepend)
/**
* Shared logic behind #BKE_reports_prepend & #BKE_reports_prependf.
*/
static void reports_prepend_impl(ReportList *reports, const char *prepend)
{
Report *report;
DynStr *ds;
const char *prepend = TIP_(_prepend);
if (!reports) {
return;
}
for (report = reports->list.first; report; report = report->next) {
ds = BLI_dynstr_new();
BLI_dynstr_append(ds, prepend);
BLI_dynstr_append(ds, report->message);
/* Caller must ensure. */
BLI_assert(reports && reports->list.first);
const size_t prefix_len = strlen(prepend);
for (Report *report = reports->list.first; report; report = report->next) {
char *message = BLI_string_joinN(prepend, report->message);
MEM_freeN((void *)report->message);
report->message = BLI_dynstr_get_cstring(ds);
report->len = BLI_dynstr_get_len(ds);
BLI_dynstr_free(ds);
report->message = message;
report->len += prefix_len;
BLI_assert(report->len == strlen(message));
}
}
void BKE_reports_prependf(ReportList *reports, const char *_prepend, ...)
void BKE_reports_prepend(ReportList *reports, const char *prepend)
{
Report *report;
DynStr *ds;
va_list args;
const char *prepend = TIP_(_prepend);
if (!reports) {
if (!reports || !reports->list.first) {
return;
}
reports_prepend_impl(reports, TIP_(prepend));
}
for (report = reports->list.first; report; report = report->next) {
ds = BLI_dynstr_new();
va_start(args, _prepend);
BLI_dynstr_vappendf(ds, prepend, args);
va_end(args);
BLI_dynstr_append(ds, report->message);
MEM_freeN((void *)report->message);
report->message = BLI_dynstr_get_cstring(ds);
report->len = BLI_dynstr_get_len(ds);
BLI_dynstr_free(ds);
void BKE_reports_prependf(ReportList *reports, const char *prepend_format, ...)
{
if (!reports || !reports->list.first) {
return;
}
va_list args;
va_start(args, prepend_format);
char *prepend = BLI_vsprintfN(TIP_(prepend_format), args);
va_end(args);
reports_prepend_impl(reports, prepend);
MEM_freeN(prepend);
}
eReportType BKE_report_print_level(ReportList *reports)