conf: Rework virDomainTimerDefFormat()

Use virXMLFormatElement() to simplify virDomainTimerDefFormat().

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Michal Privoznik 2022-01-24 06:41:09 +01:00
parent 82e902ddcc
commit a1c47cb8fc

View File

@ -26105,57 +26105,47 @@ static int
virDomainTimerDefFormat(virBuffer *buf, virDomainTimerDefFormat(virBuffer *buf,
virDomainTimerDef *def) virDomainTimerDef *def)
{ {
const char *name = virDomainTimerNameTypeToString(def->name); virBuffer timerAttr = VIR_BUFFER_INITIALIZER;
virBuffer timerChld = VIR_BUFFER_INIT_CHILD(buf);
virBuffer catchupAttr = VIR_BUFFER_INITIALIZER;
if (!name) { virBufferAsprintf(&timerAttr, " name='%s'",
virReportError(VIR_ERR_INTERNAL_ERROR, virDomainTimerNameTypeToString(def->name));
_("unexpected timer name %d"), def->name);
return -1;
}
virBufferAsprintf(buf, "<timer name='%s'", name);
if (def->present != VIR_TRISTATE_BOOL_ABSENT) { if (def->present != VIR_TRISTATE_BOOL_ABSENT) {
virBufferAsprintf(buf, " present='%s'", virBufferAsprintf(&timerAttr, " present='%s'",
virTristateBoolTypeToString(def->present)); virTristateBoolTypeToString(def->present));
} }
if (def->tickpolicy) { if (def->tickpolicy) {
virBufferAsprintf(buf, " tickpolicy='%s'", virBufferAsprintf(&timerAttr, " tickpolicy='%s'",
virDomainTimerTickpolicyTypeToString(def->tickpolicy)); virDomainTimerTickpolicyTypeToString(def->tickpolicy));
} }
if (def->track != VIR_DOMAIN_TIMER_TRACK_NONE) { if (def->track != VIR_DOMAIN_TIMER_TRACK_NONE) {
virBufferAsprintf(buf, " track='%s'", virBufferAsprintf(&timerAttr, " track='%s'",
virDomainTimerTrackTypeToString(def->track)); virDomainTimerTrackTypeToString(def->track));
} }
if (def->name == VIR_DOMAIN_TIMER_NAME_TSC) { if (def->name == VIR_DOMAIN_TIMER_NAME_TSC) {
if (def->frequency > 0) if (def->frequency > 0)
virBufferAsprintf(buf, " frequency='%llu'", def->frequency); virBufferAsprintf(&timerAttr, " frequency='%llu'", def->frequency);
if (def->mode) { if (def->mode) {
virBufferAsprintf(buf, " mode='%s'", virBufferAsprintf(&timerAttr, " mode='%s'",
virDomainTimerModeTypeToString(def->mode)); virDomainTimerModeTypeToString(def->mode));
} }
} }
if (def->catchup.threshold == 0 && def->catchup.slew == 0 && if (def->catchup.threshold > 0)
def->catchup.limit == 0) { virBufferAsprintf(&catchupAttr, " threshold='%lu'", def->catchup.threshold);
virBufferAddLit(buf, "/>\n"); if (def->catchup.slew > 0)
} else { virBufferAsprintf(&catchupAttr, " slew='%lu'", def->catchup.slew);
virBufferAddLit(buf, ">\n"); if (def->catchup.limit > 0)
virBufferAdjustIndent(buf, 2); virBufferAsprintf(&catchupAttr, " limit='%lu'", def->catchup.limit);
virBufferAddLit(buf, "<catchup");
if (def->catchup.threshold > 0) virXMLFormatElement(&timerChld, "catchup", &catchupAttr, NULL);
virBufferAsprintf(buf, " threshold='%lu'", def->catchup.threshold); virXMLFormatElement(buf, "timer", &timerAttr, &timerChld);
if (def->catchup.slew > 0)
virBufferAsprintf(buf, " slew='%lu'", def->catchup.slew);
if (def->catchup.limit > 0)
virBufferAsprintf(buf, " limit='%lu'", def->catchup.limit);
virBufferAddLit(buf, "/>\n");
virBufferAdjustIndent(buf, -2);
virBufferAddLit(buf, "</timer>\n");
}
return 0; return 0;
} }