From: Hugo Villeneuve Date: Sun, 26 Jan 2014 23:23:11 +0000 (-0500) Subject: Add better error checking when loading invalid hex files X-Git-Tag: v2.0.1~32 X-Git-Url: http://gitweb.hugovil.com/?a=commitdiff_plain;ds=sidebyside;h=d755f9638255472107ae445f4f82d8fcd3446b20;p=emu8051.git Add better error checking when loading invalid hex files Also replaced custom error message box with standard one. --- diff --git a/src/cli/emuconsole.c b/src/cli/emuconsole.c index a68acb5..a249cf4 100644 --- a/src/cli/emuconsole.c +++ b/src/cli/emuconsole.c @@ -21,6 +21,7 @@ #define _GNU_SOURCE /* For getline() */ #include +#include #include #include /* For isblank, toupper() */ #include "config.h" @@ -512,12 +513,17 @@ TooMuchParameters: int main(int argc, char **argv) { + int rc; + parse_command_line_options(argc, argv); cpu8051_init(); - if (options.filename != NULL) - LoadHexFile(options.filename); + if (options.filename != NULL) { + rc = LoadHexFile(options.filename); + if (rc == false) + exit(1); + } console_main(); diff --git a/src/common/hexfile.c b/src/common/hexfile.c index a84c117..b15f0e8 100644 --- a/src/common/hexfile.c +++ b/src/common/hexfile.c @@ -26,6 +26,7 @@ #include #include #include +#include #if STDC_HEADERS # include @@ -95,13 +96,21 @@ Ascii2Hex(char *istring, int length) return result; } -void +/* + * Return value: + * true: success + * false: failure + */ +int LoadHexFile(const char *filename) { int i, j, RecLength, LoadOffset, RecType, Data, Checksum; FILE *fp; int status; char line[HEXFILE_LINE_BUFFER_LEN]; + int valid = false; + + log_debug("LoadHexFile"); if (filename == NULL) log_fail("No file specified"); @@ -165,6 +174,7 @@ LoadHexFile(const char *filename) log_debug("hex record: data"); } else if (RecType == 1) { log_debug("hex record: End Of File"); + valid = true; goto close_file; } else { log_warn("hex record: Unsupported ($%02X)", RecType); @@ -175,4 +185,10 @@ close_file: status = fclose(fp); if (status != EXIT_SUCCESS) log_fail("Error closing hex file"); + + if (valid == false) { + log_err("Error parsing hex file"); + } + + return valid; } diff --git a/src/common/hexfile.h b/src/common/hexfile.h index 0f46527..4642ebc 100644 --- a/src/common/hexfile.h +++ b/src/common/hexfile.h @@ -31,7 +31,7 @@ asciihex2int(char *str); unsigned int Ascii2Hex(char *istring, int length); -void +int LoadHexFile(const char *filename); #endif /* HEXFILE_H */ diff --git a/src/gtk/emugtk.c b/src/gtk/emugtk.c index 72b7ea5..3374d62 100644 --- a/src/gtk/emugtk.c +++ b/src/gtk/emugtk.c @@ -41,6 +41,7 @@ #include "filemenu.h" #include "viewmenu.h" #include "helpmenu.h" +#include "messagebox.h" #include "regwin.h" #include "pgmwin.h" #include "memwin.h" @@ -577,19 +578,26 @@ AddMenuSeparator(GtkWidget *menu) void emugtk_new_file(char *file) { - emugtk_stop_running(); + int rc; - LoadHexFile(file); + emugtk_stop_running(); - if (cfg->clear_ram_on_file_load) - emugtk_Reset(); + rc = LoadHexFile(file); + if (rc == false) { + message_show_error("Error parsing hex file"); + } else { + if (cfg->clear_ram_on_file_load) + emugtk_Reset(); - emugtk_UpdateDisplay(); + emugtk_UpdateDisplay(); + } } int main(int argc, char **argv) { + int rc_load_hexfile = true; + parse_command_line_options(argc, argv); app_config_load(); @@ -600,13 +608,17 @@ main(int argc, char **argv) gtk_init(&argc, &argv); if (options.filename != NULL) - LoadHexFile(options.filename); + rc_load_hexfile = LoadHexFile(options.filename); cpu8051_Reset(); log_info("Init GUI"); emugtk_window_init(); emugtk_UpdateDisplay(); + + if (rc_load_hexfile == false) + message_show_error("Error parsing hex file"); + gtk_main(); log_info("Terminate"); diff --git a/src/gtk/messagebox.c b/src/gtk/messagebox.c index c4d3723..98ec275 100644 --- a/src/gtk/messagebox.c +++ b/src/gtk/messagebox.c @@ -28,71 +28,32 @@ #include "common.h" #include "messagebox.h" -#define MESSAGE_DIALOG_BORDER 25 - -#define BUTTON_TEXT_BORDER 3 - extern GtkWidget *mainwin; void -ShowMessage(gchar *title, gchar *message, int justification, int font_style) +message_show_error(char *message) { GtkWidget *dialog; - GtkWidget *label; - GtkWidget *label_window; - - /* Keep the dialog on top of the main window, and centered. */ - dialog = gtk_dialog_new_with_buttons( - title, GTK_WINDOW(mainwin), - GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_OK, - GTK_RESPONSE_NONE, NULL); - - /* - * The GtkLabel widget is one of a few GTK+ widgets that don't create - * their own window to render themselves into. Instead, they draw - * themselves directly onto their parents window. This means that in - * order to set a property for a GtkLabel widget, you need to change the - * property of its parent, i.e. the object that you pack it into. - * Another solution (short term workaround) is to put the label widget - * inside another widget that does get its own window, like the - * 'ViewPort' or 'EventBox' widget. - */ - - /* - * Using workaround described above to set the border width of 'label' - * widget. - */ - label_window = gtk_event_box_new(); - - /* Creating our label. */ - label = gtk_label_new(message); - gtk_label_set_justify(GTK_LABEL(label), justification); - - if (font_style == MESSAGE_DIALOG_FIXED_FONT) { - PangoFontDescription *pango_font; - pango_font = pango_font_description_from_string(FIXED_FONT); - gtk_widget_modify_font(label, pango_font); - } - - /* Adding label widget to label_window widget. */ - gtk_container_add(GTK_CONTAINER(label_window), label); - - /* - * Changing border width of the label widget by way of label_window - * widget. - */ - gtk_container_set_border_width(GTK_CONTAINER(label_window), - MESSAGE_DIALOG_BORDER); - - /* Ensure that the dialog box is destroyed when the user responds */ - g_signal_connect_swapped(dialog, "response", - G_CALLBACK(gtk_widget_destroy), dialog); + dialog = gtk_message_dialog_new(GTK_WINDOW(mainwin), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_ERROR, + GTK_BUTTONS_CLOSE, + message, NULL); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); +} - /* Add the label_window to the dialog window. */ - gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), - label_window); +void +message_show_information(char *message) +{ + GtkWidget *dialog; - /* Show everything we've added to the dialog. */ - gtk_widget_show_all(dialog); + dialog = gtk_message_dialog_new(GTK_WINDOW(mainwin), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_INFO, + GTK_BUTTONS_CLOSE, + message, NULL); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); } diff --git a/src/gtk/messagebox.h b/src/gtk/messagebox.h index 991e758..c78e003 100644 --- a/src/gtk/messagebox.h +++ b/src/gtk/messagebox.h @@ -24,10 +24,10 @@ #include -#define MESSAGE_DIALOG_NORMAL_FONT 0 -#define MESSAGE_DIALOG_FIXED_FONT 1 +void +message_show_error(char *message); void -ShowMessage(gchar *title, gchar *message, int justification, int font_style); +message_show_information(char *message); #endif /* MESSAGEBOX_H */ diff --git a/src/gtk/viewmenu.c b/src/gtk/viewmenu.c index b4ce153..fa1fd14 100644 --- a/src/gtk/viewmenu.c +++ b/src/gtk/viewmenu.c @@ -43,9 +43,8 @@ void toggle_layout(GtkWidget *widget, gpointer data) log_info(" Switching to layout %d", id); cfg->layout = id; - ShowMessage("Notice", - "You must restart for the changes to take effect", - GTK_JUSTIFY_LEFT, MESSAGE_DIALOG_NORMAL_FONT); + message_show_information( + "You must restart for the changes to take effect"); } }