[gnome-games/wip/exalm/mame: 16/18] mame: Simplify parsing



commit 4fa3c4eb44c1daa4c3b514d1f743477805309f8c
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Thu Feb 6 02:34:54 2020 +0500

    mame: Simplify parsing
    
    Since the file only contains id and name now, get rid of the expensive
    regex and just use string splitting.
    
    Fixes https://gitlab.gnome.org/GNOME/gnome-games/issues/111

 plugins/mame/src/mame-game-info.vala        | 46 +++++++----------------------
 plugins/mame/src/mame-game-uri-adapter.vala |  3 +-
 2 files changed, 12 insertions(+), 37 deletions(-)
---
diff --git a/plugins/mame/src/mame-game-info.vala b/plugins/mame/src/mame-game-info.vala
index 2a7463de..967fb949 100644
--- a/plugins/mame/src/mame-game-info.vala
+++ b/plugins/mame/src/mame-game-info.vala
@@ -1,54 +1,30 @@
 // This file is part of GNOME Games. License: GPL-3.0+.
 
-private struct Games.MameGameInfo {
-       private static HashTable<string, MameGameInfo?> supported_games;
-       private static Regex game_regex;
+private class Games.MameGameInfo {
+       private static HashTable<string, string> supported_games;
 
-       public string id;
-       public string name;
-
-       public static HashTable<string, MameGameInfo?> get_supported_games () throws Error {
+       public static HashTable<string, string> get_supported_games () throws Error {
                if (supported_games != null)
                        return supported_games;
 
-               supported_games = new HashTable<string, MameGameInfo?> (str_hash, str_equal);
+               supported_games = new HashTable<string, string> (str_hash, str_equal);
 
                var bytes = resources_lookup_data ("/org/gnome/Games/plugins/mame/supported-games", 
ResourceLookupFlags.NONE);
                var text = (string) bytes.get_data ();
 
-               if (game_regex == null) {
-                       // Data is of the form: 
GAME[L](YEAR,NAME,PARENT,MACHINE,INPUT,CLASS,INIT,MONITOR,COMPANY,FULLNAME,FLAGS[,LAYOUT])
-                       var simple = " *([^,]+) *";
-                       var quoted = " *\" *(.*?) *\" *";
-                       var pattern = 
@"$simple,$simple,$simple,$simple,$simple,$simple,$simple,$simple,$quoted,$quoted,$simple(?:,$simple)?";
-                       game_regex = new Regex ("^GAMEL?\\(" + pattern + "\\) *$");
-               }
-
                var lines = text.split ("\n");
                foreach (var line in lines) {
-                       MatchInfo match_info;
-                       if (!game_regex.match (line, 0, out match_info))
+                       var parts = line.split (" ", 2);
+
+                       if (parts.length < 2)
                                continue;
 
-                       var game_info = MameGameInfo () {
-                               id = cleanup_string (match_info.fetch (2)), // NAME
-                               name = cleanup_string (match_info.fetch (10)) // FULLNAME
-                       };
-                       supported_games[game_info.id] = game_info;
+                       var id = parts[0];
+                       var name = parts[1];
+
+                       supported_games[id] = name;
                }
 
                return supported_games;
        }
-
-       private static Regex cleanup_string_regex;
-       private static string cleanup_string (string text) {
-               if (cleanup_string_regex == null)
-                       cleanup_string_regex = /^[\s"]*(.*?)[\s"]*$/;
-
-               MatchInfo match_info;
-               if (!cleanup_string_regex.match (text, 0, out match_info))
-                       return text;
-
-               return match_info.fetch (1) ?? text;
-       }
 }
diff --git a/plugins/mame/src/mame-game-uri-adapter.vala b/plugins/mame/src/mame-game-uri-adapter.vala
index e6a4268e..fc240914 100644
--- a/plugins/mame/src/mame-game-uri-adapter.vala
+++ b/plugins/mame/src/mame-game-uri-adapter.vala
@@ -20,8 +20,7 @@ private class Games.MameGameUriAdapter : GameUriAdapter, Object {
                var uid_string = @"mame-$game_id".down ();
                var uid = new GenericUid (uid_string);
 
-               var info = supported_games[game_id];
-               var title_string = info.name;
+               var title_string = supported_games[game_id];
                title_string = title_string.split ("(")[0];
                title_string = title_string.strip ();
                var title = new GenericTitle (title_string);


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