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