Page 1 of 1

ExploreMemoryString

Posted: Tue Nov 06, 2007 4:46 pm
by Hroudtwolf
Hello,

this function strips a whole string line per line into a stringarray and retrieves this array at end.
The generated array is pb-array compatible and the fields are null terminated.
For more infos about using, look into the code(inline-comments).

Best regards

Wolf

Code: Select all

; PureBasic-Lounge.de
; Author: Hroudtwolf
; Date: 06. November
; OS: Windows, Linux, Mac
; Demo: Yes

; A Header-Stucture for the arrays
Structure tARRAY
   lSize.l
   lTyp .l
EndStructure

; This function copies a text line per line in an array.
; *ptData.CHARACTER - Memoryadress of the text
; lSize  .l         - Size of text in bytes.
; lFlag  .l         - Encoding of the text
;                     #PB_Unicode , #PB_ASCII , #NULL (Same encoding like in binary)
Procedure ExploreMemoryString (*ptData.CHARACTER , lSize.l , lFlag.l)
   Protected *EOB                   = *ptData + lSize
   Protected *ArrayBuffer  .tARRAY
   Protected *ArrayDummy   .tARRAY
   Protected lCharSize     .l
   Protected lArraySize    .l
   Protected sTemp         .s
   
   ; Checking for valid pointer
   If Not *ptData
      ProcedureReturn #Null
   EndIf
   
   ; Determining charsize 
   Select lFlag
      Case #PB_Unicode
      lCharSize = 2
      Case #PB_Ascii
      lCharSize = 1
      Default
      lCharSize = SizeOf (CHARACTER)
   EndSelect   
   
   ; Creating array header
   *ArrayBuffer       = AllocateMemory (SizeOf (tARRAY))
   *ArrayBuffer\lTyp  = 8 ; Array of the type "String"
   
   ; Reading text and writing array
   While *EOB > *ptData
      Select *ptData\c
         Case 13 , 10
         If *ptData\c = 10
            *ArrayBuffer\lSize + 1
            *ArrayDummy        = ReAllocateMemory (*ArrayBuffer , SizeOf (tARRAY) + (*ArrayBuffer\lSize * SizeOf (LONG)))   
            If Not *ArrayDummy
               ProcedureReturn #Null
            EndIf
            *ArrayBuffer = *ArrayDummy
            *ArrayDummy  + ((SizeOf (tARRAY) + (*ArrayBuffer\lSize * SizeOf (LONG))) - SizeOf (LONG))
            *ArrayString = AllocateMemory ((Len (sTemp) * lCharSize) + lCharSize)
            CopyMemory (@sTemp , *ArrayString , (Len (sTemp) * lCharSize) + lCharSize)
            PokeL (*ArrayDummy , *ArrayString)
            sTemp = ""
         EndIf
         
         Default
         sTemp + Chr (*ptData\c)
      EndSelect     
      *ptData + lCharSize
   Wend
   
   ProcedureReturn *ArrayBuffer + SizeOf (tARRAY)
EndProcedure


;----------------- Test area ---------------------------------------

; We are creating an empty array
Dim Dummy.s (0)

; We copies our text in the array
Dummy () = ExploreMemoryString (?KEYWORDS_START , ?KEYWORDS_END - ?KEYWORDS_START , #PB_Ascii)

; We want to look into our array
; (PeekL (Dummy () - 8) - 1) <-- Detecting arraysize (fields)
For x = 0 To PeekL (Dummy () - 8) - 1
   Debug Dummy (x)
Next x
End

; Test text
DataSection
   KEYWORDS_START:
   Data.s "Hallo Welt" + #CRLF$
   Data.s "Bonjour le Monde" + #CRLF$
   Data.s "Ciao il Mondo" + #CRLF$
   Data.s "Hello World" + #CRLF$
   Data.s "Hei Verden" + #CRLF$
   Data.s "Hola Mundo" + #CRLF$
   Data.s "Oi Mundo" + #CRLF$
   Data.s "hallo Wereld" + #CRLF$
   Data.s "O'zapft is" + #CRLF$
   KEYWORDS_END:
EndDataSection