[ghex] Use glib functions for figuring out the document basename



commit 5ab7fc457cbd4a1bb9ffd1cbb4e4d56ed83f9b0c
Author: Kalev Lember <kalevlember gmail com>
Date:   Sat Feb 18 19:10:26 2012 +0200

    Use glib functions for figuring out the document basename
    
    This has a better chance of working on platforms that don't use / as a
    path separator.

 src/ghex-window.c  |   11 ++---------
 src/hex-document.c |   11 +++--------
 src/ui.c           |   16 +++++++++-------
 3 files changed, 14 insertions(+), 24 deletions(-)
---
diff --git a/src/ghex-window.c b/src/ghex-window.c
index e4e502f..035eb74 100644
--- a/src/ghex-window.c
+++ b/src/ghex-window.c
@@ -1261,7 +1261,6 @@ ghex_window_save_as(GHexWindow *win)
 	if(resp == GTK_RESPONSE_OK) {
 		FILE *file;
 		gchar *flash;
-		int i;
         gchar *gtk_file_name, *path_end;
 
         if(access(filename, F_OK) == 0) {
@@ -1296,19 +1295,13 @@ ghex_window_save_as(GHexWindow *win)
                     doc->changed = FALSE;
                     win->changed = FALSE;
 
-                    for(i = strlen(doc->file_name);
-                        (i >= 0) && (doc->file_name[i] != '/');
-                        i--)
-                        ;
-                    if(doc->file_name[i] == '/')
-                        path_end = &doc->file_name[i+1];
-                    else
-                        path_end = doc->file_name;
+                    path_end = g_path_get_basename (doc->file_name);
                     doc->path_end = g_filename_to_utf8(path_end, -1, NULL, NULL, NULL);
                     ghex_window_set_doc_name(win, doc->path_end);
                     gtk_file_name = g_filename_to_utf8(doc->file_name, -1, NULL, NULL, NULL);
                     flash = g_strdup_printf(_("Saved buffer to file %s"), gtk_file_name);
                     ghex_window_flash(win, flash);
+                    g_free(path_end);
                     g_free(gtk_file_name);
                     g_free(flash);
                 }
diff --git a/src/hex-document.c b/src/hex-document.c
index d18bdbd..de927d5 100644
--- a/src/hex-document.c
+++ b/src/hex-document.c
@@ -444,7 +444,6 @@ hex_document_new_from_file(const gchar *name)
 {
 	HexDocument *doc;
 	gchar *path_end;
-	int i;
 
 	doc = HEX_DOCUMENT (g_object_new (hex_document_get_type(), NULL));
 	g_return_val_if_fail (doc != NULL, NULL);
@@ -456,14 +455,10 @@ hex_document_new_from_file(const gchar *name)
 		doc->buffer = (guchar *)g_malloc(doc->buffer_size);
 
 		/* find the start of the filename without path */
-		for(i = strlen(doc->file_name); (i >= 0) && (doc->file_name[i] != '/'); i--)
-			;
-		if(doc->file_name[i] == '/')
-			path_end = &doc->file_name[i+1];
-		else
-			path_end = doc->file_name;
-
+		path_end = g_path_get_basename (doc->file_name);
 		doc->path_end = g_filename_to_utf8 (path_end, -1, NULL, NULL, NULL);
+		g_free (path_end);
+
 		if(hex_document_read(doc)) {
 			doc_list = g_list_append(doc_list, doc);
 			return doc;
diff --git a/src/ui.c b/src/ui.c
index bf2c08e..3ab12f3 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -428,24 +428,22 @@ export_html_cb (GtkAction *action,
 	resp = gtk_dialog_run(GTK_DIALOG(file_sel));
 
 	if(resp == GTK_RESPONSE_OK) {
-		gchar *html_path = g_strdup(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(file_sel)));
+		gchar *html_path;
 		gchar *sep, *base_name, *check_path;
 		GtkHex *view = win->gh;
 
+		html_path = g_path_get_dirname (gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (file_sel)));
+		base_name = g_path_get_basename (gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (file_sel)));
+
 		gtk_widget_destroy(file_sel);
 
-		sep = html_path + strlen(html_path) - 1;
-		while(sep >= html_path && *sep != '/')
-			sep--;
-		if(sep >= html_path)
-			*sep = 0;
-		base_name = sep + 1;
 		sep = strstr(base_name, ".htm");
 		if(sep)
 			*sep = 0;
 
 		if(*base_name == 0) {
 			g_free(html_path);
+			g_free(base_name);
 			display_error_dialog(win, _("You need to specify a base name for "
 										"the HTML files."));
 			return;
@@ -459,6 +457,7 @@ export_html_cb (GtkAction *action,
 			if(access(check_path, W_OK) != 0) {
 				display_error_dialog(win, _("You don't have the permission to write to the selected path.\n"));
 				g_free(html_path);
+				g_free(base_name);
 				g_free(check_path);
 				return;
 			}
@@ -475,6 +474,7 @@ export_html_cb (GtkAction *action,
 			gtk_widget_destroy(mbox);
 			if(reply != GTK_RESPONSE_YES) {
 				g_free(html_path);
+				g_free(base_name);
 				g_free(check_path);
 				return;
 			}
@@ -483,6 +483,7 @@ export_html_cb (GtkAction *action,
 			if(access(html_path, W_OK) != 0) {
 				display_error_dialog(win, _("You don't have the permission to write to the selected path.\n"));
 				g_free(html_path);
+				g_free(base_name);
 				g_free(check_path);
 				return;
 			}
@@ -492,6 +493,7 @@ export_html_cb (GtkAction *action,
 		hex_document_export_html(doc, html_path, base_name, 0, doc->file_size,
 								 view->cpl, view->vis_lines, view->group_type);
 		g_free(html_path);
+		g_free(base_name);
 	}
 	else
 		gtk_widget_destroy(GTK_WIDGET(file_sel));



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]