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  * @fn gboolean load_file_to_analyse(heraia_window_t *main_window, gchar *filename)
00033  *  Loads the file 'filename' to analyse and populates the
00034  *  corresponfing structure 'main_window' as needed thus
00035  *  main_window and filename must NOT be NULL pointers
00036  * @param main_window : main structure (it must not be NULL)
00037  * @param filename : filename of the file to load (it must not be NULL)
00038  * @return TRUE if everything went ok, FALSE otherwise
00039  */
00040 gboolean load_file_to_analyse(heraia_window_t *main_window, gchar *filename)
00041 {
00042         struct stat *stat_buf = NULL;
00043         gboolean success = FALSE;
00044         GtkWidget *notebook = NULL;
00045         HERAIA_ERROR status = HERAIA_NOERR;
00046 
00047         g_return_val_if_fail(filename != NULL, FALSE);
00048         g_return_val_if_fail(main_window != NULL, FALSE);
00049 
00050         stat_buf = (struct stat *) g_malloc0 (sizeof(struct stat));
00051         stat(filename, stat_buf);
00052 
00053         log_message(main_window, G_LOG_LEVEL_DEBUG, "filename to load : %s", filename);
00054         
00055         if (S_ISREG(stat_buf->st_mode) && stat_buf->st_size>0)
00056                 {
00057 
00058                         status = heraia_hex_document_new(main_window, filename); /* removes the old hexdocument and adds a new one */
00059 
00060                         if (status == HERAIA_NOERR)
00061                         {
00062                                 gtk_box_pack_start(GTK_BOX(heraia_get_widget(main_window->xmls->main, "vbox1")), 
00063                                                                    main_window->current_DW->current_hexwidget, TRUE, TRUE, 0);
00064                         
00065                                 gtk_widget_show(main_window->current_DW->current_hexwidget);
00066 
00067                                 log_message(main_window, G_LOG_LEVEL_DEBUG, "Hexwidget : %p", main_window->current_DW->current_hexwidget);
00068                         
00069                                 success = TRUE;
00070 
00071                                 if (main_window->filename != filename)
00072                                         {
00073                                                 if (main_window->filename != NULL)
00074                                                 {
00075                                                         g_free(main_window->filename);
00076                                                 }
00077                                                 main_window->filename = g_strdup_printf("%s", filename);
00078                                         }
00079                 
00080                                 /* updating the window name and tab's name */
00081                                 update_main_window_name(main_window);
00082                                 set_notebook_tab_name(main_window);
00083                         
00084                                 /* Showing all the widgets */
00085                                 gtk_widget_set_sensitive(heraia_get_widget(main_window->xmls->main, "save"), TRUE);
00086                                 gtk_widget_set_sensitive(heraia_get_widget(main_window->xmls->main, "save_as"), TRUE);
00087                                 notebook = heraia_get_widget(main_window->xmls->main, "file_notebook");
00088                                 gtk_widget_show(notebook);
00089                         
00090                                 log_message(main_window, G_LOG_LEVEL_DEBUG, "file %s loaded !", main_window->filename);
00091                         }
00092                         else
00093                         {
00094                                 log_message(main_window, G_LOG_LEVEL_ERROR, "Error while trying to load file %s", main_window->filename);
00095                                 success = FALSE;
00096                         }
00097                         
00098                 } 
00099         else
00100                 {
00101                         if (S_ISREG(stat_buf->st_mode))
00102                                 {
00103                                         log_message(main_window, G_LOG_LEVEL_WARNING, "The file %s is empty !", filename);
00104                                 }
00105                         else
00106                                 {
00107                                         log_message(main_window, G_LOG_LEVEL_WARNING, "The file %s does not exist !", filename);
00108                                 }
00109                         success = FALSE;
00110                 }
00111         
00112         g_free(stat_buf);
00113 
00114         return success;
00115 }
00116 
00117 
00118 /** 
00119  * @fn GladeXML *load_glade_xml_if_it_exists(gchar *file_to_load)
00120  *  Checks if file_to_load exists and is valid and if possible, loads it
00121  *  in the xml structure
00122  * @param file_to_load : a filename of a possibly existing glade file
00123  * @return returns the GladeXML structure if any, NULL otherwise
00124  */
00125 static GladeXML *load_glade_xml_if_it_exists(gchar *file_to_load)
00126 {
00127         struct stat *stat_buf;
00128         GladeXML *xml = NULL;
00129 
00130         stat_buf = (struct stat *) g_malloc0 (sizeof(struct stat));
00131 
00132         stat(file_to_load, stat_buf);
00133         if (S_ISREG(stat_buf->st_mode) && stat_buf->st_size>0)
00134                 {
00135                         xml = glade_xml_new(file_to_load, NULL, NULL);
00136                 }
00137         else
00138                 {
00139                         xml = NULL;
00140                 }
00141 
00142         g_free(stat_buf);
00143 
00144         return xml;
00145 }
00146 
00147 /**
00148  * @fn GladeXML *load_glade_xml_file(GList *location_list, gchar *filename)
00149  *  loads the glade xml file ('filename') that describes an interface,
00150  *  tries all the paths defined in the location_list and put the definition
00151  *  in the 'xml' variable. A frontend to load_glade_xml_if_it_exists function
00152  * @param location_list : a Glist containing paths where we might found the file
00153  * @param filename : glade's file's name that we want to load (possibly)
00154  * @return returns the GladeXML structure if any, NULL otherwise
00155  */
00156 GladeXML *load_glade_xml_file(GList *location_list, gchar *filename)
00157 {       
00158         gchar *file_to_load = NULL;
00159         GList *list = g_list_first(location_list);
00160         GladeXML *xml = NULL;
00161 
00162         while (list != NULL && xml == NULL)
00163                 {
00164                         file_to_load =  g_build_filename((gchar *) list->data, filename, NULL);
00165 
00166                         xml = load_glade_xml_if_it_exists(file_to_load);
00167 
00168                         if (xml == NULL)
00169                                 {
00170                                         list = list->next;      
00171                                 }
00172                         g_free(file_to_load);
00173                 }
00174         
00175         return xml;
00176 }
00177 
00178 
00179 /**
00180  * @fn gboolean load_preference_file(heraia_window_t *main_window)
00181  *  Load the preference file
00182  * @param main_window : main structure
00183  * @return TRUE if everything went ok, FALSE otherwise
00184  */
00185 gboolean load_preference_file(heraia_window_t *main_window)
00186 {       
00187         if (main_window != NULL && main_window->prefs != NULL)
00188         {
00189                 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);
00190         }
00191         else
00192         {       
00193                 return FALSE;
00194         }
00195 }
00196 
00197 
00198 /**
00199  * @fn gboolean save_preferences_to_file(prefs_t *prefs)
00200  *  Saves the preferences to the file preferences
00201  * @param prefs : preferences (from prefs_t structure)
00202  * @return TRUE if everything went ok, FALSE otherwise
00203  */
00204 gboolean save_preferences_to_file(prefs_t *prefs)
00205 {
00206         gsize length = 0;
00207         gchar *contents = NULL;
00208         gboolean result = FALSE;
00209         
00210         if (prefs != NULL && prefs->file != NULL && prefs->filename != NULL)
00211         {
00212                 contents = g_key_file_to_data(prefs->file, &length, NULL);
00213                 result = g_file_set_contents(prefs->filename, contents, length, NULL);
00214                 g_free(contents);
00215         }
00216         
00217         return result;
00218 }
00219 
00220 
00221 

Generated on Sat Mar 14 13:44:29 2009 for Heraia by  doxygen 1.5.6