1 /*
2  * virbuffer.h: buffers for libvirt
3  *
4  * Copyright (C) 2005-2008, 2011-2014 Red Hat, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library.  If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
21 #pragma once
22 
23 #include <stdarg.h>
24 
25 #include "internal.h"
26 
27 
28 /**
29  * virBuffer:
30  *
31  * A buffer structure.
32  */
33 typedef struct _virBuffer virBuffer;
34 
35 #define VIR_BUFFER_INITIALIZER { NULL, 0 }
36 
37 /**
38  * VIR_BUFFER_INIT_CHILD:
39  * @parentbuf: parent buffer for XML element formatting
40  *
41  * Initialize a virBuffer structure and set up the indentation level for
42  * formatting XML subelements of @parentbuf.
43  */
44 #define VIR_BUFFER_INIT_CHILD(parentbuf) { NULL, (parentbuf)->indent + 2 }
45 
46 struct _virBuffer {
47     GString *str;
48     int indent;
49 };
50 
51 const char *virBufferCurrentContent(virBuffer *buf);
52 char *virBufferContentAndReset(virBuffer *buf);
53 void virBufferFreeAndReset(virBuffer *buf);
54 
55 G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(virBuffer, virBufferFreeAndReset);
56 
57 size_t virBufferUse(const virBuffer *buf);
58 void virBufferAdd(virBuffer *buf, const char *str, int len);
59 void virBufferAddBuffer(virBuffer *buf, virBuffer *toadd);
60 void virBufferAddChar(virBuffer *buf, char c);
61 void virBufferAsprintf(virBuffer *buf, const char *format, ...)
62   G_GNUC_PRINTF(2, 3);
63 void virBufferVasprintf(virBuffer *buf, const char *format, va_list ap)
64   G_GNUC_PRINTF(2, 0);
65 void virBufferStrcat(virBuffer *buf, ...)
66   G_GNUC_NULL_TERMINATED;
67 void virBufferStrcatVArgs(virBuffer *buf, va_list ap);
68 
69 void virBufferEscape(virBuffer *buf, char escape, const char *toescape,
70                      const char *format, const char *str);
71 void virBufferEscapeString(virBuffer *buf, const char *format,
72                            const char *str);
73 void virBufferEscapeSexpr(virBuffer *buf, const char *format,
74                           const char *str);
75 void virBufferEscapeRegex(virBuffer *buf,
76                           const char *format,
77                           const char *str);
78 void virBufferEscapeSQL(virBuffer *buf,
79                         const char *format,
80                         const char *str);
81 void virBufferEscapeShell(virBuffer *buf, const char *str);
82 void virBufferURIEncodeString(virBuffer *buf, const char *str);
83 
84 #define virBufferAddLit(buf_, literal_string_) \
85     virBufferAdd(buf_, "" literal_string_ "", sizeof(literal_string_) - 1)
86 
87 void virBufferAdjustIndent(virBuffer *buf, int indent);
88 void virBufferSetIndent(virBuffer *, int indent);
89 
90 size_t virBufferGetIndent(const virBuffer *buf);
91 size_t virBufferGetEffectiveIndent(const virBuffer *buf);
92 
93 void virBufferTrim(virBuffer *buf, const char *trim);
94 void virBufferTrimChars(virBuffer *buf, const char *trim);
95 void virBufferTrimLen(virBuffer *buf, int len);
96 void virBufferAddStr(virBuffer *buf, const char *str);
97