to be often used by advanced programmers, to solve 'low level' byte copying.
The signature is:
Code: Select all
memcpy(dest as any ptr, src as any ptr, size as size_t) as any ptr
as follows:
Code: Select all
copy [path]source [path]destination [switches]
change the return type (from any ptr, to boolean). Also, in order to prevent any
ambiguity, I've re-named it, as below:
Code: Select all
MemCopy(ByVal src As Any Ptr, ByVal dst As Any Ptr, ByVal siz As ULong=1) As Boolean
one Private Function as well as its declaration. This means, the Function is only
linked in, if it is actually called from main code.
MemCopy.bi:
Code: Select all
' MemCopy.bi -- 2018-07-25, by MrSwiss
'
' only ever include it once (the same, as include guards)
#Pragma Once
' renamed to avoid ambiguity issues with CRT's memcpy (with other param. order & return type)
Declare Function MemCopy(ByVal src As Any Ptr, ByVal dst As Any Ptr, ByVal siz As ULong=1) As Boolean
' ----- MemCopy() implementation -----
Private Function MemCopy( _ ' FB implementation of: CRT's memcpy(@dst, @src, size)
ByVal src As Any Ptr, _ ' source ptr (inverted params. from original)
ByVal dst As Any Ptr, _ ' target ptr
ByVal siz As ULong = 1 _ ' size (in bytes) to copy (default = 1, is min.)
) As Boolean ' FALSE = OK | TRUE = ERROR (different return-type)
If src = 0 Or dst = 0 Or siz = 0 Then Return TRUE ' parameters error checks
Dim As UByte Ptr ps = src, pd = dst ' convert Any Ptr's to UByte Ptr's (for indexing)
For i As ULong = 0 To siz - 1 ' amount of bytes to copy (0 to 0 = 1 byte = min.)
pd[i] = ps[i] ' copy them
Next
Return FALSE ' everything OK (NO ERROR)
End Function
' ----- EOF -----
Code: Select all
' MemCopy_test1.bas -- 2018-07-25, by MrSwiss
'
' compile: -s console
'
#Include "MemCopy.bi" ' instead of: #Include "crt/string.bi" or "crt.bi" (which includes a lot more)
' ===== Demo Code Start =====
Dim As ULongInt tuli = &hF0F1F2F3F4F5F6F7 ' use hex to be able to check later
Dim As UByte uba(0 To 7) ' same size as ULongInt (above)
Dim As String msg = "Greetings from FreeBASIC!" ' source string (to be copied)
Dim As ZString Ptr psz = CAllocate(Len(msg) + 1) ' allocate memory, mandatory! (incl. terminator)
Print "MemCopy(), with a numeric variable ..." : Print
Print "ULongInt : "; Hex(tuli, 16)
If MemCopy(@tuli, @uba(0), SizeOf(ULongInt)) Then ' SizeOf(ULongInt) can be written as literal: 8
Print "MemCopy() failed!" : Sleep : End 1 ' in case of error: inform user, then quit
End If
Print "UByte array: "; ' suppress LF
For i As Long = 7 To 0 Step -1 ' MSB to LSB output
Print Hex(uba(i), 2); ' byte by byte, in hex (no LF)
Next : Print ' add a LF (when finished)
Print : Print "MemCopy(), with a string ..." : Print
Print "original: "; msg ' below: instead of StrPtr(), SAdd() can be used
If MemCopy(StrPtr(msg), psz, Len(msg)) Then ' StrPtr() is needed for string-data access, _
Print "MemCopy() failed!" : Sleep : End 2 ' otherwise: @msg, whould point at 'the header'
End If
Print "copy : "; *psz ' output: dereferenced ZString Ptr
DeAllocate(psz) ' clean up: free allocated memory
Locate CsrLin + 2 ' add 2 more 'empty' lines
Print "press a key, to EXIT ... "; ' inform user
Sleep : End 0 ' wait for user then quit (no error)
' ===== Demo Code End ===== ' ----- EOF -----