PDA

View Full Version : "Restore" command replacement for thinbasic?



largo_winch
04-09-2012, 10:36
I am looking for an alternative command (or similiar command) for a) "RESTORE" (from freebasic ) for thinbasic and if it's possible to convert a b) "READ$" command and c) "DATA" for thinbasic too. Some infos I place at the begin of script example. any help is appreciated here :-), bye largo



' Empty GUI script created on 09-04-2012 09:55:46 by (thinAir)

' RESTORE (Label)
' "RESTORE" shows what saved Variables from DATA of next READ command can be read

' fictive pseudo code as followed (not working at all):

Uses "console"

Local x As Long, y As Long
RESTORE lbar
'Read x
Console_Read x
Print x

RESTORE
'Read x,y
Console_Read x, y
Print x, y

RESTORE foo
'Read x,y
Console_Read x, y
Print x, y

DATA 1, 2
foo:
DATA 3

lbar:
DATA 4
'Sleep
waitkey
'------------------ freebasic part ---------- //
'
' result from freebasic:
' 4
' 1 2
' 3 4
'

Lionheart008
05-09-2012, 11:53
hi largo, perhaps you can split the command "restore" and try to build an module if there is no other way for converting a code example.

I have not more closer knowledge about freebasic but if you have no other chance to convert split the command with sub or function to investigate what's behind it.

my try for this way could be:

"freebasic":



DECLARE SUB restorey (a As Integer)

Dim As Long x,y,bar,foo,zero
'RESTORE bar
restorey bar
READ x 'console_read (thinbasic)
PRINT x

restorey zero 'here's a unlikely problem!
'RESTORE
READ x, y 'console_read (thinbasic)
PRINT x,y

'RESTORE foo
restorey foo
READ x,y 'console_read (thinbasic)
PRINT x,y

DATA 1, 2
foo:
DATA 3
bar:
DATA 4
sleep

sub restorey (a As Integer)
Restore
End Sub

' I don't know if there is a "optional byval" paramaeter for function/sub's



2) other idea to ask in freebasic forum what's the task of "restore" command.

best regards, frank

Lionheart008
05-09-2012, 19:01
again: I've found these infos from freebasic websites and my source code archives.. perhaps you can see what's the problem to convert a whole freebasic based function into thinbasic. better and easier to use a module for it, that's my personal opinion or find a fitted command that works with same power.

"DataRestore" / problems are FBSYMBOL PTR and ASTNODE PTR, it won't be easy to convert this "types" for thinbasic.




DECLARE FUNCTION rtlDataRestore _
( _
BYVAL label AS FBSYMBOL PTR, _
BYVAL afternode AS ASTNODE PTR _ '= NULL
) AS INTEGER

DECLARE FUNCTION rtlDataRead _
( _
BYVAL varexpr AS ASTNODE PTR _
) AS INTEGER


FUNCTION rtlDataRestore _
( _
BYVAL label AS FBSYMBOL PTR, _
BYVAL afternode AS ASTNODE PTR _
) AS INTEGER

DIM proc AS ASTNODE PTR = any, expr = any
DIM expr AS ANY PTR
DIM sym AS FBSYMBOL PTR = any

FUNCTION = FALSE

proc = astNewCALL( PROCLOOKUP( DATARESTORE ), NULL )

'' byval labeladdrs as void ptr
IF( LABEL = NULL ) THEN
sym = astGetFirstDataStmtSymbol( )

'' blank RESTORE used before any DATA was found? damn..
IF( sym = NULL ) THEN
'' create an empty stmt, it should just contain a link to the next DATA
expr = astDataStmtBegin( )
astDataStmtEnd( expr )
astDelNode( expr )

sym = astGetFirstDataStmtSymbol( )
END IF

ELSE
sym = astDataStmtAdd( LABEL, 0 )
END IF

expr = astNewADDROF( astNewVAR( sym, 0, FB_DATATYPE_BYTE ) )
IF( astNewARG( proc, expr ) = NULL ) THEN
EXIT FUNCTION
END IF

''
IF( afternode = NULL ) THEN
astAdd( proc )
ELSE
astAddAfter( proc, afternode )
END IF

FUNCTION = TRUE

END FUNCTION

frank

Petr Schreiber
05-09-2012, 20:37
Hi guys,

I hate when this happens to me (I ask about solution of problem and I get response I solve wrong problem), but...

I don't want to break the party, but I think the DATA, READ$ and RESTORE made sense mostly in legacy BASICs.

As you can easily read files in ThinBASIC, I think it is much more systematic to separate data and application code, and save all you need to separate file.

One of the possible approaches is to use INI file - you can have named sections (like labels in your code) and you can read data (name them as you need) from them.

I attach supersimple example for you - it is not rework of the FB code above, but something to give idea.


Petr

largo_winch
06-09-2012, 21:46
it's often a problem to convert "alien" basic language code example. Thanks to frank and petr for your example. Data and restore example is for me a constant problem as I have tried to converte code example from freebasic to thinbasic in general. I can approach a lot of other example too but that's not important here.
I will check my problem here for converting freebasic stuff to thinbasic (better to powerbasic). load data that's not the problem but the restore probleme to make a new order for data read import files. nevertheless many thanks for frank and petr for your example and deeper knowledge. thinbasic is great. but to solve a problem with another problem (petr!) it's same stuff to make a magic function I don't know too (initkey and so one...). I am here to understand more about progamming language. and I am curious to know more about difference about powerbasic-thinbasic-freebasic and see what happens after knowing that with code example. thanks, bye, largo

zlatkoAB
06-09-2012, 22:37
Hmmm...
I'm not sure but DATA block is a sort of storage or container..
so maybe is possible to use some sort of linked list to restore them.

Michael Clease
10-09-2012, 21:56
Here is a rough idea that I am sure Petr or Eros could improve on.

Simple idea is you put the data into an array and then call the restore command with a few parameters , pointer to the array, number of elements in the array and datatype size in bytes.



Dim myData1(10) As Byte
Dim myData2(10) As Word
Dim pMyData1 As DWord
Dim pMyData2 As DWord
Dim a,b,c,d,e As Byte
Dim f,g,h,i,j As Word


Dim sMsg As String
Dim Datasize1 As DWord
Dim Datasize2 As DWord


Mydata1(1) = 1,2,3,4,5
Mydata2(1) = &h11AA,&h5522,&h3663,&h4CC4,&h578A
'pMyData1 = VarPtr(MyData1(1))
'pMyData2 = VarPtr(MyData2(1))


Datasize1 = UBound(myData1)*SizeOf(Mydata1(1))
Datasize2 = UBound(myData2)*SizeOf(Mydata2(1))


sMsg = "Data before : "+$CRLF(2)
sMsg += "a = "+Hex$( a,SizeOf(a)*2 )+$CRLF
sMsg += "b = "+Hex$( b,SizeOf(b)*2 )+$CRLF
sMsg += "c = "+Hex$( c,SizeOf(c)*2 )+$CRLF
sMsg += "d = "+Hex$( d,SizeOf(d)*2 )+$CRLF
sMsg += "e = "+Hex$( e,SizeOf(e)*2 )+$CRLF(2)




Restore(VarPtr(MyData1(1)), UBound(myData1), SizeOf(Mydata1(1)))
Read(a,b,c,d,e)


sMsg += "Data After :"+$CRLF(2)
sMsg += "a = "+Hex$( a,SizeOf(a)*2 )+$CRLF
sMsg += "b = "+Hex$( b,SizeOf(b)*2 )+$CRLF
sMsg += "c = "+Hex$( c,SizeOf(c)*2 )+$CRLF
sMsg += "d = "+Hex$( d,SizeOf(d)*2 )+$CRLF
sMsg += "e = "+Hex$( e,SizeOf(e)*2 )+$CRLF(2)
MsgBox 0, smsg




sMsg = "Data before : "+$CRLF(2)
sMsg += "f = "+Hex$( f,SizeOf(a)*2 )+$CRLF
sMsg += "g = "+Hex$( g,SizeOf(b)*2 )+$CRLF
sMsg += "h = "+Hex$( h,SizeOf(c)*2 )+$CRLF
sMsg += "i = "+Hex$( i,SizeOf(d)*2 )+$CRLF
sMsg += "j = "+Hex$( j,SizeOf(e)*2 )+$CRLF(2)


Restore(VarPtr(MyData2(1)), UBound(myData2), SizeOf(Mydata2(1)))
Read(f,g,h,i,j)




sMsg += "Data After :"+$CRLF(2)
sMsg += "f = "+Hex$( f,SizeOf(f)*2 )+$CRLF
sMsg += "g = "+Hex$( g,SizeOf(g)*2 )+$CRLF
sMsg += "h = "+Hex$( h,SizeOf(h)*2 )+$CRLF
sMsg += "i = "+Hex$( i,SizeOf(i)*2 )+$CRLF
sMsg += "j = "+Hex$( j,SizeOf(j)*2 )+$CRLF(2)
MsgBox 0, smsg


Stop
'
' Restore usage :
'
' Restore(DataIn, Elements,DataSize )
' Datain = the address of the array
' Elements = the size of the array
' Datasize = the number of bytes for the datatype (byte=1,word=2,...)
'
Function Restore(DataIn, Elements,DataSize )
Static Data As DWord
Local cParams As Long
Local flag As Long


Flag = 1
cParams = Function_CParams
If CParams = 0 Then Exit Function
If (Function_CParams => 2) Then
Data = DataIn
Read(Flag, DataIn, Elements, Datasize)
End If

End Function

Function READ(Optional ByRef aa,ByRef bb,ByRef cc,ByRef dd,ByRef ee,ByRef ff,ByRef gg,ByRef hh,ByRef ii,ByRef jj,ByRef kk,ByRef ll)
Static counter As DWord
Static Data As DWord
Static DataSize As DWord
Static Elements As DWord
Local cParam As DWord
Local varList(12) As DWord
Local N, M As DWord

cParam = Function_CParams ' return the number of passed paramaters


If aa = 1 Then
Data = bb
Elements = cc
Datasize = dd
Counter = 0
Exit Function
Else
Incr n : VarList(n) = VarPtr(aa)
Incr n : VarList(n) = VarPtr(bb)
Incr n : VarList(n) = VarPtr(cc)
Incr n : VarList(n) = VarPtr(dd)
Incr n : VarList(n) = VarPtr(ee)
Incr n : VarList(n) = VarPtr(ff)
Incr n : VarList(n) = VarPtr(gg)
Incr n : VarList(n) = VarPtr(hh)
Incr n : VarList(n) = VarPtr(ii)
Incr n : VarList(n) = VarPtr(jj)
Incr n : VarList(n) = VarPtr(kk)
Incr n : VarList(n) = VarPtr(ll)
ReDim Preserve Varlist(CParam) ' Clean up variable list
For n = 1 To CParam ' Variable counter loop
For M = 0 To Datasize-1
If Counter > Elements Then Counter = 0
Poke( VarList(n)+m, Peek( Data+Counter) )
Incr counter
Next ' m
Next ' n
End If


End Function

Lionheart008
12-09-2012, 16:08
my attempts failed to use a module for "Data", "restore" and "read". the problem is to write a command for "Data", that's not possible to make it with function or sub. "data" is old fasion. better you use array instead of antique basic commands or types.

I've done a dummy convertion though. see my comments, largo. add the testfile in zip folder.

@michael: looks good your idea ;)


Uses "restore2", "console"

' Empty GUI script created on 03-01-1999 01:54:28 by (thinAir)
Declare Function restore Lib "thinbasic_restore2.dll" Alias "restoreme" () As Long
Declare Function read Lib "thinbasic_restore2.dll" Alias "readme" () As Long
Declare Function data Lib "thinbasic_restore2.dll" Alias "datas" () As Long

Declare Function LoadLibraryA Lib "Kernel32.dll" Alias "LoadLibraryA" _
(lpLibFileName As Asciiz) As DWord

Function TBMain () As Long
Dim x As Long, y As Long
Dim s As String
Dim RetValue As Long

MsgBox 0,"test load library"
RetValue = LoadLibraryA("thinbasic_restore2.dll")
MsgBox 0,"return code from LoadLibrary: " & Str$(RetValue)

Console_WriteLine "the convertion failed here after some tests, sorry"

MsgBox 0,"test1"
restoreme
readme(x)
Console_WriteLine x
MsgBox 0, Str$(x)

MsgBox 0,"test2"
'restoreme '(x) => restoreme(x) doesn't work
readme(x)
readme(y)
MsgBox 0, Str$(x) + Str$(y)
Console_WriteLine "result for x+y: " + Str$(x) + Str$(y)

MsgBox 0,"test3"
restoreme
readme(x)
readme(y)
Datas 'this command is like a dummy because you have no chance to type in new values or chars

' a) DATA doesn't work in a function or sub, thats the main problem here!
' b) I have predefined these Data in my freebasic example:
'
' Data 1, 2
' foo:
' Data 3
' Bar:
' DATA 4

MsgBox 0, "result3: " + Str$(x)+ Str$(y)
Console_WriteLine "result3 for x+y with restore.. " + Str$(x)+ Str$(y)
Console_WriteLine ""
' c) problem is that variable x + y doesnt catch the DATA from freebasic because they cannot
' be programmed like a simple command like "DATA" in a function for parsing
Console_WriteLine "push a key to exit"

WaitKey
End Function



regards, frank

largo_winch
25-09-2012, 10:48
sorry for late reply. many thanks michael and frank for your help. I haven't found good solution for my problem, I can try to give an array to replace the strange restore or load data a chance :), bye, largo