New module recctrl. Used to manage records (extract/retrieval).
[idzebra-moved-to-github.git] / recctrl / rectext.c
1 /*
2  * Copyright (C) 1994-1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: rectext.c,v $
7  * Revision 1.1  1996-10-11 10:57:28  adam
8  * New module recctrl. Used to manage records (extract/retrieval).
9  *
10  * Revision 1.7  1996/01/17 14:57:55  adam
11  * Prototype changed for reader functions in extract/retrieve. File
12  *  is identified by 'void *' instead of 'int.
13  *
14  * Revision 1.6  1995/10/10  13:59:24  adam
15  * Function rset_open changed its wflag parameter to general flags.
16  *
17  * Revision 1.5  1995/10/02  16:24:39  adam
18  * Use attribute actually used in search requests.
19  *
20  * Revision 1.4  1995/10/02  15:42:55  adam
21  * Extract uses file descriptors instead of FILE pointers.
22  *
23  * Revision 1.3  1995/09/28  09:19:45  adam
24  * xfree/xmalloc used everywhere.
25  * Extract/retrieve method seems to work for text records.
26  *
27  * Revision 1.2  1995/09/15  14:45:21  adam
28  * Retrieve control.
29  * Work on truncation.
30  *
31  * Revision 1.1  1995/09/14  07:48:25  adam
32  * Record control management.
33  *
34  */
35 #include <stdio.h>
36 #include <assert.h>
37 #include <ctype.h>
38
39 #include <alexutil.h>
40 #include "rectext.h"
41
42 static void text_init (void)
43 {
44 }
45
46 static int text_extract (struct recExtractCtrl *p)
47 {
48     char w[256];
49     RecWord recWord;
50     int r, seqno = 1;
51
52     (*p->init)(&recWord);
53     recWord.which = Word_String;
54     do
55     {
56         int i = 0;
57
58         r = (*p->readf)(p->fh, w, 1);
59         while (r > 0 && i < 255 && isalnum(w[i]))
60         {
61             i++;
62             r = (*p->readf) (p->fh, w + i, 1);
63         }
64         if (i)
65         {
66             w[i] = 0;
67             recWord.seqno = seqno++;
68             recWord.u.string = w;
69             (*p->add)(&recWord);
70         }
71     } while (r > 0);
72     return 0;
73 }
74
75 static int text_retrieve (struct recRetrieveCtrl *p)
76 {
77     int r, text_ptr = 0;
78     static char *text_buf = NULL;
79     static int text_size = 0;
80     int start_flag = 1;
81
82     while (1)
83     {
84         if (text_ptr + 4096 >= text_size)
85         {
86             char *nb;
87
88             text_size = 2*text_size + 8192;
89             nb = xmalloc (text_size);
90             if (text_buf)
91             {
92                 memcpy (nb, text_buf, text_ptr);
93                 xfree (text_buf);
94             }
95             text_buf = nb;
96         }
97         if (start_flag)
98         {
99             start_flag = 0;
100             if (p->score >= 0)
101             {
102                 sprintf (text_buf, "Rank: %d\n", p->score);
103                 text_ptr = strlen(text_buf);
104             }
105             sprintf (text_buf + text_ptr, "Local Number: %d\n", p->localno);
106             text_ptr = strlen(text_buf);
107         }
108         r = (*p->readf)(p->fh, text_buf + text_ptr, 4096);
109         if (r <= 0)
110             break;
111         text_ptr += r;
112     }
113     p->output_format = VAL_SUTRS;
114     p->rec_buf = text_buf;
115     p->rec_len = text_ptr; 
116     return 0;
117 }
118
119 static struct recType text_type = {
120     "text",
121     text_init,
122     text_extract,
123     text_retrieve
124 };
125
126 RecType recTypeText = &text_type;