From 68b9417a6bc8344f68f8a8206d2f2781079bd713 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Tue, 12 May 2020 21:21:59 +0100 Subject: consolodate the textsearch code into a single module --- content/content.c | 81 ---------------------------------------- content/textsearch.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++-- content/textsearch.h | 23 ------------ 3 files changed, 100 insertions(+), 107 deletions(-) (limited to 'content') diff --git a/content/content.c b/content/content.c index 34602d686..bc3f48429 100644 --- a/content/content.c +++ b/content/content.c @@ -917,87 +917,6 @@ content_drop_file_at_point(struct hlcache_handle *h, } -/** - * Terminate a search. - * - * \param c content to clear - */ -static nserror content_textsearch__clear(struct content *c) -{ - free(c->textsearch.string); - c->textsearch.string = NULL; - - if (c->textsearch.context != NULL) { - content_textsearch_destroy(c->textsearch.context); - c->textsearch.context = NULL; - } - return NSERROR_OK; -} - -/* exported interface, documented in content/content.h */ -nserror -content_textsearch(struct hlcache_handle *h, - void *context, - search_flags_t flags, - const char *string) -{ - struct content *c = hlcache_handle_get_content(h); - nserror res; - - assert(c != NULL); - - if (string != NULL && - c->textsearch.string != NULL && - c->textsearch.context != NULL && - strcmp(string, c->textsearch.string) == 0) { - /* Continue prev. search */ - content_textsearch_step(c->textsearch.context, flags, string); - - } else if (string != NULL) { - /* New search */ - free(c->textsearch.string); - c->textsearch.string = strdup(string); - if (c->textsearch.string == NULL) { - return NSERROR_NOMEM; - } - - if (c->textsearch.context != NULL) { - content_textsearch_destroy(c->textsearch.context); - c->textsearch.context = NULL; - } - - res = content_textsearch_create(c, - context, - &c->textsearch.context); - if (res != NSERROR_OK) { - return res; - } - - content_textsearch_step(c->textsearch.context, flags, string); - - } else { - /* Clear search */ - content_textsearch__clear(c); - - free(c->textsearch.string); - c->textsearch.string = NULL; - } - - return NSERROR_OK; -} - - - -/* exported interface, documented in content/content.h */ -nserror content_textsearch_clear(struct hlcache_handle *h) -{ - struct content *c = hlcache_handle_get_content(h); - assert(c != 0); - - return(content_textsearch__clear(c)); -} - - /* exported interface documented in content/content.h */ nserror content_debug_dump(struct hlcache_handle *h, FILE *f, enum content_debug op) diff --git a/content/textsearch.c b/content/textsearch.c index c5359482d..7db0714dc 100644 --- a/content/textsearch.c +++ b/content/textsearch.c @@ -30,6 +30,7 @@ #include "utils/errors.h" #include "utils/utils.h" #include "content/content.h" +#include "content/hlcache.h" #include "desktop/selection.h" #include "netsurf/search.h" #include "netsurf/content_type.h" @@ -583,8 +584,16 @@ search_text(const char *string, } -/* Exported function documented in context/textsearch.h */ -nserror +/** + * Begins/continues the search process + * + * \note that this may be called many times for a single search. + * + * \param context The search context in use. + * \param flags The flags forward/back etc + * \param string The string to match + */ +static nserror content_textsearch_step(struct textsearch_context *textsearch, search_flags_t flags, const char *string) @@ -649,7 +658,15 @@ content_textsearch_ishighlighted(struct textsearch_context *textsearch, /* Exported function documented in content/textsearch.h */ -nserror +/** + * create a search_context + * + * \param c The content the search_context is connected to + * \param context A context pointer passed to the provider routines. + * \param search_out A pointer to recive the new text search context + * \return NSERROR_OK on success and \a search_out updated else error code + */ +static nserror content_textsearch_create(struct content *c, void *gui_data, struct textsearch_context **textsearch_out) @@ -716,3 +733,83 @@ nserror content_textsearch_destroy(struct textsearch_context *textsearch) return NSERROR_OK; } + +/** + * Terminate a search. + * + * \param c content to clear + */ +static nserror content_textsearch__clear(struct content *c) +{ + free(c->textsearch.string); + c->textsearch.string = NULL; + + if (c->textsearch.context != NULL) { + content_textsearch_destroy(c->textsearch.context); + c->textsearch.context = NULL; + } + return NSERROR_OK; +} + + +/* exported interface, documented in content/textsearch.h */ +nserror +content_textsearch(struct hlcache_handle *h, + void *context, + search_flags_t flags, + const char *string) +{ + struct content *c = hlcache_handle_get_content(h); + nserror res; + + assert(c != NULL); + + if (string != NULL && + c->textsearch.string != NULL && + c->textsearch.context != NULL && + strcmp(string, c->textsearch.string) == 0) { + /* Continue prev. search */ + content_textsearch_step(c->textsearch.context, flags, string); + + } else if (string != NULL) { + /* New search */ + free(c->textsearch.string); + c->textsearch.string = strdup(string); + if (c->textsearch.string == NULL) { + return NSERROR_NOMEM; + } + + if (c->textsearch.context != NULL) { + content_textsearch_destroy(c->textsearch.context); + c->textsearch.context = NULL; + } + + res = content_textsearch_create(c, + context, + &c->textsearch.context); + if (res != NSERROR_OK) { + return res; + } + + content_textsearch_step(c->textsearch.context, flags, string); + + } else { + /* Clear search */ + content_textsearch__clear(c); + + free(c->textsearch.string); + c->textsearch.string = NULL; + } + + return NSERROR_OK; +} + + +/* exported interface, documented in content/textsearch.h */ +nserror content_textsearch_clear(struct hlcache_handle *h) +{ + struct content *c = hlcache_handle_get_content(h); + assert(c != 0); + + return(content_textsearch__clear(c)); +} diff --git a/content/textsearch.h b/content/textsearch.h index f94bcdb42..c0a1acf9a 100644 --- a/content/textsearch.h +++ b/content/textsearch.h @@ -24,34 +24,11 @@ #ifndef NETSURF_CONTENT_SEARCH_H #define NETSURF_CONTENT_SEARCH_H -#include -#include - #include "desktop/search.h" struct textsearch_context; struct content; -/** - * create a search_context - * - * \param c The content the search_context is connected to - * \param context A context pointer passed to the provider routines. - * \param search_out A pointer to recive the new text search context - * \return NSERROR_OK on success and \a search_out updated else error code - */ -nserror content_textsearch_create(struct content *c, void *context, struct textsearch_context **textsearch_out); - -/** - * Begins/continues the search process - * - * \note that this may be called many times for a single search. - * - * \param context The search context in use. - * \param flags The flags forward/back etc - * \param string The string to match - */ -nserror content_textsearch_step(struct textsearch_context *textsearch, search_flags_t flags, const char *string); /** * Ends the search process, invalidating all state freeing the list of -- cgit v1.2.3