FIX: examples/supports.c: type_n: Return correct type
[libquvi.git] / src / misc / trim.c
blob56b89692e68320b7f803d7cef4618f3d5ac781f4
1 /* libquvi
2 * Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of libquvi <http://quvi.sourceforge.net/>.
6 * This library is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public
8 * License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General
17 * Public License along with this library. If not, see
18 * <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <glib.h>
25 /* -- */
26 #include "misc/re.h"
28 #define _W "[%s] libquvi: %s"
30 gchar *m_trim(const gchar *s, const gchar *p, const gchar *w)
32 GError *err;
33 GRegex *re;
34 gchar *r;
36 err = NULL;
37 re = g_regex_new(p, 0, 0, &err);
39 if (err != NULL)
41 g_warning(_W, __func__, err->message);
42 g_error_free(err);
43 err = NULL;
44 return (NULL);
47 r = g_regex_replace(re, s, -1, 0, w, 0, &err);
48 if (err != NULL)
50 g_warning(_W, __func__, err->message);
51 g_error_free(err);
52 err = NULL;
53 r = NULL;
56 g_regex_unref(re);
57 re = NULL;
59 return (r);
62 /* Trim null-terminated string of extra whitespace. */
63 gchar *m_trim_ws(const gchar *s)
65 gchar *u, *t;
67 u = NULL;
68 t = m_trim(s, "^\\s*(.+?)\\s*$", "\\1");
70 if (t != NULL)
72 u = m_trim(t, "\\s\\s+", " ");
73 g_free(t);
74 t = NULL;
76 return (u);
79 /* vim: set ts=2 sw=2 tw=72 expandtab: */