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 module gst.base.ByteWriter;
26 
27 private import glib.ConstructionException;
28 private import glib.Str;
29 private import gobject.ObjectG;
30 private import gst.base.c.functions;
31 public  import gst.base.c.types;
32 private import gstreamer.Buffer;
33 private import gtkd.Loader;
34 
35 
36 /**
37  * #GstByteWriter provides a byte writer and reader that can write/read different
38  * integer and floating point types to/from a memory buffer. It provides functions
39  * for writing/reading signed/unsigned, little/big endian integers of 8, 16, 24,
40  * 32 and 64 bits and functions for reading little/big endian floating points numbers of
41  * 32 and 64 bits. It also provides functions to write/read NUL-terminated strings
42  * in various character encodings.
43  */
44 public class ByteWriter
45 {
46 	/** the main Gtk struct */
47 	protected GstByteWriter* gstByteWriter;
48 	protected bool ownedRef;
49 
50 	/** Get the main Gtk struct */
51 	public GstByteWriter* getByteWriterStruct(bool transferOwnership = false)
52 	{
53 		if (transferOwnership)
54 			ownedRef = false;
55 		return gstByteWriter;
56 	}
57 
58 	/** the main Gtk struct as a void* */
59 	protected void* getStruct()
60 	{
61 		return cast(void*)gstByteWriter;
62 	}
63 
64 	/**
65 	 * Sets our main struct and passes it to the parent class.
66 	 */
67 	public this (GstByteWriter* gstByteWriter, bool ownedRef = false)
68 	{
69 		this.gstByteWriter = gstByteWriter;
70 		this.ownedRef = ownedRef;
71 	}
72 
73 	~this ()
74 	{
75 		if ( Linker.isLoaded(LIBRARY_GSTBASE) && ownedRef )
76 			gst_byte_writer_free(gstByteWriter);
77 	}
78 
79 	/**
80 	 * Frees @writer and all memory allocated by it except
81 	 * the current data, which is returned.
82 	 *
83 	 * Free-function: g_free
84 	 *
85 	 * Returns: the current data. g_free() after usage.
86 	 */
87 	public ubyte[] freeAndGetData()
88 	{
89 		ubyte* p = gst_byte_writer_free_and_get_data(gstByteWriter);
90 		ownedRef = false;
91 		return p[0 .. gstByteWriter.parent.size];
92 	}
93 
94 	/**
95 	 * Resets @writer and returns the current data.
96 	 *
97 	 * Free-function: g_free
98 	 *
99 	 * Returns: the current data. g_free() after
100 	 *     usage.
101 	 */
102 	public ubyte[] resetAndGetData()
103 	{
104 		auto p = gst_byte_writer_reset_and_get_data(gstByteWriter);
105 
106 		return p[0 .. gstByteWriter.parent.size];
107 	}
108 
109 	/**
110 	 */
111 
112 	/**
113 	 * Checks if enough free space from the current write cursor is
114 	 * available and reallocates if necessary.
115 	 *
116 	 * Params:
117 	 *     size = Number of bytes that should be available
118 	 *
119 	 * Returns: %TRUE if at least @size bytes are still available
120 	 */
121 	public bool ensureFreeSpace(uint size)
122 	{
123 		return gst_byte_writer_ensure_free_space(gstByteWriter, size) != 0;
124 	}
125 
126 	/**
127 	 * Writes @size bytes containing @value to @writer.
128 	 *
129 	 * Params:
130 	 *     value = Value to be written
131 	 *     size = Number of bytes to be written
132 	 *
133 	 * Returns: %TRUE if the value could be written
134 	 */
135 	public bool fill(ubyte value, uint size)
136 	{
137 		return gst_byte_writer_fill(gstByteWriter, value, size) != 0;
138 	}
139 
140 	/**
141 	 * Frees @writer and all memory allocated by it.
142 	 */
143 	public void free()
144 	{
145 		gst_byte_writer_free(gstByteWriter);
146 		ownedRef = false;
147 	}
148 
149 	/**
150 	 * Frees @writer and all memory allocated by it except
151 	 * the current data, which is returned as #GstBuffer.
152 	 *
153 	 * Free-function: gst_buffer_unref
154 	 *
155 	 * Returns: the current data as buffer. gst_buffer_unref()
156 	 *     after usage.
157 	 */
158 	public Buffer freeAndGetBuffer()
159 	{
160 		auto p = gst_byte_writer_free_and_get_buffer(gstByteWriter);
161 
162 		if(p is null)
163 		{
164 			return null;
165 		}
166 
167 		return ObjectG.getDObject!(Buffer)(cast(GstBuffer*) p, true);
168 	}
169 
170 	/**
171 	 * Returns the remaining size of data that can still be written. If
172 	 * -1 is returned the remaining size is only limited by system resources.
173 	 *
174 	 * Returns: the remaining size of data that can still be written
175 	 */
176 	public uint getRemaining()
177 	{
178 		return gst_byte_writer_get_remaining(gstByteWriter);
179 	}
180 
181 	/**
182 	 * Initializes @writer to an empty instance
183 	 */
184 	public void init()
185 	{
186 		gst_byte_writer_init(gstByteWriter);
187 	}
188 
189 	/**
190 	 * Initializes @writer with the given
191 	 * memory area. If @initialized is %TRUE it is possible to
192 	 * read @size bytes from the #GstByteWriter from the beginning.
193 	 *
194 	 * Params:
195 	 *     data = Memory area for writing
196 	 *     initialized = If %TRUE the complete data can be read from the beginning
197 	 */
198 	public void initWithData(ubyte[] data, bool initialized)
199 	{
200 		gst_byte_writer_init_with_data(gstByteWriter, data.ptr, cast(uint)data.length, initialized);
201 	}
202 
203 	/**
204 	 * Initializes @writer with the given initial data size.
205 	 *
206 	 * Params:
207 	 *     size = Initial size of data
208 	 *     fixed = If %TRUE the data can't be reallocated
209 	 */
210 	public void initWithSize(uint size, bool fixed)
211 	{
212 		gst_byte_writer_init_with_size(gstByteWriter, size, fixed);
213 	}
214 
215 	/**
216 	 * Writes @size bytes of @data to @writer.
217 	 *
218 	 * Params:
219 	 *     buffer = source #GstBuffer
220 	 *     offset = offset to copy from
221 	 *     size = total size to copy. If -1, all data is copied
222 	 *
223 	 * Returns: %TRUE if the data could be written
224 	 */
225 	public bool putBuffer(Buffer buffer, size_t offset, ptrdiff_t size)
226 	{
227 		return gst_byte_writer_put_buffer(gstByteWriter, (buffer is null) ? null : buffer.getBufferStruct(), offset, size) != 0;
228 	}
229 
230 	/**
231 	 * Writes @size bytes of @data to @writer.
232 	 *
233 	 * Params:
234 	 *     data = Data to write
235 	 *
236 	 * Returns: %TRUE if the value could be written
237 	 */
238 	public bool putData(ubyte[] data)
239 	{
240 		return gst_byte_writer_put_data(gstByteWriter, data.ptr, cast(uint)data.length) != 0;
241 	}
242 
243 	/**
244 	 * Writes a big endian 32 bit float to @writer.
245 	 *
246 	 * Params:
247 	 *     val = Value to write
248 	 *
249 	 * Returns: %TRUE if the value could be written
250 	 */
251 	public bool putFloat32Be(float val)
252 	{
253 		return gst_byte_writer_put_float32_be(gstByteWriter, val) != 0;
254 	}
255 
256 	/**
257 	 * Writes a little endian 32 bit float to @writer.
258 	 *
259 	 * Params:
260 	 *     val = Value to write
261 	 *
262 	 * Returns: %TRUE if the value could be written
263 	 */
264 	public bool putFloat32Le(float val)
265 	{
266 		return gst_byte_writer_put_float32_le(gstByteWriter, val) != 0;
267 	}
268 
269 	/**
270 	 * Writes a big endian 64 bit float to @writer.
271 	 *
272 	 * Params:
273 	 *     val = Value to write
274 	 *
275 	 * Returns: %TRUE if the value could be written
276 	 */
277 	public bool putFloat64Be(double val)
278 	{
279 		return gst_byte_writer_put_float64_be(gstByteWriter, val) != 0;
280 	}
281 
282 	/**
283 	 * Writes a little endian 64 bit float to @writer.
284 	 *
285 	 * Params:
286 	 *     val = Value to write
287 	 *
288 	 * Returns: %TRUE if the value could be written
289 	 */
290 	public bool putFloat64Le(double val)
291 	{
292 		return gst_byte_writer_put_float64_le(gstByteWriter, val) != 0;
293 	}
294 
295 	/**
296 	 * Writes a signed big endian 16 bit integer to @writer.
297 	 *
298 	 * Params:
299 	 *     val = Value to write
300 	 *
301 	 * Returns: %TRUE if the value could be written
302 	 */
303 	public bool putInt16Be(short val)
304 	{
305 		return gst_byte_writer_put_int16_be(gstByteWriter, val) != 0;
306 	}
307 
308 	/**
309 	 * Writes a signed little endian 16 bit integer to @writer.
310 	 *
311 	 * Params:
312 	 *     val = Value to write
313 	 *
314 	 * Returns: %TRUE if the value could be written
315 	 */
316 	public bool putInt16Le(short val)
317 	{
318 		return gst_byte_writer_put_int16_le(gstByteWriter, val) != 0;
319 	}
320 
321 	/**
322 	 * Writes a signed big endian 24 bit integer to @writer.
323 	 *
324 	 * Params:
325 	 *     val = Value to write
326 	 *
327 	 * Returns: %TRUE if the value could be written
328 	 */
329 	public bool putInt24Be(int val)
330 	{
331 		return gst_byte_writer_put_int24_be(gstByteWriter, val) != 0;
332 	}
333 
334 	/**
335 	 * Writes a signed little endian 24 bit integer to @writer.
336 	 *
337 	 * Params:
338 	 *     val = Value to write
339 	 *
340 	 * Returns: %TRUE if the value could be written
341 	 */
342 	public bool putInt24Le(int val)
343 	{
344 		return gst_byte_writer_put_int24_le(gstByteWriter, val) != 0;
345 	}
346 
347 	/**
348 	 * Writes a signed big endian 32 bit integer to @writer.
349 	 *
350 	 * Params:
351 	 *     val = Value to write
352 	 *
353 	 * Returns: %TRUE if the value could be written
354 	 */
355 	public bool putInt32Be(int val)
356 	{
357 		return gst_byte_writer_put_int32_be(gstByteWriter, val) != 0;
358 	}
359 
360 	/**
361 	 * Writes a signed little endian 32 bit integer to @writer.
362 	 *
363 	 * Params:
364 	 *     val = Value to write
365 	 *
366 	 * Returns: %TRUE if the value could be written
367 	 */
368 	public bool putInt32Le(int val)
369 	{
370 		return gst_byte_writer_put_int32_le(gstByteWriter, val) != 0;
371 	}
372 
373 	/**
374 	 * Writes a signed big endian 64 bit integer to @writer.
375 	 *
376 	 * Params:
377 	 *     val = Value to write
378 	 *
379 	 * Returns: %TRUE if the value could be written
380 	 */
381 	public bool putInt64Be(long val)
382 	{
383 		return gst_byte_writer_put_int64_be(gstByteWriter, val) != 0;
384 	}
385 
386 	/**
387 	 * Writes a signed little endian 64 bit integer to @writer.
388 	 *
389 	 * Params:
390 	 *     val = Value to write
391 	 *
392 	 * Returns: %TRUE if the value could be written
393 	 */
394 	public bool putInt64Le(long val)
395 	{
396 		return gst_byte_writer_put_int64_le(gstByteWriter, val) != 0;
397 	}
398 
399 	/**
400 	 * Writes a signed 8 bit integer to @writer.
401 	 *
402 	 * Params:
403 	 *     val = Value to write
404 	 *
405 	 * Returns: %TRUE if the value could be written
406 	 */
407 	public bool putInt8(byte val)
408 	{
409 		return gst_byte_writer_put_int8(gstByteWriter, val) != 0;
410 	}
411 
412 	/**
413 	 * Writes a NUL-terminated UTF16 string to @writer (including the terminator).
414 	 *
415 	 * Params:
416 	 *     data = UTF16 string to write
417 	 *
418 	 * Returns: %TRUE if the value could be written
419 	 */
420 	public bool putStringUtf16(ushort[] data)
421 	{
422 		return gst_byte_writer_put_string_utf16(gstByteWriter, data.ptr) != 0;
423 	}
424 
425 	/**
426 	 * Writes a NUL-terminated UTF32 string to @writer (including the terminator).
427 	 *
428 	 * Params:
429 	 *     data = UTF32 string to write
430 	 *
431 	 * Returns: %TRUE if the value could be written
432 	 */
433 	public bool putStringUtf32(uint[] data)
434 	{
435 		return gst_byte_writer_put_string_utf32(gstByteWriter, data.ptr) != 0;
436 	}
437 
438 	/**
439 	 * Writes a NUL-terminated UTF8 string to @writer (including the terminator).
440 	 *
441 	 * Params:
442 	 *     data = UTF8 string to
443 	 *         write
444 	 *
445 	 * Returns: %TRUE if the value could be written
446 	 */
447 	public bool putStringUtf8(string data)
448 	{
449 		return gst_byte_writer_put_string_utf8(gstByteWriter, Str.toStringz(data)) != 0;
450 	}
451 
452 	/**
453 	 * Writes a unsigned big endian 16 bit integer to @writer.
454 	 *
455 	 * Params:
456 	 *     val = Value to write
457 	 *
458 	 * Returns: %TRUE if the value could be written
459 	 */
460 	public bool putUint16Be(ushort val)
461 	{
462 		return gst_byte_writer_put_uint16_be(gstByteWriter, val) != 0;
463 	}
464 
465 	/**
466 	 * Writes a unsigned little endian 16 bit integer to @writer.
467 	 *
468 	 * Params:
469 	 *     val = Value to write
470 	 *
471 	 * Returns: %TRUE if the value could be written
472 	 */
473 	public bool putUint16Le(ushort val)
474 	{
475 		return gst_byte_writer_put_uint16_le(gstByteWriter, val) != 0;
476 	}
477 
478 	/**
479 	 * Writes a unsigned big endian 24 bit integer to @writer.
480 	 *
481 	 * Params:
482 	 *     val = Value to write
483 	 *
484 	 * Returns: %TRUE if the value could be written
485 	 */
486 	public bool putUint24Be(uint val)
487 	{
488 		return gst_byte_writer_put_uint24_be(gstByteWriter, val) != 0;
489 	}
490 
491 	/**
492 	 * Writes a unsigned little endian 24 bit integer to @writer.
493 	 *
494 	 * Params:
495 	 *     val = Value to write
496 	 *
497 	 * Returns: %TRUE if the value could be written
498 	 */
499 	public bool putUint24Le(uint val)
500 	{
501 		return gst_byte_writer_put_uint24_le(gstByteWriter, val) != 0;
502 	}
503 
504 	/**
505 	 * Writes a unsigned big endian 32 bit integer to @writer.
506 	 *
507 	 * Params:
508 	 *     val = Value to write
509 	 *
510 	 * Returns: %TRUE if the value could be written
511 	 */
512 	public bool putUint32Be(uint val)
513 	{
514 		return gst_byte_writer_put_uint32_be(gstByteWriter, val) != 0;
515 	}
516 
517 	/**
518 	 * Writes a unsigned little endian 32 bit integer to @writer.
519 	 *
520 	 * Params:
521 	 *     val = Value to write
522 	 *
523 	 * Returns: %TRUE if the value could be written
524 	 */
525 	public bool putUint32Le(uint val)
526 	{
527 		return gst_byte_writer_put_uint32_le(gstByteWriter, val) != 0;
528 	}
529 
530 	/**
531 	 * Writes a unsigned big endian 64 bit integer to @writer.
532 	 *
533 	 * Params:
534 	 *     val = Value to write
535 	 *
536 	 * Returns: %TRUE if the value could be written
537 	 */
538 	public bool putUint64Be(ulong val)
539 	{
540 		return gst_byte_writer_put_uint64_be(gstByteWriter, val) != 0;
541 	}
542 
543 	/**
544 	 * Writes a unsigned little endian 64 bit integer to @writer.
545 	 *
546 	 * Params:
547 	 *     val = Value to write
548 	 *
549 	 * Returns: %TRUE if the value could be written
550 	 */
551 	public bool putUint64Le(ulong val)
552 	{
553 		return gst_byte_writer_put_uint64_le(gstByteWriter, val) != 0;
554 	}
555 
556 	/**
557 	 * Writes a unsigned 8 bit integer to @writer.
558 	 *
559 	 * Params:
560 	 *     val = Value to write
561 	 *
562 	 * Returns: %TRUE if the value could be written
563 	 */
564 	public bool putUint8(ubyte val)
565 	{
566 		return gst_byte_writer_put_uint8(gstByteWriter, val) != 0;
567 	}
568 
569 	/**
570 	 * Resets @writer and frees the data if it's
571 	 * owned by @writer.
572 	 */
573 	public void reset()
574 	{
575 		gst_byte_writer_reset(gstByteWriter);
576 	}
577 
578 	/**
579 	 * Resets @writer and returns the current data as buffer.
580 	 *
581 	 * Free-function: gst_buffer_unref
582 	 *
583 	 * Returns: the current data as buffer. gst_buffer_unref()
584 	 *     after usage.
585 	 */
586 	public Buffer resetAndGetBuffer()
587 	{
588 		auto p = gst_byte_writer_reset_and_get_buffer(gstByteWriter);
589 
590 		if(p is null)
591 		{
592 			return null;
593 		}
594 
595 		return ObjectG.getDObject!(Buffer)(cast(GstBuffer*) p, true);
596 	}
597 
598 	/**
599 	 * Creates a new, empty #GstByteWriter instance
600 	 *
601 	 * Free-function: gst_byte_writer_free
602 	 *
603 	 * Returns: a new, empty #GstByteWriter instance
604 	 *
605 	 * Throws: ConstructionException GTK+ fails to create the object.
606 	 */
607 	public this()
608 	{
609 		auto p = gst_byte_writer_new();
610 
611 		if(p is null)
612 		{
613 			throw new ConstructionException("null returned by new");
614 		}
615 
616 		this(cast(GstByteWriter*) p);
617 	}
618 
619 	/**
620 	 * Creates a new #GstByteWriter instance with the given
621 	 * memory area. If @initialized is %TRUE it is possible to
622 	 * read @size bytes from the #GstByteWriter from the beginning.
623 	 *
624 	 * Free-function: gst_byte_writer_free
625 	 *
626 	 * Params:
627 	 *     data = Memory area for writing
628 	 *     initialized = If %TRUE the complete data can be read from the beginning
629 	 *
630 	 * Returns: a new #GstByteWriter instance
631 	 *
632 	 * Throws: ConstructionException GTK+ fails to create the object.
633 	 */
634 	public this(ubyte[] data, bool initialized)
635 	{
636 		auto p = gst_byte_writer_new_with_data(data.ptr, cast(uint)data.length, initialized);
637 
638 		if(p is null)
639 		{
640 			throw new ConstructionException("null returned by new_with_data");
641 		}
642 
643 		this(cast(GstByteWriter*) p);
644 	}
645 
646 	/**
647 	 * Creates a new #GstByteWriter instance with the given
648 	 * initial data size.
649 	 *
650 	 * Free-function: gst_byte_writer_free
651 	 *
652 	 * Params:
653 	 *     size = Initial size of data
654 	 *     fixed = If %TRUE the data can't be reallocated
655 	 *
656 	 * Returns: a new #GstByteWriter instance
657 	 *
658 	 * Throws: ConstructionException GTK+ fails to create the object.
659 	 */
660 	public this(uint size, bool fixed)
661 	{
662 		auto p = gst_byte_writer_new_with_size(size, fixed);
663 
664 		if(p is null)
665 		{
666 			throw new ConstructionException("null returned by new_with_size");
667 		}
668 
669 		this(cast(GstByteWriter*) p);
670 	}
671 }