heraia_io.c

Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
00002 /*
00003   heraia_io.c
00004   heraia_io.c - input and output functions for heraia
00005 
00006   (C) Copyright 2005 - 2008 Olivier Delhomme
00007   e-mail : heraia@delhomme.org
00008   URL    : http://heraia.tuxfamily.org
00009 
00010   This program is free software; you can redistribute it and/or modify
00011   it under the terms of the GNU General Public License as published by
00012   the Free Software Foundation; either version 2, or  (at your option)
00013   any later version.
00014 
00015   This program is distributed in the hope that it will be useful,
00016   but WITHOUT ANY WARRANTY;  without even the implied warranty of
00017   MERCHANTABILITY  or  FITNESS FOR A PARTICULAR PURPOSE.  See the
00018   GNU General Public License for more details.
00019 
00020   You should have received a copy of the GNU General Public License
00021   along with this program; if not, write to the Free Software
00022   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
00023 /**
00024  * @file heraia_io.c
00025  * Here I want to see everything that deals with I/O, files, disk and so on.
00026  */
00027 #include <libheraia.h>
00028 
00029 static GladeXML *load_glade_xml_if_it_exists(char *file_to_load);
00030 
00031 /**
00032  *  Loads the file 'filename' to analyse and populates the
00033  *  corresponfing structure 'main_window' as needed thus
00034  *  main_window and filename must NOT be NULL pointers
00035  * @param main_window : main structure (it must not be NULL)
00036  * @param filename : filename of the file to load (it must not be NULL)
00037  * @return TRUE if everything went ok, FALSE otherwise
00038  */
00039 gboolean load_file_to_analyse(heraia_window_t *main_window, gchar *filename)
00040 {
00041         struct stat *stat_buf = NULL;
00042         gboolean success = FALSE;
00043 /*      GtkWidget *notebook = NULL; */
00044         doc_t* doc = NULL;
00045 
00046         g_return_val_if_fail(filename != NULL, FALSE);
00047         g_return_val_if_fail(main_window != NULL, FALSE);
00048 
00049         stat_buf = (struct stat *) g_malloc0 (sizeof(struct stat));
00050         stat(filename, stat_buf);
00051 
00052         log_message(main_window, G_LOG_LEVEL_DEBUG, "filename to load : %s", filename);
00053 
00054         if (S_ISREG(stat_buf->st_mode) && stat_buf->st_size>0)
00055                 {
00056 
00057                         doc = heraia_hex_document_new(main_window, filename); /* removes the old hexdocument and adds a new one */
00058 
00059                         if (doc != NULL)
00060                         {
00061                                 add_new_tab_in_main_window(main_window, doc);
00062 
00063                                 log_message(main_window, G_LOG_LEVEL_DEBUG, "Hexwidget : %p", doc->hex_widget);
00064 
00065                                 success = TRUE;
00066 
00067                                 /* updating the window name and tab's name */
00068                                 update_main_window_name(main_window);
00069                                 set_notebook_tab_name(main_window);
00070 
00071                                 /* Showing all the widgets */
00072                                 grey_main_widgets(main_window->xmls->main, FALSE);
00073 
00074                                 log_message(main_window, G_LOG_LEVEL_DEBUG, "file %s loaded !", filename);
00075                         }
00076                         else
00077                         {
00078                                 log_message(main_window, G_LOG_LEVEL_ERROR, "Error while trying to load file %s", filename);
00079                                 success = FALSE;
00080                         }
00081 
00082                 }
00083         else
00084                 {
00085                         if (S_ISREG(stat_buf->st_mode))
00086                                 {
00087                                         log_message(main_window, G_LOG_LEVEL_WARNING, "The file %s is empty !", filename);
00088                                 }
00089                         else
00090                                 {
00091                                         log_message(main_window, G_LOG_LEVEL_WARNING, "The file %s does not exist !", filename);
00092                                 }
00093                         success = FALSE;
00094                 }
00095 
00096         g_free(stat_buf);
00097 
00098         return success;
00099 }
00100 
00101 
00102 /**
00103  * @fn GladeXML *load_glade_xml_if_it_exists(gchar *file_to_load)
00104  *  Checks if file_to_load exists and is valid and if possible, loads it
00105  *  in the xml structure
00106  * @param file_to_load : a filename of a possibly existing glade file
00107  * @return returns the GladeXML structure if any, NULL otherwise
00108  */
00109 static GladeXML *load_glade_xml_if_it_exists(gchar *file_to_load)
00110 {
00111         struct stat *stat_buf;
00112         GladeXML *xml = NULL;
00113 
00114         stat_buf = (struct stat *) g_malloc0 (sizeof(struct stat));
00115 
00116         stat(file_to_load, stat_buf);
00117         if (S_ISREG(stat_buf->st_mode) && stat_buf->st_size>0)
00118                 {
00119                         xml = glade_xml_new(file_to_load, NULL, NULL);
00120                 }
00121         else
00122                 {
00123                         xml = NULL;
00124                 }
00125 
00126         g_free(stat_buf);
00127 
00128         return xml;
00129 }
00130 
00131 /**
00132  * @fn GladeXML *load_glade_xml_file(GList *location_list, gchar *filename)
00133  *  loads the glade xml file ('filename') that describes an interface,
00134  *  tries all the paths defined in the location_list and put the definition
00135  *  in the 'xml' variable. A frontend to load_glade_xml_if_it_exists function
00136  * @param location_list : a Glist containing paths where we might found the file
00137  * @param filename : glade's file's name that we want to load (possibly)
00138  * @return returns the GladeXML structure if any, NULL otherwise
00139  */
00140 GladeXML *load_glade_xml_file(GList *location_list, gchar *filename)
00141 {
00142         gchar *file_to_load = NULL;
00143         GList *list = g_list_first(location_list);
00144         GladeXML *xml = NULL;
00145 
00146         while (list != NULL && xml == NULL)
00147                 {
00148                         file_to_load =  g_build_filename((gchar *) list->data, filename, NULL);
00149 
00150                         xml = load_glade_xml_if_it_exists(file_to_load);
00151 
00152                         if (xml == NULL)
00153                                 {
00154                                         list = list->next;
00155                                 }
00156                         g_free(file_to_load);
00157                 }
00158 
00159         return xml;
00160 }
00161 
00162 
00163 /**
00164  * @fn gboolean load_preference_file(heraia_window_t *main_window)
00165  *  Load the preference file
00166  * @param main_window : main structure
00167  * @return TRUE if everything went ok, FALSE otherwise
00168  */
00169 gboolean load_preference_file(heraia_window_t *main_window)
00170 {
00171         if (main_window != NULL && main_window->prefs != NULL)
00172         {
00173                 return g_key_file_load_from_file(main_window->prefs->file, main_window->prefs->filename,  G_KEY_FILE_KEEP_COMMENTS & G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
00174         }
00175         else
00176         {
00177                 return FALSE;
00178         }
00179 }
00180 
00181 
00182 /**
00183  * @fn gboolean save_preferences_to_file(prefs_t *prefs)
00184  *  Saves the preferences to the file preferences
00185  * @param prefs : preferences (from prefs_t structure)
00186  * @return TRUE if everything went ok, FALSE otherwise
00187  */
00188 gboolean save_preferences_to_file(prefs_t *prefs)
00189 {
00190         gsize length = 0;
00191         gchar *contents = NULL;
00192         gboolean result = FALSE;
00193 
00194         if (prefs != NULL && prefs->file != NULL && prefs->filename != NULL)
00195         {
00196                 contents = g_key_file_to_data(prefs->file, &length, NULL);
00197                 result = g_file_set_contents(prefs->filename, contents, length, NULL);
00198                 g_free(contents);
00199         }
00200 
00201         return result;
00202 }

Generated on Tue Jun 30 23:18:17 2009 for Heraia by  doxygen 1.5.8