[totem-pl-parser] Handle all types of newline characters in playlists by splitting on both \n and \r (Closes: #579905)



commit 34c3024ba5792965d8662642bb8a7e47ac44300c
Author: Robert Ancell <robert ancell gmail com>
Date:   Mon May 4 15:34:07 2009 +1000

    Handle all types of newline characters in playlists by splitting on both \n and \r (Closes: #579905)
---
 ChangeLog                         |   13 +++++++++++++
 plparse/totem-pl-parser-lines.c   |   30 +++++++-----------------------
 plparse/totem-pl-parser-misc.c    |   12 ++++++------
 plparse/totem-pl-parser-pls.c     |   22 +++++++---------------
 plparse/totem-pl-parser-private.h |    5 ++---
 plparse/totem-pl-parser-qt.c      |   17 +++++++++--------
 plparse/totem-pl-parser-wm.c      |   12 +++---------
 plparse/totem-pl-parser.c         |   15 +++------------
 8 files changed, 50 insertions(+), 76 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 2c0a52a..3c5d49a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2009-05-04  Robert Ancell  <robert ancell gmail com>
+
+	* plparse/totem-pl-parser-lines.c:
+	* plparse/totem-pl-parser-misc.c:
+	* plparse/totem-pl-parser-pls.c:
+	* plparse/totem-pl-parser-private.h:
+	* plparse/totem-pl-parser-qt.c:
+	* plparse/totem-pl-parser-wm.c:
+	* plparse/totem-pl-parser.c:
+	Handle all types of newline characters in playlists by splitting on both \n and \r
+	
+	(Closes: #579905)
+
 2009-05-03  Bastien Nocera  <hadess hadess net>
 
 	* lib/stub.c (___stub_so_lib_is_not_empty): Add a stub
diff --git a/plparse/totem-pl-parser-lines.c b/plparse/totem-pl-parser-lines.c
index e149726..96e7074 100644
--- a/plparse/totem-pl-parser-lines.c
+++ b/plparse/totem-pl-parser-lines.c
@@ -250,18 +250,11 @@ totem_pl_parser_add_ram (TotemPlParser *parser, GFile *file, gpointer data)
 	char *contents, **lines;
 	gsize size;
 	guint i;
-	const char *split_char;
 
 	if (g_file_load_contents (file, NULL, &contents, &size, NULL, NULL) == FALSE)
 		return TOTEM_PL_PARSER_RESULT_ERROR;
 
-	/* figure out whether we're a unix or dos RAM file */
-	if (strstr(contents,"\x0d") == NULL)
-		split_char = "\n";
-	else
-		split_char = "\x0d\n";
-
-	lines = g_strsplit (contents, split_char, 0);
+	lines = g_strsplit_set (contents, "\r\n", 0);
 	g_free (contents);
 
 	for (i = 0; lines[i] != NULL; i++) {
@@ -351,8 +344,8 @@ totem_pl_parser_add_m3u (TotemPlParser *parser,
 	char *contents, **lines;
 	gsize size;
 	guint i, num_lines;
-	gboolean dos;
-	const char *split_char, *extinfo;
+	gboolean dos_mode = FALSE;
+	const char *extinfo;
 
 	if (g_file_load_contents (file, NULL, &contents, &size, NULL, NULL) == FALSE)
 		return TOTEM_PL_PARSER_RESULT_ERROR;
@@ -381,15 +374,11 @@ totem_pl_parser_add_m3u (TotemPlParser *parser,
 	extinfo = NULL;
 
 	/* figure out whether we're a unix m3u or dos m3u */
-	if (strstr(contents,"\x0d") == NULL) {
-		split_char = "\n";
-		dos = FALSE;
-	} else {
-		split_char = "\x0d\n";
-		dos = TRUE;
+	if (strstr(contents, "\x0d")) {
+		dos_mode = TRUE;
 	}
 
-	lines = g_strsplit (contents, split_char, 0);
+	lines = g_strsplit_set (contents, "\r\n", 0);
 	g_free (contents);
 	num_lines = g_strv_length (lines);
 	/* We don't count the terminating NULL */
@@ -408,11 +397,6 @@ totem_pl_parser_add_m3u (TotemPlParser *parser,
 			continue;
 		}
 
-		/* If we're on the last line, and we're a DOS M3U,
-		 * remove the terminating \n if it's there */
-		if (dos != FALSE && i == num_lines)
-			g_strchomp (lines[i]);
-
 		/* Either it's a URI, or it has a proper path ... */
 		if (strstr(lines[i], "://") != NULL
 				|| lines[i][0] == G_DIR_SEPARATOR) {
@@ -459,7 +443,7 @@ totem_pl_parser_add_m3u (TotemPlParser *parser,
 			char sep;
 
 			_base_file = g_file_get_parent (file);
-			sep = (split_char[0] == '\n' ? '/' : '\\');
+			sep = (dos_mode ? '\\' : '/');
 			if (sep == '\\')
 				lines[i] = g_strdelimit (lines[i], "\\", '/');
 			uri = g_file_get_child (_base_file, lines[i]);
diff --git a/plparse/totem-pl-parser-misc.c b/plparse/totem-pl-parser-misc.c
index 5cc2490..36ec824 100644
--- a/plparse/totem-pl-parser-misc.c
+++ b/plparse/totem-pl-parser-misc.c
@@ -59,7 +59,7 @@ totem_pl_parser_add_gvp (TotemPlParser *parser,
 	g_free (contents);
 
 	/* We only handle GVP version 1.1 for now */
-	version = totem_pl_parser_read_ini_line_string_with_sep (lines, "gvp_version", FALSE, ":");
+	version = totem_pl_parser_read_ini_line_string_with_sep (lines, "gvp_version", ":");
 	if (version == NULL || strcmp (version, "1.1") != 0) {
 		g_free (version);
 		g_strfreev (lines);
@@ -67,7 +67,7 @@ totem_pl_parser_add_gvp (TotemPlParser *parser,
 	}
 	g_free (version);
 
-	link = totem_pl_parser_read_ini_line_string_with_sep (lines, "url", FALSE, ":");
+	link = totem_pl_parser_read_ini_line_string_with_sep (lines, "url", ":");
 	if (link == NULL) {
 		g_strfreev (lines);
 		return retval;
@@ -75,7 +75,7 @@ totem_pl_parser_add_gvp (TotemPlParser *parser,
 
 	retval = TOTEM_PL_PARSER_RESULT_SUCCESS;
 
-	title = totem_pl_parser_read_ini_line_string_with_sep (lines, "title", FALSE, ":");
+	title = totem_pl_parser_read_ini_line_string_with_sep (lines, "title", ":");
 
 	totem_pl_parser_add_one_uri (parser, link, title);
 
@@ -104,7 +104,7 @@ totem_pl_parser_add_desktop (TotemPlParser *parser,
 	lines = g_strsplit (contents, "\n", 0);
 	g_free (contents);
 
-	type = totem_pl_parser_read_ini_line_string (lines, "Type", FALSE);
+	type = totem_pl_parser_read_ini_line_string (lines, "Type");
 	if (type == NULL)
 		goto bail;
 	
@@ -113,12 +113,12 @@ totem_pl_parser_add_desktop (TotemPlParser *parser,
 		goto bail;
 	}
 
-	path = totem_pl_parser_read_ini_line_string (lines, "URL", FALSE);
+	path = totem_pl_parser_read_ini_line_string (lines, "URL");
 	if (path == NULL)
 		goto bail;
 	target = g_file_new_for_uri (path);
 
-	display_name = totem_pl_parser_read_ini_line_string (lines, "Name", FALSE);
+	display_name = totem_pl_parser_read_ini_line_string (lines, "Name");
 
 	if (totem_pl_parser_ignore (parser, path) == FALSE
 	    && g_ascii_strcasecmp (type, "FSDevice") != 0) {
diff --git a/plparse/totem-pl-parser-pls.c b/plparse/totem-pl-parser-pls.c
index c943b13..52c6ba7 100644
--- a/plparse/totem-pl-parser-pls.c
+++ b/plparse/totem-pl-parser-pls.c
@@ -153,18 +153,10 @@ totem_pl_parser_add_pls_with_contents (TotemPlParser *parser,
 	GFile *base_file;
 	char **lines;
 	int i, num_entries;
-	char *split_char, *playlist_title;
-	gboolean dos_mode = FALSE;
+	char *playlist_title;
 	gboolean fallback;
 
-	/* figure out whether we're a unix pls or dos pls */
-	if (strstr(contents,"\x0d") == NULL) {
-		split_char = "\n";
-	} else {
-		split_char = "\x0d\n";
-		dos_mode = TRUE;
-	}
-	lines = g_strsplit (contents, split_char, 0);
+	lines = g_strsplit_set (contents, "\r\n", 0);
 
 	/* [playlist] */
 	i = 0;
@@ -181,7 +173,7 @@ totem_pl_parser_add_pls_with_contents (TotemPlParser *parser,
 	}
 
 	playlist_title = totem_pl_parser_read_ini_line_string (lines,
-			"X-GNOME-Title", dos_mode);
+			"X-GNOME-Title");
 
 	if (playlist_title != NULL) {
 		totem_pl_parser_add_uri (parser,
@@ -229,10 +221,10 @@ totem_pl_parser_add_pls_with_contents (TotemPlParser *parser,
 		/* Genre is our own little extension */
 		genre_key = g_strdup_printf ("genre%d", i);
 
-		file_str = totem_pl_parser_read_ini_line_string (lines, (const char*)file_key, dos_mode);
-		title = totem_pl_parser_read_ini_line_string (lines, (const char*)title_key, dos_mode);
-		genre = totem_pl_parser_read_ini_line_string (lines, (const char*)genre_key, dos_mode);
-		length = totem_pl_parser_read_ini_line_string (lines, (const char*)length_key, dos_mode);
+		file_str = totem_pl_parser_read_ini_line_string (lines, (const char*)file_key);
+		title = totem_pl_parser_read_ini_line_string (lines, (const char*)title_key);
+		genre = totem_pl_parser_read_ini_line_string (lines, (const char*)genre_key);
+		length = totem_pl_parser_read_ini_line_string (lines, (const char*)length_key);
 
 		g_free (file_key);
 		g_free (title_key);
diff --git a/plparse/totem-pl-parser-private.h b/plparse/totem-pl-parser-private.h
index 2a3d9b8..422385b 100644
--- a/plparse/totem-pl-parser-private.h
+++ b/plparse/totem-pl-parser-private.h
@@ -87,11 +87,10 @@ struct TotemPlParserPrivate
 };
 
 #ifndef TOTEM_PL_PARSER_MINI
-char *totem_pl_parser_read_ini_line_string	(char **lines, const char *key,
-						 gboolean dos_mode);
+char *totem_pl_parser_read_ini_line_string	(char **lines, const char *key);
 int   totem_pl_parser_read_ini_line_int		(char **lines, const char *key);
 char *totem_pl_parser_read_ini_line_string_with_sep (char **lines, const char *key,
-						     gboolean dos_mode, const char *sep);
+						     const char *sep);
 char *totem_pl_parser_base_uri			(GFile *file);
 void totem_pl_parser_playlist_end		(TotemPlParser *parser,
 						 const char *playlist_title);
diff --git a/plparse/totem-pl-parser-qt.c b/plparse/totem-pl-parser-qt.c
index 03b5311..e52ea51 100644
--- a/plparse/totem-pl-parser-qt.c
+++ b/plparse/totem-pl-parser-qt.c
@@ -72,7 +72,6 @@ totem_pl_parser_add_quicktime_rtsptext (TotemPlParser *parser,
 					gpointer data)
 {
 	char *contents = NULL;
-	gboolean dos_mode = FALSE;
 	char *volume, *autoplay, *rtspuri;
 	gsize size;
 	char **lines;
@@ -80,20 +79,22 @@ totem_pl_parser_add_quicktime_rtsptext (TotemPlParser *parser,
 	if (g_file_load_contents (file, NULL, &contents, &size, NULL, NULL) == FALSE)
 		return TOTEM_PL_PARSER_RESULT_ERROR;
 
-	if (strstr(contents,"\x0d") != NULL)
-		dos_mode = TRUE;
-
-	lines = g_strsplit (contents, dos_mode ? "\x0d\n" : "\n", 0);
+	lines = g_strsplit_set (contents, "\r\n", 0);
 
 	volume = totem_pl_parser_read_ini_line_string_with_sep
-		(lines, "volume", dos_mode, "=");
+		(lines, "volume", "=");
 	autoplay = totem_pl_parser_read_ini_line_string_with_sep
-		(lines, "autoplay", dos_mode, "=");
+		(lines, "autoplay", "=");
 
 	rtspuri = g_strdup (lines[0] + strlen ("RTSPtext"));
 	if (rtspuri[0] == '\0') {
+		char **line;
 		g_free (rtspuri);
-		rtspuri = g_strdup (lines[1]);
+
+		for (line = lines; line && *line[0] == '\0'; line++);
+		if (line == NULL)
+			return TOTEM_PL_PARSER_RESULT_ERROR;
+		rtspuri = g_strdup (*line);
 	}
 	g_strstrip (rtspuri);
 
diff --git a/plparse/totem-pl-parser-wm.c b/plparse/totem-pl-parser-wm.c
index 8a0692b..94d48e6 100644
--- a/plparse/totem-pl-parser-wm.c
+++ b/plparse/totem-pl-parser-wm.c
@@ -84,23 +84,17 @@ totem_pl_parser_add_asf_reference_parser (TotemPlParser *parser,
 					  GFile *base_file,
 					  gpointer data)
 {
-	char *contents, **lines, *ref, *split_char;
+	char *contents, **lines, *ref;
 	gsize size;
 
 	if (g_file_load_contents (file, NULL, &contents, &size, NULL, NULL) == FALSE)
 		return TOTEM_PL_PARSER_RESULT_ERROR;
 
-	if (strstr(contents,"\x0d") == NULL) {
-		split_char = "\n";
-	} else {
-		split_char = "\x0d\n";
-	}
-
-	lines = g_strsplit (contents, split_char, 0);
+	lines = g_strsplit_set (contents, "\n\r", 0);
 	g_free (contents);
 
 	/* Try to get Ref1 first */
-	ref = totem_pl_parser_read_ini_line_string (lines, "Ref1", FALSE);
+	ref = totem_pl_parser_read_ini_line_string (lines, "Ref1");
 	if (ref == NULL) {
 		g_strfreev (lines);
 		return totem_pl_parser_add_asx (parser, file, base_file, data);
diff --git a/plparse/totem-pl-parser.c b/plparse/totem-pl-parser.c
index 83a35c3..bbb5cf6 100644
--- a/plparse/totem-pl-parser.c
+++ b/plparse/totem-pl-parser.c
@@ -1148,7 +1148,6 @@ totem_pl_parser_read_ini_line_int (char **lines, const char *key)
  * totem_pl_parser_read_ini_line_string_with_sep:
  * @lines: a NULL-terminated array of INI lines to read
  * @key: the key to match
- * @dos_mode: %TRUE if the returned string should end in \r\0, instead of \n\0
  * @sep: the key-value separator
  *
  * Returns the first string value case-insensitively matching the specified
@@ -1159,7 +1158,7 @@ totem_pl_parser_read_ini_line_int (char **lines, const char *key)
  **/
 char*
 totem_pl_parser_read_ini_line_string_with_sep (char **lines, const char *key,
-		gboolean dos_mode, const char *sep)
+		const char *sep)
 {
 	char *retval = NULL;
 	int i;
@@ -1175,7 +1174,6 @@ totem_pl_parser_read_ini_line_string_with_sep (char **lines, const char *key,
 
 		if (g_ascii_strncasecmp (line, key, strlen (key)) == 0) {
 			char **bits;
-			glong len;
 
 			bits = g_strsplit (line, sep, 2);
 			if (bits[0] == NULL || bits [1] == NULL) {
@@ -1184,12 +1182,6 @@ totem_pl_parser_read_ini_line_string_with_sep (char **lines, const char *key,
 			}
 
 			retval = g_strdup (bits[1]);
-			len = strlen (retval);
-			if (dos_mode && len >= 2 && retval[len-2] == '\r') {
-				retval[len-2] = '\n';
-				retval[len-1] = '\0';
-			}
-
 			g_strfreev (bits);
 		}
 	}
@@ -1201,7 +1193,6 @@ totem_pl_parser_read_ini_line_string_with_sep (char **lines, const char *key,
  * totem_pl_parser_read_ini_line_string:
  * @lines: a NULL-terminated array of INI lines to read
  * @key: the key to match
- * @dos_mode: %TRUE if the returned string should end in \r\0, instead of \n\0
  *
  * Returns the first string value case-insensitively matching the
  * specified key. The parser ignores leading whitespace on lines.
@@ -1209,9 +1200,9 @@ totem_pl_parser_read_ini_line_string_with_sep (char **lines, const char *key,
  * Return value: a newly-allocated string value, or %NULL
  **/
 char*
-totem_pl_parser_read_ini_line_string (char **lines, const char *key, gboolean dos_mode)
+totem_pl_parser_read_ini_line_string (char **lines, const char *key)
 {
-	return totem_pl_parser_read_ini_line_string_with_sep (lines, key, dos_mode, "=");
+	return totem_pl_parser_read_ini_line_string_with_sep (lines, key, "=");
 }
 
 static void



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]