diff options
author | Rogério Brito <rbrito@ime.usp.br> | 2011-03-08 15:04:35 -0300 |
---|---|---|
committer | Rogério Brito <rbrito@ime.usp.br> | 2011-03-08 17:35:58 -0300 |
commit | f82b55da8dc8389e3e4e0021804231541aa5d71b (patch) | |
tree | 0f143a0db484a0b2b4b1b34f09649a4907e4d2a1 /src | |
parent | 891b53ce003db7be264bba8b6cd3d547c050099f (diff) |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/cddb.c | 17 |
1 files changed, 11 insertions, 6 deletions
@@ -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; |