1. Saving sequences

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

new topic     » topic index » view message » categorize

2. Re: Saving sequences

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

new topic     » goto parent     » topic index » view message » categorize

3. Re: Saving sequences

znorq2 said...

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

new topic     » goto parent     » topic index » view message » categorize

4. Re: Saving sequences

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.

new topic     » goto parent     » topic index » view message » categorize

5. Re: Saving sequences

You can also save sequences in ver4.0 using map functions.

new topic     » goto parent     » topic index » view message » categorize

6. Re: Saving sequences

bernie said...

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) 
new topic     » goto parent     » topic index » view message » categorize

7. Re: Saving sequences

mattlewis said...
znorq2 said...

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.

m_sabal said...

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

DerekParnell said...
bernie said...

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

new topic     » goto parent     » topic index » view message » categorize

8. Re: Saving sequences

znorq2 said...

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.

new topic     » goto parent     » topic index » view message » categorize

9. Re: Saving sequences

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
new topic     » goto parent     » topic index » view message » categorize

10. Re: Saving sequences

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.

new topic     » goto parent     » topic index » view message » categorize

11. Re: Saving sequences

Don't forget bget

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu