[gnome-robots] Rewrite find-file in Vala



commit 6a679a40e26429b625b818642a73e6c8d37ae490
Author: Andrey Kutejko <andy128k gmail com>
Date:   Sun Aug 23 16:12:13 2020 +0200

    Rewrite find-file in Vala

 src/file-list.vala | 15 +---------
 src/find-file.c    | 84 ------------------------------------------------------
 src/find-file.h    | 31 --------------------
 src/find-file.vala | 47 ++++++++++++++++++++++++++++++
 src/graphics.c     |  2 +-
 src/meson.build    |  2 +-
 6 files changed, 50 insertions(+), 131 deletions(-)
---
diff --git a/src/file-list.vala b/src/file-list.vala
index cc8d682..a8bc5e3 100644
--- a/src/file-list.vala
+++ b/src/file-list.vala
@@ -86,19 +86,6 @@ public class GamesFileList {
         return files.size;
     }
 
-    /**
-     * @function: (scope call): The function to call on each item. It gets called with two
-     * arguments: the file name and the pointer supplied to this function in
-     * the userdata argument.
-     * @userdata: (closure): An arbitrary pointer that gets passed as the second argument
-     * to each call of function.
-     *
-     * Apply a function to each file name in the list.
-     **/
-    //public void @foreach (Func<string> func) {
-    //    files.@foreach (func);
-    //}
-
     /**
      * @function: (scope call): The function to call on each item. It gets called with two
      * arguments: the file name and the pointer supplied to this function in
@@ -134,7 +121,7 @@ public class GamesFileList {
              * library in glib. There are probably some ways these could
              * seriously mangle unicode strings. */
             if (Flags.REMOVE_EXTENSION in flags) {
-                var s = visible.last_index_of (".");
+                var s = visible.last_index_of_char ('.');
                 if (s >= 0)
                     visible = visible.substring (0, s);
             }
diff --git a/src/find-file.vala b/src/find-file.vala
new file mode 100644
index 0000000..53adfae
--- /dev/null
+++ b/src/find-file.vala
@@ -0,0 +1,47 @@
+/* find-file.vala:
+
+   Copyright 2006 Callum McKenzie
+
+   This library is free software; you can redistribute it and'or modify
+   it under the terms of the GNU Library General Public License as published
+   by the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public License
+   along with this library; if not, see <http://www.gnu.org/licenses/>.
+
+   Authors:   Callum McKenzie <callum spooky-possum org>
+*/
+
+string make_canonical_name (string name) {
+    /* Strip the path. */
+    var cname = Path.get_basename (name);
+    /* Strip the suffix. */
+    var s = cname.last_index_of_char ('.');
+    if (s >= 0)
+        cname = cname.substring (0, s);
+
+    /* Remove case-sensitivity. */
+    cname = cname.casefold ();
+    /* Normalise the UTF-8 encoding. */
+    cname = cname.normalize (-1, NormalizeMode.ALL);
+
+    return cname;
+}
+
+int compare_names (string filename, string ctarget) {
+    var cname = make_canonical_name (filename);
+    return cname.collate (ctarget);
+}
+
+public string games_find_similar_file (string target, string directory) {
+    var ctarget = make_canonical_name (target);
+    var list = new GamesFileList ("*", directory);
+    return list.find (compare_names, ctarget);
+}
+
diff --git a/src/graphics.c b/src/graphics.c
index fcbd2ab..6be5b2e 100644
--- a/src/graphics.c
+++ b/src/graphics.c
@@ -31,7 +31,7 @@
 #include <time.h>
 #include <dirent.h>
 
-#include "find-file.h"
+#include "riiv.h"
 #include "graphics.h"
 #include "gbdefs.h"
 #include "game.h"
diff --git a/src/meson.build b/src/meson.build
index 0e5f21f..4946d13 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -1,6 +1,7 @@
 vala_sources = files(
     'image-suffix-list.vala',
     'file-list.vala',
+    'find-file.vala',
 )
 
 vala_lib = static_library('riiv',
@@ -23,7 +24,6 @@ riiv_dependency = declare_dependency(
 
 sources = files(
     'cursors.c',
-    'find-file.c',
     'game.c',
     'gameconfig.c',
     'games-controls.c',


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