Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Tue Aug 20, 2019 9:00 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Converting VB Get/Put code using Random
PostPosted: Wed Nov 02, 2011 5:29 pm 
Offline

Joined: Sat Dec 04, 2010 9:14 pm
Posts: 918
Hello all,

I searched through the forum here and found some relevant posts already, so I tried the Binary stream Sample. Did not work. Can anyone tell me how to do a put then a get like this code:
Public Sub SaveFile()
Dim pathA As String
Dim root As String 'to save the root drive letter


On Error GoTo Make_root
pathA = App.path
root = Mid(pathA, 1, 1)
pathc = root & ":\Axcys Licenses\" & Trim$(LicenseData.SiteNo)
'pathc = "C:\Axcys Licenses\" & Trim$(LicenseData.SiteNo)
MkDir (pathc)

'AppPath = App.Path
SourceFile = pathc & "\SML.GBL"
Open SourceFile For Random As #3 Len = 500

' file Record where to put the data from the file
Put #3, 1, LicenseData.AllowInterface
Put #3, 2, LicenseData.Company
Put #3, 3, LicenseData.Demo
Put #3, 4, LicenseData.Expiration
Put #3, 5, LicenseData.Installed
Put #3, 6, LicenseData.License
Put #3, 7, LicenseData.SiteNo
Put #3, 8, LicenseData.StartDate
Put #3, 9, LicenseData.Units
Put #3, 10, LicenseData.ProductType
Put #3, 11, LicenseData.IsBackUpHost
Put #3, 12, LicenseData.AllowSiteLite
Close #3
Exit Sub


The gets are pretty much the same
SourceFile = "A:\SML.GBL"
Open SourceFile For Random As #5 Len = 500

' file Record where to put the data from the file
Get #5, 1, LicenseData.AllowInterface
Get #5, 2, LicenseData.Company
Get #5, 3, LicenseData.Demo
Get #5, 4, LicenseData.Expiration
Get #5, 5, LicenseData.Installed


Thank you!
Tim


Top
 Profile  
Reply with quote  
 Post subject: Re: Converting VB Get/Put code using Random
PostPosted: Wed Nov 02, 2011 6:14 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
To do fixed-length records, you simply set the Position of the BinaryStream and read or write. For Example:

bs = BinaryStream.Open(f, true)
bs.write LicenceData.AllowInterface + chr(0)
bs.position = 500
bs.write LicenseData.Company + chr(0)
bs.position = 1000
bs.write LicenseData.Demo + chr(0)
etc.

There are some minor issues to deal with such as the length and type of the data. I assumed these were strings so I added a null byte to effectively make them CStrings, or null-terminated strings.

If you can avoid fixed-length records at all, though, do so.


Top
 Profile  
Reply with quote  
 Post subject: Re: Converting VB Get/Put code using Random
PostPosted: Wed Nov 02, 2011 6:19 pm 
Offline

Joined: Mon May 31, 2010 5:35 am
Posts: 2412
Location: Netherlands
there is a class called filestream. that should help you i think.
and i have made an update for that. i will post link later.

_________________
SWORT™
- Windows Vista, Windows XP, Windows 7, Linux Mint 11 and Mac OSX Lion
- REAL Studio Enterprise(latest) and WEB Edition(latest)
- Plugins (MBS Complete, ToringoLib, Einhugur, VanHoek + Some open source plugins)
For Dutch User Groups ask me to enter!


Top
 Profile  
Reply with quote  
 Post subject: Re: Converting VB Get/Put code using Random
PostPosted: Thu Nov 03, 2011 12:13 am 
Offline

Joined: Sat Dec 04, 2010 9:14 pm
Posts: 918
Each element is part of a structure, and are of fixed length.

Believe it or not, I have never looked at the file itself. Seems that each entry has a total record length of 500 bytes, regardless of the length of data.

Is that what you are referring to in your code example? Read entry 1, then move pointer to the start of the next record? Read it, then move next and so on?


Thanks
Tim


Top
 Profile  
Reply with quote  
 Post subject: Re: Converting VB Get/Put code using Random
PostPosted: Thu Nov 03, 2011 12:40 am 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Yes, Get and Put deal with fixed length records, regardless of the size of data you put in them. You can calculate the position of the binarystream with

bs.Position = (RecordNumber - 1) * RecordSize

You can read them in any order.

Get and Put do some additional things behind the scenes, so if you want to duplicate them exactly, you should write a couple of methods and handle the various possibilities.

I would use a database.


Top
 Profile  
Reply with quote  
 Post subject: Re: Converting VB Get/Put code using Random
PostPosted: Thu Nov 03, 2011 12:44 am 
Offline

Joined: Mon May 31, 2010 5:35 am
Posts: 2412
Location: Netherlands
tseyfarth wrote:
Each element is part of a structure, and are of fixed length.

Believe it or not, I have never looked at the file itself. Seems that each entry has a total record length of 500 bytes, regardless of the length of data.

Is that what you are referring to in your code example? Read entry 1, then move pointer to the start of the next record? Read it, then move next and so on?


Thanks
Tim


ehmmz.

FileStream is a class which can do this:
- Add any type of String, Boolean, FolderItem, LocalFolderItem, Color and Integer to a binary file.
- The Index for each value is not important, this means if you set a string at index 5 and one at index 2 you can still read them even if there is no index of 1, 3, 4 etc..
- If you can write your data in the above types, then you can use FileStream.
- The file can be BaseEncoded fully, and BaseDecoded fully.
- it's possible to change the class and let's say encrypt the whole file.

The original FileStream class ships with Real Studio IDE in the examples folder.
I have made an update for this, which can open/save the file and do EncodeBase64 en DecodeBase64 by setting a boolean in the constructor or in the Save Method.

Some performance updates where done also.

You can download it here: http://swort.nl/realbasic/classes/FileStream.rbo.zip

_________________
SWORT™
- Windows Vista, Windows XP, Windows 7, Linux Mint 11 and Mac OSX Lion
- REAL Studio Enterprise(latest) and WEB Edition(latest)
- Plugins (MBS Complete, ToringoLib, Einhugur, VanHoek + Some open source plugins)
For Dutch User Groups ask me to enter!


Top
 Profile  
Reply with quote  
 Post subject: Re: Converting VB Get/Put code using Random
PostPosted: Thu Nov 03, 2011 12:57 am 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
If you're going to go to the trouble of implementing something like filestream, why not use a real database? Use a table of name/value pairs. The achilles heel of any slot based approach, be it FileStream or a fixed-length record, is that the semantics of the data is purely based on slot/record position. It is a very rigid structure.


Top
 Profile  
Reply with quote  
 Post subject: Re: Converting VB Get/Put code using Random
PostPosted: Thu Nov 03, 2011 1:59 am 
Offline

Joined: Mon May 31, 2010 5:35 am
Posts: 2412
Location: Netherlands
timhare wrote:
If you're going to go to the trouble of implementing something like filestream, why not use a real database? Use a table of name/value pairs. The achilles heel of any slot based approach, be it FileStream or a fixed-length record, is that the semantics of the data is purely based on slot/record position. It is a very rigid structure.


well when i pressed submit, the message above the message i posted before this was just about posted also.
Anyway i can agree with using a database for this purpose tim. Still the FileStream Class can be useful for others so if someone does a search they can find it too and maybe it will be good for them.

_________________
SWORT™
- Windows Vista, Windows XP, Windows 7, Linux Mint 11 and Mac OSX Lion
- REAL Studio Enterprise(latest) and WEB Edition(latest)
- Plugins (MBS Complete, ToringoLib, Einhugur, VanHoek + Some open source plugins)
For Dutch User Groups ask me to enter!


Top
 Profile  
Reply with quote  
 Post subject: Re: Converting VB Get/Put code using Random
PostPosted: Thu Nov 03, 2011 2:47 am 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
tseyfarth wrote:
Each element is part of a structure, and are of fixed length.

If that is the case, then you could simply read/write the entire thing in one go.

bs.write LicenseData.StringValue(false)

LicenseData.StringValue(false) = bs.read(LicenseData.Size)

It's still restrictive, but it gets the job done. Assuming, of course, that you can deviate from VB's Get/Put approach. It's not clear how much compatibility with that file format you need, or if you were just looking for equivalent functionality.

@swort: the FileStream class does look interesting. If nothing else, it's a good example of creating an easy to use api.


Top
 Profile  
Reply with quote  
 Post subject: Re: Converting VB Get/Put code using Random
PostPosted: Thu Nov 03, 2011 1:37 pm 
Offline

Joined: Sat Dec 04, 2010 9:14 pm
Posts: 918
This is creating/encoding a license key file. The app decodes the key file. There are several elements to it including an expiration date and other info. Have to make it somewhat backwards compatible with existing product in the field.

Thanks to both of you for your responses!
Tim


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group