From f82b55da8dc8389e3e4e0021804231541aa5d71b Mon Sep 17 00:00:00 2001 From: Rogério Brito Date: Tue, 8 Mar 2011 15:04:35 -0300 Subject: cddb: avoid calling strlen many times in just a few statements. As strlen is a potentially costly function, it is better if we make a frugal use of it. To really make things cleaner here, we should replace all the `\n` or `\r` with null chars (i.e., `\0`) in just one go. This would reduce the complexity of the code from something quadratic to something linear in the length of the string. While we are at it, `fgets` does not return `EOF` (`fgetc` does, but `fgets` doesn't) when the end of file is reached and nothing can be read: it returns `NULL` instead. --- src/cddb.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/cddb.c b/src/cddb.c index dc8cae6..d60220e 100644 --- a/src/cddb.c +++ b/src/cddb.c @@ -1798,6 +1798,7 @@ gint Cddb_Read_Line (FILE **file, gchar **cddb_out) { gchar buffer[MAX_STRING_LEN]; gchar *result; + size_t l; if (*file == NULL) { @@ -1827,12 +1828,16 @@ gint Cddb_Read_Line (FILE **file, gchar **cddb_out) } result = fgets(buffer,sizeof(buffer),*file); - if (result != NULL && result != (gchar *)EOF) + if (result != NULL) { - if (buffer && strlen(buffer)>0 && buffer[strlen(buffer)-1]=='\n') - buffer[strlen(buffer)-1]='\0'; - while (buffer && strlen(buffer)>0 && buffer[strlen(buffer)-1]=='\r') // Severals chars '\r' may be present - buffer[strlen(buffer)-1]='\0'; + l = strlen(buffer); + if (l > 0 && buffer[l-1] == '\n') + buffer[l-1] = '\0'; + + // Many '\r' chars may be present + while ((l = strlen(buffer)) > 0 && buffer[l-1] == '\r') + buffer[l-1] = '\0'; + *cddb_out = g_strdup(buffer); }else { @@ -2548,7 +2553,7 @@ gboolean Cddb_Search_Album_List_From_String_Gnudb (void) { g_free(string); g_free(cddb_server_name); - g_free(cddb_server_cgi_path); + g_free(cddb_server_cgi_path); gtk_widget_set_sensitive(GTK_WIDGET(CddbStopSearchButton),FALSE); gtk_widget_set_sensitive(GTK_WIDGET(CddbStopSearchAutoButton),FALSE); return FALSE; -- cgit v1.2.3