예제 #1
0
/**
 * Fill in all graphical elements of preview page with data from a CSV
 * file.
 *
 * \param assistant	Assistant that contains the page.
 *
 * \return		FALSE
 */
gboolean import_enter_csv_preview_page ( GtkWidget * assistant )
{
    GtkWidget * entry;
    GSList * files;
    gchar *contents, *tmp_str, *filename = NULL;
    gsize size;
    gsize bytes_written;
    GError * error;
    struct imported_file * imported = NULL;

    /* Find first CSV to import. */
    files = import_selected_files ( assistant );
    while ( files )
    {
        imported = files -> data;

        if ( !strcmp ( imported -> type, "CSV" ) )
        {
            filename = imported -> name;
            break;
        }
        files = files -> next;
    }
    g_return_val_if_fail ( filename, FALSE );

    /* Open file */
    if ( ! g_file_get_contents ( filename, &tmp_str, &size, &error ) )
    {
        g_print ( _("Unable to read file: %s\n"), error -> message);
        g_error_free ( error );
        return FALSE;
    }

    /* Convert in UTF8 */
    error = NULL;
    contents = g_convert_with_fallback ( tmp_str, -1, "UTF-8", imported -> coding_system,
                        "?", &size, &bytes_written, &error );

    if ( contents == NULL )
    {
        g_error_free ( error );
        error = NULL;
        size = 0;
        bytes_written = 0;

        dialogue_special ( GTK_MESSAGE_WARNING, make_hint (
                            _("The conversion to utf8 went wrong."),
                            _("If the result does not suit you, try again by selecting the "
                            "correct character set in the window for selecting files.") ) );

        contents = g_convert_with_fallback ( tmp_str, -1, "UTF-8", "ISO-8859-1",
                        "?", &size, &bytes_written, &error );
        if ( bytes_written == 0 )
        {
            g_print ( _("Unable to read file: %s\n"), error -> message);
            g_error_free ( error );
            return FALSE;
        }
    }

    g_free ( tmp_str );
    g_object_set_data ( G_OBJECT(assistant), "contents", contents );

    entry = g_object_get_data ( G_OBJECT(assistant), "entry" );
    if ( entry )
    {
	if ( etat.csv_separator )
	{
	    gtk_entry_set_text ( GTK_ENTRY(entry), etat.csv_separator );
	}
	else
	{
	    gtk_entry_set_text ( GTK_ENTRY(entry), csv_import_guess_separator ( contents ) );
	}
    }

    csv_import_update_validity_check ( assistant );

    return FALSE;
}
예제 #2
0
/* get the line af the file,
 * convert it in UTF8 and fill string with that line
 *
 * \param fichier
 * \param string
 * \param coding_system	the orig coding system of the string
 *
 * \return EOF, 1 if ok, 0 if problem
 * */
gint get_utf8_line_from_file ( FILE *fichier,
                        gchar **string,
                        const gchar *coding_system )
{
    gchar c = 0;
    gint i = 0;
    gint j = 0;
    gchar *pointeur_char = NULL;
    gchar *tmp_string;

    if ( !fichier )
	return 0;
	    
    /* allocate 30 characters, and increase it 30 by 30 */
    pointeur_char = (gchar*)g_realloc(pointeur_char,30*sizeof(gchar));

    if ( !pointeur_char )
    {
	/* ouch, not enough memory */
	dialogue_error ( _("Memory allocation error" ));
	return 0;
    }

    /* get the string untill \n or \r (windows format) */
    c =(gchar)fgetc(fichier);
    while ( ( c != '\n' ) && (c != '\r') && !feof (fichier))
    {
	pointeur_char[j++] = c;

	if ( ++i == 29 )
	{
	    pointeur_char = (gchar*)g_realloc(pointeur_char, j + 1 + 30*sizeof(gchar));

	    if ( !pointeur_char )
	    {
		/* ouch, not enough memory */
		dialogue_error ( _("Memory allocation error" ));
		return 0;
	    }
	    i = 0;
	}
	c =(gchar)fgetc(fichier);
    }
    pointeur_char[j] = 0;

    /* if we finished on \r, jump the \n after it */
    if ( c == '\r' )
    {
	c =(gchar)fgetc(fichier);
	if ( c != '\n' )
	{
	    ungetc ( c, fichier );
	}
    }

    tmp_string = g_convert ( pointeur_char, -1, "UTF-8", 
			     coding_system, NULL, NULL,
			     NULL );
    if (!tmp_string)
    {
        devel_debug ("convert to utf8 failed, will use latin2utf8");
        tmp_string = latin2utf8 (pointeur_char);
        if ( tmp_string == NULL )
        {
            dialogue_special ( GTK_MESSAGE_ERROR, make_hint (
                            _("Convert to utf8 failed."),
                            _("If the result is not correct, try again by selecting the "
                            "correct character set in the window for selecting files.") ) );
            return 0;
        }
    }
    *string = tmp_string;
    g_free (pointeur_char);

    if ( feof(fichier))
        return EOF;
    else
        return 1;
}