From 76eaf6cb879e74b64ed4d7d3ad82097c42d61af6 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Thu, 9 Dec 2010 01:01:36 +0530 Subject: [PATCH] tracker: SearchContainer.escape_string() -> Query The escape_string utility function meant for escaping strings to be used in SPARQL query more correctly belongs to the Query class that deals with SPARQL query generation. --- src/plugins/tracker/rygel-tracker-query.vala | 54 +++++++++++++++++++++- .../tracker/rygel-tracker-search-container.vala | 54 +--------------------- 2 files changed, 54 insertions(+), 54 deletions(-) diff --git a/src/plugins/tracker/rygel-tracker-query.vala b/src/plugins/tracker/rygel-tracker-query.vala index 81cb22e..29bd80f 100644 --- a/src/plugins/tracker/rygel-tracker-query.vala +++ b/src/plugins/tracker/rygel-tracker-query.vala @@ -1,7 +1,8 @@ /* * Copyright (C) 2008 Nokia Corporation. * - * Author: Zeeshan Ali + * Authors: Zeeshan Ali + * Ivan Frade * * This file is part of Rygel. * @@ -40,4 +41,55 @@ public abstract class Rygel.Tracker.Query { public virtual string to_string () { return this.triplets.serialize (); } + + /** + * tracker_sparql_escape_string: Escapes a string so that it can be + * used in a SPARQL query. Copied from Tracker project. + * + * @param literal A string to escape + * + * @return A newly-allocated string with the escaped version of + * literal. The returned string should be freed with g_free() when no + * longer needed. + */ + public static string escape_string (string literal) { + StringBuilder str = new StringBuilder (); + char *p = literal; + + while (*p != '\0') { + size_t len = Posix.strcspn ((string) p, "\t\n\r\b\f\"\\"); + str.append_len ((string) p, (long) len); + p += len; + + switch (*p) { + case '\t': + str.append ("\\t"); + break; + case '\n': + str.append ("\\n"); + break; + case '\r': + str.append ("\\r"); + break; + case '\b': + str.append ("\\b"); + break; + case '\f': + str.append ("\\f"); + break; + case '"': + str.append ("\\\""); + break; + case '\\': + str.append ("\\\\"); + break; + default: + continue; + } + + p++; + } + + return str.str; + } } diff --git a/src/plugins/tracker/rygel-tracker-search-container.vala b/src/plugins/tracker/rygel-tracker-search-container.vala index 7fd6d46..e40947f 100644 --- a/src/plugins/tracker/rygel-tracker-search-container.vala +++ b/src/plugins/tracker/rygel-tracker-search-container.vala @@ -4,7 +4,6 @@ * Copyright (C) 2010 MediaNet Inh. * * Authors: Zeeshan Ali - * Ivan Frade * Sunil Mohan Adapa * * This file is part of Rygel. @@ -277,7 +276,7 @@ public class Rygel.Tracker.SearchContainer : Rygel.MediaContainer { break; case SearchCriteriaOp.CONTAINS: // We need to escape this twice for Tracker - var regex = this.escape_string (Regex.escape_string (value)); + var regex = Query.escape_string (Regex.escape_string (value)); filter = "regex(" + variable + ", \"" + regex + "\", \"i\")"; break; @@ -305,56 +304,5 @@ public class Rygel.Tracker.SearchContainer : Rygel.MediaContainer { return slice; } - - /** - * tracker_sparql_escape_string: - * @literal: a string to escape - * - * Escapes a string so that it can be used in a SPARQL query. Copied from - * Tracker project. - * - * Returns: a newly-allocated string with the escaped version of @literal. - * The returned string should be freed with g_free() when no longer needed. - */ - private string escape_string (string literal) { - StringBuilder str = new StringBuilder (); - char *p = literal; - - while (*p != '\0') { - size_t len = Posix.strcspn ((string) p, "\t\n\r\b\f\"\\"); - str.append_len ((string) p, (long) len); - p += len; - - switch (*p) { - case '\t': - str.append ("\\t"); - break; - case '\n': - str.append ("\\n"); - break; - case '\r': - str.append ("\\r"); - break; - case '\b': - str.append ("\\b"); - break; - case '\f': - str.append ("\\f"); - break; - case '"': - str.append ("\\\""); - break; - case '\\': - str.append ("\\\\"); - break; - default: - continue; - } - - p++; - } - - return str.str; - } } -- 2.7.4