X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=recctrl%2Frectext.c;h=c243f8c68dad2a85e8a082ef43e78b2ccac263fc;hb=9b9f570a2960c2c8a7026b2faee943794b08ce49;hp=4c275225c764fbbab606dc2093ba7390c5e45f38;hpb=75049be3951292bbbbd53d83ca38ccbd191e4b08;p=idzebra-moved-to-github.git diff --git a/recctrl/rectext.c b/recctrl/rectext.c index 4c27522..c243f8c 100644 --- a/recctrl/rectext.c +++ b/recctrl/rectext.c @@ -1,78 +1,54 @@ -/* - * Copyright (C) 1994-1998, Index Data - * All rights reserved. - * Sebastian Hammer, Adam Dickmeiss - * - * $Log: rectext.c,v $ - * Revision 1.9 1998-10-16 08:14:38 adam - * Updated record control system. - * - * Revision 1.8 1998/05/20 10:12:27 adam - * Implemented automatic EXPLAIN database maintenance. - * Modified Zebra to work with ASN.1 compiled version of YAZ. - * - * Revision 1.7 1998/03/11 11:19:05 adam - * Changed the way sequence numbers are generated. - * - * Revision 1.6 1998/02/10 12:03:06 adam - * Implemented Sort. - * - * Revision 1.5 1997/10/27 14:33:06 adam - * Moved towards generic character mapping depending on "structure" - * field in abstract syntax file. Fixed a few memory leaks. Fixed - * bug with negative integers when doing searches with relational - * operators. - * - * Revision 1.4 1996/11/04 14:09:16 adam - * Minor changes. - * - * Revision 1.3 1996/11/01 09:00:33 adam - * This simple "text" format now supports element specs B and M. - * - * Revision 1.2 1996/10/29 14:02:45 adam - * Uses buffered read to speed up things. - * - * Revision 1.1 1996/10/11 10:57:28 adam - * New module recctrl. Used to manage records (extract/retrieval). - * - * Revision 1.7 1996/01/17 14:57:55 adam - * Prototype changed for reader functions in extract/retrieve. File - * is identified by 'void *' instead of 'int. - * - * Revision 1.6 1995/10/10 13:59:24 adam - * Function rset_open changed its wflag parameter to general flags. - * - * Revision 1.5 1995/10/02 16:24:39 adam - * Use attribute actually used in search requests. - * - * Revision 1.4 1995/10/02 15:42:55 adam - * Extract uses file descriptors instead of FILE pointers. - * - * Revision 1.3 1995/09/28 09:19:45 adam - * xfree/xmalloc used everywhere. - * Extract/retrieve method seems to work for text records. - * - * Revision 1.2 1995/09/15 14:45:21 adam - * Retrieve control. - * Work on truncation. - * - * Revision 1.1 1995/09/14 07:48:25 adam - * Record control management. - * - */ +/* $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. + +Zebra is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 2, or (at your option) any later +version. + +Zebra is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Zebra; see the file LICENSE.zebra. If not, write to the +Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA +02111-1307, USA. +*/ + + #include #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) { + struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo)); + tinfo->sep = 0; + return tinfo; } -static void text_destroy (RecType recType) +static void filter_config(void *clientData, Res res, const char *args) { + +} + +static void filter_destroy (void *clientData) +{ + struct filter_info *tinfo = clientData; + xfree (tinfo->sep); + xfree (tinfo); } struct buf_info { @@ -82,18 +58,18 @@ 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 = xmalloc (sizeof(*fi)); + struct buf_info *fi = (struct buf_info *) xmalloc (sizeof(*fi)); fi->p = p; - fi->buf = xmalloc (4096); + fi->buf = (char *) xmalloc (4096); fi->offset = 1; fi->max = 1; 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) { @@ -105,51 +81,65 @@ 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 (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; - (*p->addWord)(&recWord); + recWord.term_buf = w; + recWord.term_len = i; + (*p->tokenAdd)(&recWord); } } while (r > 0); buf_close (fi); - return 0; + return RECCTRL_EXTRACT_OK; } -static int text_retrieve (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; @@ -157,38 +147,61 @@ static int text_retrieve (struct recRetrieveCtrl *p) p->comp->u.simple->which == Z_ElementSetNames_generic) elementSetName = p->comp->u.simple->u.generic; + 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 = 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")) @@ -198,7 +211,7 @@ static int text_retrieve (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'))) @@ -206,21 +219,33 @@ static int text_retrieve (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, +};