Page 1 of 1

Create Language prefs from Pb source

Posted: Sun Jan 22, 2012 12:12 pm
by oryaaaaa
I want to, Pb support Preferences by UTF-8, now have problems.

example
L0001 = XXXX [13][10] (UTF-8)
Read "XXXX" ( remove space ) then Error
If Correct str "XXXX ", change "XXXX"+Space(1)

Fixed:2012/01/23 3:42 AM

Code: Select all

Global File.l, Fsize.l, I.l, Filename.s, PrefsName.s, StrName.s, OneBuf.s, Fsize2.l
Global PrefsSize.l, PrefsIndex.l, Pos1.l, Pos2.l
Global StrIndex.l = 0

; Read Source Code
Filename = OpenFileRequester("Select PB file", "*.PB", "*.PB|*PB",0)
If Filename<>""
  File = ReadFile(#PB_Any, Filename)
  Fsize = Lof(File)
  Global *buffer = AllocateMemory(Fsize)
  ReadData(File, *buffer, Fsize)
  CloseFile(File)
  PrefsName = ReplaceString(Filename, ".pb", ".lng",#PB_String_NoCase, Len(Filename)-3)
  
  ; Read Prefs by UTF8
  Global NewList Lng.s()
  If FileSize(PrefsName)>0
    File = ReadFile(#PB_Any, PrefsName)
    PrefsSize = Lof(File)
    Global *PrefsBuf = AllocateMemory(PrefsSize) 
    ReadData(File, *PrefsBuf, PrefsSize)
    CloseFile(File)
    Repeat
      PrefsIndex = FindString(PeekS(*PrefsBuf, PrefsSize, #PB_UTF8), "L"+RSet(Str(StrIndex),4,"0"),0)
      If PrefsIndex
        AddElement(Lng())
        Pos1 = FindString(PeekS(*PrefsBuf, PrefsSize, #PB_UTF8), " = ", PrefsIndex)+3
        Pos2 = FindString(PeekS(*PrefsBuf, PrefsSize, #PB_UTF8), Chr(13)+Chr(10), Pos1)
        Lng() = Mid(PeekS(*PrefsBuf, PrefsSize, #PB_UTF8), Pos1, Pos2-Pos1)
        StrIndex + 1
      Else
        Break
      EndIf
    ForEver
  EndIf
  
  ; Create Prefs
  CreatePreferences(PrefsName)
  StrIndex = 0
  PreferenceGroup("Default")
  ForEach Lng()
    WritePreferenceString("L"+RSet(Str(StrIndex),4,"0"), Lng()) 
    StrIndex + 1
  Next
  
  ; Buffer to List BUF()
  Global NewList Buf.s()
  Stc.l = CountString(PeekS(*buffer,Fsize, #PB_UTF8), Chr(13)+Chr(10))
  For I=0 To Stc
    AddElement(Buf())
    Buf() = StringField(PeekS(*buffer, Fsize, #PB_UTF8), I+1, Chr(13)+Chr(10))
  Next
  
  File = CreateFile(#PB_Any, GetPathPart(Filename)+"output_"+GetFilePart(Filename))
  ForEach Buf()
    Stc = CountString(Buf(), Chr(34))
    OneBuf = Buf()
    ; Remove Line
    If FindString(OneBuf, "Data.s",0)>0
      Goto Skip
    ElseIf FindString(OneBuf, "PureValid",0)>0
      Goto Skip 
    ElseIf FindString(OneBuf, "Include",0)>0
      Goto Skip 
    ElseIf FindString(OneBuf, ";",0)>0
      Goto Skip 
    ElseIf FindString(OneBuf, "WriteString",0)>0
      Goto Skip 
    ElseIf FindString(OneBuf, "Preference",0)>0
      Goto Skip 
    ElseIf FindString(OneBuf, "FileSize",0)>0
      Goto Skip 
    ElseIf FindString(OneBuf, "ErrorMessage$",0)>0
      Goto Skip 
    ElseIf FindString(OneBuf, "OpenLibrary",0)>0
      Goto Skip 
    ElseIf FindString(OneBuf, "Tiff_Error(",0)>0
      Goto Skip 
    ElseIf FindString(OneBuf, "CodeMessage$ =",0)>0
      Goto Skip 
    ElseIf FindString(OneBuf, "GetFunction(",0)>0
      Goto Skip 
    ElseIf FindString(OneBuf, "ExePath()+",0)>0
      Goto Skip 
    ElseIf FindString(OneBuf, "CallFunction(",0)>0
      Goto Skip 
    ; ElseIf FindString(OneBuf, "PureValid",0)>0
      ; Goto Skip 
    ; ElseIf FindString(OneBuf, "PureValid",0)>0
      ; Goto Skip 
    EndIf
    ;
    If Stc%2=0
      For I =1 To Stc Step 2
        StrName = StringField(Buf(), I+1, Chr(34))
        If StrName<>""
          Select StrName
            ; Remove charactor
            Case "---", "[","]",":","*",".tiff",".jpg",".tif","DPI","@","/","( ",")","\"
            Case "1","  ",".",".XML",".JPG",".TIFF",".TIF",":\", "-P", " / ","DISPLAY"
            Case "JPEG (*.JPG) | *.JPG", "TIFF (*.TIF) | *.TIF","*.*", " > ","-"," - "
            Case "http://","Japanese", "image/tiff", " : 1 ", "0.0000000","ToolTips_Class32"
            Case "Compilers\", "%yyyy:%mm:%dd %hh:%ii:%ss"," = ","TIFFn","JPEG","TIFF"
            ; Case
            ; Case 
            ; Case
            ; Change Process
            Default
              ForEach Lng()
                If Lng()=StrName
                  Break
                EndIf
              Next
              If ListSize(Lng())=0 Or Lng()<>StrName
                AddElement(Lng())
                Lng() = StrName
                WritePreferenceString("L"+RSet(Str(StrIndex),4,"0"), StrName)
                OneBuf = ReplaceString(OneBuf, Chr(34)+StrName+Chr(34), "L("+RSet(Str(StrIndex),4,"0")+")")
                StrIndex +1
              Else
                OneBuf = ReplaceString(OneBuf, Chr(34)+StrName+Chr(34), "L("+RSet(Str(ListIndex(Lng())),4,"0")+")") 
              EndIf 
          EndSelect 
        EndIf
      Next
    EndIf
    Skip:
    WriteString(File, OneBuf, #PB_UTF8)
  Next
  CloseFile(File)
  ClosePreferences()
  File = ReadFile(#PB_Any, GetPathPart(Filename)+"output_"+GetFilePart(Filename))
  Fsize2 = Lof(File)
  *buffer2 = AllocateMemory(Fsize2)
  ReadData(File, *buffer2, Fsize2)
  CloseFile(File)
  File = CreateFile(#PB_Any, GetPathPart(Filename)+"output_"+GetFilePart(Filename))
  WriteByte(File, $EF) : WriteByte(File, $BB): WriteByte(File, $BF)
  WriteStringN(File, "Global Dim L.s("+Str(ListSize(Lng()))+")",#PB_UTF8)
  WriteStringN(File, "OpenPreferences("+Chr(34)+GetFilePart(PrefsName)+Chr(34)+")",#PB_UTF8)
  WriteStringN(File, "  PreferenceGroup("+Chr(34)+"Default"+Chr(34)+") ",#PB_UTF8)
  WriteStringN(File, "  For I=0 to "+Str(ListSize(Lng())),#PB_UTF8)
  WriteStringN(File, "    L(I) = ReadPreferenceString("+Chr(34)+"L"+Chr(34)+"+RSet(Str(i),4,"+Chr(34)+"0"+Chr(34)+"), "+Chr(34)+"! Error !"+Chr(34)+")",#PB_UTF8)
  WriteStringN(File, "  Next",#PB_UTF8)
  WriteStringN(File, "ClosePreferences()",#PB_UTF8) 
  WriteData(File, *buffer2+3, Fsize2-3)
  For I=1 To Fsize
    If PeekB(*buffer+I)=0
      WriteData(File, *buffer+I, Fsize-I) 
      Break
    EndIf
  Next
  CloseFile(File)
  MessageRequester("Finish", "OK", #MB_OK) 
  FreeMemory(*buffer)
  FreeMemory(*buffer2)
EndIf

Re: Create Language prefs from Pb source

Posted: Sun Feb 05, 2012 1:18 pm
by de_vipp
Hey ... I have problems with UTF-8 in preferences too since a longer time ....

do u have checked REAL special charackters ?

I believe there no PB way to find a way to handle them ... here a code (not a preference, but its the same effect)

Code: Select all


If CreateFile (1,"test123.txt") 
  WriteString(1, "Подключение к серверу",  #PB_UTF8) 
  CloseFile (1) 
EndIf

ReadFile(1, "test123.txt") 
*Speicher = AllocateMemory(Lof(1))
*Speicher2 = AllocateMemory(Lof(1) * 2) ;*2 zur Sicherheit
ReadData(1, *Speicher, Lof(1))
PokeS(*speicher2, PeekS(*speicher, -1, #PB_UTF8))

Repeat 
   String.s + PeekS(*Speicher2 + Position, 1) 
   Position + 1 + #PB_Compiler_Unicode ; eigentlich + sizeof(CHARACTER), Fehler in Residents
Until position >= MemoryStringLength(*speicher2) * (1 + #PB_Compiler_Unicode)

Debug String

#FORMAT = #PB_UTF8

; Schreiben
If CreateFile (1,"test123.txt")
  WriteStringFormat(1, #FORMAT)
  WriteString(1, "Dies ist äin Test",  #FORMAT) 
  CloseFile (1) 
EndIf


; Lesen
ReadFile(1, "test123.txt")
format.l = ReadStringFormat(1) 
*Speicher = AllocateMemory(Lof(1))
*Speicher2 = AllocateMemory(Lof(1) * 2) ;*2 zur Sicherheit
ReadData(1, *Speicher, Lof(1))
PokeS(*speicher2, PeekS(*speicher, -1, format))

Repeat 
   String.s + PeekS(*Speicher2 + Position, 1) 
   Position + 1 + #PB_Compiler_Unicode ; eigentlich + sizeof(CHARACTER), Fehler in Residents
Until position >= MemoryStringLength(*speicher2) * (1 + #PB_Compiler_Unicode)

Debug String




PS: Sorry about my English ! ;)

Re: Create Language prefs from Pb source

Posted: Sun Feb 05, 2012 2:09 pm
by infratec
Hi,

one hint:

If you use ReadData() for a text file, and a buffer of exactly the size of the file,
than you can not use directly string operations on it (like FindString() or PeekS()).
(PeekS() only with the filesize as second parameter)

Why :?:
Very simple:

In PB a string is zero terminated.
If you read in the file in a buffer of exactly the length of the file, there is no trailing zero :!:
So you can get an IMA.

Workaround:
Make the buffer 2 bytes larger than the filesize, than it's ok, since PB initializes an allocated buffer with zeros.

Why 2 bytes:
If you use unicode, a string is terminated by 2 zeros
The code 0x0000 is the Unicode string terminator for a null-terminated string. A single null byte is not sufficient for this code
Thanks to Danilo for pointing this out.

Bernd