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 - 2011 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 GtkBuilder *load_xml_if_it_exists(char *file_to_load);
00030 
00031 
00032 /**
00033  *  Loads the file 'filename' to analyse and populates the
00034  *  corresponfing structure 'main_struct' as needed thus
00035  *  main_struct and filename must NOT be NULL pointers
00036  * @param main_struct : 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_struct_t *main_struct, gchar *filename)
00041 {
00042     struct stat *stat_buf = NULL;
00043     gboolean success = FALSE;
00044     doc_t* doc = NULL;
00045 
00046     g_return_val_if_fail(filename != NULL, FALSE);
00047     g_return_val_if_fail(main_struct != NULL, FALSE);
00048 
00049     stat_buf = (struct stat *) g_malloc0 (sizeof(struct stat));
00050     stat(filename, stat_buf);
00051 
00052     log_message(main_struct, G_LOG_LEVEL_DEBUG, Q_("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_struct, filename); /* Adds an new hexdocument */
00058 
00059             if (doc != NULL)
00060                 {
00061                     add_new_tab_in_main_window(main_struct, doc);
00062 
00063                     log_message(main_struct, G_LOG_LEVEL_DEBUG, Q_("Hexwidget : %p"), doc->hex_widget);
00064 
00065                     success = TRUE;
00066 
00067                     /* updating the window name */
00068                     update_main_window_name(main_struct);
00069 
00070                     /* Showing all the widgets (in the menu and such) */
00071                     grey_main_widgets(main_struct->xmls->main, FALSE);
00072 
00073                     log_message(main_struct, G_LOG_LEVEL_DEBUG, Q_("file %s loaded !"), filename);
00074                 }
00075             else
00076                 {
00077                     log_message(main_struct, G_LOG_LEVEL_ERROR, Q_("Error while trying to load file %s"), filename);
00078                     success = FALSE;
00079                 }
00080 
00081         }
00082     else
00083         {
00084             if (S_ISREG(stat_buf->st_mode))
00085                 {
00086                     log_message(main_struct, G_LOG_LEVEL_WARNING, Q_("The file %s is empty !"), filename);
00087                 }
00088             else
00089                 {
00090                     log_message(main_struct, G_LOG_LEVEL_WARNING, Q_("The file %s does not exist !"), filename);
00091                 }
00092             success = FALSE;
00093         }
00094 
00095     g_free(stat_buf);
00096 
00097     return success;
00098 }
00099 
00100 
00101 /**
00102  *  Checks if file_to_load exists and is valid and if possible, loads it
00103  *  in the xml structure
00104  * @param file_to_load : a filename of a possibly existing GtkBuilder file
00105  * @return returns the GtkBuilder XML structure if any, NULL otherwise
00106  */
00107 static GtkBuilder *load_xml_if_it_exists(gchar *file_to_load)
00108 {
00109     struct stat *stat_buf;
00110     GtkBuilder *xml = NULL;
00111 
00112     stat_buf = (struct stat *) g_malloc0 (sizeof(struct stat));
00113 
00114     stat(file_to_load, stat_buf);
00115     if (S_ISREG(stat_buf->st_mode) && stat_buf->st_size>0)
00116         {
00117             GError* error = NULL;
00118             xml = gtk_builder_new ();
00119 
00120             if (!gtk_builder_add_from_file(xml, file_to_load, &error))
00121                 {
00122                     g_warning (Q_("Couldn't load builder file: %s"), error->message);
00123                     g_error_free (error);
00124                 }
00125         }
00126     else
00127         {
00128             xml = NULL;
00129         }
00130 
00131     g_free(stat_buf);
00132 
00133     return xml;
00134 }
00135 
00136 
00137 /**
00138  *  loads the GtkBuilder xml file ('filename') that describes an interface,
00139  *  tries all the paths defined in the location_list and put the definition
00140  *  in the 'xml' variable. A frontend to load_xml_if_it_exists function
00141  * @param location_list : a Glist containing paths where we might found the file
00142  * @param filename : GtkBuilder filename that we want to load (possibly)
00143  * @return returns the GtkBuilder XML structure if any, NULL otherwise
00144  */
00145 GtkBuilder *load_xml_file(GList *location_list, gchar *filename)
00146 {
00147     gchar *file_to_load = NULL;
00148     GList *list = g_list_first(location_list);
00149     GtkBuilder *xml = NULL;
00150 
00151     while (list != NULL && xml == NULL)
00152         {
00153             file_to_load =  g_build_filename((gchar *) list->data, filename, NULL);
00154 
00155             xml = load_xml_if_it_exists(file_to_load);
00156 
00157             if (xml == NULL)
00158                 {
00159                     list = list->next;
00160                 }
00161             g_free(file_to_load);
00162         }
00163 
00164     return xml;
00165 }
00166 
00167 
00168 /**
00169  * Load the preference file from the disk
00170  * @param main_struct : main structure
00171  * @return TRUE if everything went ok, FALSE otherwise
00172  */
00173 gboolean load_preference_file(prefs_t *prefs)
00174 {
00175     if (prefs != NULL && prefs->file != NULL && prefs->filename != NULL)
00176         {
00177             return g_key_file_load_from_file(prefs->file, prefs->filename,  G_KEY_FILE_KEEP_COMMENTS & G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
00178         }
00179     else
00180         {
00181             return FALSE;
00182         }
00183 }
00184 
00185 
00186 /**
00187  * Saves the preferences to the file preferences
00188  * @param prefs : preferences (from prefs_t structure)
00189  * @return TRUE if everything went ok, FALSE otherwise
00190  */
00191 gboolean save_preferences_to_file(prefs_t *prefs)
00192 {
00193     gsize length = 0;
00194     gchar *contents = NULL;
00195     gboolean result = FALSE;
00196 
00197     if (prefs != NULL && prefs->file != NULL && prefs->filename != NULL)
00198         {
00199             contents = g_key_file_to_data(prefs->file, &length, NULL);
00200             result = g_file_set_contents(prefs->filename, contents, length, NULL);
00201             g_free(contents);
00202         }
00203 
00204     return result;
00205 }
Generated on Mon May 2 21:04:49 2011 for Heraia by  doxygen 1.6.3