source: trunk/third/gtkhtml3/src/htmlsearch.c @ 21116

Revision 21116, 4.4 KB checked in by ghudson, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r21115, which included commits to RCS files with non-trunk default branches.
Line 
1/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2/*  This file is part of the GtkHTML library.
3
4    Copyright (C) 2000 Helix Code, Inc.
5    Authors:           Radek Doulik (rodo@helixcode.com)
6
7    This library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Library General Public
9    License as published by the Free Software Foundation; either
10    version 2 of the License, or (at your option) any later version.
11
12    This 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 License
18    along with this library; see the file COPYING.LIB.  If not, write to
19    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.
21
22    TODO:
23
24           - now we go thru the html tree without take care about vertical
25             position of paragraph. so it is possible to find first match
26             on bottom of page (ie. first column of table) and the second
27             one on top (ie. top of second comlumn)
28             [also Netscape didn't take care of it]
29
30*/
31
32#include <config.h>
33#include <string.h>
34
35#include "htmlsearch.h"
36#include "htmlobject.h"
37#include "htmlentity.h"
38#include "htmlengine.h"
39
40static void
41set_text (HTMLSearch *s, const gchar *text)
42{
43        s->text = g_strdup (text);
44        s->text_bytes = strlen (text);
45}
46
47HTMLSearch *
48html_search_new (HTMLEngine *e, const gchar *text, gboolean case_sensitive, gboolean forward, gboolean regular)
49{
50        HTMLSearch *ns = g_new (HTMLSearch, 1);
51        gint i;
52
53        set_text (ns, text);
54        ns->case_sensitive = case_sensitive;
55        ns->forward        = forward;
56        ns->found          = NULL;
57        ns->engine         = e;
58
59        if (html_engine_get_editable (e)) {
60                HTMLObject *o;
61 
62                ns->stack = NULL;
63                ns->start_pos = e->cursor->offset - 1;
64                for (o = e->cursor->object; o; o = o->parent)
65                        html_search_push (ns, o);
66                ns->stack = g_slist_reverse (ns->stack);
67                ns->found = g_list_append (ns->found, e->cursor->object);
68        } else {
69                ns->stack     = NULL;
70                ns->start_pos = 0;
71                html_search_push (ns, e->clue);
72        }
73
74        /* translate table
75           could translate uppercase to lowercase if non case_sensitive */
76        ns->trans = g_new (gchar, 256);
77        for (i=0; i<256; i++) {
78                ns->trans [i] = (case_sensitive) ? i : ((i>='A' && i<='Z') ? i+('a'-'A') : i);
79        }
80        /*
81         * FIXME translating &nbsp; breaks horribly
82         * with utf8 and nonutf8 regex (see bug #24446)
83         * so we won't do it
84         */
85        /* ns->trans [ENTITY_NBSP] = ' '; */
86
87        ns->regular = regular;
88        if (regular) {
89#ifdef HAVE_GNU_REGEX
90                const gchar *rv;
91
92                ns->reb = g_new0 (regex_t, 1);
93
94                ns->reb->translate = ns->trans;
95                rv = re_compile_pattern (ns->text, ns->text_bytes, ns->reb);
96                if (rv) {
97                        g_warning (rv);
98                }
99#else
100                int rv_int;
101
102                ns->reb = g_new0 (regex_t, 1);
103
104                rv_int = regcomp (ns->reb, ns->text, (case_sensitive) ? 0 : REG_ICASE);
105                if (rv_int) {
106                        char buf[1024];
107                        if (regerror(rv_int, ns->reb, buf, sizeof(buf))) {
108                                g_warning (buf);
109                        } else {
110                                g_warning ("regcomp failed, error code %d", rv_int);
111                        }
112                }
113#endif
114        } else {
115                ns->reb = NULL;
116        }
117
118        return ns;
119}
120
121void
122html_search_destroy (HTMLSearch *search)
123{
124        g_free (search->text);
125        if (search->stack)
126                g_slist_free (search->stack);
127        if (search->reb) {
128                /* FIXME why this segfault for me? regfree (search->reb); */
129                g_free (search->reb);
130        }
131        g_free (search->trans);
132
133        g_free (search);
134}
135
136void
137html_search_push (HTMLSearch *search, HTMLObject *obj)
138{
139        search->stack = g_slist_prepend (search->stack, obj);
140}
141
142HTMLObject *
143html_search_pop (HTMLSearch *search)
144{
145        HTMLObject *obj;
146
147        obj = HTML_OBJECT (search->stack->data);
148        search->stack = g_slist_remove (search->stack, obj);
149
150        return obj;
151}
152
153gboolean
154html_search_child_on_stack (HTMLSearch *search, HTMLObject *obj)
155{
156        return search->stack && HTML_OBJECT (search->stack->data)->parent == obj;
157}
158
159gboolean
160html_search_next_parent (HTMLSearch *search)
161{
162        return search->stack && search->stack->next
163                ? html_object_search (HTML_OBJECT (search->stack->next->data), search)
164                : FALSE;
165}
166
167void
168html_search_set_text (HTMLSearch *search, const gchar *text)
169{
170        g_free (search->text);
171        set_text (search, text);
172}
173
174void
175html_search_set_forward (HTMLSearch *search, gboolean forward)
176{
177        if (search)
178                search->forward = forward;
179}
Note: See TracBrowser for help on using the repository browser.