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 gio.DataInputStream; 26 27 private import gio.AsyncResultIF; 28 private import gio.BufferedInputStream; 29 private import gio.Cancellable; 30 private import gio.InputStream; 31 private import gio.c.functions; 32 public import gio.c.types; 33 private import glib.ConstructionException; 34 private import glib.ErrorG; 35 private import glib.GException; 36 private import glib.Str; 37 private import gobject.ObjectG; 38 public import gtkc.giotypes; 39 40 41 /** 42 * Data input stream implements #GInputStream and includes functions for 43 * reading structured data directly from a binary input stream. 44 */ 45 public class DataInputStream : BufferedInputStream 46 { 47 /** the main Gtk struct */ 48 protected GDataInputStream* gDataInputStream; 49 50 /** Get the main Gtk struct */ 51 public GDataInputStream* getDataInputStreamStruct(bool transferOwnership = false) 52 { 53 if (transferOwnership) 54 ownedRef = false; 55 return gDataInputStream; 56 } 57 58 /** the main Gtk struct as a void* */ 59 protected override void* getStruct() 60 { 61 return cast(void*)gDataInputStream; 62 } 63 64 /** 65 * Sets our main struct and passes it to the parent class. 66 */ 67 public this (GDataInputStream* gDataInputStream, bool ownedRef = false) 68 { 69 this.gDataInputStream = gDataInputStream; 70 super(cast(GBufferedInputStream*)gDataInputStream, ownedRef); 71 } 72 73 74 /** */ 75 public static GType getType() 76 { 77 return g_data_input_stream_get_type(); 78 } 79 80 /** 81 * Creates a new data input stream for the @base_stream. 82 * 83 * Params: 84 * baseStream = a #GInputStream. 85 * 86 * Returns: a new #GDataInputStream. 87 * 88 * Throws: ConstructionException GTK+ fails to create the object. 89 */ 90 public this(InputStream baseStream) 91 { 92 auto p = g_data_input_stream_new((baseStream is null) ? null : baseStream.getInputStreamStruct()); 93 94 if(p is null) 95 { 96 throw new ConstructionException("null returned by new"); 97 } 98 99 this(cast(GDataInputStream*) p, true); 100 } 101 102 /** 103 * Gets the byte order for the data input stream. 104 * 105 * Returns: the @stream's current #GDataStreamByteOrder. 106 */ 107 public GDataStreamByteOrder getByteOrder() 108 { 109 return g_data_input_stream_get_byte_order(gDataInputStream); 110 } 111 112 /** 113 * Gets the current newline type for the @stream. 114 * 115 * Returns: #GDataStreamNewlineType for the given @stream. 116 */ 117 public GDataStreamNewlineType getNewlineType() 118 { 119 return g_data_input_stream_get_newline_type(gDataInputStream); 120 } 121 122 /** 123 * Reads a 16-bit/2-byte value from @stream. 124 * 125 * In order to get the correct byte order for this read operation, 126 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order(). 127 * 128 * Params: 129 * cancellable = optional #GCancellable object, %NULL to ignore. 130 * 131 * Returns: a signed 16-bit/2-byte value read from @stream or %0 if 132 * an error occurred. 133 * 134 * Throws: GException on failure. 135 */ 136 public short readInt16(Cancellable cancellable) 137 { 138 GError* err = null; 139 140 auto p = g_data_input_stream_read_int16(gDataInputStream, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 141 142 if (err !is null) 143 { 144 throw new GException( new ErrorG(err) ); 145 } 146 147 return p; 148 } 149 150 /** 151 * Reads a signed 32-bit/4-byte value from @stream. 152 * 153 * In order to get the correct byte order for this read operation, 154 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order(). 155 * 156 * If @cancellable is not %NULL, then the operation can be cancelled by 157 * triggering the cancellable object from another thread. If the operation 158 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 159 * 160 * Params: 161 * cancellable = optional #GCancellable object, %NULL to ignore. 162 * 163 * Returns: a signed 32-bit/4-byte value read from the @stream or %0 if 164 * an error occurred. 165 * 166 * Throws: GException on failure. 167 */ 168 public int readInt32(Cancellable cancellable) 169 { 170 GError* err = null; 171 172 auto p = g_data_input_stream_read_int32(gDataInputStream, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 173 174 if (err !is null) 175 { 176 throw new GException( new ErrorG(err) ); 177 } 178 179 return p; 180 } 181 182 /** 183 * Reads a 64-bit/8-byte value from @stream. 184 * 185 * In order to get the correct byte order for this read operation, 186 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order(). 187 * 188 * If @cancellable is not %NULL, then the operation can be cancelled by 189 * triggering the cancellable object from another thread. If the operation 190 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 191 * 192 * Params: 193 * cancellable = optional #GCancellable object, %NULL to ignore. 194 * 195 * Returns: a signed 64-bit/8-byte value read from @stream or %0 if 196 * an error occurred. 197 * 198 * Throws: GException on failure. 199 */ 200 public long readInt64(Cancellable cancellable) 201 { 202 GError* err = null; 203 204 auto p = g_data_input_stream_read_int64(gDataInputStream, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 205 206 if (err !is null) 207 { 208 throw new GException( new ErrorG(err) ); 209 } 210 211 return p; 212 } 213 214 /** 215 * Reads a line from the data input stream. Note that no encoding 216 * checks or conversion is performed; the input is not guaranteed to 217 * be UTF-8, and may in fact have embedded NUL characters. 218 * 219 * If @cancellable is not %NULL, then the operation can be cancelled by 220 * triggering the cancellable object from another thread. If the operation 221 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 222 * 223 * Params: 224 * length = a #gsize to get the length of the data read in. 225 * cancellable = optional #GCancellable object, %NULL to ignore. 226 * 227 * Returns: a NUL terminated byte array with the line that was read in 228 * (without the newlines). Set @length to a #gsize to get the length 229 * of the read line. On an error, it will return %NULL and @error 230 * will be set. If there's no content to read, it will still return 231 * %NULL, but @error won't be set. 232 * 233 * Throws: GException on failure. 234 */ 235 public string readLine(out size_t length, Cancellable cancellable) 236 { 237 GError* err = null; 238 239 auto retStr = g_data_input_stream_read_line(gDataInputStream, &length, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 240 241 if (err !is null) 242 { 243 throw new GException( new ErrorG(err) ); 244 } 245 246 scope(exit) Str.freeString(retStr); 247 return Str.toString(retStr); 248 } 249 250 /** 251 * The asynchronous version of g_data_input_stream_read_line(). It is 252 * an error to have two outstanding calls to this function. 253 * 254 * When the operation is finished, @callback will be called. You 255 * can then call g_data_input_stream_read_line_finish() to get 256 * the result of the operation. 257 * 258 * Params: 259 * ioPriority = the [I/O priority][io-priority] of the request 260 * cancellable = optional #GCancellable object, %NULL to ignore. 261 * callback = callback to call when the request is satisfied. 262 * userData = the data to pass to callback function. 263 * 264 * Since: 2.20 265 */ 266 public void readLineAsync(int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData) 267 { 268 g_data_input_stream_read_line_async(gDataInputStream, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData); 269 } 270 271 /** 272 * Finish an asynchronous call started by 273 * g_data_input_stream_read_line_async(). Note the warning about 274 * string encoding in g_data_input_stream_read_line() applies here as 275 * well. 276 * 277 * Params: 278 * result = the #GAsyncResult that was provided to the callback. 279 * length = a #gsize to get the length of the data read in. 280 * 281 * Returns: a NUL-terminated byte array with the line that was read in 282 * (without the newlines). Set @length to a #gsize to get the length 283 * of the read line. On an error, it will return %NULL and @error 284 * will be set. If there's no content to read, it will still return 285 * %NULL, but @error won't be set. 286 * 287 * Since: 2.20 288 * 289 * Throws: GException on failure. 290 */ 291 public string readLineFinish(AsyncResultIF result, out size_t length) 292 { 293 GError* err = null; 294 295 auto retStr = g_data_input_stream_read_line_finish(gDataInputStream, (result is null) ? null : result.getAsyncResultStruct(), &length, &err); 296 297 if (err !is null) 298 { 299 throw new GException( new ErrorG(err) ); 300 } 301 302 scope(exit) Str.freeString(retStr); 303 return Str.toString(retStr); 304 } 305 306 /** 307 * Finish an asynchronous call started by 308 * g_data_input_stream_read_line_async(). 309 * 310 * Params: 311 * result = the #GAsyncResult that was provided to the callback. 312 * length = a #gsize to get the length of the data read in. 313 * 314 * Returns: a string with the line that 315 * was read in (without the newlines). Set @length to a #gsize to 316 * get the length of the read line. On an error, it will return 317 * %NULL and @error will be set. For UTF-8 conversion errors, the set 318 * error domain is %G_CONVERT_ERROR. If there's no content to read, 319 * it will still return %NULL, but @error won't be set. 320 * 321 * Since: 2.30 322 * 323 * Throws: GException on failure. 324 */ 325 public string readLineFinishUtf8(AsyncResultIF result, out size_t length) 326 { 327 GError* err = null; 328 329 auto retStr = g_data_input_stream_read_line_finish_utf8(gDataInputStream, (result is null) ? null : result.getAsyncResultStruct(), &length, &err); 330 331 if (err !is null) 332 { 333 throw new GException( new ErrorG(err) ); 334 } 335 336 scope(exit) Str.freeString(retStr); 337 return Str.toString(retStr); 338 } 339 340 /** 341 * Reads a UTF-8 encoded line from the data input stream. 342 * 343 * If @cancellable is not %NULL, then the operation can be cancelled by 344 * triggering the cancellable object from another thread. If the operation 345 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 346 * 347 * Params: 348 * length = a #gsize to get the length of the data read in. 349 * cancellable = optional #GCancellable object, %NULL to ignore. 350 * 351 * Returns: a NUL terminated UTF-8 string 352 * with the line that was read in (without the newlines). Set 353 * @length to a #gsize to get the length of the read line. On an 354 * error, it will return %NULL and @error will be set. For UTF-8 355 * conversion errors, the set error domain is %G_CONVERT_ERROR. If 356 * there's no content to read, it will still return %NULL, but @error 357 * won't be set. 358 * 359 * Since: 2.30 360 * 361 * Throws: GException on failure. 362 */ 363 public string readLineUtf8(out size_t length, Cancellable cancellable) 364 { 365 GError* err = null; 366 367 auto retStr = g_data_input_stream_read_line_utf8(gDataInputStream, &length, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 368 369 if (err !is null) 370 { 371 throw new GException( new ErrorG(err) ); 372 } 373 374 scope(exit) Str.freeString(retStr); 375 return Str.toString(retStr); 376 } 377 378 /** 379 * Reads an unsigned 16-bit/2-byte value from @stream. 380 * 381 * In order to get the correct byte order for this read operation, 382 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order(). 383 * 384 * Params: 385 * cancellable = optional #GCancellable object, %NULL to ignore. 386 * 387 * Returns: an unsigned 16-bit/2-byte value read from the @stream or %0 if 388 * an error occurred. 389 * 390 * Throws: GException on failure. 391 */ 392 public ushort readUint16(Cancellable cancellable) 393 { 394 GError* err = null; 395 396 auto p = g_data_input_stream_read_uint16(gDataInputStream, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 397 398 if (err !is null) 399 { 400 throw new GException( new ErrorG(err) ); 401 } 402 403 return p; 404 } 405 406 /** 407 * Reads an unsigned 32-bit/4-byte value from @stream. 408 * 409 * In order to get the correct byte order for this read operation, 410 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order(). 411 * 412 * If @cancellable is not %NULL, then the operation can be cancelled by 413 * triggering the cancellable object from another thread. If the operation 414 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 415 * 416 * Params: 417 * cancellable = optional #GCancellable object, %NULL to ignore. 418 * 419 * Returns: an unsigned 32-bit/4-byte value read from the @stream or %0 if 420 * an error occurred. 421 * 422 * Throws: GException on failure. 423 */ 424 public uint readUint32(Cancellable cancellable) 425 { 426 GError* err = null; 427 428 auto p = g_data_input_stream_read_uint32(gDataInputStream, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 429 430 if (err !is null) 431 { 432 throw new GException( new ErrorG(err) ); 433 } 434 435 return p; 436 } 437 438 /** 439 * Reads an unsigned 64-bit/8-byte value from @stream. 440 * 441 * In order to get the correct byte order for this read operation, 442 * see g_data_input_stream_get_byte_order(). 443 * 444 * If @cancellable is not %NULL, then the operation can be cancelled by 445 * triggering the cancellable object from another thread. If the operation 446 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 447 * 448 * Params: 449 * cancellable = optional #GCancellable object, %NULL to ignore. 450 * 451 * Returns: an unsigned 64-bit/8-byte read from @stream or %0 if 452 * an error occurred. 453 * 454 * Throws: GException on failure. 455 */ 456 public ulong readUint64(Cancellable cancellable) 457 { 458 GError* err = null; 459 460 auto p = g_data_input_stream_read_uint64(gDataInputStream, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 461 462 if (err !is null) 463 { 464 throw new GException( new ErrorG(err) ); 465 } 466 467 return p; 468 } 469 470 /** 471 * Reads a string from the data input stream, up to the first 472 * occurrence of any of the stop characters. 473 * 474 * Note that, in contrast to g_data_input_stream_read_until_async(), 475 * this function consumes the stop character that it finds. 476 * 477 * Don't use this function in new code. Its functionality is 478 * inconsistent with g_data_input_stream_read_until_async(). Both 479 * functions will be marked as deprecated in a future release. Use 480 * g_data_input_stream_read_upto() instead, but note that that function 481 * does not consume the stop character. 482 * 483 * Params: 484 * stopChars = characters to terminate the read. 485 * length = a #gsize to get the length of the data read in. 486 * cancellable = optional #GCancellable object, %NULL to ignore. 487 * 488 * Returns: a string with the data that was read 489 * before encountering any of the stop characters. Set @length to 490 * a #gsize to get the length of the string. This function will 491 * return %NULL on an error. 492 * 493 * Throws: GException on failure. 494 */ 495 public string readUntil(string stopChars, out size_t length, Cancellable cancellable) 496 { 497 GError* err = null; 498 499 auto retStr = g_data_input_stream_read_until(gDataInputStream, Str.toStringz(stopChars), &length, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 500 501 if (err !is null) 502 { 503 throw new GException( new ErrorG(err) ); 504 } 505 506 scope(exit) Str.freeString(retStr); 507 return Str.toString(retStr); 508 } 509 510 /** 511 * The asynchronous version of g_data_input_stream_read_until(). 512 * It is an error to have two outstanding calls to this function. 513 * 514 * Note that, in contrast to g_data_input_stream_read_until(), 515 * this function does not consume the stop character that it finds. You 516 * must read it for yourself. 517 * 518 * When the operation is finished, @callback will be called. You 519 * can then call g_data_input_stream_read_until_finish() to get 520 * the result of the operation. 521 * 522 * Don't use this function in new code. Its functionality is 523 * inconsistent with g_data_input_stream_read_until(). Both functions 524 * will be marked as deprecated in a future release. Use 525 * g_data_input_stream_read_upto_async() instead. 526 * 527 * Params: 528 * stopChars = characters to terminate the read. 529 * ioPriority = the [I/O priority][io-priority] of the request 530 * cancellable = optional #GCancellable object, %NULL to ignore. 531 * callback = callback to call when the request is satisfied. 532 * userData = the data to pass to callback function. 533 * 534 * Since: 2.20 535 */ 536 public void readUntilAsync(string stopChars, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData) 537 { 538 g_data_input_stream_read_until_async(gDataInputStream, Str.toStringz(stopChars), ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData); 539 } 540 541 /** 542 * Finish an asynchronous call started by 543 * g_data_input_stream_read_until_async(). 544 * 545 * Params: 546 * result = the #GAsyncResult that was provided to the callback. 547 * length = a #gsize to get the length of the data read in. 548 * 549 * Returns: a string with the data that was read 550 * before encountering any of the stop characters. Set @length to 551 * a #gsize to get the length of the string. This function will 552 * return %NULL on an error. 553 * 554 * Since: 2.20 555 * 556 * Throws: GException on failure. 557 */ 558 public string readUntilFinish(AsyncResultIF result, out size_t length) 559 { 560 GError* err = null; 561 562 auto retStr = g_data_input_stream_read_until_finish(gDataInputStream, (result is null) ? null : result.getAsyncResultStruct(), &length, &err); 563 564 if (err !is null) 565 { 566 throw new GException( new ErrorG(err) ); 567 } 568 569 scope(exit) Str.freeString(retStr); 570 return Str.toString(retStr); 571 } 572 573 /** 574 * Reads a string from the data input stream, up to the first 575 * occurrence of any of the stop characters. 576 * 577 * In contrast to g_data_input_stream_read_until(), this function 578 * does not consume the stop character. You have to use 579 * g_data_input_stream_read_byte() to get it before calling 580 * g_data_input_stream_read_upto() again. 581 * 582 * Note that @stop_chars may contain '\0' if @stop_chars_len is 583 * specified. 584 * 585 * Params: 586 * stopChars = characters to terminate the read 587 * stopCharsLen = length of @stop_chars. May be -1 if @stop_chars is 588 * nul-terminated 589 * length = a #gsize to get the length of the data read in 590 * cancellable = optional #GCancellable object, %NULL to ignore 591 * 592 * Returns: a string with the data that was read 593 * before encountering any of the stop characters. Set @length to 594 * a #gsize to get the length of the string. This function will 595 * return %NULL on an error 596 * 597 * Since: 2.26 598 * 599 * Throws: GException on failure. 600 */ 601 public string readUpto(string stopChars, ptrdiff_t stopCharsLen, out size_t length, Cancellable cancellable) 602 { 603 GError* err = null; 604 605 auto retStr = g_data_input_stream_read_upto(gDataInputStream, Str.toStringz(stopChars), stopCharsLen, &length, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 606 607 if (err !is null) 608 { 609 throw new GException( new ErrorG(err) ); 610 } 611 612 scope(exit) Str.freeString(retStr); 613 return Str.toString(retStr); 614 } 615 616 /** 617 * The asynchronous version of g_data_input_stream_read_upto(). 618 * It is an error to have two outstanding calls to this function. 619 * 620 * In contrast to g_data_input_stream_read_until(), this function 621 * does not consume the stop character. You have to use 622 * g_data_input_stream_read_byte() to get it before calling 623 * g_data_input_stream_read_upto() again. 624 * 625 * Note that @stop_chars may contain '\0' if @stop_chars_len is 626 * specified. 627 * 628 * When the operation is finished, @callback will be called. You 629 * can then call g_data_input_stream_read_upto_finish() to get 630 * the result of the operation. 631 * 632 * Params: 633 * stopChars = characters to terminate the read 634 * stopCharsLen = length of @stop_chars. May be -1 if @stop_chars is 635 * nul-terminated 636 * ioPriority = the [I/O priority][io-priority] of the request 637 * cancellable = optional #GCancellable object, %NULL to ignore 638 * callback = callback to call when the request is satisfied 639 * userData = the data to pass to callback function 640 * 641 * Since: 2.26 642 */ 643 public void readUptoAsync(string stopChars, ptrdiff_t stopCharsLen, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData) 644 { 645 g_data_input_stream_read_upto_async(gDataInputStream, Str.toStringz(stopChars), stopCharsLen, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData); 646 } 647 648 /** 649 * Finish an asynchronous call started by 650 * g_data_input_stream_read_upto_async(). 651 * 652 * Note that this function does not consume the stop character. You 653 * have to use g_data_input_stream_read_byte() to get it before calling 654 * g_data_input_stream_read_upto_async() again. 655 * 656 * Params: 657 * result = the #GAsyncResult that was provided to the callback 658 * length = a #gsize to get the length of the data read in 659 * 660 * Returns: a string with the data that was read 661 * before encountering any of the stop characters. Set @length to 662 * a #gsize to get the length of the string. This function will 663 * return %NULL on an error. 664 * 665 * Since: 2.24 666 * 667 * Throws: GException on failure. 668 */ 669 public string readUptoFinish(AsyncResultIF result, out size_t length) 670 { 671 GError* err = null; 672 673 auto retStr = g_data_input_stream_read_upto_finish(gDataInputStream, (result is null) ? null : result.getAsyncResultStruct(), &length, &err); 674 675 if (err !is null) 676 { 677 throw new GException( new ErrorG(err) ); 678 } 679 680 scope(exit) Str.freeString(retStr); 681 return Str.toString(retStr); 682 } 683 684 /** 685 * This function sets the byte order for the given @stream. All subsequent 686 * reads from the @stream will be read in the given @order. 687 * 688 * Params: 689 * order = a #GDataStreamByteOrder to set. 690 */ 691 public void setByteOrder(GDataStreamByteOrder order) 692 { 693 g_data_input_stream_set_byte_order(gDataInputStream, order); 694 } 695 696 /** 697 * Sets the newline type for the @stream. 698 * 699 * Note that using G_DATA_STREAM_NEWLINE_TYPE_ANY is slightly unsafe. If a read 700 * chunk ends in "CR" we must read an additional byte to know if this is "CR" or 701 * "CR LF", and this might block if there is no more data available. 702 * 703 * Params: 704 * type = the type of new line return as #GDataStreamNewlineType. 705 */ 706 public void setNewlineType(GDataStreamNewlineType type) 707 { 708 g_data_input_stream_set_newline_type(gDataInputStream, type); 709 } 710 }