virbuffer.h 3.9 KB
Newer Older
D
Daniel Veillard 已提交
1
/*
2
 * virbuffer.h: buffers for libvirt
D
Daniel Veillard 已提交
3
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2005-2008, 2011-2014 Red Hat, Inc.
D
Daniel Veillard 已提交
5
 *
O
Osier Yang 已提交
6 7 8 9 10 11 12 13 14 15 16
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
D
Daniel Veillard 已提交
19 20
 */

21
#pragma once
D
Daniel Veillard 已提交
22

23
#include <stdarg.h>
24

25 26
#include "internal.h"
#include "virautoclean.h"
D
Daniel Veillard 已提交
27

E
Eric Blake 已提交
28

D
Daniel Veillard 已提交
29
/**
D
Daniel P. Berrange 已提交
30
 * virBuffer:
D
Daniel Veillard 已提交
31 32 33
 *
 * A buffer structure.
 */
D
Daniel P. Berrange 已提交
34 35
typedef struct _virBuffer virBuffer;
typedef virBuffer *virBufferPtr;
36

37
#define VIR_BUFFER_INITIALIZER { 0, 0, 0, 0, NULL }
38

D
Daniel P. Berrange 已提交
39
struct _virBuffer {
40 41
    size_t size;
    size_t use;
42
    int error; /* errno value, or -1 for usage error */
43 44
    int indent;
    char *content;
D
Daniel Veillard 已提交
45 46
};

47
const char *virBufferCurrentContent(virBufferPtr buf);
E
Eric Blake 已提交
48 49
char *virBufferContentAndReset(virBufferPtr buf);
void virBufferFreeAndReset(virBufferPtr buf);
50
int virBufferError(const virBuffer *buf);
J
Ján Tomko 已提交
51 52 53 54 55
int virBufferCheckErrorInternal(const virBuffer *buf,
                                int domcode,
                                const char *filename,
                                const char *funcname,
                                size_t linenr)
56
    ATTRIBUTE_NONNULL(1);
57 58 59

VIR_DEFINE_AUTOCLEAN_FUNC(virBuffer, virBufferFreeAndReset);

J
Ján Tomko 已提交
60 61 62 63 64
/**
 * virBufferCheckError
 *
 * Checks if the buffer is in error state and reports an error.
 *
E
Eric Blake 已提交
65
 * Returns 0 if no error has occurred, otherwise an error is reported
J
Ján Tomko 已提交
66 67
 * and -1 is returned.
 */
68
#define virBufferCheckError(buf) \
J
Ján Tomko 已提交
69 70
    virBufferCheckErrorInternal(buf, VIR_FROM_THIS, __FILE__, __FUNCTION__, \
    __LINE__)
71
size_t virBufferUse(const virBuffer *buf);
E
Eric Blake 已提交
72
void virBufferAdd(virBufferPtr buf, const char *str, int len);
73
void virBufferAddBuffer(virBufferPtr buf, virBufferPtr toadd);
E
Eric Blake 已提交
74 75
void virBufferAddChar(virBufferPtr buf, char c);
void virBufferAsprintf(virBufferPtr buf, const char *format, ...)
76
  ATTRIBUTE_FMT_PRINTF(2, 3);
E
Eric Blake 已提交
77
void virBufferVasprintf(virBufferPtr buf, const char *format, va_list ap)
E
Eric Blake 已提交
78
  ATTRIBUTE_FMT_PRINTF(2, 0);
E
Eric Blake 已提交
79
void virBufferStrcat(virBufferPtr buf, ...)
80
  ATTRIBUTE_SENTINEL;
81 82
void virBufferStrcatVArgs(virBufferPtr buf, va_list ap);

83
void virBufferEscape(virBufferPtr buf, char escape, const char *toescape,
E
Eric Blake 已提交
84 85 86 87 88
                     const char *format, const char *str);
void virBufferEscapeString(virBufferPtr buf, const char *format,
                           const char *str);
void virBufferEscapeSexpr(virBufferPtr buf, const char *format,
                          const char *str);
89 90 91
void virBufferEscapeRegex(virBufferPtr buf,
                          const char *format,
                          const char *str);
L
Ladi Prosek 已提交
92 93 94
void virBufferEscapeSQL(virBufferPtr buf,
                        const char *format,
                        const char *str);
G
Guido Günther 已提交
95
void virBufferEscapeShell(virBufferPtr buf, const char *str);
E
Eric Blake 已提交
96
void virBufferURIEncodeString(virBufferPtr buf, const char *str);
D
Daniel Veillard 已提交
97

98
#define virBufferAddLit(buf_, literal_string_) \
99
    virBufferAdd(buf_, "" literal_string_ "", sizeof(literal_string_) - 1)
100

E
Eric Blake 已提交
101
void virBufferAdjustIndent(virBufferPtr buf, int indent);
102 103
void virBufferSetIndent(virBufferPtr, int indent);

104 105 106 107 108 109
/**
 * virBufferSetChildIndent
 *
 * Gets the parent indentation, increments it by 2 and sets it to
 * child buffer.
 */
110
#define virBufferSetChildIndent(childBuf_, parentBuf_) \
111
    virBufferSetIndent(childBuf_, virBufferGetIndent(parentBuf_, false) + 2)
112

113
int virBufferGetIndent(const virBuffer *buf, bool dynamic);
E
Eric Blake 已提交
114

J
Ján Tomko 已提交
115
void virBufferTrim(virBufferPtr buf, const char *trim, int len);
116
void virBufferAddStr(virBufferPtr buf, const char *str);