Page 1 of 1

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

Posted: Tue Apr 10, 2007 12:37 pm
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

Posted: Tue Apr 10, 2007 12:44 pm
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....

Posted: Tue Apr 10, 2007 12:49 pm
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

Posted: Tue Apr 10, 2007 1:08 pm
by Kaeru Gaman
> "the string is written in UTF8 format if the program is compiled in unicode mode"

ok, I missed this one. thnx.