devhelp r1115 - in trunk: . src



Author: rhult
Date: Fri Oct  3 19:16:52 2008
New Revision: 1115
URL: http://svn.gnome.org/viewvc/devhelp?rev=1115&view=rev

Log:
2008-10-03  Richard Hult  <richard imendio com>

	* src/dh-parser.c: More preparations, reorder things a bit.


Modified:
   trunk/ChangeLog
   trunk/src/dh-parser.c

Modified: trunk/src/dh-parser.c
==============================================================================
--- trunk/src/dh-parser.c	(original)
+++ trunk/src/dh-parser.c	Fri Oct  3 19:16:52 2008
@@ -56,25 +56,19 @@
 	gint                 version;
 } DhParser;
 
-static void     parser_start_node_cb (GMarkupParseContext  *context,
-				      const gchar          *node_name,
-				      const gchar         **attribute_names,
-				      const gchar         **attribute_values,
-				      gpointer              user_data,
-				      GError              **error);
-static void     parser_end_node_cb   (GMarkupParseContext  *context,
-				      const gchar          *node_name,
-				      gpointer              user_data,
-				      GError              **error);
-static void     parser_error_cb      (GMarkupParseContext  *context,
-				      GError               *error,
-				      gpointer              user_data);
-static gboolean parser_read_gz_file  (const gchar          *path,
-				      GNode                *book_tree,
-				      GList               **keywords,
-				      GError              **error);
-static gchar   *extract_page_name    (const gchar          *uri);
+static gchar *
+extract_page_name (const gchar *uri)
+{
+	gchar  *page = NULL;
+	gchar **split;
 
+	if ((split = g_strsplit (uri, ".", 2)) != NULL) {
+		page = split[0];
+                split[0] = NULL;
+		g_strfreev (split);
+	}
+	return page;
+}
 
 static void
 parser_start_node_book (DhParser             *parser,
@@ -386,17 +380,8 @@
 {
 	DhParser *parser = user_data;
 
-	if (!parser->book_node) {
-                parser_start_node_book (parser,
-                                        context,
-                                        node_name,
-                                        attribute_names,
-                                        attribute_values,
-                                        error);
-		return;
-	}
-        else if (parser->parsing_chapters) {
-                parser_start_node_chapter (parser,
+        if (parser->parsing_keywords) {
+                parser_start_node_keyword (parser,
                                            context,
                                            node_name,
                                            attribute_names,
@@ -404,8 +389,8 @@
                                            error);
                 return;
         }
-	else if (parser->parsing_keywords) {
-                parser_start_node_keyword (parser,
+        else if (parser->parsing_chapters) {
+                parser_start_node_chapter (parser,
                                            context,
                                            node_name,
                                            attribute_names,
@@ -413,11 +398,20 @@
                                            error);
                 return;
         }
+	else if (g_ascii_strcasecmp (node_name, "functions") == 0) {
+		parser->parsing_keywords = TRUE;
+	}
 	else if (g_ascii_strcasecmp (node_name, "chapters") == 0) {
 		parser->parsing_chapters = TRUE;
 	}
-	else if (g_ascii_strcasecmp (node_name, "functions") == 0) {
-		parser->parsing_keywords = TRUE;
+	if (!parser->book_node) {
+                parser_start_node_book (parser,
+                                        context,
+                                        node_name,
+                                        attribute_names,
+                                        attribute_values,
+                                        error);
+		return;
 	}
 }
 
@@ -429,7 +423,12 @@
 {
 	DhParser *parser = user_data;
 
-	if (parser->parsing_chapters) {
+        if (parser->parsing_keywords) {
+                if (g_ascii_strcasecmp (node_name, "functions") == 0) {
+			parser->parsing_keywords = FALSE;
+		}
+	}
+	else if (parser->parsing_chapters) {
 		g_node_reverse_children (parser->parent);
 		if (g_ascii_strcasecmp (node_name, "sub") == 0) {
 			parser->parent = parser->parent->parent;
@@ -439,15 +438,6 @@
 			parser->parsing_chapters = FALSE;
 		}
 	}
-	else if (parser->parsing_keywords) {
-		if (g_ascii_strcasecmp (node_name, "function") == 0) {
-			/* Do nothing */
-			return;
-		}
-		else if (g_ascii_strcasecmp (node_name, "functions") == 0) {
-			parser->parsing_keywords = FALSE;
-		}
-	}
 }
 
 static void
@@ -461,23 +451,15 @@
  	parser->context = NULL;
 }
 
-gboolean
-dh_parser_read_file (const gchar  *path,
+static gboolean
+parser_read_gz_file (const gchar  *path,
 		     GNode        *book_tree,
 		     GList       **keywords,
 		     GError      **error)
 {
-	DhParser   *parser;
-	GIOChannel *io;
-	gchar       buf[BYTES_PER_READ];
-	gboolean    result = TRUE;
-
-	if (g_str_has_suffix (path, ".gz")) {
-		return parser_read_gz_file (path,
-					    book_tree,
-					    keywords,
-					    error);
-	}
+	DhParser *parser;
+	gchar     buf[BYTES_PER_READ];
+	gzFile    file;
 
 	parser = g_new0 (DhParser, 1);
 	if (!parser) {
@@ -488,12 +470,6 @@
 		return FALSE;
 	}
 
-	if (g_str_has_suffix (path, ".devhelp2")) {
-		parser->version = 2;
-	} else {
-		parser->version = 1;
-	}
-
 	parser->m_parser = g_new0 (GMarkupParser, 1);
 	if (!parser->m_parser) {
 		g_free (parser);
@@ -504,6 +480,12 @@
 		return FALSE;
 	}
 
+	if (g_str_has_suffix (path, ".devhelp2")) {
+		parser->version = 2;
+	} else {
+		parser->version = 1;
+	}
+	
 	parser->m_parser->start_element = parser_start_node_cb;
 	parser->m_parser->end_element   = parser_end_node_cb;
 	parser->m_parser->error         = parser_error_cb;
@@ -521,57 +503,72 @@
 	parser->keywords  = keywords;
 
 	/* Parse the string */
-	io = g_io_channel_new_file (path, "r", error);
+	file = gzopen (path, "r");
 
-	if (!io) {
-		result = FALSE;
-		goto exit;
+	if (!file) {
+		g_markup_parse_context_free (parser->context);
+		g_free (parser);
+		g_set_error (error,
+			     DH_ERROR,
+			     DH_ERROR_FILE_NOT_FOUND,
+			     "%s", g_strerror (errno));
+		return FALSE;
 	}
 
 	while (TRUE) {
-		GIOStatus io_status;
-		gsize     bytes_read;
+		gsize bytes_read;
 
-		io_status = g_io_channel_read_chars (io, buf, BYTES_PER_READ,
-						     &bytes_read, error);
-		if (io_status == G_IO_STATUS_ERROR) {
-			result = FALSE;
-			goto exit;
-		}
-		if (io_status != G_IO_STATUS_NORMAL) {
-			break;
+		bytes_read = gzread (file, buf, BYTES_PER_READ);
+		if (bytes_read == -1) {
+			const gchar *message;
+			gint         err;
+
+			g_markup_parse_context_free (parser->context);
+			g_free (parser);
+			message = gzerror (file, &err);
+			g_set_error (error,
+				     DH_ERROR,
+				     DH_ERROR_INTERNAL_ERROR,
+				     _("Cannot uncompress book '%s': %s"),
+				     path, message);
+			return FALSE;
 		}
 
 		g_markup_parse_context_parse (parser->context, buf,
 					      bytes_read, error);
 		if (error != NULL && *error != NULL) {
-			result = FALSE;
-			goto exit;
+			return FALSE;
 		}
-
 		if (bytes_read < BYTES_PER_READ) {
 			break;
 		}
 	}
 
- exit:
-	g_io_channel_unref (io);
+	gzclose (file);
+
 	g_markup_parse_context_free (parser->context);
-	g_free (parser->m_parser);
 	g_free (parser);
 
-	return result;
+	return TRUE;
 }
 
-static gboolean
-parser_read_gz_file (const gchar  *path,
+gboolean
+dh_parser_read_file (const gchar  *path,
 		     GNode        *book_tree,
 		     GList       **keywords,
 		     GError      **error)
 {
-	DhParser *parser;
-	gchar     buf[BYTES_PER_READ];
-	gzFile    file;
+	DhParser   *parser;
+	GIOChannel *io;
+	gchar       buf[BYTES_PER_READ];
+	gboolean    result = TRUE;
+
+	if (g_str_has_suffix (path, ".gz")) {
+		return parser_read_gz_file (path,
+					    book_tree,
+					    keywords,
+					    error);
+	}
 
 	parser = g_new0 (DhParser, 1);
 	if (!parser) {
@@ -582,6 +579,12 @@
 		return FALSE;
 	}
 
+	if (g_str_has_suffix (path, ".devhelp2")) {
+		parser->version = 2;
+	} else {
+		parser->version = 1;
+	}
+
 	parser->m_parser = g_new0 (GMarkupParser, 1);
 	if (!parser->m_parser) {
 		g_free (parser);
@@ -592,12 +595,6 @@
 		return FALSE;
 	}
 
-	if (g_str_has_suffix (path, ".devhelp2")) {
-		parser->version = 2;
-	} else {
-		parser->version = 1;
-	}
-	
 	parser->m_parser->start_element = parser_start_node_cb;
 	parser->m_parser->end_element   = parser_end_node_cb;
 	parser->m_parser->error         = parser_error_cb;
@@ -615,66 +612,44 @@
 	parser->keywords  = keywords;
 
 	/* Parse the string */
-	file = gzopen (path, "r");
+	io = g_io_channel_new_file (path, "r", error);
 
-	if (!file) {
-		g_markup_parse_context_free (parser->context);
-		g_free (parser);
-		g_set_error (error,
-			     DH_ERROR,
-			     DH_ERROR_FILE_NOT_FOUND,
-			     "%s", g_strerror (errno));
-		return FALSE;
+	if (!io) {
+		result = FALSE;
+		goto exit;
 	}
 
 	while (TRUE) {
-		gsize bytes_read;
-
-		bytes_read = gzread (file, buf, BYTES_PER_READ);
-		if (bytes_read == -1) {
-			const gchar *message;
-			gint         err;
+		GIOStatus io_status;
+		gsize     bytes_read;
 
-			g_markup_parse_context_free (parser->context);
-			g_free (parser);
-			message = gzerror (file, &err);
-			g_set_error (error,
-				     DH_ERROR,
-				     DH_ERROR_INTERNAL_ERROR,
-				     _("Cannot uncompress book '%s': %s"),
-				     path, message);
-			return FALSE;
+		io_status = g_io_channel_read_chars (io, buf, BYTES_PER_READ,
+						     &bytes_read, error);
+		if (io_status == G_IO_STATUS_ERROR) {
+			result = FALSE;
+			goto exit;
+		}
+		if (io_status != G_IO_STATUS_NORMAL) {
+			break;
 		}
 
 		g_markup_parse_context_parse (parser->context, buf,
 					      bytes_read, error);
 		if (error != NULL && *error != NULL) {
-			return FALSE;
+			result = FALSE;
+			goto exit;
 		}
+
 		if (bytes_read < BYTES_PER_READ) {
 			break;
 		}
 	}
 
-	gzclose (file);
-
+ exit:
+	g_io_channel_unref (io);
 	g_markup_parse_context_free (parser->context);
+	g_free (parser->m_parser);
 	g_free (parser);
 
-	return TRUE;
-}
-
-
-static gchar *
-extract_page_name (const gchar *uri)
-{
-	gchar  *page = NULL;
-	gchar **split;
-
-	if ((split = g_strsplit (uri, ".", 2)) != NULL) {
-		page = split[0];
-                split[0] = NULL;
-		g_strfreev (split);
-	}
-	return page;
+	return result;
 }



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