LTP GCOV extension - code coverage report
Current view: directory - trunk/src - heraia_io.c
Test: coverage.info
Date: 2008-08-24 Instrumented lines: 43
Code covered: 88.4 % Executed lines: 38

       1                 : /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
       2                 : /*
       3                 :   io.c
       4                 :   io.c - input and output functions for heraia
       5                 :  
       6                 :   (C) Copyright 2005 - 2007 Olivier Delhomme
       7                 :   e-mail : heraia@delhomme.org
       8                 :   URL    : http://heraia.tuxfamily.org
       9                 :  
      10                 :   This program is free software; you can redistribute it and/or modify
      11                 :   it under the terms of the GNU General Public License as published by
      12                 :   the Free Software Foundation; either version 2, or  (at your option) 
      13                 :   any later version.
      14                 :  
      15                 :   This program is distributed in the hope that it will be useful,
      16                 :   but WITHOUT ANY WARRANTY;  without even the implied warranty of
      17                 :   MERCHANTABILITY  or  FITNESS FOR A PARTICULAR PURPOSE.  See the
      18                 :   GNU General Public License for more details.
      19                 :  
      20                 :   You should have received a copy of the GNU General Public License
      21                 :   along with this program; if not, write to the Free Software
      22                 :   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
      23                 : 
      24                 : #include "heraia_types.h"
      25                 : 
      26                 : static GladeXML *load_glade_xml_if_it_exists(char *file_to_load);
      27                 : 
      28                 : /**
      29                 :  *  Loads the file 'filename' to analyse and populates the
      30                 :  *  corresponfing structure 'main_window' as needed thus
      31                 :  *  main_window and filename must NOT be NULL pointers
      32                 :  */
      33                 : gboolean load_file_to_analyse(heraia_window_t *main_window, gchar *filename)
      34               2 : {
      35               2 :         struct stat *stat_buf = NULL;
      36               2 :         gboolean success = FALSE;
      37                 : 
      38               2 :         g_return_val_if_fail(filename != NULL, FALSE);
      39               2 :         g_return_val_if_fail(main_window != NULL, FALSE);
      40                 : 
      41               2 :         stat_buf = (struct stat *) g_malloc0 (sizeof(struct stat));
      42                 :         stat(filename, stat_buf);
      43                 : 
      44               2 :         log_message(main_window, G_LOG_LEVEL_DEBUG, "filename to load : %s", filename);
      45                 :         
      46               4 :         if (S_ISREG(stat_buf->st_mode) && stat_buf->st_size>0)
      47                 :                 {
      48                 : 
      49               2 :                         heraia_hex_document_new(main_window, filename); /* removes the old hexdocument and adds a new one */
      50                 : 
      51               2 :                         gtk_box_pack_start(GTK_BOX(heraia_get_widget(main_window->xmls->main, "vbox1")), 
      52                 :                                                            main_window->current_DW->current_hexwidget, TRUE, TRUE, 0);
      53                 :                         
      54               2 :                         gtk_widget_show(main_window->current_DW->current_hexwidget);
      55                 : 
      56               2 :                         log_message(main_window, G_LOG_LEVEL_DEBUG, "Hexwidget : %p", main_window->current_DW->current_hexwidget);
      57                 :                         
      58               2 :                         success = TRUE;
      59                 : 
      60               2 :                         if (main_window->filename != filename)
      61                 :                                 {
      62               1 :                                         if (main_window->filename != NULL)
      63                 :                                                 {
      64               0 :                                                         g_free(main_window->filename);
      65                 :                                                 }
      66               1 :                                         main_window->filename = g_strdup_printf("%s", filename);
      67                 :                                 }
      68                 :                 
      69                 :                         /* updating the window name */
      70               2 :                     update_main_window_name(main_window);
      71                 :                         
      72               2 :                         log_message(main_window, G_LOG_LEVEL_DEBUG, "file %s loaded !", main_window->filename);
      73                 :                                 
      74                 :                 } 
      75                 :         else
      76                 :                 {
      77               0 :                         if (S_ISREG(stat_buf->st_mode))
      78                 :                                 {
      79               0 :                                         log_message(main_window, G_LOG_LEVEL_WARNING, "The file %s is empty !\n", filename);
      80                 :                                 }
      81                 :                         else
      82                 :                                 {
      83               0 :                                         log_message(main_window, G_LOG_LEVEL_WARNING, "The file %s does not exist !\n", filename);
      84                 :                                 }
      85               0 :                         success = FALSE;
      86                 :                 }
      87                 :         
      88               2 :         g_free(stat_buf);
      89                 : 
      90               2 :         return success;
      91                 : }
      92                 : 
      93                 : 
      94                 : /** 
      95                 :  *  Checks if file_to_load exists and is valid and if possible, loads it
      96                 :  *  in the xml structure
      97                 :  */
      98                 : static GladeXML *load_glade_xml_if_it_exists(gchar *file_to_load)
      99                 : {
     100                 :         struct stat *stat_buf;
     101              14 :         GladeXML *xml = NULL;
     102                 : 
     103              14 :         stat_buf = (struct stat *) g_malloc0 (sizeof(struct stat));
     104                 : 
     105                 :         stat(file_to_load, stat_buf);
     106              14 :         if (S_ISREG(stat_buf->st_mode) && stat_buf->st_size>0)
     107                 :                 {
     108               2 :                         xml = glade_xml_new(file_to_load, NULL, NULL);
     109                 :                 }
     110                 :         else
     111                 :                 {
     112              12 :                         xml = NULL;
     113                 :                 }
     114                 : 
     115              14 :         g_free(stat_buf);
     116                 : 
     117              14 :         return xml;
     118                 : }
     119                 : 
     120                 : /* loads the glade xml file ('filename') that describes an interface,
     121                 :    tries all the paths defined in the location_list and put the definition
     122                 :    in the 'xml' variable
     123                 :  */
     124                 : GladeXML *load_glade_xml_file(GList *location_list, gchar *filename)
     125               2 : {       
     126               2 :         gchar *file_to_load = NULL;
     127               2 :         GList *list = g_list_first(location_list);
     128               2 :         GladeXML *xml = NULL;
     129                 : 
     130              18 :         while (list != NULL && xml == NULL)
     131                 :                 {
     132              14 :                         file_to_load =  g_build_filename((gchar *) list->data, filename, NULL);
     133                 : 
     134              14 :                         xml = load_glade_xml_if_it_exists(file_to_load);
     135                 : 
     136              14 :                         if (xml == NULL)
     137                 :                                 {
     138              12 :                                         list = list->next;   
     139                 :                                 }
     140              14 :                         g_free(file_to_load);
     141                 :                 }
     142                 :         
     143               2 :         return xml;
     144                 : }

Generated by: LTP GCOV extension version 1.6