consolodate the textsearch code into a single module

This commit is contained in:
Vincent Sanders 2020-05-12 21:21:59 +01:00
parent 3c7538a9f9
commit 68b9417a6b
4 changed files with 102 additions and 107 deletions

View File

@ -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 */ /* exported interface documented in content/content.h */
nserror nserror
content_debug_dump(struct hlcache_handle *h, FILE *f, enum content_debug op) content_debug_dump(struct hlcache_handle *h, FILE *f, enum content_debug op)

View File

@ -30,6 +30,7 @@
#include "utils/errors.h" #include "utils/errors.h"
#include "utils/utils.h" #include "utils/utils.h"
#include "content/content.h" #include "content/content.h"
#include "content/hlcache.h"
#include "desktop/selection.h" #include "desktop/selection.h"
#include "netsurf/search.h" #include "netsurf/search.h"
#include "netsurf/content_type.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, content_textsearch_step(struct textsearch_context *textsearch,
search_flags_t flags, search_flags_t flags,
const char *string) const char *string)
@ -649,7 +658,15 @@ content_textsearch_ishighlighted(struct textsearch_context *textsearch,
/* Exported function documented in content/textsearch.h */ /* 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, content_textsearch_create(struct content *c,
void *gui_data, void *gui_data,
struct textsearch_context **textsearch_out) struct textsearch_context **textsearch_out)
@ -716,3 +733,83 @@ nserror content_textsearch_destroy(struct textsearch_context *textsearch)
return NSERROR_OK; 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));
}

View File

@ -24,34 +24,11 @@
#ifndef NETSURF_CONTENT_SEARCH_H #ifndef NETSURF_CONTENT_SEARCH_H
#define NETSURF_CONTENT_SEARCH_H #define NETSURF_CONTENT_SEARCH_H
#include <ctype.h>
#include <string.h>
#include "desktop/search.h" #include "desktop/search.h"
struct textsearch_context; struct textsearch_context;
struct content; 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 * Ends the search process, invalidating all state freeing the list of

View File

@ -23,6 +23,8 @@
* Free text search (core) * Free text search (core)
*/ */
#include <stdbool.h>
#include "utils/errors.h" #include "utils/errors.h"
#include "content/content.h" #include "content/content.h"
#include "netsurf/types.h" #include "netsurf/types.h"