I try to read PB source line by line and include
includeFile "..."
to write it out in another file.
I am struggling a little bit with Unicode vs. UTF-8.
While string in PB source is stored in Unicode, files are stored in UTF-8.
When I read line by line and write line by line the written source can no more be compiled due to syntax error in source code.
It seems the syntax error occurs due to non-UTF-8 chars because empty lines are complaint about by the compiler.
When I use WriteStringFormat at begin of generated file, does not work either.
Any idea how I can read source from and write to so characters are kept?
Thank in advance!
Btw I have this source:
Code: Select all
macro AddListElement(PList, PElement)
AddElement(PList)
PList = PElement
endMacro
procedure joinSourceFiles(PathToMainFile.s, PathToJoinedFile.s)
protected i .i
protected newlist StackOfLines.s()
protected newList StackOfPaths.s()
protected newlist StackOfFiles.i()
protected Path .s
protected Pos1 .i
protected Pos2 .i
protected F .s ; current file
protected MainPath .s
protected NrF .i ; nr of main file
protected NrJ .i ; nr of joined file
protected Line .s
protected IDEOptionsText .s
protected IncFileText .s
protected DQ .s
IDEOptionsText = "; ide options = "
IncFileText = "includefile"
DQ = Chr(34)
F = PathToMainFile
MainPath = GetPathPart(F)
;i = 1
;AddListElement(StackOfLines(), i)
NrJ = CreateFile(#PB_Any, PathToJoinedFile, #PB_UTF8)
if NrJ
;WriteStringFormat(NrJ, #PB_Unicode); doesn't work
if FileSize(PathToMainFile)
NrF = ReadFile(#PB_Any, F, #PB_File_SharedRead | #PB_UTF8)
if NrF
while eof(NrF)=0
Line = ReadString(NrF)
if sourceLineStartsWith(Line, IncFileText)
AddListElement(StackOfPaths(), MainPath)
AddListElement(StackOfFiles(), NrF)
Pos1 = FindString(Line, DQ)
Pos2 = FindString(Line, DQ, Pos1 + 1)
Path = mid(Line, Pos1 + 1, Pos2 - Pos1 - 1)
debug Path
if mid(Path, 2, 1)=":"
NrF = ReadFile(#PB_Any, Path, #PB_File_SharedRead | #PB_UTF8)
else
NrF = ReadFile(#PB_Any, MainPath + Path, #PB_File_SharedRead | #PB_UTF8)
MainPath + GetPathPart(Path)
endif
if EOF(NrF)
CloseFile(NrF)
NrF = StackOfFiles()
DeleteElement(StackOfFiles())
MainPath = StackOfPaths()
DeleteElement(StackOfPaths())
else
Line = ReadString(NrF)
WriteStringN(NrJ, Line, #PB_UTF8)
endif
elseif sourceLineStartsWith(Line, IDEOptionsText)
if ListSize(StackOfFiles())>0
CloseFile(NrF)
NrF = StackOfFiles()
DeleteElement(StackOfFiles())
MainPath = StackOfPaths()
DeleteElement(StackOfPaths())
;Line = StackOfLines()
;DeleteElement(StackOfLines())
else
break
endif
else
WriteStringN(NrJ, Line, #PB_UTF8)
endif
wend
CloseFile(NrF)
endif
endif
endif
CloseFile(NrJ)
EndProcedure ; joinSourceFiles(PathToMainFile.s, PathToJoinedFile.s)