Skip to content

Commit 11506a3

Browse files
committed
Much better binary->utf8 support
1 parent f0354de commit 11506a3

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

commands.c

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,34 @@ int open_file(char * filename, struct Document * document) {
2828
fread(contents, sizeof(char), len, f);
2929
fclose(f);
3030

31-
char * new = g_convert(contents, len, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
32-
31+
document->ro = FALSE;
32+
33+
if (g_utf8_validate(contents, len, NULL) == FALSE) {
34+
document->ro = TRUE;
35+
gsize read;
36+
gsize wrote;
37+
38+
char * new = g_convert(contents, len, "UTF-8", "ISO-8859-15", &read, &wrote, NULL);
39+
free(contents);
40+
contents = malloc(wrote);
41+
for (gsize x = 0; x < wrote; x++) {
42+
if(new[x] != 0) {
43+
contents[x] = new[x];
44+
}
45+
else {
46+
contents[x] = ' ';
47+
}
48+
}
49+
contents[wrote] = 0;
50+
free(new);
51+
}
3352

3453
// Insert file
35-
gtk_text_buffer_set_text(document->buffer, new, -1);
54+
gtk_text_buffer_set_text(document->buffer, contents, -1);
3655
gtk_text_buffer_set_modified(document->buffer, FALSE);
3756
strcpy(document->name, filename);
3857
filename_to_title(document);
3958
free(contents);
40-
free(new);
4159

4260
return 0;
4361
}
@@ -69,6 +87,13 @@ void new_command(void) {
6987
}
7088
}
7189

90+
void read_only_popup(struct Document * document) {
91+
GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT;
92+
GtkWidget * dialog = gtk_message_dialog_new (document->window, flags, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,"File is read only", NULL);
93+
gtk_dialog_run (GTK_DIALOG (dialog));
94+
gtk_widget_destroy (dialog);
95+
}
96+
7297
int save(struct Document * document) {
7398

7499
// Collect all text
@@ -91,6 +116,11 @@ int save(struct Document * document) {
91116

92117
void save_as_command(GtkWidget * self, struct Document * document) {
93118

119+
if (document->ro == TRUE) {
120+
read_only_popup(document);
121+
return;
122+
}
123+
94124
GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_SAVE;
95125
GtkWidget *dialog = gtk_file_chooser_dialog_new ("Save File", document->window, action, ("_Cancel"), GTK_RESPONSE_CANCEL, ("_Save"), GTK_RESPONSE_ACCEPT, NULL);
96126

@@ -118,6 +148,11 @@ void save_command(GtkWidget * self, struct Document * document) {
118148
return;
119149
}
120150

151+
if (document->ro == TRUE) {
152+
read_only_popup(document);
153+
return;
154+
}
155+
121156
save(document);
122157

123158
}

global.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
struct Document {
2-
char name[256];
3-
GtkTextBuffer * buffer;
4-
GtkWidget * view;
5-
GtkWindow * window;
6-
GtkSourceSearchContext * context;
2+
char name[256];
3+
GtkTextBuffer * buffer;
4+
GtkWidget * view;
5+
GtkWindow * window;
6+
GtkSourceSearchContext * context;
7+
int ro;
78
};

0 commit comments

Comments
 (0)