1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
---|
2 | /* gnome-vfs-regexp-filter.c - Regexp-based filter for the GNOME |
---|
3 | Virtual File System. |
---|
4 | |
---|
5 | Copyright (C) 1999 Free Software Foundation |
---|
6 | |
---|
7 | The Gnome Library is free software; you can redistribute it and/or |
---|
8 | modify it under the terms of the GNU Library General Public License as |
---|
9 | published by the Free Software Foundation; either version 2 of the |
---|
10 | License, or (at your option) any later version. |
---|
11 | |
---|
12 | The Gnome Library is distributed in the hope that it will be useful, |
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
15 | Library General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU Library General Public |
---|
18 | License along with the Gnome Library; see the file COPYING.LIB. If not, |
---|
19 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
20 | Boston, MA 02111-1307, USA. |
---|
21 | |
---|
22 | Author: Ettore Perazzoli <ettore@comm2000.it> */ |
---|
23 | |
---|
24 | #include <config.h> |
---|
25 | #include "gnome-vfs-regexp-filter.h" |
---|
26 | |
---|
27 | #include "gnome-vfs-private.h" |
---|
28 | #include "gnome-vfs.h" |
---|
29 | #include <regex.h> |
---|
30 | #include <sys/types.h> |
---|
31 | |
---|
32 | struct GnomeVFSRegexpFilter { |
---|
33 | regex_t regex; |
---|
34 | }; |
---|
35 | |
---|
36 | GnomeVFSRegexpFilter * |
---|
37 | gnome_vfs_regexp_filter_new (const gchar *regexp, |
---|
38 | GnomeVFSDirectoryFilterOptions options) |
---|
39 | { |
---|
40 | GnomeVFSRegexpFilter *new; |
---|
41 | gint regflags; |
---|
42 | |
---|
43 | new = g_new (GnomeVFSRegexpFilter, 1); |
---|
44 | |
---|
45 | regflags = REG_NOSUB; |
---|
46 | if (options & GNOME_VFS_DIRECTORY_FILTER_IGNORECASE) |
---|
47 | regflags |= REG_ICASE; |
---|
48 | if (options & GNOME_VFS_DIRECTORY_FILTER_EXTENDEDREGEXP) |
---|
49 | regflags |= REG_EXTENDED; |
---|
50 | |
---|
51 | if (regcomp (&new->regex, regexp, regflags) != 0) { |
---|
52 | g_free (new); |
---|
53 | return NULL; |
---|
54 | } |
---|
55 | |
---|
56 | return new; |
---|
57 | } |
---|
58 | |
---|
59 | void |
---|
60 | gnome_vfs_regexp_filter_destroy (GnomeVFSRegexpFilter *filter) |
---|
61 | { |
---|
62 | g_return_if_fail (filter != NULL); |
---|
63 | |
---|
64 | regfree (&filter->regex); |
---|
65 | g_free (filter); |
---|
66 | } |
---|
67 | |
---|
68 | gboolean |
---|
69 | gnome_vfs_regexp_filter_apply (GnomeVFSRegexpFilter *filter, |
---|
70 | GnomeVFSFileInfo *info) |
---|
71 | { |
---|
72 | gint result; |
---|
73 | |
---|
74 | result = regexec (&filter->regex, info->name, 0, NULL, 0); |
---|
75 | |
---|
76 | if (result == 0) |
---|
77 | return TRUE; |
---|
78 | else |
---|
79 | return FALSE; |
---|
80 | } |
---|