[frogs] My First Patch: issue #1063

[ Thread Index | Date Index | More lilynet.net/frogs Archives ]


Hi all,
I made the little patch that should resolve #1063. Was made with diff
-n, and I tried my best to follow GNU coding standards. This is my
first attempt, so please be clement :).

The modifications work like this:
before reverting something, it makes sure another copy of the element
you are reverting is present in the alist, so that you never drop the
original value (this is what happens now when you revert a nested
property non previously overridden).

Comments are welcome! :)

Rodolfo
--- ORIG-nested-property.cc	2010-05-17 05:02:32.000000000 -0400
+++ nested-property.cc	2010-05-19 11:25:06.000000000 -0400
@@ -61,6 +61,8 @@
 SCM 
 nested_property_revert_alist (SCM alist, SCM prop_path)
 {
+  int copy_found = 0;
+  bool drop = false;
   assert(scm_is_pair (prop_path));
   
   SCM wanted_sym = scm_car (prop_path);
@@ -71,6 +73,7 @@
     {
       SCM sub_sym = scm_caar (s);
       SCM old_val = scm_cdar (s);
+      drop = false;
 
       if (sub_sym == wanted_sym)
 	{
@@ -84,22 +87,41 @@
 	      
 	      *tail = scm_acons (sub_sym, new_val, SCM_EOL);
 	      tail = SCM_CDRLOC(*tail);
+              *tail = scm_cdr (s);
+              return new_list;
 	    }
 	  else
 	    {
-	      /* old value is dropped. */
+              /* old value should be dropped only if we have another copy of it in the alist */
+              copy_found++;
+              /* Only drop the first instance found.
+               * the overridden value is always the first
+               * if this was the only copy, we will return
+               * the original list anyways so it is not relevant
+               * if we drop this pair
+               */
+              if (copy_found == 1)
+                drop = true;
 	    }
-	  
-	  *tail = scm_cdr (s);
-	  return new_list;
+	  /* we now iterate over every item */
 	}
-
-      *tail = scm_acons (sub_sym, old_val, SCM_EOL);
-      tail = SCM_CDRLOC (*tail);
+      /* Make a new list with every item
+       * except for the eventual dropped one
+       */
+      if (!drop) {
+        *tail = scm_acons (sub_sym, old_val, SCM_EOL);
+        tail = SCM_CDRLOC (*tail);
+      }
     }
 
-  /* Wanted symbol not found: drop newly constructed list. */
-  return alist;
+  /* if we find more than one copy of the property
+   * push the new list, else it means we are trying to
+   * revert the original value
+   */
+  if (copy_found > 1)
+    return new_list;
+  else
+    return alist;
 }
 
 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/