summaryrefslogtreecommitdiff
path: root/src/media-export/rygel-media-export-string-utils.c
blob: 26595a3b8687927fbd4e075c506d8f873e809b97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * Copyright (C) 2009, 2010 Jens Georg <mail@jensge.org>.
 * Copyright (C) 2013 Intel Corporation.
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Rygel is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <string.h>

#include "rygel-media-export-string-utils.h"

gchar *
rygel_media_export_string_replace (const gchar  *self,
                                   const gchar  *old,
                                   const gchar  *replacement,
                                   GError      **error) {
  gchar* result;
  gchar *escaped;
  GError *inner_error;
  GRegex *regex;

  g_return_val_if_fail (self != NULL, NULL);
  g_return_val_if_fail (old != NULL, NULL);
  g_return_val_if_fail (replacement != NULL, NULL);

  result = NULL;
  escaped = g_regex_escape_string (old, -1);
  inner_error = NULL;
  regex = g_regex_new (escaped, 0, 0, &inner_error);
  g_free (escaped);
  if (inner_error) {
    g_propagate_error (error, inner_error);
  } else {
    result = g_regex_replace_literal (regex, self, -1, 0, replacement, 0, &inner_error);

    if (inner_error) {
      g_propagate_error (error, inner_error);
    }
    g_regex_unref (regex);
  }

  return result;
}

gchar *
rygel_media_export_string_slice (const gchar *self,
                                 glong        start,
                                 glong        end) {
  glong string_length;
  gboolean outside;

  g_return_val_if_fail (self != NULL, NULL);

  string_length = strlen (self);
  if (start < 0) {
    start += string_length;
  }
  if (end < 0) {
    end += string_length;
  }
  if (start >= 0) {
    outside = (start > string_length);
  } else {
    outside = TRUE;
  }
  if (outside) {
    g_warning ("Start offset (%ld) is outside string (%s).", start, self);
    return NULL;
  }
  if (end >= 0) {
    outside = (end > string_length);
  } else {
    outside = TRUE;
  }
  if (outside) {
    g_warning ("End offset (%ld) is outside string (%s).", end, self);
    return NULL;
  }
  if (start > end) {
    g_warning ("Start offset (%ld) is later than end offset (%ld).", start, end);
    return NULL;
  }
  return g_strndup (self + start, (gsize) (end - start));
}