Author Topic: New Load & Save  (Read 3594 times)

0 Members and 1 Guest are viewing this topic.

Peter

  • Guest
New Load & Save
« on: March 10, 2011, 11:52:58 AM »
/* Peter  10/o3/2011 */

« Last Edit: July 07, 2013, 11:37:09 AM by peter »

Charles Pegge

  • Guest
Re: New Load & Save
« Reply #1 on: March 10, 2011, 02:46:14 PM »

Hi Peter,

This is my best effort using your API and Alpha029 which correctly handles "byref any"

Supports floats, and other types including fixed size UDTs and objects

A few indirection adjustments were required NB: **string


Code: [Select]

IndexBase 0

Declare Function ReadFile    Lib "kernel32.dll" (ByVal hFile As Long, ByRef lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, ByRef lpNumberOfBytesRead As Long, ByRef lpOverlapped As any) As Long
Declare Function WriteFile   Lib "kernel32.dll" (ByVal hFile As Long, ByRef lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, ByRef lpNumberOfBytesWritten As Long, ByRef lpOverlapped As any) As Long
Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Declare Function CreateFile  Lib "kernel32.dll" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Declare Function DeleteFile  Lib "kernel32.dll" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
Declare Function MoveFile    Lib "kernel32.dll" Alias "MoveFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String) As Long
Declare Function CopyFile    Lib "kernel32.dll" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
Declare Function CreateDirectory Lib "kernel32.dll" Alias "CreateDirectoryA" (ByVal lpPathName As String, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long

% GENERIC_WRITE = &H40000000
% GENERIC_READ  = &H80000000
% OPEN_EXISTING = 3
% OPEN_ALWAYS   = 4
% FILE_SHARE_READ  = 1
% FILE_SHARE_WRITE = 2

Dim byref SYS_SECUT as long
Dim byref NO as long
Dim dwcount as long

'----------------------------------------------------------------------------------------------------
Function ReadFileData(file as string,byref dest as any, byval count as long,byval scale as long) as long 
Long vRet
vRet = CreateFile(file, GENERIC_WRITE or GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, SYS_SECUT, OPEN_ALWAYS, 0, 0)
ReadFile vRet,dest,count*scale,dwcount,NO
dwcount/=scale
CloseHandle vRet
Function =  vRet 
End Function

'--------------------------------------------------------------------------------------------------------
Function WriteFileData(file as string,byref dest as any, byval count as long,byval scale as long) as long 
Long vRet
vRet = CreateFile(file, GENERIC_WRITE or GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, SYS_SECUT, OPEN_ALWAYS, 0, 0)
WriteFile vRet,dest,count*scale,dwcount,NO
dwcount/=scale
CloseHandle vRet
Function =  vRet
End Function
'------------------------------------------------------------------------------------------------------

Long i
String Mess = "It seems to be night, I can't see my fingers"
String Yess = "oooooooooooooooooooooooooooooooooooooooooooo"

Long a[100]
Long b[100]
Quad q = 123456789123
Quad w

For i=0 To 9
a(i) =65536+i
Next

WriteFileData "Quad.txt",q,1,8
ReadFileData  "Quad.txt",w,1,8
print w

Double d=12345.12345678
Double e

WriteFileData "Double.txt",d,1,sizeof double
ReadFileData  "Double.txt",e,1,sizeof double
print e

WriteFileData "Hoppla.txt",**mess,45,1
print yess
ReadFileData "Hoppla.txt",**yess,45,1
print yess


WriteFileData "Test.zaa",a,100,4
ReadFileData  "Test.zaa",b,100,4

For i=0 to 5
Print Str(b(i))
Next

CreateDirectory "MyFirstTest",0
CopyFile "Test.zaa","MyFirstTest/Test.zaa",0
DeleteFile "Test.zaa"

Charles

Charles Pegge

  • Guest
Re: New Load & Save
« Reply #2 on: March 10, 2011, 10:29:48 PM »
Hi Peter,

This is because dynamic strings are double pointered :) (and we are passing implicitly byref)

Here is how to use optional parameters:

Code: [Select]

'===================
'OPTIONAL PARAMETERS
'===================


  '------------------------------------------------------------
  function f(v as sys, optional byval a as sys, byref b as sys)
  '============================================================
  string opts
  '
  opts+= "OPTIONAL A = " (a) "    "
  '
  'check to see if the address of b is null
  '
  if & b then opts+="B = "+str(b) else opts+="B is not specified "
  '
  print "V = " str(v) chr(13) chr(10) opts
  '
  end function

sys a[100]

for i=1 to 100
  a[i]=i
next

'======
'TESTS:
'======

f a[1]
f a[10],123
f a[20],0,456
f a[30],123,456

' f a[40],,456 'this format is not supported yet



Charles

Charles Pegge

  • Guest
Re: New Load & Save
« Reply #3 on: March 11, 2011, 12:34:41 AM »

This is a solution to the string pointer dilemma.

I have used overloaded functions (functions of the same name but different prototypes) to handle both Ascii and wide strings.

If the variable type is not a string (gstring) then the generic Read/Write functions are automatically used instead.

The string variables will automatically adjust in size if the count is set to 0

Code: [Select]

==========================================================
'THIS VERSION SUPPORTS ANSI AND WIDE DYNAMIC STRINGS
'Using the function overloading feature (and gstrings)
'=========================================================

IndexBase 0

Declare Function ReadFile    Lib "kernel32.dll" (ByVal hFile As Long, ByRef lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, ByRef lpNumberOfBytesRead As Long, ByRef lpOverlapped As any) As Long
Declare Function WriteFile   Lib "kernel32.dll" (ByVal hFile As Long, ByRef lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, ByRef lpNumberOfBytesWritten As Long, ByRef lpOverlapped As any) As Long
Declare Function GetFileSizeEx Lib "kernel32.dll" (ByVal hFile As Long, ByRef lpFileSize as quad) as long
Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Declare Function CreateFile  Lib "kernel32.dll" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Declare Function DeleteFile  Lib "kernel32.dll" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
Declare Function MoveFile    Lib "kernel32.dll" Alias "MoveFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String) As Long
Declare Function CopyFile    Lib "kernel32.dll" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
Declare Function CreateDirectory Lib "kernel32.dll" Alias "CreateDirectoryA" (ByVal lpPathName As String, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long

% GENERIC_WRITE = &H40000000
% GENERIC_READ  = &H80000000
% OPEN_EXISTING = 3
% OPEN_ALWAYS   = 4
% FILE_SHARE_READ  = 1
% FILE_SHARE_WRITE = 2

Dim byref SYS_SECUT as long
Dim byref NO as long
Dim dwcount as long

'----------------------------------------------------------------------------------------------------
Function ReadFileData(file as string,byref dest as any, byval count as long,byval scale as long) as long 
Long vRet
vRet = CreateFile(file, GENERIC_WRITE or GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, SYS_SECUT, OPEN_ALWAYS, 0, 0)
ReadFile vRet,dest,count*scale,dwcount,NO
dwcount/=scale
CloseHandle vRet
Function =  vRet 
End Function

'--------------------------------------------------------------------------------------------------------
Function WriteFileData(file as string,byref dest as any, byval count as long,byval scale as long) as long 
Long vRet
vRet = CreateFile(file, GENERIC_WRITE or GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, SYS_SECUT, OPEN_ALWAYS, 0, 0)
WriteFile vRet,dest,count*scale,dwcount,NO
dwcount/=scale
CloseHandle vRet
Function =  vRet
End Function


'===========================================================================
'OVERLOADED FUNCTIONS FOR ASCII (scale=1) and wide (scale=2) DYNAMIC STRINGS
'The length of the string buffer is automatically adjusted
'===========================================================================


'-----------------------------------------------------------------------------------------------------------
Function ReadFileData(file as string,byref dest as gstring, byval count as long,byval scale as long) as long 
Long vRet
Quad le
vRet = CreateFile(file, GENERIC_WRITE or GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, SYS_SECUT, OPEN_ALWAYS, 0, 0)
if count=0 then GetFileSizeEx vRet,le : count=le/scale
dest=nuls count*scale
ReadFile vRet,*dest,count*scale,dwcount,NO
dwcount/=scale
CloseHandle vRet
Function =  vRet 
End Function

'------------------------------------------------------------------------------------------------------------
Function WriteFileData(file as string,byref dest as gstring, byval count as long,byval scale as long) as long 
Long vRet,le
le=len dest 'in bytes
if count=0 then count=le
if count>le then count=le
vRet = CreateFile(file, GENERIC_WRITE or GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, SYS_SECUT, OPEN_ALWAYS, 0, 0)
WriteFile vRet,*dest,count,dwcount,NO
dwcount/=scale
CloseHandle vRet
Function =  vRet
End Function



'=====
'TESTS
'=====


Long i
String Mess = "It seems to be night, I can't see my fingers"
String Yess = "ooooo!"

Long a[100]
Long b[100]
Quad q = 123456789123
Quad w

For i=0 To 9
a(i) =65536+i
Next

WriteFileData "Quad.txt",q,1,8
ReadFileData  "Quad.txt",w,1,8
print w

Double d=12345.12345678
Double e

WriteFileData "Double.txt",d,1,sizeof double
ReadFileData  "Double.txt",e,1,sizeof double
print e

WriteFileData "Hoppla.txt",mess,0,1
print yess
ReadFileData "Hoppla.txt",yess,0,1
print yess


WriteFileData "Test.zaa",a,100,4
ReadFileData  "Test.zaa",b,100,4

For i=0 to 5
Print Str(b(i))
Next

CreateDirectory "MyFirstTest",0
CopyFile "Test.zaa","MyFirstTest/Test.zaa",0
DeleteFile "Test.zaa"

Charles

Aurel

  • Guest
Re: New Load & Save
« Reply #4 on: March 24, 2011, 05:11:57 AM »
I dont know is my question on right place.
But i dont see anywhere pointer type of varable?
Like ;
Dim p as pointer
Is this posibile ?
Or will be added in future?

What is any
Is this pointer or variant type like in VB?
In chm help i dont see explanation how properly set var types.

Charles Pegge

  • Guest
Re: New Load & Save
« Reply #5 on: March 24, 2011, 09:21:17 AM »
Hi Aurel,

Oxygen pointers are different to C, though it is able to understand them in C headers etc.

Once a pointer variable has been set up and assigned an address, it is used like a normal variable and its pointy nature is hidden. This is how variables passed byref behave in a function with most dialects of compiled basic.

There are a number of ways to declare pointer variables. This is the most traditional:

Code: [Select]
dim pa as long ptr
dim a  as long

a=42

& pa=& a ' address of pa is assigned the address of a

print pa 'result 42


"Any" is generally equivalent to long or sys but will suppress automatic type conversion when used in a function prototype.


Charles

Aurel

  • Guest
Re: New Load & Save
« Reply #6 on: March 24, 2011, 09:53:32 AM »
Hmm this is little bit strange to me becose pointer need type.
I try this:
Code: [Select]
dim s as string ptr
dim novi as string
novi = "New"

&s = &novi

print s

Only on this way work properly.

Charles Pegge

  • Guest
Re: New Load & Save
« Reply #7 on: March 24, 2011, 10:27:41 AM »
Yes pointers need to be associated with a type.

You can of course obtain the address of any variable and assign it to a sys or long variable.

s="hello"
long p=& s

This is useful for low level coding.

Charles

PS beware &h hexadecimal prefix and &o octal prefix &b binary prefix.
For addresses always leave a space between & and the vriable name.
Or use @ instead