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 private import glib.Bytes;
64 
65 
66 
67 /**
68  * GByteArray is a mutable array of bytes based on GArray, to provide arrays
69  * of bytes which grow automatically as elements are added.
70  *
71  * To create a new GByteArray use g_byte_array_new(). To add elements to a
72  * GByteArray, use g_byte_array_append(), and g_byte_array_prepend().
73  *
74  * To set the size of a GByteArray, use g_byte_array_set_size().
75  *
76  * To free a GByteArray, use g_byte_array_free().
77  *
78  * $(DDOC_COMMENT example)
79  *
80  * See GBytes if you are interested in an immutable object representing a
81  * sequence of bytes.
82  */
83 public class ByteArray
84 {
85 	
86 	/** the main Gtk struct */
87 	protected GByteArray* gByteArray;
88 	
89 	
90 	/** Get the main Gtk struct */
91 	public GByteArray* getByteArrayStruct()
92 	{
93 		return gByteArray;
94 	}
95 	
96 	
97 	/** the main Gtk struct as a void* */
98 	protected void* getStruct()
99 	{
100 		return cast(void*)gByteArray;
101 	}
102 	
103 	/**
104 	 * Sets our main struct and passes it to the parent class
105 	 */
106 	public this (GByteArray* gByteArray)
107 	{
108 		this.gByteArray = gByteArray;
109 	}
110 	
111 	/**
112 	 */
113 	
114 	/**
115 	 * Creates a new GByteArray with a reference count of 1.
116 	 * Throws: ConstructionException GTK+ fails to create the object.
117 	 */
118 	public this ()
119 	{
120 		// GByteArray * g_byte_array_new (void);
121 		auto p = g_byte_array_new();
122 		if(p is null)
123 		{
124 			throw new ConstructionException("null returned by g_byte_array_new()");
125 		}
126 		this(cast(GByteArray*) p);
127 	}
128 	
129 	/**
130 	 * Create byte array containing the data. The data will be owned by the array
131 	 * and will be freed with g_free(), i.e. it could be allocated using g_strdup().
132 	 * Since 2.32
133 	 * Params:
134 	 * data = byte data for the array. [transfer full][array length=len]
135 	 * Throws: ConstructionException GTK+ fails to create the object.
136 	 */
137 	public this (ubyte[] data)
138 	{
139 		// GByteArray * g_byte_array_new_take (guint8 *data,  gsize len);
140 		auto p = g_byte_array_new_take(data.ptr, cast(int) data.length);
141 		if(p is null)
142 		{
143 			throw new ConstructionException("null returned by g_byte_array_new_take(data.ptr, cast(int) data.length)");
144 		}
145 		this(cast(GByteArray*) p);
146 	}
147 	
148 	/**
149 	 * Creates a new GByteArray with reserved_size bytes preallocated.
150 	 * This avoids frequent reallocation, if you are going to add many
151 	 * bytes to the array. Note however that the size of the array is still
152 	 * 0.
153 	 * Params:
154 	 * reservedSize = number of bytes preallocated.
155 	 * Returns: the new GByteArray.
156 	 */
157 	public static ByteArray sizedNew(uint reservedSize)
158 	{
159 		// GByteArray * g_byte_array_sized_new (guint reserved_size);
160 		auto p = g_byte_array_sized_new(reservedSize);
161 		
162 		if(p is null)
163 		{
164 			return null;
165 		}
166 		
167 		return new ByteArray(cast(GByteArray*) p);
168 	}
169 	
170 	/**
171 	 * Atomically increments the reference count of array by one. This
172 	 * function is MT-safe and may be called from any thread.
173 	 * Since 2.22
174 	 * Returns: The passed in GByteArray.
175 	 */
176 	public ByteArray doref()
177 	{
178 		// GByteArray * g_byte_array_ref (GByteArray *array);
179 		auto p = g_byte_array_ref(gByteArray);
180 		
181 		if(p is null)
182 		{
183 			return null;
184 		}
185 		
186 		return new ByteArray(cast(GByteArray*) p);
187 	}
188 	
189 	/**
190 	 * Atomically decrements the reference count of array by one. If the
191 	 * reference count drops to 0, all memory allocated by the array is
192 	 * released. This function is MT-safe and may be called from any
193 	 * thread.
194 	 * Since 2.22
195 	 */
196 	public void unref()
197 	{
198 		// void g_byte_array_unref (GByteArray *array);
199 		g_byte_array_unref(gByteArray);
200 	}
201 	
202 	/**
203 	 * Adds the given bytes to the end of the GByteArray. The array will
204 	 * grow in size automatically if necessary.
205 	 * Params:
206 	 * data = the byte data to be added.
207 	 * Returns: the GByteArray.
208 	 */
209 	public ByteArray append(ubyte[] data)
210 	{
211 		// GByteArray * g_byte_array_append (GByteArray *array,  const guint8 *data,  guint len);
212 		auto p = g_byte_array_append(gByteArray, data.ptr, cast(int) data.length);
213 		
214 		if(p is null)
215 		{
216 			return null;
217 		}
218 		
219 		return new ByteArray(cast(GByteArray*) p);
220 	}
221 	
222 	/**
223 	 * Adds the given data to the start of the GByteArray. The array will
224 	 * grow in size automatically if necessary.
225 	 * Params:
226 	 * data = the byte data to be added.
227 	 * Returns: the GByteArray.
228 	 */
229 	public ByteArray prepend(ubyte[] data)
230 	{
231 		// GByteArray * g_byte_array_prepend (GByteArray *array,  const guint8 *data,  guint len);
232 		auto p = g_byte_array_prepend(gByteArray, data.ptr, cast(int) data.length);
233 		
234 		if(p is null)
235 		{
236 			return null;
237 		}
238 		
239 		return new ByteArray(cast(GByteArray*) p);
240 	}
241 	
242 	/**
243 	 * Removes the byte at the given index from a GByteArray. The
244 	 * following bytes are moved down one place.
245 	 * Params:
246 	 * index = the index of the byte to remove.
247 	 * Returns: the GByteArray.
248 	 */
249 	public ByteArray removeIndex(uint index)
250 	{
251 		// GByteArray * g_byte_array_remove_index (GByteArray *array,  guint index_);
252 		auto p = g_byte_array_remove_index(gByteArray, index);
253 		
254 		if(p is null)
255 		{
256 			return null;
257 		}
258 		
259 		return new ByteArray(cast(GByteArray*) p);
260 	}
261 	
262 	/**
263 	 * Removes the byte at the given index from a GByteArray. The last
264 	 * element in the array is used to fill in the space, so this function
265 	 * does not preserve the order of the GByteArray. But it is faster
266 	 * than g_byte_array_remove_index().
267 	 * Params:
268 	 * index = the index of the byte to remove.
269 	 * Returns: the GByteArray.
270 	 */
271 	public ByteArray removeIndexFast(uint index)
272 	{
273 		// GByteArray * g_byte_array_remove_index_fast (GByteArray *array,  guint index_);
274 		auto p = g_byte_array_remove_index_fast(gByteArray, index);
275 		
276 		if(p is null)
277 		{
278 			return null;
279 		}
280 		
281 		return new ByteArray(cast(GByteArray*) p);
282 	}
283 	
284 	/**
285 	 * Removes the given number of bytes starting at the given index from a
286 	 * GByteArray. The following elements are moved to close the gap.
287 	 * Since 2.4
288 	 * Params:
289 	 * index = the index of the first byte to remove.
290 	 * length = the number of bytes to remove.
291 	 * Returns: the GByteArray.
292 	 */
293 	public ByteArray removeRange(uint index, uint length)
294 	{
295 		// GByteArray * g_byte_array_remove_range (GByteArray *array,  guint index_,  guint length);
296 		auto p = g_byte_array_remove_range(gByteArray, index, length);
297 		
298 		if(p is null)
299 		{
300 			return null;
301 		}
302 		
303 		return new ByteArray(cast(GByteArray*) p);
304 	}
305 	
306 	/**
307 	 * Sorts a byte array, using compare_func which should be a
308 	 * qsort()-style comparison function (returns less than zero for first
309 	 * arg is less than second arg, zero for equal, greater than zero if
310 	 * first arg is greater than second arg).
311 	 * If two array elements compare equal, their order in the sorted array
312 	 * is undefined. If you want equal elements to keep their order (i.e.
313 	 * you want a stable sort) you can write a comparison function that,
314 	 * if two elements would otherwise compare equal, compares them by
315 	 * their addresses.
316 	 * Params:
317 	 * compareFunc = comparison function.
318 	 */
319 	public void sort(GCompareFunc compareFunc)
320 	{
321 		// void g_byte_array_sort (GByteArray *array,  GCompareFunc compare_func);
322 		g_byte_array_sort(gByteArray, compareFunc);
323 	}
324 	
325 	/**
326 	 * Like g_byte_array_sort(), but the comparison function takes an extra
327 	 * user data argument.
328 	 * Params:
329 	 * compareFunc = comparison function.
330 	 * userData = data to pass to compare_func.
331 	 */
332 	public void sortWithData(GCompareDataFunc compareFunc, void* userData)
333 	{
334 		// void g_byte_array_sort_with_data (GByteArray *array,  GCompareDataFunc compare_func,  gpointer user_data);
335 		g_byte_array_sort_with_data(gByteArray, compareFunc, userData);
336 	}
337 	
338 	/**
339 	 * Sets the size of the GByteArray, expanding it if necessary.
340 	 * Params:
341 	 * length = the new size of the GByteArray.
342 	 * Returns: the GByteArray.
343 	 */
344 	public ByteArray setSize(uint length)
345 	{
346 		// GByteArray * g_byte_array_set_size (GByteArray *array,  guint length);
347 		auto p = g_byte_array_set_size(gByteArray, length);
348 		
349 		if(p is null)
350 		{
351 			return null;
352 		}
353 		
354 		return new ByteArray(cast(GByteArray*) p);
355 	}
356 	
357 	/**
358 	 * Frees the memory allocated by the GByteArray. If free_segment is
359 	 * TRUE it frees the actual byte data. If the reference count of
360 	 * array is greater than one, the GByteArray wrapper is preserved but
361 	 * the size of array will be set to zero.
362 	 * Params:
363 	 * freeSegment = if TRUE the actual byte data is freed as well.
364 	 * Returns: the element data if free_segment is FALSE, otherwise NULL. The element data should be freed using g_free().
365 	 */
366 	public ubyte* free(int freeSegment)
367 	{
368 		// guint8 * g_byte_array_free (GByteArray *array,  gboolean free_segment);
369 		return g_byte_array_free(gByteArray, freeSegment);
370 	}
371 	
372 	/**
373 	 * Transfers the data from the GByteArray into a new immutable GBytes.
374 	 * The GByteArray is freed unless the reference count of array is greater
375 	 * than one, the GByteArray wrapper is preserved but the size of array
376 	 * will be set to zero.
377 	 * This is identical to using g_bytes_new_take() and g_byte_array_free()
378 	 * together.
379 	 * Since 2.32
380 	 * Returns: a new immutable GBytes representing same byte data that was in the array. [transfer full]
381 	 */
382 	public Bytes freeToBytes()
383 	{
384 		// GBytes * g_byte_array_free_to_bytes (GByteArray *array);
385 		auto p = g_byte_array_free_to_bytes(gByteArray);
386 		
387 		if(p is null)
388 		{
389 			return null;
390 		}
391 		
392 		return new Bytes(cast(GBytes*) p);
393 	}
394 }