[gmime] Updated mime-part iters to be more consistent with IMAP



commit 226fc5adad261bc6d20afded3f05b4148f7701d4
Author: Jeffrey Stedfast <fejj gnome org>
Date:   Sun Aug 14 07:53:14 2011 -0400

    Updated mime-part iters to be more consistent with IMAP
    
    2011-08-14  Jeffrey Stedfast  <fejj gnome org>
    
    	* gmime/gmime-part-iter.c: Updated to be more consistent with
    	IMAP part specifiers.

 ChangeLog                         |    5 ++
 docs/reference/gmime-sections.txt |    1 +
 gmime/gmime-part-iter.c           |  140 +++++++++++++------------------------
 gmime/gmime-part-iter.h           |    1 +
 tests/test-parser.c               |   74 +++++++++-----------
 5 files changed, 88 insertions(+), 133 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 487d0b5..3e45fd8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2011-08-14  Jeffrey Stedfast  <fejj gnome org>
+
+	* gmime/gmime-part-iter.c: Updated to be more consistent with IMAP
+	part specifiers.
+
 2011-08-13  Jeffrey Stedfast  <fejj gnome org>
 
 	* gmime/gmime-part-iter.c: New class for iterating over a tree of
diff --git a/docs/reference/gmime-sections.txt b/docs/reference/gmime-sections.txt
index 7e53d8e..2c7ecbc 100644
--- a/docs/reference/gmime-sections.txt
+++ b/docs/reference/gmime-sections.txt
@@ -756,6 +756,7 @@ g_mime_part_iter_jump_to
 g_mime_part_iter_is_valid
 g_mime_part_iter_next
 g_mime_part_iter_prev
+g_mime_part_iter_get_toplevel
 g_mime_part_iter_get_current
 g_mime_part_iter_get_parent
 g_mime_part_iter_get_path
diff --git a/gmime/gmime-part-iter.c b/gmime/gmime-part-iter.c
index 0d8da7d..f7fa857 100644
--- a/gmime/gmime-part-iter.c
+++ b/gmime/gmime-part-iter.c
@@ -72,12 +72,15 @@ g_mime_part_iter_push (GMimePartIter *iter, GMimeObject *object, int index)
 	iter->parent = node;
 }
 
-static void
+static gboolean
 g_mime_part_iter_pop (GMimePartIter *iter)
 {
 	GMimeObjectStack *node;
 	GMimeObject *object;
 	
+	if (!iter->parent || !iter->parent->parent)
+		return FALSE;
+	
 	iter->index = g_array_index (iter->path, int, iter->path->len - 1);
 	g_array_set_size (iter->path, iter->path->len - 1);
 	
@@ -86,6 +89,8 @@ g_mime_part_iter_pop (GMimePartIter *iter)
 	node = iter->parent;
 	iter->parent = node->parent;
 	g_slice_free (GMimeObjectStack, node);
+	
+	return TRUE;
 }
 
 
@@ -155,7 +160,10 @@ g_mime_part_iter_reset (GMimePartIter *iter)
 	g_slice_free_chain (GMimeObjectStack, iter->parent, parent);
 	g_array_set_size (iter->path, 0);
 	iter->parent = NULL;
-	iter->index = 0;
+	iter->index = -1;
+	
+	/* set our initial 'current' part to our first child */
+	g_mime_part_iter_next (iter);
 }
 
 
@@ -177,8 +185,9 @@ g_mime_part_iter_jump_to (GMimePartIter *iter, const char *path)
 	GMimeMultipart *multipart;
 	GMimeMessage *message;
 	GMimeObject *current;
+	GMimeObject *parent;
 	const char *inptr;
-	long int index;
+	int index;
 	char *dot;
 	
 	g_return_val_if_fail (iter != NULL, FALSE);
@@ -188,51 +197,40 @@ g_mime_part_iter_jump_to (GMimePartIter *iter, const char *path)
 	if (!path || !path[0])
 		return TRUE;
 	
-	current = iter->toplevel;
+	parent = iter->parent->object;
 	iter->current = NULL;
+	current = NULL;
 	inptr = path;
+	index = -1;
 	
 	while (*inptr) {
 		/* Note: path components are 1-based instead of 0-based */
 		if ((index = strtol (inptr, &dot, 10)) <= 0 || errno == ERANGE ||
-		    index > G_MAXINT || !(*dot == '.' || *dot == '\0'))
+		    index == G_MAXINT || !(*dot == '.' || *dot == '\0'))
 			return FALSE;
 		
 		/* normalize to a 0-based index */
 		index--;
 		
-		if (GMIME_IS_MESSAGE_PART (current)) {
+		if (GMIME_IS_MESSAGE_PART (parent)) {
 			if (index != 0)
 				return FALSE;
 			
-			message_part = (GMimeMessagePart *) current;
+			message_part = (GMimeMessagePart *) parent;
 			if (!(message = g_mime_message_part_get_message (message_part)))
 				return FALSE;
 			
-			g_mime_part_iter_push (iter, current, iter->index);
-			
 			if (!(current = g_mime_message_get_mime_part (message)))
 				return FALSE;
 			
 			iter->index = 0;
-		} else if (GMIME_IS_MULTIPART (current)) {
-			multipart = (GMimeMultipart *) current;
+		} else if (GMIME_IS_MULTIPART (parent)) {
+			multipart = (GMimeMultipart *) parent;
 			if (index >= g_mime_multipart_get_count (multipart))
 				return FALSE;
 			
-			g_mime_part_iter_push (iter, current, iter->index);
-			
 			current = g_mime_multipart_get_part (multipart, index);
 			iter->index = index;
-		} else if (GMIME_IS_MESSAGE (current)) {
-			/* Note: it should only be possible to get here on the first iteration */
-			if (index != 0)
-				return FALSE;
-			
-			if (!(current = g_mime_message_get_mime_part ((GMimeMessage *) current)))
-				return FALSE;
-			
-			iter->index = 0;
 		} else {
 			return FALSE;
 		}
@@ -240,6 +238,11 @@ g_mime_part_iter_jump_to (GMimePartIter *iter, const char *path)
 		if (*dot != '.')
 			break;
 		
+		g_mime_part_iter_push (iter, current, iter->index);
+		parent = current;
+		current = NULL;
+		index = -1;
+		
 		inptr = dot + 1;
 	}
 	
@@ -261,70 +264,9 @@ g_mime_part_iter_jump_to (GMimePartIter *iter, const char *path)
 gboolean
 g_mime_part_iter_is_valid (GMimePartIter *iter)
 {
-	GMimeMessagePart *message_part;
-	GMimeMultipart *multipart;
-	GMimeMessage *message;
-	GMimeObject *current;
-	int i, index;
-	
 	g_return_val_if_fail (iter != NULL, FALSE);
 	
-	/* quick check */
-	if (iter->current == NULL)
-		return FALSE;
-
-#ifdef EXPENSIVE_ITER_VALIDATION
-	/* find our root part */
-	if (GMIME_IS_MESSAGE (iter->toplevel)) {
-		message = (GMimeMessage *) iter->toplevel;
-		current = g_mime_message_get_mime_part (message);
-	} else {
-		current = iter->toplevel;
-	}
-	
-	for (i = 0; i < iter->path->len; i++) {
-		index = g_array_index (iter->path, int, i);
-		if (GMIME_IS_MESSAGE_PART (current)) {
-			if (index != 0)
-				return FALSE;
-			
-			message_part = (GMimeMessagePart *) current;
-			message = g_mime_message_part_get_message (message_part);
-			current = g_mime_message_get_mime_part (message);
-		} else if (GMIME_IS_MULTIPART (current)) {
-			multipart = (GMimeMultipart *) current;
-			if (index >= g_mime_multipart_get_count (multipart))
-				return FALSE;
-			
-			current = g_mime_multipart_get_part (multipart, index);
-		} else {
-			return FALSE;
-		}
-	}
-	
-	index = iter->index;
-	if (GMIME_IS_MESSAGE_PART (current)) {
-		if (index != 0)
-			return FALSE;
-		
-		message_part = (GMimeMessagePart *) current;
-		message = g_mime_message_part_get_message (message_part);
-		current = g_mime_message_get_mime_part (message);
-	} else if (GMIME_IS_MULTIPART (current)) {
-		multipart = (GMimeMultipart *) current;
-		if (index >= g_mime_multipart_get_count (multipart))
-			return FALSE;
-		
-		current = g_mime_multipart_get_part (multipart, index);
-	} else if (index != 0) {
-		return FALSE;
-	}
-	
-	if (current != iter->current)
-		return FALSE;
-#endif /* EXPENSIVE_ITER_VALIDATION */
-	
-	return TRUE;
+	return iter->current != NULL;
 }
 
 
@@ -383,7 +325,8 @@ g_mime_part_iter_next (GMimePartIter *iter)
 			}
 		}
 		
-		g_mime_part_iter_pop (iter);
+		if (!g_mime_part_iter_pop (iter))
+			break;
 	}
 	
 	iter->current = NULL;
@@ -428,9 +371,24 @@ g_mime_part_iter_prev (GMimePartIter *iter)
 		}
 	}
 	
-	g_mime_part_iter_pop (iter);
+	return g_mime_part_iter_pop (iter);
+}
+
+
+/**
+ * g_mime_part_iter_get_toplevel:
+ * @iter: a #GMimePartIter
+ *
+ * Gets the toplevel #GMimeObject used to initialize @iter.
+ *
+ * Returns: the toplevel #GMimeObject.
+ **/
+GMimeObject *
+g_mime_part_iter_get_toplevel (GMimePartIter *iter)
+{
+	g_return_val_if_fail (iter != NULL, NULL);
 	
-	return TRUE;
+	return iter->toplevel;
 }
 
 
@@ -446,8 +404,7 @@ g_mime_part_iter_prev (GMimePartIter *iter)
 GMimeObject *
 g_mime_part_iter_get_current (GMimePartIter *iter)
 {
-	if (!g_mime_part_iter_is_valid (iter))
-		return NULL;
+	g_return_val_if_fail (iter != NULL, NULL);
 	
 	return iter->current;
 }
@@ -466,10 +423,9 @@ g_mime_part_iter_get_current (GMimePartIter *iter)
 GMimeObject *
 g_mime_part_iter_get_parent (GMimePartIter *iter)
 {
-	if (!g_mime_part_iter_is_valid (iter))
-		return NULL;
+	g_return_val_if_fail (iter != NULL, NULL);
 	
-	return iter->parent ? iter->parent->object : iter->toplevel;
+	return iter->parent->object;
 }
 
 
diff --git a/gmime/gmime-part-iter.h b/gmime/gmime-part-iter.h
index 548ad29..45f7d5f 100644
--- a/gmime/gmime-part-iter.h
+++ b/gmime/gmime-part-iter.h
@@ -45,6 +45,7 @@ gboolean g_mime_part_iter_is_valid (GMimePartIter *iter);
 gboolean g_mime_part_iter_next (GMimePartIter *iter);
 gboolean g_mime_part_iter_prev (GMimePartIter *iter);
 
+GMimeObject *g_mime_part_iter_get_toplevel (GMimePartIter *iter);
 GMimeObject *g_mime_part_iter_get_current (GMimePartIter *iter);
 GMimeObject *g_mime_part_iter_get_parent (GMimePartIter *iter);
 char *g_mime_part_iter_get_path (GMimePartIter *iter);
diff --git a/tests/test-parser.c b/tests/test-parser.c
index 23f9a3c..7ea07d6 100644
--- a/tests/test-parser.c
+++ b/tests/test-parser.c
@@ -91,66 +91,58 @@ print_mime_struct (GMimeObject *part, int depth)
 }
 
 static void
-print_mime_struct_iter (GMimeMessage *message)
+print_mime_part_info (const char *path, GMimeObject *object)
 {
 	const GMimeContentType *type;
+	gboolean has_md5;
+	
+	type = g_mime_object_get_content_type (object);
+	
+	if (GMIME_IS_PART (object))
+		has_md5 = g_mime_object_get_header (object, "Content-Md5") != NULL;
+	else
+		has_md5 = FALSE;
+	
+	fprintf (stdout, "%s\tContent-Type: %s/%s%s", path,
+		 type->type, type->subtype, has_md5 ? "; md5sum=" : "\n");
+	
+	if (has_md5) {
+		/* validate the Md5 sum */
+		if (g_mime_part_verify_content_md5 ((GMimePart *) object))
+			fprintf (stdout, "GOOD\n");
+		else
+			fprintf (stdout, "BAD\n");
+	}
+}
+
+static void
+print_mime_struct_iter (GMimeMessage *message)
+{
 	GMimePartIter *iter;
 	GMimeObject *part;
-	gboolean has_md5;
 	char *path;
 	
 	iter = g_mime_part_iter_new ((GMimeObject *) message);
 	
+	part = g_mime_part_iter_get_parent (iter);
+	print_mime_part_info ("", part);
+	
 	do {
 		part = g_mime_part_iter_get_current (iter);
-		type = g_mime_object_get_content_type (part);
 		path = g_mime_part_iter_get_path (iter);
-		
-		if (GMIME_IS_PART (part))
-			has_md5 = g_mime_object_get_header (part, "Content-Md5") != NULL;
-		else
-			has_md5 = FALSE;
-		
-		fprintf (stdout, "%s\tContent-Type: %s/%s%s", path,
-			 type->type, type->subtype, has_md5 ? "; md5sum=" : "\n");
-		
-		if (has_md5) {
-			/* validate the Md5 sum */
-			if (g_mime_part_verify_content_md5 ((GMimePart *) part))
-				fprintf (stdout, "GOOD\n");
-			else
-				fprintf (stdout, "BAD\n");
-		}
-		
+		print_mime_part_info (path, part);
 		g_free (path);
 	} while (g_mime_part_iter_next (iter));
 
 #if 0
-	fprintf (stdout, "Jumping to 1.1.2\n");
-	if (g_mime_part_iter_jump_to (iter, "1.1.2")) {
+	fprintf (stdout, "Jumping to 1.2\n");
+	if (g_mime_part_iter_jump_to (iter, "1.2")) {
 		part = g_mime_part_iter_get_current (iter);
-		type = g_mime_object_get_content_type (part);
 		path = g_mime_part_iter_get_path (iter);
-		
-		if (GMIME_IS_PART (part))
-			has_md5 = g_mime_object_get_header (part, "Content-Md5") != NULL;
-		else
-			has_md5 = FALSE;
-		
-		fprintf (stdout, "%s\tContent-Type: %s/%s%s", path,
-			 type->type, type->subtype, has_md5 ? "; md5sum=" : "\n");
-		
-		if (has_md5) {
-			/* validate the Md5 sum */
-			if (g_mime_part_verify_content_md5 ((GMimePart *) part))
-				fprintf (stdout, "GOOD\n");
-			else
-				fprintf (stdout, "BAD\n");
-		}
-		
+		print_mime_part_info (path, part);
 		g_free (path);
 	} else {
-		fprintf (stdout, "Failed to jump to 1.1.2\n");
+		fprintf (stdout, "Failed to jump to 1.2\n");
 	}
 #endif
 	



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