[retro-gtk] game-info: Simplify construction



commit cc831f380cbe7369c0ec6b0309db36b88cddccb4
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Sun Apr 11 09:46:46 2021 +0200

    game-info: Simplify construction
    
    This merges the two constructor into one, directly taking a URI to match
    the data we actually have to simplify usage of the constructor.

 retro-runner/retro-core.c              | 16 +++-----------
 retro-runner/retro-game-info-private.h |  6 +++---
 retro-runner/retro-game-info.c         | 39 ++++++++++++++++++----------------
 3 files changed, 27 insertions(+), 34 deletions(-)
---
diff --git a/retro-runner/retro-core.c b/retro-runner/retro-core.c
index a1f86d2..784b182 100644
--- a/retro-runner/retro-core.c
+++ b/retro-runner/retro-core.c
@@ -870,12 +870,9 @@ load_discs (RetroCore  *self,
 
   fullpath = get_needs_full_path (self);
   for (gsize index = 0; index < length; index++) {
-    g_autoptr (GFile) file = g_file_new_for_uri (self->media_uris[index]);
-    g_autofree gchar *path = g_file_get_path (file);
-    g_autoptr (RetroGameInfo) game_info = fullpath ?
-      retro_game_info_new (path) :
-      retro_game_info_new_with_data (path, &tmp_error);
+    g_autoptr (RetroGameInfo) game_info = NULL;
 
+    game_info = retro_game_info_new (self->media_uris[index], fullpath, &tmp_error);
     if (G_UNLIKELY (tmp_error != NULL)) {
       g_propagate_error (error, tmp_error);
 
@@ -951,8 +948,6 @@ load_medias (RetroCore  *self,
              GError    **error)
 {
   guint length;
-  g_autoptr (GFile) file = NULL;
-  g_autofree gchar *path = NULL;
   g_autoptr (RetroGameInfo) game_info = NULL;
   GError *tmp_error = NULL;
 
@@ -964,12 +959,7 @@ load_medias (RetroCore  *self,
     return;
   }
 
-  file = g_file_new_for_uri (self->media_uris[0]);
-  path = g_file_get_path (file);
-  game_info = get_needs_full_path (self) ?
-    retro_game_info_new (path) :
-    retro_game_info_new_with_data (path, &tmp_error);
-
+  game_info = retro_game_info_new (self->media_uris[0], get_needs_full_path (self), &tmp_error);
   if (G_UNLIKELY (tmp_error != NULL)) {
     g_propagate_error (error, tmp_error);
 
diff --git a/retro-runner/retro-game-info-private.h b/retro-runner/retro-game-info-private.h
index a8745de..9432c7d 100644
--- a/retro-runner/retro-game-info-private.h
+++ b/retro-runner/retro-game-info-private.h
@@ -22,9 +22,9 @@ struct _RetroGameInfo
   gchar *meta;
 };
 
-RetroGameInfo *retro_game_info_new (const gchar *file_name);
-RetroGameInfo * retro_game_info_new_with_data (const gchar  *file_name,
-                                               GError      **error);
+RetroGameInfo *retro_game_info_new (const gchar  *uri,
+                                    gboolean      needs_full_path,
+                                    GError      **error);
 RetroGameInfo *retro_game_info_copy (RetroGameInfo *self);
 void retro_game_info_free (RetroGameInfo *self);
 
diff --git a/retro-runner/retro-game-info.c b/retro-runner/retro-game-info.c
index 1edd74a..f5c84ec 100644
--- a/retro-runner/retro-game-info.c
+++ b/retro-runner/retro-game-info.c
@@ -2,35 +2,38 @@
 
 #include "retro-game-info-private.h"
 
+#include <gio/gio.h>
+
 G_DEFINE_BOXED_TYPE (RetroGameInfo, retro_game_info, retro_game_info_copy, retro_game_info_free)
 
 RetroGameInfo *
-retro_game_info_new (const gchar *file_name)
+retro_game_info_new (const gchar  *uri,
+                     gboolean      needs_full_path,
+                     GError      **error)
 {
-  RetroGameInfo *self;
+  g_autoptr (RetroGameInfo) self = NULL;
+  g_autoptr (GFile) file = NULL;
 
-  g_return_val_if_fail (file_name != NULL, NULL);
+  g_return_val_if_fail (uri != NULL, NULL);
 
   self = g_slice_new0 (RetroGameInfo);
 
-  self->path = g_strdup (file_name);
-  self->data = g_new0 (guint8, 0);
-  return self;
-}
-
-RetroGameInfo *
-retro_game_info_new_with_data (const gchar  *file_name,
-                               GError      **error)
-{
-  RetroGameInfo *self;
+  file = g_file_new_for_uri (uri);
+  self->path = g_file_get_path (file);
+  if (needs_full_path)
+    self->data = g_new0 (guint8, 0);
+  else {
+    GError *tmp_error = NULL;
 
-  g_return_val_if_fail (file_name != NULL, NULL);
+    g_file_get_contents (self->path, (gchar **) &self->data, &self->size, &tmp_error);
+    if (G_UNLIKELY (tmp_error != NULL)) {
+      g_propagate_error (error, tmp_error);
 
-  self = g_slice_new0 (RetroGameInfo);
+      return NULL;
+    }
+  }
 
-  self->path = g_strdup (file_name);
-  g_file_get_contents (file_name, (gchar **) &self->data, &self->size, error);
-  return self;
+  return g_steal_pointer (&self);
 }
 
 RetroGameInfo *


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