summaryrefslogtreecommitdiff
path: root/content/handlers/text/textplain.c
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2020-05-12 23:22:05 +0100
committerVincent Sanders <vince@kyllikki.org>2020-05-12 23:22:05 +0100
commitf4e50b45c834b644caa6a82bd044faa82f6f4860 (patch)
tree0b25cc743861a1f296bd4903324cac9dbf2f5990 /content/handlers/text/textplain.c
parent68b9417a6bc8344f68f8a8206d2f2781079bd713 (diff)
downloadnetsurf-f4e50b45c834b644caa6a82bd044faa82f6f4860.tar.gz
netsurf-f4e50b45c834b644caa6a82bd044faa82f6f4860.tar.bz2
make free text search content handler agnostic
Diffstat (limited to 'content/handlers/text/textplain.c')
-rw-r--r--content/handlers/text/textplain.c104
1 files changed, 104 insertions, 0 deletions
diff --git a/content/handlers/text/textplain.c b/content/handlers/text/textplain.c
index 750c5eb3d..a233d827f 100644
--- a/content/handlers/text/textplain.c
+++ b/content/handlers/text/textplain.c
@@ -1212,6 +1212,107 @@ textplain_coord_from_offset(const char *text, size_t offset, size_t length)
return x;
}
+/**
+ * Finds all occurrences of a given string in a textplain content
+ *
+ * \param c the content to be searched
+ * \param context The search context to add the entry to.
+ * \param pattern the string pattern to search for
+ * \param p_len pattern length
+ * \param case_sens whether to perform a case sensitive search
+ * \return NSERROR_OK on success else error code on faliure
+ */
+static nserror
+textplain_textsearch_find(struct content *c,
+ struct textsearch_context *context,
+ const char *pattern,
+ int p_len,
+ bool case_sens)
+{
+ int nlines = textplain_line_count(c);
+ int line;
+ nserror res = NSERROR_OK;
+
+ for(line = 0; line < nlines; line++) {
+ size_t offset, length;
+ const char *text;
+
+ text = textplain_get_line(c, line, &offset, &length);
+ if (text) {
+ while (length > 0) {
+ unsigned match_length;
+ size_t start_idx;
+ const char *new_text;
+ const char *pos;
+
+ pos = content_textsearch_find_pattern(
+ text,
+ length,
+ pattern,
+ p_len,
+ case_sens,
+ &match_length);
+ if (!pos)
+ break;
+
+ /* found string in line => add to list */
+ start_idx = offset + (pos - text);
+ res = content_textsearch_add_match(context,
+ start_idx,
+ start_idx + match_length,
+ NULL,
+ NULL);
+ if (res != NSERROR_OK) {
+ return res;
+ }
+
+ new_text = pos + match_length;
+ offset += (new_text - text);
+ length -= (new_text - text);
+ text = new_text;
+ }
+ }
+ }
+
+ return res;
+}
+
+
+/**
+ * get bounds of a free text search match
+ */
+static nserror
+textplain_textsearch_bounds(struct content *c,
+ unsigned start_idx,
+ unsigned end_idx,
+ struct box *start_box,
+ struct box *end_box,
+ struct rect *bounds)
+{
+ textplain_coords_from_range(c, start_idx, end_idx, bounds);
+
+ return NSERROR_OK;
+}
+
+
+/**
+ * create a selection object suitable for this content
+ */
+static nserror
+textplain_create_selection(struct content *c, struct selection **sel_out)
+{
+ struct selection *sel;
+ sel = selection_create(c, false);
+ if (sel == NULL) {
+ return NSERROR_NOMEM;
+ }
+
+ selection_init(sel, NULL, NULL);
+
+ *sel_out = sel;
+ return NSERROR_OK;
+}
+
/**
* plain text content handler table
@@ -1232,6 +1333,9 @@ static const content_handler textplain_content_handler = {
.get_selection = textplain_get_selection,
.clone = textplain_clone,
.type = textplain_content_type,
+ .textsearch_find = textplain_textsearch_find,
+ .textsearch_bounds = textplain_textsearch_bounds,
+ .create_selection = textplain_create_selection,
.no_share = true,
};