Xbase++
Xbase++
Implement Preprocessor #include with StringBuilder
Demonstrates how to implement #include with a Chilkat StringBuilder.Chilkat Xbase++ Downloads
LOCAL oSbSrc
LOCAL cFilePath
LOCAL oSbIncludeFile
// First build a string that has a preprocessor include
oSbSrc := CreateObject("Chilkat.StringBuilder")
oSbSrc:Append("1" + Chr(13) + Chr(10) + "2" + Chr(13) + Chr(10) + "3" + Chr(13) + Chr(10))
oSbSrc:Append("#include <qa_data/txt/helloWorld.txt>" + Chr(13) + Chr(10))
oSbSrc:Append("4" + Chr(13) + Chr(10) + "5" + Chr(13) + Chr(10))
? oSbSrc:GetAsString()
// sbSrc contains:
// 1
// 2
// 3
// #include <qa_data/txt/helloWorld.txt>
// 4
// 5
// The qa_data/txt/helloWorld.txt file contains "Hello World!"
cFilePath := oSbSrc:GetAfterBetween("#include", "<", ">")
IF (oSbSrc:LastMethodSuccess != 1)
? "No #include's found."
oSbSrc:destroy()
RETURN
ENDIF
? "filePath: " + cFilePath
// Load the contents of the filePath
oSbIncludeFile := CreateObject("Chilkat.StringBuilder")
oSbIncludeFile:LoadFile(cFilePath, "utf-8")
// Replace the first occurrence of #include <...> line with the contents of the include file.
oSbSrc:ReplaceAllBetween("#include", ">", oSbIncludeFile:GetAsString(), 1)
? oSbSrc:GetAsString()
// sbSrce now contains:
// 1
// 2
// 3
// Hello World!
// 4
// 5
oSbSrc:destroy()
oSbIncludeFile:destroy()