[gnome-games] master-system: Use StringInputStream in MasterSystemHeader
- From: Adrien Plazas <aplazas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-games] master-system: Use StringInputStream in MasterSystemHeader
- Date: Thu, 28 Jul 2016 15:24:58 +0000 (UTC)
commit 93cd92994979fdf02364377a7c0880dd77abb611
Author: Adrien Plazas <kekun plazas laposte net>
Date: Thu Jul 28 17:06:52 2016 +0200
master-system: Use StringInputStream in MasterSystemHeader
This makes the code easier to read and avoid duplication.
.../master-system/src/master-system-header.vala | 40 ++++++++------------
1 files changed, 16 insertions(+), 24 deletions(-)
---
diff --git a/plugins/master-system/src/master-system-header.vala
b/plugins/master-system/src/master-system-header.vala
index 08cf5da..ded175f 100644
--- a/plugins/master-system/src/master-system-header.vala
+++ b/plugins/master-system/src/master-system-header.vala
@@ -14,6 +14,16 @@ private class Games.MasterSystemHeader : Object {
if (_region_code != null)
return _region_code;
+ FileInputStream stream;
+ try {
+ stream = file.read ();
+ }
+ catch (Error e) {
+ _region_code = MasterSystemRegion.INVALID;
+
+ return _region_code;
+ }
+
try {
stream.seek (REGION_CODE_OFFSET, SeekType.SET);
}
@@ -47,32 +57,15 @@ private class Games.MasterSystemHeader : Object {
}
}
- private FileInputStream stream;
+ private File file;
- public MasterSystemHeader (File file) throws Error {
- stream = file.read ();
-
- check_validity ();
+ public MasterSystemHeader (File file) {
+ this.file = file;
}
- public void check_validity () throws MasterSystemError {
- try {
- stream.seek (MAGIC_OFFSET, SeekType.SET);
- }
- catch (Error e) {
- throw new MasterSystemError.INVALID_SIZE (_("Invalid Master System header size: %s"),
e.message);
- }
-
- var buffer = new uint8[MAGIC_VALUE.length];
- try {
- stream.read (buffer);
- }
- catch (Error e) {
- throw new MasterSystemError.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 MasterSystemError.INVALID_HEADER (_("The file doesn't have a Master System
header."));
}
}
@@ -87,6 +80,5 @@ private enum Games.MasterSystemRegion {
}
errordomain Games.MasterSystemError {
- INVALID_SIZE,
INVALID_HEADER,
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]