Announcement

Collapse
No announcement yet.

pointers from C++ to Pb

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • pointers from C++ to Pb

    Hi,
    I trying to conver the C++ code below to PB Code.
    I put the C++ and pb code together, the C++ is commented, and below
    the commented C++ line you will probably see my translation of it to PB.
    as you can see, I get stuck where the C++ code says "Table[0]"

    Code:
    #INCLUDE "Win32API.inc"
    
    TYPE DATAITEM
        Uid AS DWORD
        Gid AS DWORD
        mode AS DWORD
        Crc32 AS DWORD
        Filename AS ASCIIZ PTR
        l_Filename AS DWORD
        Hash AS DWORD
        Context AS ASCIIZ PTR
        l_Context AS DWORD
    END TYPE
    
    GLOBAL pHeap AS LONG
    
    'UINT Database_Read(HANDLE FileHandle, DATABASE *lpDataBase) {
    FUNCTION Database_Read (FileHandle AS LONG, BYVAL DATABASE AS DATAITEM) AS DWORD
        'LPDATAITEM  DataItem;
        'DATABASE    DataBase;
        'DWORD       Read;
        DIM vRead AS DWORD
        'PUINT       Table, Offset;
        DIM Table AS DWORD PTR, Offset AS DWORD PTR
        'UINT        l_FileName, l_Context;
        DIM l_FileName AS DWORD, l_Context AS DWORD
        'UINT        i_Size, b_Size, db_Size, db_Version, b_Crc32, t_Size, i_Read;
        DIM i_Size AS DWORD, b_Size AS DWORD, db_Size AS DWORD, db_Version AS DWORD, b_Crc32 AS DWORD,t_Size AS DWORD,i_Read AS DWORD
        DIM p AS DWORD
        t_Size      = 512
        i_Read      = 0
        Table       = HeapAlloc(pHeap, 0&, t_Size * SIZEOF(p))
        'if (! Table)
        IF ISFALSE Table THEN
        '{
        '    // Could not allocate memory
        MSGBOX "Could not allocate memory"
        '    return 0;
        '}
        END IF
        DIM g AS OVERLAPPED
        'if (! ReadFile(FileHandle, Table,2 * sizeof(UINT), &Read ,NULL))
        IF ISFALSE ReadFile(FileHandle, Table,2 * SIZEOF(p), vRead ,g) THEN
        '{
        '    // Could not read from file
        MSGBOX "Could not read from file"
        '    goto CLEANUP;
        GOTO CLEANUP
        '}
        END IF
        'db_Version  = Table[0];
        'db_Size     = Table[1];
        'if (db_Version != 0)
        '{
        '    //  Current database version is always zero
        '    goto CLEANUP;
        '}
        'DataBase    = (DATABASE)HeapAlloc(pHeap, NULL, sizeof(LPDATAITEM) * db_Size);
        'if (! DataBase)
        '{
        '    //  Could not allocate enough memory
        '    goto CLEANUP;
        '}
        'for (; 
        '{
        '    if (! ReadFile(FileHandle, Table, 2 * sizeof(UINT), &Read, NULL) | | Read != 2 * sizeof(UINT))
        '    {
        '        // Could not read (enough?)
        '        break;
        '    }
        '
        '    // Get block size
        '    b_Size  = Table[0];
        '    // Get block crc32
        '    b_Crc32     = Table[1];
        '    //  Reduce block size (Block size & Crc variables are included in block size)
        '    b_Size  -= sizeof(UINT) * 2;
        '    if (b_Size > (t_Size * sizeof(UINT)))
        '    {
        '        t_Size  = b_Size / sizeof(UINT) + 10;
        '        //  Free memory
        '        HeapFree(pHeap, NULL, Table);
        '        //  Allocate memory
        '        Table   = (PUINT)HeapAlloc(pHeap, NULL, t_Size * sizeof(UINT));
        '        if (! Table)
        '        {
        '            // Could not allocate memory
        '            break;
        '        }
        '    }
        '    if (! ReadFile(FileHandle, Table, b_Size, &Read, NULL) | | Read != b_Size)
        '    {
        '        // Could not read (enough?)
        '        break;
        '    }
        '    // Here we could calculate crc32 for the block and compare it against stored crc
        '    for (Offset=Table;b_Size;b_Size -= i_Size)
        '    {
        '        i_Size      = Offset[0];
        '        l_FileName  = Offset[1];
        '        l_Context   = i_Size - l_FileName - 7 * sizeof(UINT);
        '        //  Allocate new entry in database
        '        DataItem    = (LPDATAITEM)HeapAlloc(pHeap, NULL, sizeof(DATAITEM) + l_FileName + l_Context);
        '        if (! DataItem)
        '        {
        '            //  Could not allocate memory
        '            break;
        '        }
        '        DataBase[i_Read++]  = DataItem;
        '        //  Initialize pointers
        '        DataItem->FileName  = (PCHAR)&DataItem[1];
        '        DataItem->Context   = (LPVOID)((ULONG)&DataItem[1] + l_FileName);
        '        //  Copy contents
        '        DataItem->Uid           = Offset[2];
        '        DataItem->Gid           = Offset[3];
        '        DataItem->Mode          = Offset[4];
        '        DataItem->Crc32             = Offset[5];
        '        DataItem->Hash          = Offset[6];
        '        DataItem->l_FileName    = l_FileName;
        '        DataItem->l_Context         = l_Context;
        '        //  FileName does not get zero padding
        '        CopyMemory(DataItem->FileName, (PCHAR)&Offset[7], l_FileName);
        '        CopyMemory(DataItem->Context, (LPVOID)((ULONG)&Offset[7] + l_FileName), l_Context);
        '        // Move offset
        '        Offset      = (PUINT)((ULONG)Offset + i_Size);
        '    }
        '}
        '//  Set pointer
        '*lpDataBase     = DataBase;
    
    CLEANUP:
        'HeapFree(pHeap, NULL, Table);
        HeadFree pHeap, 0&, Table
        'return i_Read;
        FUNCTION = i_Read
    END FUNCTION
    
    FUNCTION PBMAIN
        'DATABASE    DataBase;'
        DIM Database AS DATAITEM
        'HANDLE      FileHandle;
        DIM FileHandle AS LONG
        'PCHAR       FileName;
        DIM Filename AS ASCIIZ PTR
        'UINT        Size;
        DIM vSize AS DWORD
    
        'pHeap       = GetProcessHeap();
        pHeap       = GetProcessHeap()
        'FileName    = "d:\\ioFTPD\\site\\home\\ioftpd\\.ioFTPD";
        DIM wtf AS STRING
        wtf = "E:\ioFTPD\site\NoCheck\wtf\.ioFTPD"
        FileName  = STRPTR(wtf)
        DIM p AS SECURITY_ATTRIBUTES
    
        'FileHandle  = CreateFile(FileName, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
        '    OPEN_EXISTING, NULL, NULL);
        FileHandle  = CreateFile(@FileName, %GENERIC_READ, %FILE_SHARE_READ OR %FILE_SHARE_WRITE, p, _
            %OPEN_EXISTING, 0&, 0&)
    
        'if (FileHandle != INVALID_HANDLE_VALUE)
        IF FileHandle <> -1 THEN
        '{
        '    //  We should lock file here
        '    Size    = Database_Read(FileHandle, &amp;DataBase);
            vsize = Database_Read(FileHandle, DataBase)
        '    //  And unlock it here
        '    CloseHandle(FileHandle);
        CloseHandle FileHandle
        '    //  Use database here
        '    if (Size) MessageBox(NULL, "YEP", "GOD YOU ARE", NULL);
        '    //  Free database
        '    Database_Free(DataBase, Size);
        '}
        END IF
        'return 0;
        CloseHandle FileHandle
        MSGBOX "done"
    END FUNCTION
    ------------------
    -snype
    -snype

  • #2
    Alberto,

    you write according to your code

    @Table[0]

    regards,

    Torsten



    ------------------

    Comment


    • #3
      Indexed pointers - you can use the following to access the values
      in Table "array".
      Code:
        db_Version  = @Table[0] 'first Table item
        db_Size     = @Table[1] 'second Table item

      ------------------
      http://www.tolkenxp.com/pb
      Download: incLean, PBcodec, custom controls and code, etc.
      Borje Hagsten - [email protected]

      Comment


      • #4
        for some reason
        Code:
        db_Version  = @Table[0] 'first Table item  
        db_Size     = @Table[1] 'second Table item
        gave me memory read errors (Visual C++ Debugger Msg pops up)
        but I played around with it and this worked.
        Code:
        DIM xPtr AS DWORD POINTER
        xPtr = VARPTR(table)
        db_Version = @xptr[0]
        db_Size = @xptr[1]
        Now I am stuck here
        Code:
        'DataBase    = (DATABASE)HeapAlloc(pHeap, NULL, sizeof(LPDATAITEM) * db_Size);   
        Database = HeapAlloc(pHeap, %NULL,sizeof(Database) * db_Size)
        I keep getting an error that says "Variable Expected"

        ------------------
        -snype
        -snype

        Comment


        • #5
          keep getting an error that says "Variable Expected
          Check where in the line the error is...those error messages mean..
          Code:
          Error 426 filename (linenumber:columnnumber) messagetext.
          Also check the DECLARE for HeapAlloc...mine reads...

          Code:
          DECLARE FUNCTION HeapAlloc LIB "KERNEL32.DLL" ALIAS "HeapAlloc" (BYVAL hHeap AS DWORD, BYVAL dwFlags AS DWORD, BYVAL dwBytes AS DWORD) AS DWORD
          .. but I think some earlier versions used an "AS ANY" somewhere in there.

          Regardless, you might be able to get this to work by changing %NULL to BYVAL %NULL (although I'd use some flags myself)..

          MCM
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment

          Working...
          X