Created
May 11, 2011 00:56
-
-
Save codebrainz/965705 to your computer and use it in GitHub Desktop.
Add basic support for loading binary files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 696cc02fa371f75390a6dc28f1551faf34759df0 Mon Sep 17 00:00:00 2001 | |
From: Matthew Brush <matthewbrush(at)gmail(dot)com> | |
Date: Tue, 10 May 2011 16:53:21 -0700 | |
Subject: [PATCH] Add basic support for loading binary files. | |
Read the entire file instead of only to the first nul byte, then, | |
if converting it to UTF-8 fails, assume it's binary, make it read-only, | |
warn the user and set the text in Scintilla anyway. | |
Add sci_set_text2() function (terrible name) to support setting | |
text in Scintilla with embedded nul bytes. | |
Allow defaulting to ENCODING_NONE if charset detected was NULL. | |
--- | |
src/document.c | 23 ++++++++++++----------- | |
src/encodings.c | 21 ++++++++++++--------- | |
src/sciwrappers.c | 10 ++++++++++ | |
3 files changed, 34 insertions(+), 20 deletions(-) | |
diff --git a/src/document.c b/src/document.c | |
index 971936b..0fecc69 100644 | |
--- a/src/document.c | |
+++ b/src/document.c | |
@@ -850,14 +850,13 @@ static gboolean load_text_file(const gchar *locale_filename, const gchar *displa | |
filedata->mtime = st.st_mtime; | |
- if (! g_file_get_contents(locale_filename, &filedata->data, NULL, &err)) | |
+ if (! g_file_get_contents(locale_filename, &filedata->data, &filedata->len, &err)) | |
{ | |
ui_set_statusbar(TRUE, "%s", err->message); | |
g_error_free(err); | |
return FALSE; | |
} | |
- filedata->len = (gsize) st.st_size; | |
if (! encodings_convert_to_utf8_auto(&filedata->data, &filedata->len, forced_enc, | |
&filedata->enc, &filedata->bom, &filedata->readonly)) | |
{ | |
@@ -872,21 +871,23 @@ static gboolean load_text_file(const gchar *locale_filename, const gchar *displa | |
_("The file \"%s\" does not look like a text file or the file encoding is not supported."), | |
display_filename); | |
} | |
- g_free(filedata->data); | |
- return FALSE; | |
+ | |
+ filedata->readonly = TRUE; | |
} | |
if (filedata->readonly) | |
{ | |
- const gchar *warn_msg = _( | |
- "The file \"%s\" could not be opened properly and has been truncated. " \ | |
- "This can occur if the file contains a NULL byte. " \ | |
- "Be aware that saving it can cause data loss.\nThe file was set to read-only."); | |
+ const gchar *short_msg = _("The file \"%s\" could not be opened " \ | |
+ "properly and the text shown may have been truncated.%s"); | |
+ | |
+ const gchar *warn_msg = _("This can occur if the file contains a " \ | |
+ "NULL byte. Be aware that saving it can cause data loss.\nThe " \ | |
+ "file was set to read-only."); | |
if (main_status.main_window_realized) | |
- dialogs_show_msgbox(GTK_MESSAGE_WARNING, warn_msg, display_filename); | |
+ dialogs_show_msgbox(GTK_MESSAGE_WARNING, short_msg, display_filename, warn_msg); | |
- ui_set_statusbar(TRUE, warn_msg, display_filename); | |
+ ui_set_statusbar(TRUE, short_msg, display_filename, ""); | |
} | |
return TRUE; | |
@@ -1200,7 +1201,7 @@ GeanyDocument *document_open_file_full(GeanyDocument *doc, const gchar *filename | |
/* add the text to the ScintillaObject */ | |
sci_set_readonly(doc->editor->sci, FALSE); /* to allow replacing text */ | |
- sci_set_text(doc->editor->sci, filedata.data); /* NULL terminated data */ | |
+ sci_set_text2(doc->editor->sci, filedata.data, filedata.len); /* NULL terminated data */ | |
queue_colourise(doc); /* Ensure the document gets colourised. */ | |
/* detect & set line endings */ | |
diff --git a/src/encodings.c b/src/encodings.c | |
index e128cf8..289684e 100644 | |
--- a/src/encodings.c | |
+++ b/src/encodings.c | |
@@ -301,17 +301,20 @@ void encodings_select_radio_item(const gchar *charset) | |
{ | |
gint i; | |
- g_return_if_fail(charset != NULL); | |
- | |
- i = 0; | |
- while (i < GEANY_ENCODINGS_MAX) | |
+ if (charset == NULL) | |
+ i = GEANY_ENCODING_NONE; | |
+ else | |
{ | |
- if (utils_str_equal(charset, encodings[i].charset)) | |
- break; | |
- i++; | |
+ i = 0; | |
+ while (i < GEANY_ENCODINGS_MAX) | |
+ { | |
+ if (utils_str_equal(charset, encodings[i].charset)) | |
+ break; | |
+ i++; | |
+ } | |
+ if (i == GEANY_ENCODINGS_MAX) | |
+ i = GEANY_ENCODING_UTF_8; /* fallback to UTF-8 */ | |
} | |
- if (i == GEANY_ENCODINGS_MAX) | |
- i = GEANY_ENCODING_UTF_8; /* fallback to UTF-8 */ | |
/* ignore_callback has to be set by the caller */ | |
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(radio_items[i]), TRUE); | |
diff --git a/src/sciwrappers.c b/src/sciwrappers.c | |
index b34fefa..0dab884 100644 | |
--- a/src/sciwrappers.c | |
+++ b/src/sciwrappers.c | |
@@ -180,6 +180,16 @@ void sci_add_text(ScintillaObject *sci, const gchar *text) | |
} | |
+void sci_set_text2(ScintillaObject *sci, const gchar *text, gsize len) | |
+{ | |
+ g_return_if_fail(sci != NULL); | |
+ g_return_if_fail(text != NULL); | |
+ | |
+ SSM(sci, SCI_CLEARALL, 0, 0); | |
+ SSM(sci, SCI_ADDTEXT, (uptr_t) len, (sptr_t) text); | |
+} | |
+ | |
+ | |
/** Sets all text. | |
* @param sci Scintilla widget. | |
* @param text Text. */ | |
-- | |
1.7.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment