Wireshark-dev: [Wireshark-dev] [PATCH] ptvcursor and subtrees
From: Sebastien Tandel <sebastien@xxxxxxxxx>
Date: Fri, 08 Dec 2006 17:51:08 +0100
Hi,

It is a re-post. Can you consider it, please?

Thx,

Sebastien

------
Hi,

   here is a patch which adds the support to ptvcursor to create one
level of subtree which helps to reduce/simplify the code written with ptvcursor.

- instead of writing :

gint current_offset;
current_offset = ptvcursor_current_offset(cursor);

subtree = proto_item_add_subtree(it, ett_sub);
ptvcursor_new(subtree, ptvcursor_tvb(cursor),  current_offset); 
...
ptvcursor_free(cursor);


* or *

proto_tree * initial_tree = proto_tree(cursor);
subtree = proto_item_add_subtree(it, ett_sub);
ptvcursor_set_tree(cursor, subtree);
...
ptvcursor_set_tree(initial_tree);


- you may write now :

ptvcursor_push_subtree(cursor, it, ett_sub);
...
ptvcursor_pop_subtree(cursor);



As it is only a one level of push/pop, I also add a function
ptvcursor_set_subtree(cursor, it, ett_sub) which only sets the subtree
without saving the old one. it is useful only in this kind to reach a
fake second level of subtree ... should no more exists with multi-level
support.


With this simple feature, I don't have the need for a piece of the old
code shown above in the plugin for Homeplug.

it is possible to extend it to support multiple level of subtrees but
remains the question of fixed-length table or a dynamic one?



Sebastien

Index: epan/proto.c
===================================================================
--- epan/proto.c	(révision 20052)
+++ epan/proto.c	(copie de travail)
@@ -45,6 +45,8 @@
 #include "emem.h"
 
 struct ptvcursor {
+	/* Support for only one push/pop */
+	proto_tree	*pushed_tree;
 	proto_tree	*tree;
 	tvbuff_t	*tvb;
 	gint		offset;
@@ -612,6 +614,7 @@
 	ptvc->tree	= tree;
 	ptvc->tvb	= tvb;
 	ptvc->offset	= offset;
+	ptvc->pushed_tree= NULL;
 	return ptvc;
 }
 
@@ -648,6 +651,37 @@
 	ptvc->tree = tree;
 }
 
+/* creates a subtree, sets it as the working tree and save the old working tree */ 
+proto_tree* 
+ptvcursor_push_subtree(ptvcursor_t *ptvc, proto_item *it, gint ett_subtree) 
+{
+  ptvc->pushed_tree = ptvc->tree;
+  return ptvcursor_set_subtree(ptvc, it, ett_subtree);
+}
+
+/* pops a subtree */
+void 
+ptvcursor_pop_subtree(ptvcursor_t *ptvc) 
+{
+  if (ptvc->pushed_tree == NULL)
+    return;
+    
+  ptvc->tree = ptvc->pushed_tree;
+  ptvc->pushed_tree = NULL;
+}
+
+/* Creates a subtree and adds it to the cursor as the working tree but does not
+ * save the old working tree */
+proto_tree* 
+ptvcursor_set_subtree(ptvcursor_t *ptvc, proto_item *it, gint ett_subtree) 
+{
+  ptvc->tree = proto_item_add_subtree(it, ett_subtree);
+  return ptvc->tree;
+}
+
 /* Add a text-only node, leaving it to our caller to fill the text in */
 static proto_item *
 proto_tree_add_text_node(proto_tree *tree, tvbuff_t *tvb, gint start, gint length)
Index: epan/ptvcursor.h
===================================================================
--- epan/ptvcursor.h	(révision 20052)
+++ epan/ptvcursor.h	(copie de travail)
@@ -77,4 +77,16 @@
 void
 ptvcursor_set_tree(ptvcursor_t* ptvc, proto_tree *tree);
 
+/* push a subtree in the tree stack of the cursor */
+proto_tree* 
+ptvcursor_push_subtree(ptvcursor_t *ptvc, proto_item *it, gint ett_subtree);
+
+/* pop a subtree in the tree stack of the cursor */
+void ptvcursor_pop_subtree(ptvcursor_t *ptvc);
+
+/* Creates a subtree and adds it to the cursor as the working tree but does not
+ * save the old working tree */
+proto_tree* 
+ptvcursor_set_subtree(ptvcursor_t *ptvc, proto_item *it, gint ett_subtree); 
+
 #endif /* __PTVCURSOR_H__ */