/* * Copyright (C) 2004 Tamara Roberson * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ using System; using System.Runtime.InteropServices; namespace Muine { public class Database { // Static // Static :: Methods // Static :: Methods :: Pack // Static :: Methods :: Pack :: PackStart [DllImport ("libmuine")] private static extern IntPtr db_pack_start (); /// /// Get a location in memory where we can pack values. /// /// /// An public static IntPtr PackStart () { return db_pack_start (); } // Static :: Methods :: Pack :: PackEnd [DllImport ("libmuine")] private static extern IntPtr db_pack_end (IntPtr p, out int length); /// /// Finish packing. /// /// /// An to where the last value ends. /// /// /// Location to store the length of the data that has been /// packed. /// /// /// An to the end of the packed values. /// public static IntPtr PackEnd (IntPtr p, out int length) { return db_pack_end (p, out length); } // Static :: Methods :: Pack :: PackPixbuf // TODO: Take a Gdk.Pixbuf as an argument, rather than // its handle. [DllImport ("libmuine")] private static extern void db_pack_pixbuf (IntPtr p, IntPtr pixbuf); /// /// Pack a so it can be stored in /// the database. /// /// /// An to where the value should be stored. /// /// /// An to the . /// public static void PackPixbuf (IntPtr p, IntPtr pixbuf) { db_pack_pixbuf (p, pixbuf); } // Static :: Methods :: Pack :: PackString [DllImport ("libmuine")] private static extern void db_pack_string (IntPtr p, string str); /// /// Pack a so it can be stored in /// the database. /// /// /// The string is packed as: /// length + string + null. /// /// /// An to where the value should be stored. /// /// /// A . /// public static void PackString (IntPtr p, string str) { db_pack_string (p, str); } // Static :: Methods :: Pack :: PackStringArray /// /// Pack an array of strings so they /// can be stored in the database. /// /// /// The array is packed as: /// length + strings. /// Note that each string ends with a null character. /// /// /// An to where the value should be stored. /// /// /// An array of strings. /// public static void PackStringArray (IntPtr p, string [] array) { Database.PackInt (p, array.Length); foreach (string s in array) Database.PackString (p, s); } // Static :: Methods :: Pack :: PackInt [DllImport ("libmuine")] private static extern void db_pack_int (IntPtr p, int i); /// /// Pack an integer so it can be /// stored in the database. /// /// /// An to where the value should be stored. /// /// /// An integer. /// public static void PackInt (IntPtr p, int i) { db_pack_int (p, i); } // Static :: Methods :: Pack :: PackBool [DllImport ("libmuine")] private static extern void db_pack_bool (IntPtr p, bool b); /// /// Pack a so it can be stored in /// the database. /// /// /// An to where the value should be stored. /// /// /// A . /// public static void PackBool (IntPtr p, bool b) { db_pack_bool (p, b); } // Static :: Methods :: Pack :: PackDouble [DllImport ("libmuine")] private static extern void db_pack_double (IntPtr p, double d); /// /// Pack a so it can be stored in /// the database. /// /// /// An to where the value should be stored. /// /// /// A . /// public static void PackDouble (IntPtr p, double d) { db_pack_double (p, d); } // Static :: Methods :: Unpack // Static :: Methods :: Unpack :: UnpackBool [DllImport ("libmuine")] private static extern IntPtr db_unpack_bool (IntPtr p, out bool b); /// /// Unpack a from the database. /// /// /// An to where the value is stored. /// /// /// Location to store the unpacked value. /// /// /// An to where the end of the value /// is stored. /// public static IntPtr UnpackBool (IntPtr p, out bool b) { return db_unpack_bool (p, out b); } // Static :: Methods :: Unpack :: UnpackDouble [DllImport ("libmuine")] private static extern IntPtr db_unpack_double (IntPtr p, out double d); /// /// Unpack a from the database. /// /// /// An to where the value is stored. /// /// /// Location to store the unpacked value. /// /// /// An to where the end of the value /// is stored. /// public static IntPtr UnpackDouble (IntPtr p, out double d) { return db_unpack_double (p, out d); } // Static :: Methods :: Unpack :: UnpackInt [DllImport ("libmuine")] private static extern IntPtr db_unpack_int (IntPtr p, out int i); /// /// Unpack an integer from the database. /// /// /// An to where the value is stored. /// /// /// Location to store the unpacked value. /// /// /// An to where the end of the value is stored. /// public static IntPtr UnpackInt (IntPtr p, out int i) { return db_unpack_int (p, out i); } // Static :: Methods :: Unpack :: UnpackPixbuf // TODO: Return a Gdk.Pixbuf in the out parameter. [DllImport ("libmuine")] private static extern IntPtr db_unpack_pixbuf (IntPtr p, out IntPtr pixbuf); /// /// Unpack a from the database. /// /// /// A to where the value is stored. /// /// /// Location to store the to the /// unpacked . /// /// /// An to where the end of the value /// is stored. /// public static IntPtr UnpackPixbuf (IntPtr p, out IntPtr pixbuf) { return db_unpack_pixbuf (p, out pixbuf); } // Static :: Methods :: Unpack :: UnpackString // TODO: Merge the second overload into the first one since // that is the only place that uses it. [DllImport ("libmuine")] private static extern IntPtr db_unpack_string (IntPtr p, out IntPtr str_ptr); /// /// Unpack a from the database. /// /// /// An to where the value is stored. /// /// /// Location to store the unpacked value. /// /// /// An to where the end of the value /// is stored. /// public static IntPtr UnpackString (IntPtr p, out string str) { IntPtr str_ptr; IntPtr ret = UnpackString (p, out str_ptr); str = GLib.Marshaller.PtrToStringGFree (str_ptr); return ret; } /// /// Unpack a from the database. /// /// /// An to where the value is stored. /// /// /// Location to store the to the /// unpacked value. /// /// /// An to where the end of the value /// is stored. /// public static IntPtr UnpackString (IntPtr p, out IntPtr str_ptr) { return db_unpack_string (p, out str_ptr); } // Static :: Methods :: Unpack :: UnpackStringArray /// /// Unpack an array of strings from /// the database. /// /// /// An to where the value is stored. /// /// /// Location to store the unpacked value. /// /// /// An to where the end of the value /// is stored. /// public static IntPtr UnpackStringArray (IntPtr p, out string [] array) { IntPtr ret = p; int len; ret = Database.UnpackInt (ret, out len); array = new string [len]; for (int i = 0; i < len; i++) ret = Database.UnpackString (ret, out array [i]); return ret; } // Delegates public delegate void DecodeFunctionDelegate (string key, IntPtr data); // Variables private IntPtr db_ptr; // Constructor [DllImport ("libmuine")] private static extern IntPtr db_open (string filename, int version, out IntPtr error); /// /// Creates a new object. /// /// /// The location of the database. /// /// /// The version of the database which we support. /// /// /// Thrown if the database cannot be opened. /// public Database (string filename, int version) { IntPtr error_ptr; db_ptr = db_open (filename, version, out error_ptr); if (db_ptr == IntPtr.Zero) { string msg = GLib.Marshaller.PtrToStringGFree (error_ptr); throw new Exception (msg); } } // Properties // Properties :: Handle (get;) /// /// Contains the handle of the /// database object. /// /// /// This is used to communicate with libmuine. /// public IntPtr Handle { get { return db_ptr; } } // Methods // Methods :: Public // Methods :: Public :: Load [DllImport ("libmuine")] private static extern void db_foreach (IntPtr db_ptr, DecodeFunctionDelegate decode_function, IntPtr data); /// /// Load the database. /// /// /// The delegate used to decode the database. /// public void Load (DecodeFunctionDelegate decode_function) { db_foreach (db_ptr, decode_function, IntPtr.Zero); } // Methods :: Public :: Store [DllImport ("libmuine")] private static extern void db_store (IntPtr db_ptr, string key, bool overwrite, IntPtr data, int data_size); /// /// Store a key-value pair in the database. If the key /// already exists, do not overwrite. /// /// /// The key. /// /// /// An to where the data is stored. /// /// /// The size of the data to store. /// public void Store (string key, IntPtr data, int data_size) { Store (key, data, data_size, false); } /// /// Store a key-value pair in the database. /// /// /// The key. /// /// /// An to where the data is stored. /// /// /// The size of the data to store. /// /// /// Whether to overwrite if the key already exists. /// public void Store (string key, IntPtr data, int data_size, bool overwrite) { db_store (db_ptr, key, overwrite, data, data_size); } // Methods :: Public :: Delete [DllImport ("libmuine")] private static extern void db_delete (IntPtr db_ptr, string key); /// /// Remove a key-value pair from the database. /// /// /// The key to remove. /// public void Delete (string key) { db_delete (db_ptr, key); } } }