/* Katoob * Copyright (c) 2002,2003 Arabeyes, Mohammed Sameer. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifdef HAVE_CONFIG_H #include #endif #include "katoob.h" #include /* This must be before file.h "FILE" */ #include /* atoi() */ #include /* access() */ #include #include #include /* strlen() */ #include #include "file.h" #include "encodings.h" #include "misc.h" static gboolean katoob_strip_codes (gchar * src, gchar ** dist); File * katoob_create_file_selection (gchar * title) { extern UI *katoob; File *f; GtkWidget *file_selector; GtkWidget *ehbox; GtkWidget *encodingl; GtkWidget *cbox; GtkFileSelection *fs; extern conf *config; katoob_debug (__FUNCTION__); f = g_malloc (sizeof (File)); file_selector = gtk_file_selection_new (title); fs = GTK_FILE_SELECTION (file_selector); ehbox = gtk_hbox_new (FALSE, 0); gtk_widget_show (ehbox); gtk_box_pack_end (GTK_BOX (fs->action_area), ehbox, TRUE, TRUE, 0); encodingl = gtk_label_new (_("Encoding")); gtk_widget_show (encodingl); gtk_box_pack_start (GTK_BOX (ehbox), encodingl, FALSE, FALSE, 0); gtk_misc_set_padding (GTK_MISC (encodingl), 20, 0); cbox = gtk_combo_new (); gtk_combo_set_popdown_strings (GTK_COMBO (cbox), config->enc); gtk_combo_set_value_in_list (GTK_COMBO (cbox), TRUE, FALSE); gtk_combo_set_case_sensitive (GTK_COMBO (cbox), TRUE); gtk_entry_set_text (GTK_ENTRY (GTK_COMBO (cbox)->entry), katoob_encodings_get_name_by_number (config->defenc)); gtk_widget_show (cbox); gtk_box_pack_start (GTK_BOX (ehbox), cbox, TRUE, TRUE, 0); gtk_window_set_transient_for (GTK_WINDOW (file_selector), GTK_WINDOW (katoob->win)); f->result = gtk_dialog_run (GTK_DIALOG (file_selector)); f->encoding = katoob_encodings_get_name_from_string ((gchar *) gtk_entry_get_text (GTK_ENTRY (GTK_COMBO (cbox)->entry))); f->file = g_strdup (gtk_file_selection_get_filename (GTK_FILE_SELECTION (file_selector))); gtk_widget_destroy (file_selector); return f; } /* TODO: WE SHOULD OPEN THE FILE AFTER CONVERTING THE ENCODING! */ gboolean katoob_write (gchar * buff, gint encoding, FILE * fp, gchar * file) { GError *error = NULL; gint e; katoob_debug (__FUNCTION__); if ((e = strcmp (katoob_encodings_get_by_number (encoding), "UTF-8")) == 0) { katoob_debug ("UTF-8 encoding."); if (!katoob_real_write (buff, fp, file)) { katoob_debug ("Failed to write!"); return FALSE; } return TRUE; } else { gchar *final_buff = NULL; if (!katoob_strip_codes (buff, &final_buff)) { katoob_debug ("Failed to strip!"); return FALSE; } if ((e = strcmp (katoob_encodings_get_by_number (encoding), "PLAIN TEXT")) == 0) { if (!katoob_real_write (final_buff, fp, file)) { katoob_debug ("Failed to write!"); return FALSE; } g_free (final_buff); return TRUE; } else { gchar *b; b = g_convert (final_buff, strlen (final_buff), katoob_encodings_get_by_number (encoding), "UTF-8", NULL, NULL, &error); g_free (final_buff); if (!b) { katoob_error (error->message); g_error_free (error); katoob_debug ("Failed to convert!"); return FALSE; } if (!katoob_real_write (b, fp, file)) { g_free (b); return FALSE; } g_free (b); return TRUE; } } return FALSE; } gboolean katoob_create_file_if_required (gchar * file) { FILE *infile; gchar *_tmp; katoob_debug (__FUNCTION__); infile = fopen (file, "r"); if (!infile) { if (errno == ENOENT) { /* No such file or directory */ gint x; _tmp = g_strdup_printf (_("The file %s doesn't exist, Create it?"), file); x = katoob_create_question (_tmp); g_free (_tmp); switch (x) { case GTK_RESPONSE_YES: { if ((infile = fopen (file, "w+")) == NULL) { _tmp = g_strdup_printf (_("Couldn't create file %s, %s"), file, strerror (errno)); katoob_error (_tmp); g_free (_tmp); return FALSE; } break; } case GTK_RESPONSE_NO: { return FALSE; } } } else { _tmp = g_strdup_printf (_("Couldn't open file %s, %s"), file, strerror (errno)); katoob_error (_tmp); g_free (_tmp); return FALSE; } } fclose (infile); return TRUE; } gboolean katoob_check_file_access (gchar * file) { gint i = 0; katoob_debug (__FUNCTION__); if ((i = access (file, W_OK)) != 0) { /* write permission denied */ return FALSE; } else { return TRUE; } } gboolean katoob_file_get_contents (gchar * file, gchar ** content) { FILE *infile; gchar *_tmp; struct stat fstat; gint i = 0; gchar *buf = NULL; *content = buf; katoob_debug (__FUNCTION__); if (stat (file, &fstat)) /* failed! return -1; */ { katoob_error ((gchar *) g_strerror (errno)); return FALSE; } if (fstat.st_size == 0) { return TRUE; } infile = fopen (file, "r"); if (!infile) { katoob_error ((gchar *) g_strerror (errno)); return FALSE; } buf = g_malloc (fstat.st_size + 1); i = fread (buf, fstat.st_size, 1, infile); if (i != 1) { _tmp = g_strdup_printf (_("An error has occured while reading file %s"), file); katoob_error (_tmp); g_free (_tmp); fclose (infile); return FALSE; } fclose (infile); buf[fstat.st_size] = '\0'; *content = buf; return TRUE; } gboolean katoob_real_write (gchar * buff, FILE * fp, gchar * file) { gchar *_tmp; katoob_debug (__FUNCTION__); if (strlen (buff) == 0) { katoob_debug ("Empty buffer."); fclose (fp); return TRUE; } if (fwrite (buff, strlen (buff), 1, fp) < 1) { _tmp = g_strdup_printf ("An error has occured while writing to file %s\n%s", file, g_strerror (errno)); katoob_error (_tmp); g_free (_tmp); fclose (fp); return FALSE; } fclose (fp); return TRUE; } static gboolean katoob_strip_codes (gchar * src, gchar ** dist) { gunichar *ch; gunichar *buf; gchar *final_buff; glong len; glong i = 0; glong x = 0; GError *error = NULL; katoob_debug (__FUNCTION__); ch = g_utf8_to_ucs4_fast (src, strlen (src), &len); buf = g_malloc (sizeof (gunichar) * len); while (1) { if (i == len) { break; } switch (ch[i]) { case 0x200e: i++; break; case 0x200f: i++; break; case 0x202a: i++; break; case 0x202b: i++; break; case 0x202d: i++; break; case 0x202e: i++; break; case 0x202c: i++; break; case 0x200b: i++; break; case 0x200d: i++; break; case 0x200c: i++; break; default: { buf[x] = ch[i]; x++; i++; } } } final_buff = g_ucs4_to_utf8 (buf, x, NULL, NULL, &error); g_free (ch); g_free (buf); if (!final_buff) { katoob_error (error->message); g_error_free (error); return FALSE; } *dist = final_buff; return TRUE; }