X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=recctrl%2Frectext.c;h=c243f8c68dad2a85e8a082ef43e78b2ccac263fc;hb=b912fcbe609de53861c5cf9b5196986dd97a1462;hp=99957160e14c15d27e8f2c9242750ece899e18ec;hpb=896c0427df9d8eff5de6a1735dcd992e067df844;p=idzebra-moved-to-github.git diff --git a/recctrl/rectext.c b/recctrl/rectext.c index 9995716..c243f8c 100644 --- a/recctrl/rectext.c +++ b/recctrl/rectext.c @@ -1,6 +1,6 @@ -/* $Id: rectext.c,v 1.15 2002-08-02 19:26:56 adam Exp $ - Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002 - Index Data Aps +/* $Id: rectext.c,v 1.26 2005-03-31 12:42:07 adam Exp $ + Copyright (C) 1995-2005 + Index Data ApS This file is part of the Zebra server. @@ -25,16 +25,30 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include #include -#include -#include "rectext.h" +#include +#include -static void *text_init (RecType recType) +struct filter_info { + char *sep; +}; + +static void *filter_init (Res res, RecType recType) { - return 0; + struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo)); + tinfo->sep = 0; + return tinfo; +} + +static void filter_config(void *clientData, Res res, const char *args) +{ + } -static void text_destroy (void *clientData) +static void filter_destroy (void *clientData) { + struct filter_info *tinfo = clientData; + xfree (tinfo->sep); + xfree (tinfo); } struct buf_info { @@ -44,7 +58,7 @@ struct buf_info { int max; }; -struct buf_info *buf_open (struct recExtractCtrl *p) +static struct buf_info *buf_open (struct recExtractCtrl *p) { struct buf_info *fi = (struct buf_info *) xmalloc (sizeof(*fi)); @@ -55,7 +69,7 @@ struct buf_info *buf_open (struct recExtractCtrl *p) return fi; } -int buf_read (struct buf_info *fi, char *dst) +static int buf_read (struct filter_info *tinfo, struct buf_info *fi, char *dst) { if (fi->offset >= fi->max) { @@ -67,38 +81,51 @@ int buf_read (struct buf_info *fi, char *dst) return 0; } *dst = fi->buf[(fi->offset)++]; + if (tinfo->sep && *dst == *tinfo->sep) + { + off_t off = (*fi->p->tellf)(fi->p->fh); + (*fi->p->endf)(fi->p->fh, off - (fi->max - fi->offset)); + return 0; + } return 1; } -void buf_close (struct buf_info *fi) +static void buf_close (struct buf_info *fi) { xfree (fi->buf); xfree (fi); } -static int text_extract (void *clientData, struct recExtractCtrl *p) +static int filter_extract (void *clientData, struct recExtractCtrl *p) { + struct filter_info *tinfo = clientData; char w[512]; RecWord recWord; int r; struct buf_info *fi = buf_open (p); +#if 0 + yaz_log(YLOG_LOG, "filter_extract off=%ld", + (long) (*fi->p->tellf)(fi->p->fh)); +#endif + xfree(tinfo->sep); + tinfo->sep = 0; (*p->init)(p, &recWord); recWord.reg_type = 'w'; do { int i = 0; - r = buf_read (fi, w); + r = buf_read (tinfo, fi, w); while (r > 0 && i < 511 && w[i] != '\n' && w[i] != '\r') { i++; - r = buf_read (fi, w + i); - } + r = buf_read (tinfo, fi, w + i); + } if (i) { - recWord.string = w; - recWord.length = i; + recWord.term_buf = w; + recWord.term_len = i; (*p->tokenAdd)(&recWord); } } while (r > 0); @@ -106,12 +133,13 @@ static int text_extract (void *clientData, struct recExtractCtrl *p) return RECCTRL_EXTRACT_OK; } -static int text_retrieve (void *clientData, struct recRetrieveCtrl *p) +static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p) { - int r, text_ptr = 0; - static char *text_buf = NULL; - static int text_size = 0; - int start_flag = 1; + int r, filter_ptr = 0; + static char *filter_buf = NULL; + static int filter_size = 0; + int make_header = 1; + int make_body = 1; const char *elementSetName = NULL; int no_lines = 0; @@ -119,41 +147,61 @@ static int text_retrieve (void *clientData, struct recRetrieveCtrl *p) p->comp->u.simple->which == Z_ElementSetNames_generic) elementSetName = p->comp->u.simple->u.generic; - /* don't make header for the R(aw) element set name */ - if (elementSetName && !strcmp(elementSetName, "R")) - start_flag = 0; + if (elementSetName) + { + /* don't make header for the R(aw) element set name */ + if (!strcmp(elementSetName, "R")) + { + make_header = 0; + make_body = 1; + } + /* only make header for the H(eader) element set name */ + else if (!strcmp(elementSetName, "H")) + { + make_header = 1; + make_body = 0; + } + } while (1) { - if (text_ptr + 4096 >= text_size) + if (filter_ptr + 4096 >= filter_size) { char *nb; - text_size = 2*text_size + 8192; - nb = (char *) xmalloc (text_size); - if (text_buf) + filter_size = 2*filter_size + 8192; + nb = (char *) xmalloc (filter_size); + if (filter_buf) { - memcpy (nb, text_buf, text_ptr); - xfree (text_buf); + memcpy (nb, filter_buf, filter_ptr); + xfree (filter_buf); } - text_buf = nb; + filter_buf = nb; } - if (start_flag) + if (make_header && filter_ptr == 0) { - start_flag = 0; if (p->score >= 0) { - sprintf (text_buf, "Rank: %d\n", p->score); - text_ptr = strlen(text_buf); + sprintf (filter_buf, "Rank: %d\n", p->score); + filter_ptr = strlen(filter_buf); } - sprintf (text_buf + text_ptr, "Local Number: %d\n", p->localno); - text_ptr = strlen(text_buf); + sprintf (filter_buf + filter_ptr, "Local Number: " ZINT_FORMAT "\n", + p->localno); + filter_ptr = strlen(filter_buf); + if (p->fname) + { + sprintf (filter_buf + filter_ptr, "Filename: %s\n", p->fname); + filter_ptr = strlen(filter_buf); + } + strcpy(filter_buf+filter_ptr++, "\n"); } - r = (*p->readf)(p->fh, text_buf + text_ptr, 4096); + if (!make_body) + break; + r = (*p->readf)(p->fh, filter_buf + filter_ptr, 4096); if (r <= 0) break; - text_ptr += r; + filter_ptr += r; } - text_buf[text_ptr] = '\0'; + filter_buf[filter_ptr] = '\0'; if (elementSetName) { if (!strcmp (elementSetName, "B")) @@ -163,7 +211,7 @@ static int text_retrieve (void *clientData, struct recRetrieveCtrl *p) } if (no_lines) { - char *p = text_buf; + char *p = filter_buf; int i = 0; while (++i <= no_lines && (p = strchr (p, '\n'))) @@ -171,21 +219,33 @@ static int text_retrieve (void *clientData, struct recRetrieveCtrl *p) if (p) { p[1] = '\0'; - text_ptr = p-text_buf; + filter_ptr = p-filter_buf; } } p->output_format = VAL_SUTRS; - p->rec_buf = text_buf; - p->rec_len = text_ptr; + p->rec_buf = filter_buf; + p->rec_len = filter_ptr; return 0; } -static struct recType text_type = { +static struct recType filter_type = { + 0, "text", - text_init, - text_destroy, - text_extract, - text_retrieve + filter_init, + filter_config, + filter_destroy, + filter_extract, + filter_retrieve }; -RecType recTypeText = &text_type; +RecType +#ifdef IDZEBRA_STATIC_TEXT +idzebra_filter_text +#else +idzebra_filter +#endif + +[] = { + &filter_type, + 0, +};