Enumerate log entries using delegates and lambdas
authorEnrico Zini <enrico@enricozini.org>
Sat, 27 Mar 2010 22:00:33 +0000 (22:00 +0000)
committerEnrico Zini <enrico@enricozini.org>
Sat, 27 Mar 2010 22:00:33 +0000 (22:00 +0000)
src/log.vala
src/zavai.vala

index 84b0d2056922e5db9ae4bf0393367d29758409b8..2f10dbc8b5766b69ffe60e728e0cc2870e86cced 100644 (file)
@@ -367,34 +367,27 @@ public class Logger : Resource, Object
         return parser.result;
     }
 
         return parser.result;
     }
 
-    protected size_t list_dir(string dir, ref List<string> res)
+    public delegate bool EntriesVisitor(string dir, string name);
+
+
+    protected void list_dir(string dir, EntriesVisitor visitor)
     {
     {
-        size_t count = 0;
         var d = File.new_for_path(dir);
         var enumerator = d.enumerate_children(FILE_ATTRIBUTE_STANDARD_NAME, 0, null);
         FileInfo file_info;
         while ((file_info = enumerator.next_file(null)) != null)
         {
             if (!file_info.get_name().has_suffix(".gpx")) continue;
         var d = File.new_for_path(dir);
         var enumerator = d.enumerate_children(FILE_ATTRIBUTE_STANDARD_NAME, 0, null);
         FileInfo file_info;
         while ((file_info = enumerator.next_file(null)) != null)
         {
             if (!file_info.get_name().has_suffix(".gpx")) continue;
-            res.append(file_info.get_name());
-            ++count;
+            if (!visitor(dir, file_info.get_name()))
+                break;
         }
         }
-        return count;
     }
 
     }
 
-    public string[] list_entries(bool only_unacked=true)
+    public void list_entries(EntriesVisitor visitor, bool only_unacked=true)
     {
     {
-        size_t count = 0;
-        List<string> entries = new List<string>();
         if (!only_unacked)
         if (!only_unacked)
-            count += list_dir(config.homedir + "/archive", ref entries);
-        count += list_dir(config.homedir + "/log", ref entries);
-        string[] res = new string[count+1];
-        size_t cur = 0;
-        for (weak List<string> i = entries; i != null; i = i.next)
-            res[cur++] = i.data;
-        res[cur] = null;
-        return res;
+            list_dir(config.homedir + "/archive", visitor);
+        list_dir(config.homedir + "/log", visitor);
     }
 
     public void instant(string tag, string msg)
     }
 
     public void instant(string tag, string msg)
index 29df675775917ad12bb4047c1e2c3fadd2cb73f8..caa6f3f166a2dcf9b0d3658313620a25f93940c7 100644 (file)
@@ -279,12 +279,14 @@ static int main (string[] args) {
 
     if (args.length > 2 && args[1] == "showlog")
     {
 
     if (args.length > 2 && args[1] == "showlog")
     {
-        string[] z = zavai.log.log.list_entries(false);
-        for (int i = 0; z[i] != null; ++i)
-            stderr.printf("FALSE %s\n", z[i]);
-        z = zavai.log.log.list_entries(true);
-        for (int i = 0; z[i] != null; ++i)
-            stderr.printf("TRUE %s\n", z[i]);
+        zavai.log.log.list_entries((d, f) => {
+            stderr.printf("FALSE %s %s\n", d, f);
+            return true;
+        }, false);
+        zavai.log.log.list_entries((d, f) => {
+            stderr.printf("TRUE %s %s\n", d, f);
+            return true;
+        }, true);
         zavai.log.Log l = zavai.log.log.load(args[2]);
         l.dump();
         return 0;
         zavai.log.Log l = zavai.log.log.load(args[2]);
         l.dump();
         return 0;