• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

Translating PB to C/C++

Started by Patrice Terrier, July 25, 2013, 12:23:52 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

James C. Fuller

Patrice,
  What are you using for c++ Random file access of a typical PowerBasic TYPE ?

James

Patrice Terrier

#46
James--

I am using something like that:

ReadFile(fHandle, &ga_Sprite[0], BufferSize, &dwBytes, NULL);

Where BufferSize matches either the size of an array, or the size of a specific fixed record.

...
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

James C. Fuller

Patrice,
  Did you do any speed/size tests using <fstream> ,<cstdio>, and  api (CreateFile,Read,Write)?

James

Patrice Terrier

James--

Direct use of API (CreateFile,Read,Write), definitly produces smaller/faster code.
I am using them from the origine, and i have written a couple of functions that are working exactly like those of Ethan Winer (FOpen) and Charles Petzold.

Each time you use directly the flat API, you bypass any kind of CRT, what ever the language being used.

Example of generic FOpen function
function FOpen (szFileName as asciiz, byval AccessMode as long, byval ShareMode as long, byref hFile as long) as long

    LOCAL AccessIs, ShareIs, FlagAndAttribute as long

    AccessMode = min&(max&(AccessMode, 0), 2)     '// Coherce between 0-2
    if (AccessMode = 0) then                      '// 0 Open for read only.
        AccessIs = %GENERIC_READ
    elseif AccessMode = 1 then                    '// 1 Open for write only.
        AccessIs = %GENERIC_WRITE
    else                                          '// 2 Open for read and write.
        AccessIs = %GENERIC_READ OR %GENERIC_WRITE
    end if

    ShareMode = MIN(MAX(ShareMode, 1), 4)         '// Coherce between 1-4
    if ShareMode = 1 then                         '// 1 Deny read/write access.
       ShareIs = 0
    elseif ShareMode = 2 then                     '// 2 Deny write access.
       ShareIs = %FILE_SHARE_READ
    elseif ShareMode = 3 then                     '// 3 Deny read access.
       ShareIs = %FILE_SHARE_WRITE
    else                                          '// 4 Deny none (full share mode).
       ShareIs = %FILE_SHARE_READ OR %FILE_SHARE_WRITE
    end if

    if (hFile = -1) then
        FlagAndAttribute = %FILE_ATTRIBUTE_NORMAL OR %FILE_FLAG_WRITE_THROUGH
    else
        FlagAndAttribute = %FILE_ATTRIBUTE_NORMAL
    end if

    hFile = CreateFile(szFileName, AccessIs, ShareIs, byval %NULL, %OPEN_ALWAYS, FlagAndAttribute, byval %NULL)

    if (hFile = CLNG(%INVALID_HANDLE_VALUE)) then '// -1 Fail to create the file
        function = GetLastError()                 '// Set the error code
        hFile = 0                                 '// Reset handle number
    end if

end function



Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

James C. Fuller

Patrice,
  What are you using for PowerBASIC REDIM / REDIM PRESERVE replacement?

James

Patrice Terrier

Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

James C. Fuller

Patrice,
  Thanks I knew that :) and is what I use for c++

I'm doing a bit of "c" now and will revert to the malloc series I guess.

James

Patrice Terrier

Then alloc a larger buffer than needed, and create a new one, only if the extra member couldn't fit in memory.
That would avoid to cause memory fragmentation.

...
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com