December 04, 2020, 11:26:00 AM

Author Topic: [bmx] Null-terminated string read/write by Pineapple [ 1+ years ago ]  (Read 480 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Null-terminated string read/write
Author : Pineapple
Posted : 1+ years ago

Description : Read and write null-terminated strings in a stream

Code :
Code: BlitzMax
  1. '       --+-----------------------------------------------------------------------------------------+--
  2. '         |   This code was originally written by Sophie Kirschner (sophiek@pineapplemachine.com)   |  
  3. '         | It is released as public domain. Please don't interpret that as liberty to claim credit |  
  4. '         |   that isn't yours, or to sell this code when it could otherwise be obtained for free   |  
  5. '         |                because that would be a really shitty thing of you to do.                |
  6. '       --+-----------------------------------------------------------------------------------------+--
  7.  
  8. Rem
  9. bbdoc: Write a string to a stream.
  10. EndRem
  11. Function WriteNullString(f:TStream,str$)
  12.         Assert Instr(str,Chr(0))=0,"Cannot write a string containing a null character."
  13.         WriteString f,str
  14.         WriteByte f,0
  15. End Function
  16.  
  17. Rem
  18. bbdoc: Read a string from a stream.
  19. returns: The string that was read.
  20. EndRem
  21. Function ReadNullString$(f:TStream)
  22.         Local ret$=""
  23.         Repeat
  24.                 Assert Not Eof(f),"Failed to read null-terminated string."
  25.                 Local b%=ReadByte(f)
  26.                 If b=0 Then Return ret
  27.                 ret:+Chr(b)
  28.         Forever
  29. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal