/** * virBufferAdd: * @buf: the buffer to append to * @str: the string * @len: the number of bytes to add, or -1 * * Add a string range to an XML buffer. If @len == -1, the length of * str is recomputed to the full string. Auto indentation may be applied. * */ void virBufferAdd(virBufferPtr buf, const char *str, int len) { unsigned int needSize; int indent; if (!str || !buf || (len == 0 && buf->indent == 0)) return; indent = virBufferGetIndent(buf, true); if (indent < 0) return; if (len < 0) len = strlen(str); needSize = buf->use + indent + len + 2; if (virBufferGrow(buf, needSize - buf->use) < 0) return; memset(&buf->content[buf->use], ' ', indent); memcpy(&buf->content[buf->use + indent], str, len); buf->use += indent + len; buf->content[buf->use] = '\0'; }
/** * virSysinfoFormat: * @buf: buffer to append output to (may use auto-indentation) * @def: structure to convert to xml string * * Returns 0 on success, -1 on failure after generating an error message. */ int virSysinfoFormat(virBufferPtr buf, virSysinfoDefPtr def) { virBuffer childrenBuf = VIR_BUFFER_INITIALIZER; const char *type = virSysinfoTypeToString(def->type); int indent = virBufferGetIndent(buf, false); int ret = -1; if (!type) { virReportError(VIR_ERR_INTERNAL_ERROR, _("unexpected sysinfo type model %d"), def->type); virBufferFreeAndReset(buf); goto cleanup; } virBufferAdjustIndent(&childrenBuf, indent + 2); virSysinfoBIOSFormat(&childrenBuf, def->bios); virSysinfoSystemFormat(&childrenBuf, def->system); virSysinfoBaseBoardFormat(&childrenBuf, def->baseBoard, def->nbaseBoard); virSysinfoProcessorFormat(&childrenBuf, def); virSysinfoMemoryFormat(&childrenBuf, def); virBufferAsprintf(buf, "<sysinfo type='%s'", type); if (virBufferUse(&childrenBuf)) { virBufferAddLit(buf, ">\n"); virBufferAddBuffer(buf, &childrenBuf); virBufferAddLit(buf, "</sysinfo>\n"); } else { virBufferAddLit(buf, "/>\n"); } if (virBufferCheckError(buf) < 0) goto cleanup; ret = 0; cleanup: virBufferFreeAndReset(&childrenBuf); return ret; }