1 module soup.Buffer;
2 
3 private import glib.Bytes;
4 private import glib.ConstructionException;
5 private import glib.MemorySlice;
6 private import gobject.ObjectG;
7 private import soup.c.functions;
8 public  import soup.c.types;
9 
10 
11 /**
12  * A data buffer, generally used to represent a chunk of a
13  * #SoupMessageBody.
14  * 
15  * @data is a #char because that's generally convenient; in some
16  * situations you may need to cast it to #guchar or another type.
17  */
18 public final class Buffer
19 {
20 	/** the main Gtk struct */
21 	protected SoupBuffer* soupBuffer;
22 	protected bool ownedRef;
23 
24 	/** Get the main Gtk struct */
25 	public SoupBuffer* getBufferStruct(bool transferOwnership = false)
26 	{
27 		if (transferOwnership)
28 			ownedRef = false;
29 		return soupBuffer;
30 	}
31 
32 	/** the main Gtk struct as a void* */
33 	protected void* getStruct()
34 	{
35 		return cast(void*)soupBuffer;
36 	}
37 
38 	/**
39 	 * Sets our main struct and passes it to the parent class.
40 	 */
41 	public this (SoupBuffer* soupBuffer, bool ownedRef = false)
42 	{
43 		this.soupBuffer = soupBuffer;
44 		this.ownedRef = ownedRef;
45 	}
46 
47 	~this ()
48 	{
49 		if ( ownedRef )
50 			soup_buffer_free(soupBuffer);
51 	}
52 
53 
54 	/**
55 	 * the data
56 	 */
57 	public @property void* data()
58 	{
59 		return soupBuffer.data;
60 	}
61 
62 	/** Ditto */
63 	public @property void data(void* value)
64 	{
65 		soupBuffer.data = value;
66 	}
67 
68 	/**
69 	 * length of @data
70 	 */
71 	public @property size_t length()
72 	{
73 		return soupBuffer.length;
74 	}
75 
76 	/** Ditto */
77 	public @property void length(size_t value)
78 	{
79 		soupBuffer.length = value;
80 	}
81 
82 	/** */
83 	public static GType getType()
84 	{
85 		return soup_buffer_get_type();
86 	}
87 
88 	/**
89 	 * Creates a new #SoupBuffer containing @length bytes from @data.
90 	 *
91 	 * Params:
92 	 *     use = how @data is to be used by the buffer
93 	 *     data = data
94 	 *
95 	 * Returns: the new #SoupBuffer.
96 	 *
97 	 * Throws: ConstructionException GTK+ fails to create the object.
98 	 */
99 	public this(SoupMemoryUse use, ubyte[] data)
100 	{
101 		auto __p = soup_buffer_new(use, data.ptr, cast(size_t)data.length);
102 
103 		if(__p is null)
104 		{
105 			throw new ConstructionException("null returned by new");
106 		}
107 
108 		this(cast(SoupBuffer*) __p);
109 	}
110 
111 	/**
112 	 * Creates a new #SoupBuffer containing @length bytes from @data.
113 	 *
114 	 * This function is exactly equivalent to soup_buffer_new() with
115 	 * %SOUP_MEMORY_TAKE as first argument; it exists mainly for
116 	 * convenience and simplifying language bindings.
117 	 *
118 	 * Params:
119 	 *     data = data
120 	 *
121 	 * Returns: the new #SoupBuffer.
122 	 *
123 	 * Since: 2.32
124 	 *
125 	 * Throws: ConstructionException GTK+ fails to create the object.
126 	 */
127 	public this(char[] data)
128 	{
129 		auto __p = soup_buffer_new_take(data.ptr, cast(size_t)data.length);
130 
131 		if(__p is null)
132 		{
133 			throw new ConstructionException("null returned by new_take");
134 		}
135 
136 		this(cast(SoupBuffer*) __p);
137 	}
138 
139 	/**
140 	 * Creates a new #SoupBuffer containing @length bytes from @data. When
141 	 * the #SoupBuffer is freed, it will call @owner_dnotify, passing
142 	 * @owner to it. You must ensure that @data will remain valid until
143 	 * @owner_dnotify is called.
144 	 *
145 	 * For example, you could use this to create a buffer containing data
146 	 * returned from libxml without needing to do an extra copy:
147 	 *
148 	 * <informalexample><programlisting>
149 	 * xmlDocDumpMemory (doc, &xmlbody, &len);
150 	 * return soup_buffer_new_with_owner (xmlbody, len, xmlbody,
151 	 * (GDestroyNotify)xmlFree);
152 	 * </programlisting></informalexample>
153 	 *
154 	 * In this example, @data and @owner are the same, but in other cases
155 	 * they would be different (eg, @owner would be a object, and @data
156 	 * would be a pointer to one of the object's fields).
157 	 *
158 	 * Params:
159 	 *     data = data
160 	 *     owner = pointer to an object that owns @data
161 	 *     ownerDnotify = a function to free/unref @owner when
162 	 *         the buffer is freed
163 	 *
164 	 * Returns: the new #SoupBuffer.
165 	 *
166 	 * Throws: ConstructionException GTK+ fails to create the object.
167 	 */
168 	public this(ubyte[] data, void* owner, GDestroyNotify ownerDnotify)
169 	{
170 		auto __p = soup_buffer_new_with_owner(data.ptr, cast(size_t)data.length, owner, ownerDnotify);
171 
172 		if(__p is null)
173 		{
174 			throw new ConstructionException("null returned by new_with_owner");
175 		}
176 
177 		this(cast(SoupBuffer*) __p);
178 	}
179 
180 	/**
181 	 * Makes a copy of @buffer. In reality, #SoupBuffer is a refcounted
182 	 * type, and calling soup_buffer_copy() will normally just increment
183 	 * the refcount on @buffer and return it. However, if @buffer was
184 	 * created with #SOUP_MEMORY_TEMPORARY memory, then soup_buffer_copy()
185 	 * will actually return a copy of it, so that the data in the copy
186 	 * will remain valid after the temporary buffer is freed.
187 	 *
188 	 * Returns: the new (or newly-reffed) buffer
189 	 */
190 	public Buffer copy()
191 	{
192 		auto __p = soup_buffer_copy(soupBuffer);
193 
194 		if(__p is null)
195 		{
196 			return null;
197 		}
198 
199 		return ObjectG.getDObject!(Buffer)(cast(SoupBuffer*) __p, true);
200 	}
201 
202 	/**
203 	 * Frees @buffer. (In reality, as described in the documentation for
204 	 * soup_buffer_copy(), this is actually an "unref" operation, and may
205 	 * or may not actually free @buffer.)
206 	 */
207 	public void free()
208 	{
209 		soup_buffer_free(soupBuffer);
210 		ownedRef = false;
211 	}
212 
213 	/**
214 	 * Creates a #GBytes pointing to the same memory as @buffer. The
215 	 * #GBytes will hold a reference on @buffer to ensure that it is not
216 	 * freed while the #GBytes is still valid.
217 	 *
218 	 * Returns: a new #GBytes which has the same content
219 	 *     as the #SoupBuffer.
220 	 *
221 	 * Since: 2.40
222 	 */
223 	public Bytes getAsBytes()
224 	{
225 		auto __p = soup_buffer_get_as_bytes(soupBuffer);
226 
227 		if(__p is null)
228 		{
229 			return null;
230 		}
231 
232 		return new Bytes(cast(GBytes*) __p, true);
233 	}
234 
235 	/**
236 	 * This function exists for use by language bindings, because it's not
237 	 * currently possible to get the right effect by annotating the fields
238 	 * of #SoupBuffer.
239 	 *
240 	 * Params:
241 	 *     data = the pointer
242 	 *         to the buffer data is stored here
243 	 *
244 	 * Since: 2.32
245 	 */
246 	public void getData(out ubyte[] data)
247 	{
248 		ubyte* outdata = null;
249 		size_t length;
250 
251 		soup_buffer_get_data(soupBuffer, &outdata, &length);
252 
253 		data = outdata[0 .. length];
254 	}
255 
256 	/**
257 	 * Gets the "owner" object for a buffer created with
258 	 * soup_buffer_new_with_owner().
259 	 *
260 	 * Returns: the owner pointer
261 	 */
262 	public void* getOwner()
263 	{
264 		return soup_buffer_get_owner(soupBuffer);
265 	}
266 
267 	/**
268 	 * Creates a new #SoupBuffer containing @length bytes "copied" from
269 	 * @parent starting at @offset. (Normally this will not actually copy
270 	 * any data, but will instead simply reference the same data as
271 	 * @parent does.)
272 	 *
273 	 * Params:
274 	 *     offset = offset within @parent to start at
275 	 *     length = number of bytes to copy from @parent
276 	 *
277 	 * Returns: the new #SoupBuffer.
278 	 */
279 	public Buffer newSubbuffer(size_t offset, size_t length)
280 	{
281 		auto __p = soup_buffer_new_subbuffer(soupBuffer, offset, length);
282 
283 		if(__p is null)
284 		{
285 			return null;
286 		}
287 
288 		return ObjectG.getDObject!(Buffer)(cast(SoupBuffer*) __p, true);
289 	}
290 }