X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=database%2Ftags.c;h=5b3b3ce9ab8ed5c5c3ece9f7b3ac6ed462705c48;hb=efabc7da2c7dff11ab6835d23655a023f1cbc296;hp=8386705054c56f189359382b84c02cd4c9f9aee2;hpb=54789cc8538714fc3622646721f6f2ecceae94ac;p=blerg.git diff --git a/database/tags.c b/database/tags.c index 8386705..5b3b3ce 100644 --- a/database/tags.c +++ b/database/tags.c @@ -12,28 +12,26 @@ #include #include "tags.h" #include "util.h" +#include "database.h" #include "config.h" -#define MAX_TAG_LENGTH 64 -#define MAX_TAGS 1024 - -#define TAG_CHAR(c) ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '_') int tag_scan(const char *author, const char *data, int len, uint64_t record) { - char *taglist[MAX_TAGS]; + char *taglist[MAX_TAGS_PER_RECORD]; + char last_char = ' '; int n_tags = 0; int i, j; for (i = 0; i < len; i++) { tag_scan_start: - if (data[i] == '#' || data[i] == '@') { - if (n_tags == MAX_TAGS) { + if (WHITESPACE(last_char) && (data[i] == '#' || data[i] == '@')) { + if (n_tags == MAX_TAGS_PER_RECORD) { fprintf(stderr, "Too many tags in message\n"); break; } int begin = i; int start = ++i; - while (i < len && TAG_CHAR(data[i]) && (i - start < MAX_TAG_LENGTH)) { + while (i < len && VALID_CHAR(data[i]) && (i - start < MAX_TAG_LENGTH)) { i++; } if (start - i == 0) continue; @@ -43,14 +41,17 @@ tag_scan_start: if (!strncmp(tag, taglist[j], MAX_TAG_LENGTH)) { // We already have this tag. Start over. free(tag); + last_char = data[i-1]; goto tag_scan_start; } } taglist[n_tags] = tag; n_tags++; + last_char = data[i-1]; // We goto here so i doesn't get incremented goto tag_scan_start; } + last_char = data[i]; } for (i = 0; i < n_tags; i++) { @@ -103,7 +104,7 @@ struct tag * tag_list(const char *tag, uint64_t offset, int *count, int directio struct tag *retlist; uint64_t n_tag_records; - if (!valid_name(tag + 1)) + if (!valid_tag_name(tag + 1)) return NULL; switch(tag[0]) { @@ -127,26 +128,28 @@ struct tag * tag_list(const char *tag, uint64_t offset, int *count, int directio fstat(tag_fd, &st); if (st.st_size == 0) { - close(tag_fd); *count = 0; - return NULL; + goto tag_list_map_failed; } n_tag_records = st.st_size / sizeof(struct tag); - if (*count > n_tag_records) - *count = n_tag_records; + if (*count > n_tag_records - offset) + *count = n_tag_records - offset; if (offset > n_tag_records) { fprintf(stderr, "Cannot access tag record beyond end\n"); - return NULL; + *count = 0; + goto tag_list_map_failed; } taglist = (struct tag *) mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, tag_fd, 0); if (taglist == MAP_FAILED) { perror("Could not mmap tag file"); + *count = 0; goto tag_list_map_failed; } retlist = (struct tag *) malloc(sizeof(struct tag) * *count); if (retlist == NULL) { perror("Could not allocate memory for tag list"); + *count = 0; goto tag_list_malloc_failed; } switch(direction) { @@ -173,7 +176,7 @@ tag_list_open_failed: int tag_exists(const char *tag) { char filename[512]; - if (!valid_name(tag + 1)) + if (!valid_tag_name(tag + 1)) return 0; if (!(tag[0] == '@' || tag[0] == '#')) {