virbuffer.h 4.1 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 22
#ifndef LIBVIRT_VIRBUFFER_H
# define LIBVIRT_VIRBUFFER_H
D
Daniel Veillard 已提交
23

24 25
# include <stdarg.h>

26
# include "internal.h"
27
# include "viralloc.h"
D
Daniel Veillard 已提交
28

E
Eric Blake 已提交
29

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

38
# ifndef __VIR_BUFFER_C__
E
Eric Blake 已提交
39
#  define VIR_BUFFER_INITIALIZER { 0, 0, 0, 0, NULL }
40

E
Eric Blake 已提交
41
/* This struct must be kept in sync with the real struct
42
   in the buf.c impl file */
D
Daniel P. Berrange 已提交
43
struct _virBuffer {
44 45 46
    unsigned int a;
    unsigned int b;
    unsigned int c;
E
Eric Blake 已提交
47 48
    int d;
    char *e;
D
Daniel Veillard 已提交
49
};
50
# endif
D
Daniel Veillard 已提交
51

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

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

102
# define virBufferAddLit(buf_, literal_string_) \
103
    virBufferAdd(buf_, "" literal_string_ "", sizeof(literal_string_) - 1)
104

E
Eric Blake 已提交
105
void virBufferAdjustIndent(virBufferPtr buf, int indent);
106 107
void virBufferSetIndent(virBufferPtr, int indent);

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

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

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

122
#endif /* LIBVIRT_VIRBUFFER_H */