[gnome-games] utils: Add StringInputStream



commit 02bd45c1fd45782535300fbc458546f94550bf0f
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Thu Jul 28 12:17:09 2016 +0200

    utils: Add StringInputStream
    
    This will be used in next commits to read strings from files,
    refactoring code that is implemented in multiple places.

 src/Makefile.am                    |    1 +
 src/utils/string-input-stream.vala |   32 ++++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 1740e7a..57da1bb 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -116,6 +116,7 @@ gnome_games_SOURCES = \
        utils/generic-title.vala \
        utils/generic-uid.vala \
        utils/grep.vala \
+       utils/string-input-stream.vala \
        \
        config.vala \
        credits.vala \
diff --git a/src/utils/string-input-stream.vala b/src/utils/string-input-stream.vala
new file mode 100644
index 0000000..aeace73
--- /dev/null
+++ b/src/utils/string-input-stream.vala
@@ -0,0 +1,32 @@
+// This file is part of GNOME Games. License: GPLv3
+
+public class Games.StringInputStream : Object {
+       private File file;
+
+       public StringInputStream (File file) {
+               this.file = file;
+       }
+
+       public bool has_string (size_t offset, string value) throws Error {
+               return read_string_for_size (offset, value.length) == value;
+       }
+
+       public string read_string (size_t offset) throws Error {
+               var stream = new DataInputStream (file.read ());
+               stream.seek (offset, SeekType.SET);
+
+               size_t length;
+
+               return stream.read_upto ("\0", 1, out length);
+       }
+
+       public string read_string_for_size (size_t offset, size_t size) throws Error {
+               var stream = file.read ();
+               stream.seek (offset, SeekType.SET);
+
+               var buffer = new uint8[size];
+               stream.read (buffer);
+
+               return (string) buffer;
+       }
+}


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