GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Sep 17, 2014 3:32 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: error with concating string
PostPosted: Sun Oct 23, 2011 4:40 pm 
Offline
GTK+ Geek

Joined: Sun Apr 03, 2011 11:12 am
Posts: 63
hello everyone, i am facing error with concating strings, filename and filepath. Actually,the problem is I want to get filename from filepath.
So, that's the algorithm I have developed:

Find the last occurrence of "/" from beginning of string, filepath or first occurrence from end of string.
Then get each character from string by filepath[i] and concating it with filename using for loop.

The code is this:

Code:
void save_activated(GtkWidget *widget, GtkWindow *parent)
{
    GtkWidget *dialog;
    GtkTextIter start_iter, end_iter;
    GtkTextBuffer *buffer;
    GtkSourceLanguageManager *languagemanager = gtk_source_language_manager_new();
    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(txtinput[gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook))]));
    dialog = gtk_file_chooser_dialog_new("Save File", parent, GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL,GTK_RESPONSE_CANCEL,GTK_STOCK_SAVE,GTK_RESPONSE_ACCEPT,NULL);
    if (gtk_dialog_run(GTK_DIALOG(dialog))== GTK_RESPONSE_ACCEPT)
    {
        char *filepath,*text;
        gchar *filename;
        int i;
        gssize length = -1;
        filepath = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
        gtk_text_buffer_get_end_iter(buffer,&end_iter);
        gtk_text_buffer_get_start_iter(buffer,&start_iter);
        text = gtk_text_buffer_get_text(buffer,&start_iter,&end_iter,FALSE);
        g_file_set_contents(filepath,text,length,NULL);
        GtkSourceLanguage *language = gtk_source_language_manager_guess_language(languagemanager,filepath,NULL);
        for(i = rfind(filepath,"/")+1;i<strlen(filepath)-1;i++)
        {
            filename = g_strconcat("%s%c",filename,filepath[i]);
            }       
        gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(notebook),scrollwin[gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook))],filename);
        gtk_source_buffer_set_language(GTK_SOURCE_BUFFER(buffer),language);       
        g_free(filepath);
        g_free(text);
        }
    gtk_widget_destroy(dialog);
    }
int strlen(char string[])
{
    char *p;
    p = &string[0];
    int i = 0;
    while (*p++ != 0)
       i++;
    return i;
    }
       
int rfind(char *string, char *c)
{
    int index=0;
    char *p;
    p = &string[strlen(string)-1];
    index = strlen(string)-1;
    while (*p++)
    {
        if (*p == *c)
           break;
        index--;
        }
    return index;
    }




As, you can see, I want to get the file name and make it the text of label of the current page of notebook. But whenever I try to save file, I get following error in terminal.
Pango-WARNING **: Invalid UTF-8 string passed to pango_layout_set_text() and some unusaul string containing unusual symbols become the label text of the notebook page.
Please Help!! Solve this problem.
Thanks in advance


Top
 Profile  
 
 Post subject: Re: error with concating string
PostPosted: Mon Oct 24, 2011 5:51 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 745
Location: UK
Hi,

The most probable reason you are getting random characters in your display string is that you are using g_strconcat() incorrectly. It takes a variable number of pointers to strings with the final pointer being NULL. For example
Code:
final_string = g_strconcat(string_1, string_2, string_3, NULL);
And the returned string must then be freed using g_free() when it is not needed any more.

Next you are trying to reinvent the wheel! There is no need to re-write standard library functions such as strlen(). They are there ready for you to use and the library has probably got a much more optimised version for you to use. Next we go back to your original problem of extracting the file name. The POSIX library and GLib both contain functions for extracting the file name from a full path name. They work in slightly different ways, but do the same job. The GLib ones also work with MS Windows file systems, so making your job of porting easier.

Code:
void save_activated(GtkWidget *widget, GtkWindow *parent)
{
    GtkWidget *dialog;
    GtkTextIter start_iter, end_iter;
    GtkTextBuffer *buffer;
    GtkSourceLanguageManager *languagemanager = gtk_source_language_manager_new();

    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(txtinput[gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook))]));
    dialog = gtk_file_chooser_dialog_new("Save File", parent,
        GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
        GTK_STOCK_SAVE,GTK_RESPONSE_ACCEPT, NULL);

    if (gtk_dialog_run(GTK_DIALOG(dialog))== GTK_RESPONSE_ACCEPT)
    {
        gchar *text, *filename, *filepath;
        GtkSourceLanguage *language;

        filepath = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));

        gtk_text_buffer_get_end_iter(buffer, &end_iter);
        gtk_text_buffer_get_start_iter(buffer, &start_iter);
        text = gtk_text_buffer_get_text(buffer, &start_iter, &end_iter, FALSE);
        g_file_set_contents(filepath, text, -1, NULL);

        language = gtk_source_language_manager_guess_language(languagemanager, filepath, NULL);

        filename = g_filename_display_basename(filepath);
   
        gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(notebook), scrollwin[gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook))],filename);
        gtk_source_buffer_set_language(GTK_SOURCE_BUFFER(buffer), language);

        g_free(filename);
        g_free(filepath);
        g_free(text);
    }

    gtk_widget_destroy(dialog);
}


In this case I have used the function g_filename_display_basename(). This function returns a newly allocated string that contains the basename (your file name) and guarantees that it is in UTF8 as file names could be encoded using other character encodings.

So by using library functions that are already there waiting for you to use, you can shorten your code, make it easier to read, and less prone to errors.

E.

_________________
E.


Top
 Profile  
 
 Post subject: Re: error with concating string
PostPosted: Tue Oct 25, 2011 7:47 am 
Offline
GTK+ Geek

Joined: Sun Apr 03, 2011 11:12 am
Posts: 63
thx for reply.
can you tell me where I can all Glib functions? I didn't found g_filename_display_basename in the following page http://developer.gnome.org/glib/2.30/gl ... t-contents


Top
 Profile  
 
 Post subject: Re: error with concating string
PostPosted: Wed Oct 26, 2011 10:16 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 745
Location: UK
A description of this function can be found at http://developer.gnome.org/glib/2.30/glib-Character-Set-Conversion.html#g-filename-display-basename. Also note that this function is for display purposes only. Do not use the returned value as part of a file name later on. Always keep a copy of your original file name stored in its original character encoding.

Please explore the GLib library as it is rich in functions what can help you make your code easier to write and more portable from system to system. There are functions for threads, string handling in UTF8 as well as character based, file IO, timers, lexical scanner, pattern matching, data structures, etc., etc., etc.

The base URL is http://developer.gnome.org/glib/2.30/index.html

E.

_________________
E.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group