Page 1 of 1

How to make StringByteLengthand Copymemory with SB?

Posted: Thu Apr 16, 2020 3:53 pm
by kingwolf71
I am doing some cross platform coding, and I am getting stuck with these 2 functions

StringByteLength()
CopyMemory()

How can I replicate these 2 functons with SpiderBasic?

Re: How to make StringByteLengthand Copymemory with SB?

Posted: Thu Apr 16, 2020 5:28 pm
by Fred
Both should be doable with Peek/Poke combination, could you post your sample code to see if it can be done otherwise ?

Re: How to make StringByteLengthand Copymemory with SB?

Posted: Thu Apr 16, 2020 5:30 pm
by Peter
StringByteLength(): Perhaps this one?

Code: Select all

Procedure StringByteLength(String$, Format = #PB_Unicode)
  
  Protected sbl
  
  Select Format
    Case #PB_Ascii
      sbl = Len(String$)
    Case #PB_UTF8, #PB_Unicode
      ! v_sbl = new Blob([v_string$]).size;
  EndSelect
  
  ProcedureReturn sbl
  
EndProcedure

Debug StringByteLength("a", #PB_Ascii)
Debug StringByteLength("a", #PB_UTF8)
Debug StringByteLength("a", #PB_Unicode)

Debug StringByteLength("ä", #PB_Ascii)
Debug StringByteLength("ä", #PB_UTF8)
Debug StringByteLength("ä", #PB_Unicode)

Re: How to make StringByteLengthand Copymemory with SB?

Posted: Thu Apr 16, 2020 5:55 pm
by kingwolf71

Code: Select all


 Structure _membersMemoryFileClass
      eof.w
      flags.w
      fileBase.i
      fileSize.i
      initialSize.i
      initialPageSize.i
      usedFile.i    ;Lof.
      filePointer.i ;Loc.
EndStructure

Global Dim           *Handle._membersMemoryFileClass(1)

memPTR() = *handle(0)\FilePointer;
       
            CompilerIf Defined(SB_Compiler_SpiderBasic, #PB_Constant)
               For i = 0 To lengthToRead - 1
                  PokeB( memoryBuffer, i, PeekB(memPTR(), i + memPTR()\filePointer) )
               Next
            CompilerElse
               CopyMemory(memPTR()\FileBase + memPTR()\filePointer, memoryBuffer, lengthToRead)   
            CompilerEndIf
this is the code I tried which doesn't actually work (in spiderbasic)

Re: How to make StringByteLengthand Copymemory with SB?

Posted: Thu Apr 16, 2020 5:56 pm
by kingwolf71
Peter wrote:StringByteLength(): Perhaps this one?

Code: Select all

Procedure StringByteLength(String$, Format = #PB_Unicode)
  
  Protected sbl
  
  Select Format
    Case #PB_Ascii
      sbl = Len(String$)
    Case #PB_UTF8, #PB_Unicode
      ! v_sbl = new Blob([v_string$]).size;
  EndSelect
  
  ProcedureReturn sbl
  
EndProcedure

Debug StringByteLength("a", #PB_Ascii)
Debug StringByteLength("a", #PB_UTF8)
Debug StringByteLength("a", #PB_Unicode)

Debug StringByteLength("ä", #PB_Ascii)
Debug StringByteLength("ä", #PB_UTF8)
Debug StringByteLength("ä", #PB_Unicode)

Thats pretty cool, thanks

Re: How to make StringByteLengthand Copymemory with SB?

Posted: Thu Apr 16, 2020 6:05 pm
by Peter
kingwolf71 wrote:Thats pretty cool, thanks
Note that the procedure has not been sufficiently tested. It may therefore return incorrect values.