Just starting out? Need help? Post your questions and find answers here.
kingwolf71
 
Posts: 21
Joined: Wed May 01, 2019 10:14 am

Peek/poke issues

by kingwolf71 Fri Apr 17, 2020 7:04 am

When this is complete I will post it on both PB and SB forums so everyone can use it. It is a memory filesystem which works on PureBasic and SpiderBasic. In PB, everything works well.

This is a downsized version of what I am trying to do; Code never makes it to the end; and I believe one of the issues is that it works in a module.



Code: Select all


EnableJS

function memcpy(dest, src, n)
{
   dest.set(src.slice(0, n))
}
DisableJS

DeclareModule test
   
    Structure _membersMemoryFileClass
      eof.w
      flags.w
      *fileBase
      fileSize.i
      initialSize.i
      initialPageSize.i
      usedFile.i    ;Lof.
      filePointer.i ;Loc.
      pageSize.l
   EndStructure

   Global Dim           *Handle._membersMemoryFileClass(1)
   
   
   Declare     Create( initialSize.i = 8192, pageSize.l = 4096 )
   Declare     Write(handle, text$, format = 0)
   Declare.s   ReadS(handle, format=0)
   
EndDeclareModule

Module test
   
Macro  memPTR()
   *Handle(handle)
EndMacro

 Macro                   CheckFormat()
      If format <> #PB_Ascii And format <> #PB_UTF8 And format <> #PB_Unicode
         format = #PB_Unicode
      EndIf
 EndMacro

Global      gHandle = 1


   Procedure              Create( initialSize.i = 8192, pageSize.l = 4096 )
      Protected         handle
     
      handle = gHandle
      gHandle + 1
      ReDim *Handle(gHandle)
      memPTR() = AllocateStructure(_membersMemoryFileClass)
     
      ;Attempt to allocate the initial memory.
      memPTR()\FileBase = AllocateMemory(initialSize, #PB_Memory_NoClear)
      If memPTR()\FileBase
         memPTR()\FileSize       = initialSize
         memPTR()\initialSize    = initialSize
         memPTR()\usedFile       = 0
         memPTR()\pageSize       = pageSize
         memPTR()\initialPageSize= pageSize
      Else
        FreeStructure(memPTR())
        memPTR() = 0
        ProcedureReturn 0
      EndIf
     
      ProcedureReturn handle
   EndProcedure
   
   Procedure            Write(handle, text$, format = 0)
      Protected         result.i, ptr.i, byteLength.i, *p
 
      CheckFormat()
      ;byteLength  = (Len(text$) + 1) * SizeOf(Character)
      ;result = memAllocateExtraBytes( handle, byteLength )
     
      byteLength = PokeS(memPTR()\FileBase, memPTR()\filePointer, text$,-1,format)
      Debug byteLength
         
      If format = #PB_Ascii Or format = #PB_UTF8
         byteLength + 1
      Else
         byteLength + 2
      EndIf
         
         memPTR()\filePointer+byteLength
         If memPTR()\filePointer > memPTR()\usedFile
            memPTR()\usedFile = memPTR()\filePointer
         EndIf
 
      ProcedureReturn result
   EndProcedure
   
    Procedure.s          ReadS(handle, format=0)
      Protected         result$, *ptr.ASCII, *ptrChar.CHARACTER, byteLength.i, t1
 
      CheckFormat()

      If memPTR()\filePointer < memPtr()\usedFile
         
         result$ = PeekS( memPTR()\filePointer, memPTR()\filePointer, -1, format )
         byteLength = Len( result$ ) * SizeOf( Character )
         
         If format = #PB_Ascii Or format = #PB_UTF8
            byteLength + 1
         Else
            byteLength + 2
         EndIf
       
         memPtr()\filePointer + byteLength
         
         If memPtr()\filePointer >= memPtr()\usedFile
            memPtr()\filePointer = memPtr()\usedFile
            memPTR()\EOF = #True
         EndIf
      EndIf
 
      ProcedureReturn result$
   EndProcedure
 
EndModule

Procedure   test1()
   *p1 = 0
   *p2 = 0
   
   !p_p1 = new Uint8Array(100)
   !p_p2 = new Uint8Array(100)
   PokeS( *p1, 0, "today is a good day", -1 )
   PokeS( *p2, 0, "another test string", -1 )
   
   ;!memcpy( p_2, p_1, 10 )
   
   Debug PeekS( *p1, 0, -1 )
   Debug PeekS( *p2, 0, -1 )     
EndProcedure

Procedure   test2()
   *p1 = AllocateMemory(200)
   h = test::Create()
   test$ = "today is a good day"
 
   
   If *p1 And h
       test::Write(h,text$,#PB_Unicode)
       test::*Handle(h)\filePointer = 0
       Debug test::ReadS( h, #PB_Unicode )
     
   Else
      Debug "Failed to allocate memory"
   EndIf

EndProcedure


Debug "Test 2 start"
test2()
Debug "Test 2 end"


Return to Coding Questions

Who is online

Users browsing this forum: No registered users and 7 guests