From aefd95484f0825cbbf2dfa8664ea3a8ceaeae242 Mon Sep 17 00:00:00 2001 From: Charlie Gordon Date: Fri, 21 Dec 2007 14:27:11 +0000 Subject: [PATCH] added const void *memstr(const void *buf, int size, const char *str); added line_len and total_size in ModeProbeData structure extended SEARCH_LENGTH to 256, allowing upto 256 matches in incremental C-s --- qe.c | 19 +++++++++++++------ qe.h | 8 +++++--- util.c | 23 +++++++++++++++++++++++ 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/qe.c b/qe.c index 8f2c60c..1fedca6 100644 --- a/qe.c +++ b/qe.c @@ -4937,20 +4937,25 @@ static void get_default_path(EditState *s, char *buf, int buf_size) splitpath(buf, buf_size, NULL, 0, buf1); } -static ModeDef *probe_mode(EditState *s, int mode, uint8_t *buf, int len) +static ModeDef *probe_mode(EditState *s, int mode, const uint8_t *buf, + int len, long total_size) { EditBuffer *b = s->b; ModeDef *m, *selected_mode; ModeProbeData probe_data; int best_probe_percent, percent; + const uint8_t *p; m = s->qe_state->first_mode; selected_mode = NULL; best_probe_percent = 0; probe_data.buf = buf; probe_data.buf_size = len; + p = memchr(buf, '\n', len); + probe_data.line_len = p ? p - buf : len; probe_data.filename = b->filename; probe_data.mode = mode; + probe_data.total_size = total_size; while (m != NULL) { if (m->mode_probe) { @@ -5015,7 +5020,7 @@ static void do_load1(EditState *s, const char *filename1, /* Try to determine the desired mode based on the filename. * This avoids having to set c-mode for each new .c or .h file. */ buf[0] = '\0'; - selected_mode = probe_mode(s, S_IFREG, buf, 0); + selected_mode = probe_mode(s, S_IFREG, buf, 0, 0); /* XXX: avoid loading file */ if (selected_mode) do_set_mode(s, selected_mode, NULL); @@ -5023,6 +5028,7 @@ static void do_load1(EditState *s, const char *filename1, } else { mode = st.st_mode; buf_size = 0; + f = NULL; if (S_ISREG(mode)) { f = fopen(filename, "r"); if (!f) @@ -5031,14 +5037,13 @@ static void do_load1(EditState *s, const char *filename1, if (buf_size < 0) { fail1: fclose(f); + f = NULL; goto fail; } - } else { - f = NULL; } } buf[buf_size] = '\0'; - selected_mode = probe_mode(s, mode, buf, buf_size); + selected_mode = probe_mode(s, mode, buf, buf_size, st.st_size); if (!selected_mode) goto fail1; bdt = selected_mode->data_type; @@ -5058,6 +5063,7 @@ static void do_load1(EditState *s, const char *filename1, /* XXX: invalid place */ edit_invalidate(s); return; + fail: put_status(s, "Could not open '%s'", filename); } @@ -5388,7 +5394,8 @@ int eb_search(EditBuffer *b, int offset, int dir, u8 *buf, int size, } } -#define SEARCH_LENGTH 80 +/* should separate search string length and number of match positions */ +#define SEARCH_LENGTH 256 #define FOUND_TAG 0x80000000 /* store last searched string */ diff --git a/qe.h b/qe.h index e30ce35..c8fd659 100644 --- a/qe.h +++ b/qe.h @@ -218,6 +218,7 @@ void css_strtolower(char *buf, int buf_size); void skip_spaces(const char **pp); int strfind(const char *keytable, const char *str, int casefold); +const void *memstr(const void *buf, int size, const char *str); #define stristart(str, val, ptr) qe_stristart(str, val, ptr) int stristart(const char *str, const char *val, const char **ptr); @@ -961,11 +962,12 @@ struct EditState { struct DisplayState; typedef struct ModeProbeData { - char *filename; - unsigned char *buf; + const char *filename; + const u8 *buf; int buf_size; - //int total_size; + int line_len; int mode; /* unix mode */ + long total_size; } ModeProbeData; /* private data saved by a mode so that it can be restored when the diff --git a/util.c b/util.c index b9e209c..788a6ae 100644 --- a/util.c +++ b/util.c @@ -391,6 +391,29 @@ int strfind(const char *keytable, const char *str, int casefold) } } +const void *memstr(const void *buf, int size, const char *str) +{ + int c, len; + const u8 *p, *buf_max; + + c = *str++; + if (!c) { + /* empty string matches start of buffer */ + return buf; + } + + len = strlen(str); + if (len >= size) + return NULL; + + buf_max = (const u8*)buf + size - len; + for (p = buf; p < buf_max; p++) { + if (*p == c && !memcmp(p + 1, str, len)) + return p; + } + return NULL; +} + void skip_spaces(const char **pp) { const char *p;