[gnome-games] game-boy: Use StringInputStream in GameBoyHeader



commit 6dc88cc241436852a36ecb36cb6199892156fbff
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Thu Jul 28 16:58:50 2016 +0200

    game-boy: Use StringInputStream in GameBoyHeader
    
    This makes the code easier to read and avoid duplication.

 plugins/game-boy/src/game-boy-header.vala |   43 +++++++++++------------------
 1 files changed, 16 insertions(+), 27 deletions(-)
---
diff --git a/plugins/game-boy/src/game-boy-header.vala b/plugins/game-boy/src/game-boy-header.vala
index 52f9cd1..27f1acf 100644
--- a/plugins/game-boy/src/game-boy-header.vala
+++ b/plugins/game-boy/src/game-boy-header.vala
@@ -19,6 +19,16 @@ private class Games.GameBoyHeader : Object {
                        if (_game_boy_type != null)
                                return _game_boy_type;
 
+                       FileInputStream stream;
+                       try {
+                               stream = file.read ();
+                       }
+                       catch (Error e) {
+                               _game_boy_type = GameBoyType.INVALID;
+
+                               return _game_boy_type;
+                       }
+
                        uint8 buffer[1];
 
                        uint8 color_value = 0;
@@ -71,39 +81,19 @@ private class Games.GameBoyHeader : Object {
                                break;
                        }
 
-                       if (_game_boy_type == null)
-                               _game_boy_type = GameBoyType.INVALID;
-
                        return _game_boy_type;
                }
        }
 
-       private FileInputStream stream;
-
-       public GameBoyHeader (File file) throws Error {
-               stream = file.read ();
+       private File file;
 
-               check_validity ();
+       public GameBoyHeader (File file) {
+               this.file = file;
        }
 
-       public void check_validity () throws GameBoyError {
-               try {
-                       stream.seek (MAGIC_OFFSET, SeekType.SET);
-               }
-               catch (Error e) {
-                       throw new GameBoyError.INVALID_SIZE (_("Invalid Game Boy header size: %s"), 
e.message);
-               }
-
-               var buffer = new uint8[MAGIC_VALUE.length];
-               try {
-                       stream.read (buffer);
-               }
-               catch (Error e) {
-                       throw new GameBoyError.INVALID_SIZE (e.message);
-               }
-
-               var magic = (string) buffer;
-               if (magic != MAGIC_VALUE)
+       public void check_validity () throws Error {
+               var stream = new StringInputStream (file);
+               if (!stream.has_string (MAGIC_OFFSET, MAGIC_VALUE))
                        throw new GameBoyError.INVALID_HEADER (_("The file doesn't have a Game Boy header."));
        }
 }
@@ -118,6 +108,5 @@ private enum Games.GameBoyType {
 }
 
 errordomain Games.GameBoyError {
-       INVALID_SIZE,
        INVALID_HEADER,
 }


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