1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19  
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 /*
25  * Conversion parameters:
26  * inFile  = glib-Byte-Arrays.html
27  * outPack = glib
28  * outFile = ByteArray
29  * strct   = GByteArray
30  * realStrct=
31  * ctorStrct=
32  * clss    = ByteArray
33  * interf  = 
34  * class Code: No
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_byte_array_
41  * omit structs:
42  * omit prefixes:
43  * 	- g_bytes_
44  * omit code:
45  * omit signals:
46  * imports:
47  * 	- glib.Bytes
48  * structWrap:
49  * 	- GByteArray* -> ByteArray
50  * 	- GBytes* -> Bytes
51  * module aliases:
52  * local aliases:
53  * overrides:
54  */
55 
56 module glib.ByteArray;
57 
58 public  import gtkc.glibtypes;
59 
60 private import gtkc.glib;
61 private import glib.ConstructionException;
62 
63 
64 private import glib.Bytes;
65 
66 
67 
68 
69 /**
70  * GByteArray is a mutable array of bytes based on GArray, to provide arrays
71  * of bytes which grow automatically as elements are added.
72  *
73  * To create a new GByteArray use g_byte_array_new(). To add elements to a
74  * GByteArray, use g_byte_array_append(), and g_byte_array_prepend().
75  *
76  * To set the size of a GByteArray, use g_byte_array_set_size().
77  *
78  * To free a GByteArray, use g_byte_array_free().
79  *
80  * $(DDOC_COMMENT example)
81  *
82  * See GBytes if you are interested in an immutable object representing a
83  * sequence of bytes.
84  */
85 public class ByteArray
86 {
87 	
88 	/** the main Gtk struct */
89 	protected GByteArray* gByteArray;
90 	
91 	
92 	public GByteArray* getByteArrayStruct()
93 	{
94 		return gByteArray;
95 	}
96 	
97 	
98 	/** the main Gtk struct as a void* */
99 	protected void* getStruct()
100 	{
101 		return cast(void*)gByteArray;
102 	}
103 	
104 	/**
105 	 * Sets our main struct and passes it to the parent class
106 	 */
107 	public this (GByteArray* gByteArray)
108 	{
109 		this.gByteArray = gByteArray;
110 	}
111 	
112 	/**
113 	 */
114 	
115 	/**
116 	 * Creates a new GByteArray with a reference count of 1.
117 	 * Throws: ConstructionException GTK+ fails to create the object.
118 	 */
119 	public this ()
120 	{
121 		// GByteArray * g_byte_array_new (void);
122 		auto p = g_byte_array_new();
123 		if(p is null)
124 		{
125 			throw new ConstructionException("null returned by g_byte_array_new()");
126 		}
127 		this(cast(GByteArray*) p);
128 	}
129 	
130 	/**
131 	 * Create byte array containing the data. The data will be owned by the array
132 	 * and will be freed with g_free(), i.e. it could be allocated using g_strdup().
133 	 * Since 2.32
134 	 * Params:
135 	 * data = byte data for the array. [transfer full][array length=len]
136 	 * Throws: ConstructionException GTK+ fails to create the object.
137 	 */
138 	public this (ubyte[] data)
139 	{
140 		// GByteArray * g_byte_array_new_take (guint8 *data,  gsize len);
141 		auto p = g_byte_array_new_take(data.ptr, cast(int) data.length);
142 		if(p is null)
143 		{
144 			throw new ConstructionException("null returned by g_byte_array_new_take(data.ptr, cast(int) data.length)");
145 		}
146 		this(cast(GByteArray*) p);
147 	}
148 	
149 	/**
150 	 * Creates a new GByteArray with reserved_size bytes preallocated.
151 	 * This avoids frequent reallocation, if you are going to add many
152 	 * bytes to the array. Note however that the size of the array is still
153 	 * 0.
154 	 * Params:
155 	 * reservedSize = number of bytes preallocated.
156 	 * Returns: the new GByteArray.
157 	 */
158 	public static ByteArray sizedNew(uint reservedSize)
159 	{
160 		// GByteArray * g_byte_array_sized_new (guint reserved_size);
161 		auto p = g_byte_array_sized_new(reservedSize);
162 		
163 		if(p is null)
164 		{
165 			return null;
166 		}
167 		
168 		return new ByteArray(cast(GByteArray*) p);
169 	}
170 	
171 	/**
172 	 * Atomically increments the reference count of array by one. This
173 	 * function is MT-safe and may be called from any thread.
174 	 * Since 2.22
175 	 * Returns: The passed in GByteArray.
176 	 */
177 	public ByteArray doref()
178 	{
179 		// GByteArray * g_byte_array_ref (GByteArray *array);
180 		auto p = g_byte_array_ref(gByteArray);
181 		
182 		if(p is null)
183 		{
184 			return null;
185 		}
186 		
187 		return new ByteArray(cast(GByteArray*) p);
188 	}
189 	
190 	/**
191 	 * Atomically decrements the reference count of array by one. If the
192 	 * reference count drops to 0, all memory allocated by the array is
193 	 * released. This function is MT-safe and may be called from any
194 	 * thread.
195 	 * Since 2.22
196 	 */
197 	public void unref()
198 	{
199 		// void g_byte_array_unref (GByteArray *array);
200 		g_byte_array_unref(gByteArray);
201 	}
202 	
203 	/**
204 	 * Adds the given bytes to the end of the GByteArray. The array will
205 	 * grow in size automatically if necessary.
206 	 * Params:
207 	 * data = the byte data to be added.
208 	 * Returns: the GByteArray.
209 	 */
210 	public ByteArray append(ubyte[] data)
211 	{
212 		// GByteArray * g_byte_array_append (GByteArray *array,  const guint8 *data,  guint len);
213 		auto p = g_byte_array_append(gByteArray, data.ptr, cast(int) data.length);
214 		
215 		if(p is null)
216 		{
217 			return null;
218 		}
219 		
220 		return new ByteArray(cast(GByteArray*) p);
221 	}
222 	
223 	/**
224 	 * Adds the given data to the start of the GByteArray. The array will
225 	 * grow in size automatically if necessary.
226 	 * Params:
227 	 * data = the byte data to be added.
228 	 * Returns: the GByteArray.
229 	 */
230 	public ByteArray prepend(ubyte[] data)
231 	{
232 		// GByteArray * g_byte_array_prepend (GByteArray *array,  const guint8 *data,  guint len);
233 		auto p = g_byte_array_prepend(gByteArray, data.ptr, cast(int) data.length);
234 		
235 		if(p is null)
236 		{
237 			return null;
238 		}
239 		
240 		return new ByteArray(cast(GByteArray*) p);
241 	}
242 	
243 	/**
244 	 * Removes the byte at the given index from a GByteArray. The
245 	 * following bytes are moved down one place.
246 	 * Params:
247 	 * index = the index of the byte to remove.
248 	 * Returns: the GByteArray.
249 	 */
250 	public ByteArray removeIndex(uint index)
251 	{
252 		// GByteArray * g_byte_array_remove_index (GByteArray *array,  guint index_);
253 		auto p = g_byte_array_remove_index(gByteArray, index);
254 		
255 		if(p is null)
256 		{
257 			return null;
258 		}
259 		
260 		return new ByteArray(cast(GByteArray*) p);
261 	}
262 	
263 	/**
264 	 * Removes the byte at the given index from a GByteArray. The last
265 	 * element in the array is used to fill in the space, so this function
266 	 * does not preserve the order of the GByteArray. But it is faster
267 	 * than g_byte_array_remove_index().
268 	 * Params:
269 	 * index = the index of the byte to remove.
270 	 * Returns: the GByteArray.
271 	 */
272 	public ByteArray removeIndexFast(uint index)
273 	{
274 		// GByteArray * g_byte_array_remove_index_fast (GByteArray *array,  guint index_);
275 		auto p = g_byte_array_remove_index_fast(gByteArray, index);
276 		
277 		if(p is null)
278 		{
279 			return null;
280 		}
281 		
282 		return new ByteArray(cast(GByteArray*) p);
283 	}
284 	
285 	/**
286 	 * Removes the given number of bytes starting at the given index from a
287 	 * GByteArray. The following elements are moved to close the gap.
288 	 * Since 2.4
289 	 * Params:
290 	 * index = the index of the first byte to remove.
291 	 * length = the number of bytes to remove.
292 	 * Returns: the GByteArray.
293 	 */
294 	public ByteArray removeRange(uint index, uint length)
295 	{
296 		// GByteArray * g_byte_array_remove_range (GByteArray *array,  guint index_,  guint length);
297 		auto p = g_byte_array_remove_range(gByteArray, index, length);
298 		
299 		if(p is null)
300 		{
301 			return null;
302 		}
303 		
304 		return new ByteArray(cast(GByteArray*) p);
305 	}
306 	
307 	/**
308 	 * Sorts a byte array, using compare_func which should be a
309 	 * qsort()-style comparison function (returns less than zero for first
310 	 * arg is less than second arg, zero for equal, greater than zero if
311 	 * first arg is greater than second arg).
312 	 * If two array elements compare equal, their order in the sorted array
313 	 * is undefined. If you want equal elements to keep their order (i.e.
314 	 * you want a stable sort) you can write a comparison function that,
315 	 * if two elements would otherwise compare equal, compares them by
316 	 * their addresses.
317 	 * Params:
318 	 * compareFunc = comparison function.
319 	 */
320 	public void sort(GCompareFunc compareFunc)
321 	{
322 		// void g_byte_array_sort (GByteArray *array,  GCompareFunc compare_func);
323 		g_byte_array_sort(gByteArray, compareFunc);
324 	}
325 	
326 	/**
327 	 * Like g_byte_array_sort(), but the comparison function takes an extra
328 	 * user data argument.
329 	 * Params:
330 	 * compareFunc = comparison function.
331 	 * userData = data to pass to compare_func.
332 	 */
333 	public void sortWithData(GCompareDataFunc compareFunc, void* userData)
334 	{
335 		// void g_byte_array_sort_with_data (GByteArray *array,  GCompareDataFunc compare_func,  gpointer user_data);
336 		g_byte_array_sort_with_data(gByteArray, compareFunc, userData);
337 	}
338 	
339 	/**
340 	 * Sets the size of the GByteArray, expanding it if necessary.
341 	 * Params:
342 	 * length = the new size of the GByteArray.
343 	 * Returns: the GByteArray.
344 	 */
345 	public ByteArray setSize(uint length)
346 	{
347 		// GByteArray * g_byte_array_set_size (GByteArray *array,  guint length);
348 		auto p = g_byte_array_set_size(gByteArray, length);
349 		
350 		if(p is null)
351 		{
352 			return null;
353 		}
354 		
355 		return new ByteArray(cast(GByteArray*) p);
356 	}
357 	
358 	/**
359 	 * Frees the memory allocated by the GByteArray. If free_segment is
360 	 * TRUE it frees the actual byte data. If the reference count of
361 	 * array is greater than one, the GByteArray wrapper is preserved but
362 	 * the size of array will be set to zero.
363 	 * Params:
364 	 * freeSegment = if TRUE the actual byte data is freed as well.
365 	 * Returns: the element data if free_segment is FALSE, otherwise NULL. The element data should be freed using g_free().
366 	 */
367 	public ubyte* free(int freeSegment)
368 	{
369 		// guint8 * g_byte_array_free (GByteArray *array,  gboolean free_segment);
370 		return g_byte_array_free(gByteArray, freeSegment);
371 	}
372 	
373 	/**
374 	 * Transfers the data from the GByteArray into a new immutable GBytes.
375 	 * The GByteArray is freed unless the reference count of array is greater
376 	 * than one, the GByteArray wrapper is preserved but the size of array
377 	 * will be set to zero.
378 	 * This is identical to using g_bytes_new_take() and g_byte_array_free()
379 	 * together.
380 	 * Since 2.32
381 	 * Returns: a new immutable GBytes representing same byte data that was in the array. [transfer full]
382 	 */
383 	public Bytes freeToBytes()
384 	{
385 		// GBytes * g_byte_array_free_to_bytes (GByteArray *array);
386 		auto p = g_byte_array_free_to_bytes(gByteArray);
387 		
388 		if(p is null)
389 		{
390 			return null;
391 		}
392 		
393 		return new Bytes(cast(GBytes*) p);
394 	}
395 }