Sample code for 30+ languages & platforms
PureBasic

Implement Preprocessor #include with StringBuilder

Demonstrates how to implement #include with a Chilkat StringBuilder.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkStringBuilder.pb"

Procedure ChilkatExample()

    ; First build a string that has a preprocessor include
    sbSrc.i = CkStringBuilder::ckCreate()
    If sbSrc.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(sbSrc,"1" + Chr(13) + Chr(10) + "2" + Chr(13) + Chr(10) + "3" + Chr(13) + Chr(10))
    CkStringBuilder::ckAppend(sbSrc,"#include <qa_data/txt/helloWorld.txt>" + Chr(13) + Chr(10))
    CkStringBuilder::ckAppend(sbSrc,"4" + Chr(13) + Chr(10) + "5" + Chr(13) + Chr(10))

    Debug CkStringBuilder::ckGetAsString(sbSrc)

    ; sbSrc contains:
    ; 	1
    ; 	2
    ; 	3
    ; 	#include <qa_data/txt/helloWorld.txt>
    ; 	4
    ; 	5

    ; The qa_data/txt/helloWorld.txt file contains "Hello World!"

    filePath.s = CkStringBuilder::ckGetAfterBetween(sbSrc,"#include","<",">")
    If CkStringBuilder::ckLastMethodSuccess(sbSrc) <> 1
        Debug "No #include's found."
        CkStringBuilder::ckDispose(sbSrc)
        ProcedureReturn
    EndIf

    Debug "filePath: " + filePath

    ; Load the contents of the filePath
    sbIncludeFile.i = CkStringBuilder::ckCreate()
    If sbIncludeFile.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckLoadFile(sbIncludeFile,filePath,"utf-8")

    ; Replace the first occurrence of #include <...> line with the contents of the include file.
    CkStringBuilder::ckReplaceAllBetween(sbSrc,"#include",">",CkStringBuilder::ckGetAsString(sbIncludeFile),1)

    Debug CkStringBuilder::ckGetAsString(sbSrc)

    ; sbSrce now contains:
    ; 	1
    ; 	2
    ; 	3
    ; 	Hello World!
    ; 	4
    ; 	5


    CkStringBuilder::ckDispose(sbSrc)
    CkStringBuilder::ckDispose(sbIncludeFile)


    ProcedureReturn
EndProcedure