NetSurf
Data Structures | Macros | Functions
search.c File Reference

Free text search (core) More...

#include <ctype.h>
#include <string.h>
#include <dom/dom.h>
#include "utils/config.h"
#include "utils/log.h"
#include "utils/messages.h"
#include "utils/utils.h"
#include "content/content.h"
#include "content/hlcache.h"
#include "desktop/selection.h"
#include "netsurf/search.h"
#include "netsurf/misc.h"
#include "desktop/gui_internal.h"
#include "text/textplain.h"
#include "html/box.h"
#include "html/box_inspect.h"
#include "html/html.h"
#include "html/private.h"
#include "html/search.h"
Include dependency graph for search.c:

Go to the source code of this file.

Data Structures

struct  list_entry
 
struct  search_context
 

Macros

#define NOF_ELEMENTS(array)   (sizeof(array)/sizeof(*(array)))
 

Functions

struct search_contextsearch_create_context (struct content *c, content_type type, void *gui_data)
 create a search_context More...
 
static void free_matches (struct search_context *context)
 Release the memory used by the list of matches, deleting selection objects too. More...
 
static const char * find_pattern (const char *string, int s_len, const char *pattern, int p_len, bool case_sens, unsigned int *m_len)
 Find the first occurrence of 'match' in 'string' and return its index. More...
 
static struct list_entryadd_entry (unsigned start_idx, unsigned end_idx, struct search_context *context)
 Add a new entry to the list of matches. More...
 
static bool find_occurrences_html (const char *pattern, int p_len, struct box *cur, bool case_sens, struct search_context *context)
 Finds all occurrences of a given string in the html box tree. More...
 
static bool find_occurrences_text (const char *pattern, int p_len, struct content *c, bool case_sens, struct search_context *context)
 Finds all occurrences of a given string in a textplain content. More...
 
static void search_show_all (bool all, struct search_context *context)
 Specifies whether all matches or just the current match should be highlighted in the search text. More...
 
static void search_text (const char *string, int string_len, struct search_context *context, search_flags_t flags)
 Search for a string in the box tree. More...
 
void search_step (struct search_context *context, search_flags_t flags, const char *string)
 Begins/continues the search process. More...
 
bool search_term_highlighted (struct content *c, unsigned start_offset, unsigned end_offset, unsigned *start_idx, unsigned *end_idx, struct search_context *context)
 Determines whether any portion of the given text box should be selected because it matches the current search string. More...
 
void search_destroy_context (struct search_context *context)
 Ends the search process, invalidating all state freeing the list of found boxes. More...
 

Detailed Description

Free text search (core)

Definition in file search.c.

Macro Definition Documentation

◆ NOF_ELEMENTS

#define NOF_ELEMENTS (   array)    (sizeof(array)/sizeof(*(array)))

Definition at line 49 of file search.c.

Referenced by find_pattern().

Function Documentation

◆ add_entry()

static struct list_entry* add_entry ( unsigned  start_idx,
unsigned  end_idx,
struct search_context context 
)
static

Add a new entry to the list of matches.

Parameters
start_idxOffset of match start within textual representation
end_idxOffset of match end
contextThe search context to add the entry to.
Returns
Pointer to added entry, NULL iff failed.

Definition at line 278 of file search.c.

References list_entry::end_idx, search_context::found, list_entry::next, list_entry::prev, list_entry::sel, and list_entry::start_idx.

Referenced by find_occurrences_html(), and find_occurrences_text().

Here is the caller graph for this function:

◆ find_occurrences_html()

static bool find_occurrences_html ( const char *  pattern,
int  p_len,
struct box cur,
bool  case_sens,
struct search_context context 
)
static

Finds all occurrences of a given string in the html box tree.

Parameters
patternthe string pattern to search for
p_lenpattern length
curpointer to the current box
case_senswhether to perform a case sensitive search
contextThe search context to add the entry to.
Returns
true on success, false on memory allocation failure

Definition at line 317 of file search.c.

References add_entry(), box::byte_offset, box::children, list_entry::end_box, find_pattern(), box::length, box::next, box::object, list_entry::start_box, box::text, and text().

Referenced by search_text().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_occurrences_text()

static bool find_occurrences_text ( const char *  pattern,
int  p_len,
struct content c,
bool  case_sens,
struct search_context context 
)
static

Finds all occurrences of a given string in a textplain content.

Parameters
patternthe string pattern to search for
p_lenpattern length
cthe content to be searched
case_senswhether to perform a case sensitive search
contextThe search context to add the entry to.
Returns
true on success, false on memory allocation failure

Definition at line 380 of file search.c.

References add_entry(), find_pattern(), line(), list_entry::start_idx, text(), textplain_get_line(), and textplain_line_count().

Referenced by search_text().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_pattern()

static const char* find_pattern ( const char *  string,
int  s_len,
const char *  pattern,
int  p_len,
bool  case_sens,
unsigned int *  m_len 
)
static

Find the first occurrence of 'match' in 'string' and return its index.

Parameters
stringthe string to be searched (unterminated)
s_lenlength of the string to be searched
patternthe pattern for which we are searching (unterminated)
p_lenlength of pattern
case_senstrue iff case sensitive match required
m_lenaccepts length of match in bytes
Returns
pointer to first match, NULL if none

Definition at line 163 of file search.c.

References max, and NOF_ELEMENTS.

Referenced by find_occurrences_html(), and find_occurrences_text().

Here is the caller graph for this function:

◆ free_matches()

static void free_matches ( struct search_context context)
static

Release the memory used by the list of matches, deleting selection objects too.

Definition at line 126 of file search.c.

References search_context::found, list_entry::next, list_entry::prev, list_entry::sel, selection_clear(), and selection_destroy().

Referenced by search_destroy_context(), search_step(), and search_text().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ search_create_context()

struct search_context* search_create_context ( struct content c,
content_type  type,
void *  context 
)

create a search_context

Parameters
cThe content the search_context is connected to
typeThe content type of c
contextA context pointer passed to the provider routines.
Returns
A new search context or NULL on error.

Definition at line 80 of file search.c.

References search_context::c, CONTENT_HTML, CONTENT_TEXTPLAIN, search_context::current, list_entry::end_box, list_entry::end_idx, search_context::found, search_context::gui_p, search_context::is_html, search_context::newsearch, list_entry::next, list_entry::prev, search_context::prev_case_sens, list_entry::sel, list_entry::start_box, list_entry::start_idx, and search_context::string.

Referenced by html_search(), and textplain_search().

Here is the caller graph for this function:

◆ search_destroy_context()

void search_destroy_context ( struct search_context context)

Ends the search process, invalidating all state freeing the list of found boxes.

Definition at line 648 of file search.c.

References gui_search_table::add_recent, gui_search_table::back_state, gui_search_table::forward_state, free_matches(), search_context::gui_p, guit, netsurf_table::search, and search_context::string.

Referenced by html_close(), html_search(), html_search_clear(), textplain_close(), textplain_search(), and textplain_search_clear().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ search_show_all()

static void search_show_all ( bool  all,
struct search_context context 
)
static

Specifies whether all matches or just the current match should be highlighted in the search text.

Definition at line 426 of file search.c.

References search_context::c, search_context::current, list_entry::end_idx, search_context::found, search_context::is_html, html_content::layout, html_content::len_ctx, list_entry::next, list_entry::sel, selection_clear(), selection_create(), selection_destroy(), selection_init(), selection_set_end(), selection_set_start(), and list_entry::start_idx.

Referenced by search_text().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ search_step()

void search_step ( struct search_context context,
search_flags_t  flags,
const char *  string 
)

Begins/continues the search process.

Note
that this may be called many times for a single search.
Parameters
contextThe search context in use.
flagsThe flags forward/back etc
stringThe string to match

Definition at line 592 of file search.c.

References gui_search_table::add_recent, content_msg_data::area, gui_search_table::back_state, search_context::c, content_broadcast(), CONTENT_MSG_SCROLL, gui_search_table::forward_state, free_matches(), search_context::gui_p, guit, content_msg_data::scroll, netsurf_table::search, search_text(), gui_search_table::status, content_msg_data::x0, and content_msg_data::y0.

Referenced by html_search(), and textplain_search().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ search_term_highlighted()

bool search_term_highlighted ( struct content c,
unsigned  start_offset,
unsigned  end_offset,
unsigned *  start_idx,
unsigned *  end_idx,
struct search_context context 
)

Determines whether any portion of the given text box should be selected because it matches the current search string.

Parameters
cThe content to hilight within.
start_offsetbyte offset within text of string to be checked
end_offsetbyte offset within text
start_idxbyte offset within string of highlight start
end_idxbyte offset of highlight end
contextThe search context to hilight entries from.
Returns
true iff part of the box should be highlighted

Definition at line 626 of file search.c.

References search_context::c, search_context::found, list_entry::next, list_entry::sel, selection_defined, and selection_highlighted().

Referenced by text_draw(), text_redraw(), and textplain_redraw().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ search_text()

static void search_text ( const char *  string,
int  string_len,
struct search_context context,
search_flags_t  flags 
)
static

Search for a string in the box tree.

Parameters
stringthe string to search for
string_lenlength of search string
contextThe search context to add the entry to.
flagsflags to control the search.

Definition at line 474 of file search.c.

References content_msg_data::area, gui_search_table::back_state, box_coords(), search_context::c, content_broadcast(), CONTENT_MSG_SCROLL, search_context::current, list_entry::end_box, list_entry::end_idx, find_occurrences_html(), find_occurrences_text(), gui_search_table::forward_state, search_context::found, free_matches(), search_context::gui_p, guit, box::height, gui_search_table::hourglass, search_context::is_html, html_content::layout, search_context::newsearch, list_entry::next, list_entry::prev, search_context::prev_case_sens, content_msg_data::scroll, netsurf_table::search, SEARCH_FLAG_CASE_SENSITIVE, SEARCH_FLAG_FORWARDS, SEARCH_FLAG_SHOWALL, search_show_all(), list_entry::start_box, list_entry::start_idx, gui_search_table::status, search_context::string, textplain_coords_from_range(), box::width, rect::x0, content_msg_data::x0, rect::x1, content_msg_data::x1, rect::y0, content_msg_data::y0, rect::y1, and content_msg_data::y1.

Referenced by search_step().

Here is the call graph for this function:
Here is the caller graph for this function: