Saving and Restoring a GTK:TreeModel

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

Here is a small snippet of my code that saves and restores a
Gtk::Treemodel to/from a file. Just because i searched a while, before i
decided, i had to do it all by myself

The code is without any error handling, is tailored for a special
TreeStore and saves a model with two columns( string, string), which
should be no problem to change, if more columns or other datatypes
occure.

class Tagtree

def self.store treemodel, file_name
file = File.new(file_name,‘w+’)
treemodel.each do |model, path, iter|
file.puts path.to_s << “|” << iter[0] << “|” << iter[1]
end
file.close
end

def self.load model, file_name
anchors=Array.new
olditer, depth =nil, 0
IO.foreach(file_name) do |line|
fields=line.split(’|’)
position=fields[0].split(’:’)
anchors[depth] = olditer
entry=model.append(anchors[position.length-1])
entry[0]=fields[1]
entry[1]=fields[2]
depth, olditer = position.length, entry
end
end
end

Usage:

Tagtree.store(treemodel,“filetostore”)
Tagtree.loade(treemodel,“filetostore”)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkvAprkACgkQjsqVO1/Pdpw5GgCgp4sxA+du04rlmKlTclM/9XmB
W7YAoKb16OjNaAICIE/si2HoL+dBdXlg
=4YI4
-----END PGP SIGNATURE-----

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs