1. Saving sequences
- Posted by znorq2 Jul 01, 2009
- 1083 views
Hi,
Is there a set of routines that saves and loads sequences? (Eu v3) I want to be able to save it in a binary file format, and then restore it in it's original form...
Let's say that I have the following code;
atom TRUE = (1=1) sequence mySeq, myNewSeq mySeq = {"This is a test", {9999999999,{1010111100011, {}},"blahblah"}, -1, TRUE} myNewSeq = {} saveSequence("sequencefile.bin", myTest) -- Just an example loadSequence("sequencefile.bin", myNewSeq) -- Just an example
Result: myNewSeq = {"This is a test", {9999999999,{1010111100011, {}},"blahblah"}, -1, TRUE}
Kenneth / ZNorQ
2. Re: Saving sequences
- Posted by znorq2 Jul 01, 2009
- 1095 views
Ups, a small bug in the code;
saveSequence("sequencefile.bin", myTest) -- Just an example
.. should be ..
saveSequence("sequencefile.bin", mySeq) -- Just an example
Kenneth / ZNorQ
3. Re: Saving sequences
- Posted by mattlewis (admin) Jul 01, 2009
- 1130 views
Is there a set of routines that saves and loads sequences? (Eu v3) I want to be able to save it in a binary file format, and then restore it in it's original form...
This is basically what EDS does. I used that in ooeu. You can get the code here:
http://ooeu.svn.sourceforge.net/viewvc/ooeu/trunk/sequencef.e
Just use the compress() and decompress() functions to turn the sequence into a stream of bytes and vice versa. This is essentially the same code available in the 4.0 standard library.
To read the sequence, you need to set current_db to the file handle. You can see how I wrapped decompress() for the files I needed to use. compress() simply returns the byte stream, which you can write to the file using puts(). Make sure that you're reading and writing in binary mode if you're on windows or DOS.
Matt
4. Re: Saving sequences
- Posted by m_sabal Jul 01, 2009
- 1118 views
The simplest method is print() and get(). If you want your file to be human readable, you could use pretty_print() instead. If you'd rather store your sequences in a database, Euphoria's database.e handles this natively. The library.doc file that comes with the Euphoria distribution has all the details and examples you need.
5. Re: Saving sequences
- Posted by bernie Jul 01, 2009
- 1092 views
You can also save sequences in ver4.0 using map functions.
6. Re: Saving sequences
- Posted by DerekParnell (admin) Jul 01, 2009
- 1066 views
You can also save sequences in ver4.0 using map functions.
Though the new serialization routine in v4.0 are even easier, IMHO.
dump(sequence data, sequence filename) data = load(sequence filename)
7. Re: Saving sequences
- Posted by znorq2 Jul 02, 2009
- 1101 views
Is there a set of routines that saves and loads sequences? (Eu v3) I want to be able to save it in a binary file format, and then restore it in it's original form...
This is basically what EDS does. I used that in ooeu. You can get the code here:
http://ooeu.svn.sourceforge.net/viewvc/ooeu/trunk/sequencef.e
Just use the compress() and decompress() functions to turn the sequence into a stream of bytes and vice versa. This is essentially the same code available in the 4.0 standard library.
To read the sequence, you need to set current_db to the file handle. You can see how I wrapped decompress() for the files I needed to use. compress() simply returns the byte stream, which you can write to the file using puts(). Make sure that you're reading and writing in binary mode if you're on windows or DOS.
Matt
Hi Matt,
Sorry, but I'm not quite following you on this one.. However, if I'm not mistaking, you're right about EDS storing the orginal structure of a sequence. I'm pretty familiar with EDS.
The simplest method is print() and get(). If you want your file to be human readable, you could use pretty_print() instead. If you'd rather store your sequences in a database, Euphoria's database.e handles this natively. The library.doc file that comes with the Euphoria distribution has all the details and examples you need.
Hi Sabal,
I thought print() and get() where only able to store/retrieve data in a flat format, meaning that the sequence structure would be lost? Am I wrong?
As for pretty_print(), I know of this, and it's not quite what I'm looking for as I want the data to be stored in a binary format that is compact and isn't easily readable. However, if I wanted to store the data using pretty_print() - how would I be able to read the data back in the original sequence structure without having to create my own routine?
These questions might seem abit silly, if so, you'll just have to forgive me... :p
You can also save sequences in ver4.0 using map functions.
Though the new serialization routine in v4.0 are even easier, IMHO.
dump(sequence data, sequence filename) data = load(sequence filename)
Sounds good, but unfortunately I'm still stuck at v3 @ work; I dare not change to v4 as I'm maintaining old apps there.
Kenneth / ZNorQ
8. Re: Saving sequences
- Posted by m_sabal Jul 02, 2009
- 1072 views
I thought print() and get() where only able to store/retrieve data in a flat format, meaning that the sequence structure would be lost? Am I wrong?
print() and get() work perfectly fine with sequence data, no matter how complex. Perhaps where you're getting confused is that print saves strings as a sequence of integers, instead of human readable text. get() is quite happy to read either string literals or sequences of integers. If the first character of your file is the open brace ({), and the last character is the close brace (}), you only need a single get() to retrieve the entire file. If the file was created with print(), that's what you'll see. I recommend experimenting with this a bit so you can see it for yourself. Remember that get() returns {GET_SUCCESS | GET_FAIL,data}.
The file created with print() is ASCII, and can easily be interpreted by anyone who understands the ASCII table. If the project requires compression and obfuscation, use database.e.
9. Re: Saving sequences
- Posted by doncole2009 Jul 02, 2009
- 1060 views
This is what I use to load and save binary files.
It could be a complicated sequence,text or anything.
include bget.e ------------------the binary way -------------------- global function get_file(sequence file_name)--binary file integer fn object database fn=exist(file_name) if fn=0 then --does not exist fn=open(file_name,"wb") end if fn = open(file_name, "rb") if fn=-1 then puts(1,"Can't open file:"& file_name) end if database = bget(fn) close(fn) if length(database)<2 then return {} else return database[2] end if end function global procedure save_file(sequence db,sequence file_name) integer fn fn = open(file_name, "wb") bprint(fn,db) close(fn) end procedure Don Cole
10. Re: Saving sequences
- Posted by znorq2 Jul 06, 2009
- 1060 views
If I only bothered to read the manual, I'd see that m_sabal's method is exactly what I'm looking for. I knew about print()/get(), but I didn't know that you could save/retrieve the sequences/objects in it's original state.
Thank you guys, you've been quite helpful.
Kenneth / ZNorQ.
11. Re: Saving sequences
- Posted by mraley2 Jul 24, 2009
- 1011 views
- Last edited Jul 25, 2009
Don't forget bget