[gnome-games] utils: Add the Uri class
- From: Adrien Plazas <aplazas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-games] utils: Add the Uri class
- Date: Sat, 6 May 2017 07:58:06 +0000 (UTC)
commit 1dee7c3474e467a9eb2f69608ca8ee2009ce57f8
Author: Adrien Plazas <kekun plazas laposte net>
Date: Sat May 6 07:22:02 2017 +0200
utils: Add the Uri class
This allows to handle URIs more easily, avoids some useless string
copies and makes the type checking stronger.
https://bugzilla.gnome.org/show_bug.cgi?id=781334
src/Makefile.am | 2 ++
src/utils/uri-error.vala | 5 +++++
src/utils/uri.vala | 43 +++++++++++++++++++++++++++++++++++++++++++
3 files changed, 50 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index ba97b4a..543e1fb 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -151,6 +151,8 @@ gnome_games_SOURCES = \
utils/grep.vala \
utils/local-cover.vala \
utils/string-input-stream.vala \
+ utils/uri.vala \
+ utils/uri-error.vala \
utils/xml-doc.vala \
\
config.vala \
diff --git a/src/utils/uri-error.vala b/src/utils/uri-error.vala
new file mode 100644
index 0000000..d78bab6
--- /dev/null
+++ b/src/utils/uri-error.vala
@@ -0,0 +1,5 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private errordomain Games.UriError {
+ INVALID_SCHEME,
+}
diff --git a/src/utils/uri.vala b/src/utils/uri.vala
new file mode 100644
index 0000000..818ebff
--- /dev/null
+++ b/src/utils/uri.vala
@@ -0,0 +1,43 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+public class Games.Uri {
+ private static Regex scheme_regex;
+
+ private string uri;
+
+ public Uri (string uri) {
+ this.uri = uri;
+ }
+
+ public Uri.from_file (File file) {
+ uri = file.get_uri ();
+ }
+
+ static construct {
+ scheme_regex = /^([a-z][a-z0-9\+\.\-]*):/i;
+ }
+
+ public static uint hash (Uri uri) {
+ return str_hash (uri.uri);
+ }
+
+ public static bool equal (Uri a, Uri b) {
+ return str_equal (a.uri, b.uri);
+ }
+
+ public string to_string () {
+ return uri;
+ }
+
+ public File to_file () {
+ return File.new_for_uri (uri);
+ }
+
+ public string get_scheme () throws Error {
+ MatchInfo info;
+ if (!scheme_regex.match (uri, 0, out info))
+ throw new UriError.INVALID_SCHEME ("The URI doesn't have a valid scheme: %s.", uri);
+
+ return info.fetch (1);
+ }
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]