[maemo-commits] [maemo-commits] r18139 - projects/haf/trunk/hildon-thumbnail/daemon
From: subversion at stage.maemo.org subversion at stage.maemo.orgDate: Wed Apr 22 17:46:16 EEST 2009
- Previous message: [maemo-commits] r18138 - in projects/haf/trunk/clutter0.8: clutter/eglx debian
- Next message: [maemo-commits] r18140 - projects/haf/trunk/ke-recv/debian
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Author: pvanhoof Date: 2009-04-22 17:46:03 +0300 (Wed, 22 Apr 2009) New Revision: 18139 Modified: projects/haf/trunk/hildon-thumbnail/daemon/utils.c Log: 2009-04-22 Philip Van Hoof <pvanhoof at codeminded.be> * daemon/utils.c: Trimming of leading and trailing white spaces Modified: projects/haf/trunk/hildon-thumbnail/daemon/utils.c =================================================================== --- projects/haf/trunk/hildon-thumbnail/daemon/utils.c 2009-04-22 14:01:45 UTC (rev 18138) +++ projects/haf/trunk/hildon-thumbnail/daemon/utils.c 2009-04-22 14:46:03 UTC (rev 18139) @@ -173,97 +173,130 @@ } - -static gchar* -strip_characters (const gchar *original) +static gboolean +strip_find_next_block (const gchar *original, + const gunichar open_char, + const gunichar close_char, + gint *open_pos, + gint *close_pos) { - const gchar *foo = "()[]<>{}_!@#$^&*+=|\\/\"'?~"; - guint osize = strlen (original); - gchar *retval = (gchar *) g_malloc0 (sizeof (gchar *) * osize + 1); - guint i = 0, y = 0; + const gchar *p1, *p2; - while (i < osize) { + if (open_pos) { + *open_pos = -1; + } - /* Remove (anything) */ + if (close_pos) { + *close_pos = -1; + } - if (original[i] == '(') { - gchar *loc = strchr (original+i, ')'); - if (loc) { - i = loc - original + 1; - continue; - } + p1 = g_utf8_strchr (original, -1, open_char); + if (p1) { + if (open_pos) { + *open_pos = p1 - original; } - /* Remove [anything] */ - - if (original[i] == '[') { - gchar *loc = strchr (original+i, ']'); - if (loc) { - i = loc - original + 1; - continue; + p2 = g_utf8_strchr (g_utf8_next_char (p1), -1, close_char); + if (p2) { + if (close_pos) { + *close_pos = p2 - original; } + + return TRUE; } + } - /* Remove {anything} */ + return FALSE; +} - if (original[i] == '{') { - gchar *loc = strchr (original+i, '}'); - if (loc) { - i = loc - original + 1; - continue; - } - } +static gchar * +strip_characters (const gchar *original) +{ + GString *str_no_blocks; + gchar **strv; + gchar *str; + gboolean blocks_done = FALSE; + const gchar *p; + const gchar *invalid_chars = "()[]<>{}_!@#$^&*+=|\\/\"'?~"; + const gchar *invalid_chars_delimiter = "*"; + const gchar *convert_chars = "\t"; + const gchar *convert_chars_delimiter = " "; + const gunichar blocks[5][2] = { + { '(', ')' }, + { '{', '}' }, + { '[', ']' }, + { '<', '>' }, + { 0, 0 } + }; - /* Remove <anything> */ + str_no_blocks = g_string_new (""); - if (original[i] == '<') { - gchar *loc = strchr (original+i, '>'); - if (loc) { - i = loc - original + 1; - continue; - } - } + p = original; - /* Remove double whitespaces */ + while (!blocks_done) { + gint pos1, pos2, i; - if ((y > 0) && - (original[i] == ' ' || original[i] == '\t') && - (retval[y-1] == ' ' || retval[y-1] == '\t')) { - i++; - continue; + pos1 = -1; + pos2 = -1; + + for (i = 0; blocks[i][0] != 0; i++) { + gint start, end; + + /* Go through blocks, find the earliest block we can */ + if (strip_find_next_block (p, blocks[i][0], blocks[i][1], &start, &end)) { + if (pos1 == -1 || start < pos1) { + pos1 = start; + pos2 = end; + } + } } + + /* If either are -1 we didn't find any */ + if (pos1 == -1) { + /* This means no blocks were found */ + g_string_append (str_no_blocks, p); + blocks_done = TRUE; + } else { + /* Append the test BEFORE the block */ + if (pos1 > 0) { + g_string_append_len (str_no_blocks, p, pos1); + } - /* Remove strange characters */ + p = g_utf8_next_char (p + pos2); - if (!strchr (foo, original[i])) { - retval[y] = original[i]!='\t'?original[i]:' '; - y++; - } - - i++; + /* Do same again for position AFTER block */ + if (*p == '\0') { + blocks_done = TRUE; + } + } } - retval[y] = 0; + str = g_string_free (str_no_blocks, FALSE); - y--; - while (retval[y] == ' ') { - retval[y] = 0; - y--; - } + /* Now strip invalid chars */ + g_strdelimit (str, invalid_chars, *invalid_chars_delimiter); + strv = g_strsplit (str, invalid_chars_delimiter, -1); + g_free (str); + str = g_strjoinv (NULL, strv); + g_strfreev (strv); - if (retval[0] == ' ') { - guint r = 0; - gchar *newr; + /* Now convert chars */ + g_strdelimit (str, convert_chars, *convert_chars_delimiter); + strv = g_strsplit (str, convert_chars_delimiter, -1); + g_free (str); + str = g_strjoinv (convert_chars_delimiter, strv); + g_strfreev (strv); - while (retval[r] == ' ') - r++; + /* Now remove double spaces */ + strv = g_strsplit (str, " ", -1); + g_free (str); + str = g_strjoinv (" ", strv); + g_strfreev (strv); + + /* Now strip leading/trailing white space */ + g_strstrip (str); - newr = g_strdup (retval + r); - g_free (retval); - retval = newr; - } - - return retval; + return str; } void
- Previous message: [maemo-commits] r18138 - in projects/haf/trunk/clutter0.8: clutter/eglx debian
- Next message: [maemo-commits] r18140 - projects/haf/trunk/ke-recv/debian
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]