WriteStringNullByte(FileID [, Format]) & BOMSize(Format)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

WriteStringNullByte(FileID [, Format]) & BOMSize(Format)

Post by nco2k »

hi fred,

i played with unicode a bit and must say, it sure is a great thing. thanks for Write- & ReadStringFormat() and the optional flags in Write- & ReadXYZ(), it makes our way of coding very flexible. and because of that, i am asking for two more functions to improve the flexibility even more:

Code: Select all

Procedure WriteStringNullByte(File, Format=-1);specifies the end of a string 
  If Format = -1 
    Select SizeOf(Character) 
      Case 1 
        Format = #PB_Ascii 
      Case 2 
        Format = #PB_UTF8 
    EndSelect 
  EndIf 
  Select Format 
    Case #PB_Ascii, #PB_UTF8 
      WriteByte(File, 0) 
    Case #PB_Unicode 
      WriteWord(File, 0) 
  EndSelect 
EndProcedure 

Procedure BOMSize(Format=-1);returns the size in byte of a BOM (Byte Order Mark) 
  If Format = -1 
    Select SizeOf(Character) 
      Case 1 
        Format = #PB_Ascii 
      Case 2 
        Format = #PB_UTF8 
    EndSelect 
  EndIf 
  Select Format 
    Case #PB_Ascii 
      Result = 0 
    Case #PB_Unicode 
      Result = 2 
    Case #PB_UTF8 
      Result = 3 
  EndSelect 
  ProcedureReturn Result 
EndProcedure 

FileName.s = "C:\Test.txt" 
Format = #PB_Unicode 

DeleteFile(FileName) 
FileID = OpenFile(#PB_Any, FileName) 
If FileID 
  
  WriteStringFormat(FileID, Format) 
  
  WriteString(FileID, "TestA", Format) 
  WriteStringNullByte(FileID, Format) 
  
  WriteString(FileID, "TestB", Format) 
  WriteStringNullByte(FileID, Format) 
  
  WriteString(FileID, "TestC", Format) 
  WriteStringNullByte(FileID, Format) 
  
  FileSeek(FileID, BOMSize(Format)) 
  Debug ReadString(FileID, Format) 
  Debug ReadString(FileID, Format) 
  Debug ReadString(FileID, Format) 
  
  CloseFile(FileID) 
EndIf 
DeleteFile(FileName)
i hope the code and the use behind this, is self explaining. :D

edit: added If Format = -1 in the BOMSize() procedure aswell.

c ya,
nco2k
Last edited by nco2k on Tue Apr 10, 2007 2:09 pm, edited 1 time in total.
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

Code: Select all

    Select SizeOf(Character)
      Case 1
        Format = #PB_Ascii
      Case 2
        Format = #PB_UTF8
    EndSelect 
shouldn't that be

Code: Select all

      Case 2
        Format = #PB_Unicode
?
...guess it's just a typo....
oh... and have a nice day.
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Post by nco2k »

no typo there, i made that (Format=-1) in case you dont specify any flag, like an optional param:
Help wrote:Without any 'Flags', the string is written in UTF8 format if the program is compiled in unicode mode, else it's written in ascii format.
c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

> "the string is written in UTF8 format if the program is compiled in unicode mode"

ok, I missed this one. thnx.
oh... and have a nice day.
Post Reply