1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2024-12-23 21:34:54 +03:00

snapshot: simplify indentation of nwfilter

Fixing this involved some refactoring of common code out of
domain_conf and nwfilter_conf into nwfilter_params.

* src/conf/nwfilter_params.h (virNWFilterFormatParamAttributes):
Adjust signature.
* src/conf/nwfilter_params.c (_formatParameterAttrs)
(virNWFilterFormatParamAttributes): Adjust indentation handling,
and handle filterref here.
(formatterParam): Delete unused struct.
* src/conf/domain_conf.c (virDomainNetDefFormat): Adjust caller.
* src/conf/nwfilter_conf.c (virNWFilterIncludeDefFormat): Likewise.
This commit is contained in:
Eric Blake 2011-09-22 13:05:03 -06:00
parent c04beb5d3a
commit 46e1a426f9
4 changed files with 37 additions and 48 deletions

View File

@ -9554,7 +9554,6 @@ virDomainNetDefFormat(virBufferPtr buf,
unsigned int flags)
{
const char *type = virDomainNetTypeToString(def->type);
char *attrs;
if (!type) {
virDomainReportError(VIR_ERR_INTERNAL_ERROR,
@ -9675,15 +9674,11 @@ virDomainNetDefFormat(virBufferPtr buf,
}
}
if (def->filter) {
virBufferEscapeString(buf, " <filterref filter='%s'",
def->filter);
attrs = virNWFilterFormatParamAttributes(def->filterparams,
" ");
if (!attrs || strlen(attrs) <= 1)
virBufferAddLit(buf, "/>\n");
else
virBufferAsprintf(buf, ">\n%s </filterref>\n", attrs);
VIR_FREE(attrs);
virBufferAdjustIndent(buf, 6);
if (virNWFilterFormatParamAttributes(buf, def->filterparams,
def->filter) < 0)
return -1;
virBufferAdjustIndent(buf, -6);
}
if (def->bootIndex)
virBufferAsprintf(buf, " <boot order='%d'/>\n", def->bootIndex);

View File

@ -2853,19 +2853,15 @@ no_memory:
static char *
virNWFilterIncludeDefFormat(virNWFilterIncludeDefPtr inc)
{
char *attrs;
virBuffer buf = VIR_BUFFER_INITIALIZER;
virBufferAsprintf(&buf," <filterref filter='%s'",
inc->filterref);
attrs = virNWFilterFormatParamAttributes(inc->params, " ");
if (!attrs || strlen(attrs) <= 1)
virBufferAddLit(&buf, "/>\n");
else
virBufferAsprintf(&buf, ">\n%s </filterref>\n", attrs);
virBufferAdjustIndent(&buf, 2);
if (virNWFilterFormatParamAttributes(&buf, inc->params,
inc->filterref) < 0) {
virBufferFreeAndReset(&buf);
return NULL;
}
virBufferAdjustIndent(&buf, -2);
if (virBufferError(&buf)) {
virReportOOMError();
virBufferFreeAndReset(&buf);

View File

@ -258,41 +258,36 @@ skip_entry:
}
struct formatterParam {
virBufferPtr buf;
const char *indent;
};
static void
_formatParameterAttrs(void *payload, const void *name, void *data)
{
struct formatterParam *fp = (struct formatterParam *)data;
virBufferPtr buf = data;
virBufferAsprintf(fp->buf, "%s<parameter name='%s' value='%s'/>\n",
fp->indent,
virBufferAsprintf(buf, " <parameter name='%s' value='%s'/>\n",
(const char *)name,
(char *)payload);
}
char *
virNWFilterFormatParamAttributes(virNWFilterHashTablePtr table,
const char *indent)
int
virNWFilterFormatParamAttributes(virBufferPtr buf,
virNWFilterHashTablePtr table,
const char *filterref)
{
virBuffer buf = VIR_BUFFER_INITIALIZER;
struct formatterParam fp = {
.buf = &buf,
.indent = indent,
};
int count = virHashSize(table->hashTable);
virHashForEach(table->hashTable, _formatParameterAttrs, &fp);
if (virBufferError(&buf)) {
virReportOOMError();
virBufferFreeAndReset(&buf);
return NULL;
if (count < 0) {
virNWFilterReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("missing filter parameter table"));
return -1;
}
return virBufferContentAndReset(&buf);
virBufferAsprintf(buf, "<filterref filter='%s'", filterref);
if (count) {
virBufferAddLit(buf, ">\n");
virHashForEach(table->hashTable, _formatParameterAttrs, buf);
virBufferAddLit(buf, "</filterref>\n");
} else {
virBufferAddLit(buf, "/>\n");
}
return 0;
}

View File

@ -1,6 +1,7 @@
/*
* nwfilter_params.h: parsing and data maintenance of filter parameters
*
* Copyright (C) 2011 Red Hat, Inc.
* Copyright (C) 2010 IBM Corporation
*
* This library is free software; you can redistribute it and/or
@ -23,6 +24,7 @@
# define NWFILTER_PARAMS_H
# include "hash.h"
# include "buf.h"
typedef struct _virNWFilterHashTable virNWFilterHashTable;
typedef virNWFilterHashTable *virNWFilterHashTablePtr;
@ -35,8 +37,9 @@ struct _virNWFilterHashTable {
virNWFilterHashTablePtr virNWFilterParseParamAttributes(xmlNodePtr cur);
char * virNWFilterFormatParamAttributes(virNWFilterHashTablePtr table,
const char *indent);
int virNWFilterFormatParamAttributes(virBufferPtr buf,
virNWFilterHashTablePtr table,
const char *filterref);
virNWFilterHashTablePtr virNWFilterHashTableCreate(int n);
void virNWFilterHashTableFree(virNWFilterHashTablePtr table);