V3.0.0 - Read Write Null Terminated String

Share your advanced PureBasic knowledge/code with the community.
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

V3.0.0 - Read Write Null Terminated String

Post by Guimauve »

Hello everyone,

This a small lib I need to read 3DS and MD3 files. I have not put it to extreme test but it seem to work with or without Unicode mode.

Have fun !

Edit : Commands simplifications

Best Regards.
Guimauve

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Read Write Null Terminated String
; File Name : ReadNullTerminatedString.pb
; File version: 2.0.0
; Programmation : OK
; Programmed by : Guimauve
; Date : 08-02-2012
; Last Update : 08-02-2012
; PureBasic code : 4.61
; Plateform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Macro WriteNullTerminatedString(FileID, String)
  
  WriteString(FileID, String, #PB_Ascii)
  WriteAsciiCharacter(FileID, 0)
  
EndMacro

Macro ReadNullTerminatedString(FileID)
  
  ReadString(FileID, #PB_Ascii)
  
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
; <<<<< !!! WARNING - YOU ARE NOW IN A TESTING ZONE - WARNING !!! <<<<< 
; <<<<< !!! WARNING - THIS CODE SHOULD BE COMMENTED - WARNING !!! <<<<< 
; <<<<< !!! WARNING - BEFORE THE FINAL COMPILATION. - WARNING !!! <<<<< 
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 

NewList Texte.s()

AddElement(Texte())
Texte() = FormatDate("A=%yyyy, M= %mm, J=%dd - %hh:%ii:%ss", Date())

AddElement(Texte())
Texte() = "PureBasic 4.61 Beta 1"

AddElement(Texte())
Texte() = "FreeMat 4.0 (Similiaire à MatLab mais sans Simulink)"

AddElement(Texte())
Texte() = "PureBasic is the best programming language, period !"

AddElement(Texte())
Texte() = "Linux Mint 12 x64 + Gnome-Shell"

If CreateFile(0, "Test.bin")
  
  ForEach Texte()
    WriteNullTerminatedString(0, Texte())
  Next
  
  CloseFile(0)
  
EndIf

If ReadFile(1, "Test.bin")
  
  While Eof(1) = 0 
    Debug ReadNullTerminatedString(1)
  Wend
  
  CloseFile(1)
  DeleteFile("Test.bin")
  
EndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Last edited by Guimauve on Thu Feb 09, 2012 3:14 am, edited 2 times in total.
User avatar
STARGÅTE
Addict
Addict
Posts: 2235
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Read Write Null Terminated String

Post by STARGÅTE »

sry but why you not use Read/Write String, with Ascii-Flag?

Code: Select all


NewList Texte.s()

AddElement(Texte())
Texte() = FormatDate("A=%yyyy, M= %mm, J=%dd - %hh:%ii:%ss", Date())

AddElement(Texte())
Texte() = "PureBasic 4.61 Beta 1"

AddElement(Texte())
Texte() = "FreeMat 4.0 (Similiaire à MatLab mais sans Simulink)"

AddElement(Texte())
Texte() = "PureBasic is the best programming language, period !"

AddElement(Texte())
Texte() = "Linux Mint 12 x64 + Gnome-Shell"

If CreateFile(0, "Test.bin")
  
  ForEach Texte()
    WriteString(0, Texte(), #PB_Ascii) : WriteAsciiCharacter(0, #NUL)
  Next
  
  CloseFile(0)
  
EndIf

If ReadFile(1, "Test.bin")
  
  While Eof(1) = 0 
    Debug ReadString(1, #PB_Ascii)
  Wend
  
  CloseFile(1)
  DeleteFile("Test.bin")
  
EndIf
you can write an ascii-string with WriteString(#File, String, #PB_Ascii) und add a NUL with WriteAsciiCharacter(#File, #NUL)

and ReadString(#File, #PB_Ascii) reads a null terminated string.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Re: Read Write Null Terminated String

Post by Guimauve »

Sorry but why you not use Read/Write String, with Ascii-Flag?
Because in the French help file, : Lit une chaîne de caractères à partir du #Fichier spécifié, jusqu'au prochain retour chariot . ---> Translation : Read string from file until an carriage return character is found.
In English help file : Read a string from a file until an 'End Of Line' character is found.

Maybe the Frech help file has to be modifed to :
Lit une chaîne de caractère à partir du fichier jusqu'à ce qu'un caractère de fin de chaine ('\0') soit trouvé
Once again I have wasted my time to recreate the wheel. :evil:
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Re: Read Write Null Terminated String

Post by Guimauve »

Hello everyone,

This is the V3.0.0 of this set of commands with Ascii, UTF-8 and Unicode String support.

Tested only on LinuxMint 12 x64 but it should work on all platform.
  • Ascii Program <-> Ascii String : OK
  • Ascii Program <-> UTF-8 String : OK
  • Ascii Program <-> Unicode String : OK
  • Unicode Program <-> Ascii String : OK
  • Unicode Program <-> UTF-8 String : OK
  • Unicode Program <-> Unicode String : OK
Have fun !

Best regards.
Guimauve

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Read Write Null Terminated String
; File Name : Read Write Null Terminated String.pb
; File version: 3.0.0
; Programmation : OK
; Programmed by : Guimauve
; Date : 08-02-2012
; Last Update : 09-02-2012
; PureBasic code : 4.61
; Plateform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Notes :
;
; This set of commands Read and write a null ('\0')
; Terminated string on the current binary file with
; the C Ascii string read and write style.
;
; For the V3.0.0, I have returned back to Procedures 
; and I have added the Null Terminated UTF-8 and 
; Unicode string for anyone who need these String
; encoding.
;
; The Ascii mode is the default one but UTF-8 or the 
; Unicode can be used as well,
;
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Procedure.s WriteNullTerminatedString(FileID, String.s, Flag.b = #PB_Ascii)
  
  If Flag = #PB_Ascii
    WriteString(FileID, String, #PB_Ascii)
    WriteAsciiCharacter(FileID, 0)
  ElseIf Flag = #PB_UTF8
    WriteString(FileID, String, #PB_UTF8)
    WriteAsciiCharacter(FileID, 0)
  Else
    WriteString(FileID, String, #PB_Unicode)
    WriteUnicodeCharacter(FileID, 0)
  EndIf
  
EndProcedure

Procedure.s ReadNullTerminatedString(FileID, Flag.b = #PB_Ascii)
  
  If Flag = #PB_Ascii
    Null_Terminated_String.s = ReadString(FileID, #PB_Ascii)
  ElseIf Flag = #PB_UTF8
    Null_Terminated_String = ReadString(FileID, #PB_UTF8)
  Else
    Null_Terminated_String = ReadString(FileID, #PB_Unicode)
  EndIf
  
  ProcedureReturn Null_Terminated_String
EndProcedure

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Post Reply