Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / DtSearch / dtsrutil.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*
24  *   COMPONENT_NAME: austext
25  *
26  *   FUNCTIONS: clear_hitwords
27  *              clear_usrblk_record
28  *              get_hitlist_text
29  *              print_dittolist
30  *              print_usrblk_record
31  *
32  *   ORIGINS: 27
33  *
34  *
35  *   (C) COPYRIGHT International Business Machines Corp. 1991,1995
36  *   All Rights Reserved
37  *   Licensed Materials - Property of IBM
38  *   US Government Users Restricted Rights - Use, duplication or
39  *   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
40  */
41 /*********************** DTSRUTIL.C ***********************
42  * $XConsortium: dtsrutil.c /main/5 1996/08/12 13:18:30 cde-ibm $
43  * October 1991.
44  * Set of utility functions for opera User Interfaces (UIs),
45  * although may also be used by Opera Engine (OE) itself.
46  * Function free_dittolist() replaced by macro call to free_llist().
47  * Ausapi wrapped aa_free_dittolist around free_llist().
48  * DtSearch renamed aa_free_dittolist to DtSearchFreeResults().
49  *
50  * $Log$
51  * Revision 2.4  1996/03/13  22:54:21  miker
52  * Changed char to UCHAR several places.
53  *
54  * Revision 2.3  1996/03/05  16:05:45  miker
55  * Added print_stems().
56  *
57  * Revision 2.2  1995/10/25  18:36:09  miker
58  * Renamed from uiutil.c.  Added prolog.
59  *
60  * Log: uiutil.c,v
61  * Revision 2.1  1995/09/22  22:19:47  miker
62  * Freeze DtSearch 0.1, AusText 2.1.8
63  *
64  * Revision 1.13  1995/09/05  19:17:28  miker
65  * Minor name and function changes for DtSearch.  Made usrblk global.
66  */
67 #include "SearchE.h"
68 #include <string.h>
69 #include <sys/stat.h>
70 #define MS_misc         1
71
72 /********#define DEBUG_CLEARREC*********/
73 /**********#define DEBUG_FAX***********/
74 #define PROGNAME        "DTSRUTIL"
75
76
77 /************************************************/
78 /*                                              */
79 /*                print_stems                   */
80 /*                                              */
81 /************************************************/
82 /* For debugging.  Prints passed stems array to aa_stderr. */
83 void    print_stems (int stemcount, void *stems, char *prefix)
84 {
85     int         i;
86     UCHAR       *cptr;
87
88     if (stemcount <= 0) {
89         fprintf (aa_stderr, "%s Stems array is empty.\n", prefix);
90         fflush (aa_stderr);
91         return;
92     }
93     fprintf (aa_stderr, "%s stemct = %d:\n", prefix, stemcount);
94     for (i=0;  i<stemcount;  i++) {
95         cptr = (UCHAR *) stems + (i * DtSrMAXWIDTH_HWORD);
96         if (i == 3 || i == 6)
97             fputc ('\n', aa_stderr);
98         fprintf (aa_stderr, "   #%d:'%c%s'",
99             i,  (*cptr < 32) ? '~' : *cptr,  cptr + 1);
100     }
101     fputc ('\n', aa_stderr);
102     fflush (aa_stderr);
103     return;
104 } /* print_stems() */
105
106
107 /************************************************/
108 /*                                              */
109 /*              print_dittolist                 */
110 /*                                              */
111 /************************************************/
112 /* Only for debugging in oe.  Dumps first 10 list nodes. */
113 void            print_dittolist (DtSrResult * dittolist, char *prefix)
114 {
115     DtSrResult     *dit;
116     char            datebuf[48];
117     int             maxcount = 10;
118
119     if (prefix == NULL)
120         prefix = "HITLIST";
121     if (dittolist == NULL) {
122         fprintf (aa_stderr, "%s Dittolist is empty.\n", prefix);
123         return;
124     }
125     fprintf (aa_stderr, "%s First %d hits on dittolist at %p:\n",
126         prefix, maxcount, dittolist);
127     for (dit = dittolist; dit != NULL && maxcount-- > 0; dit = dit->link) {
128         if (dit->objdate == 0)
129             strcpy (datebuf, "0");
130         else
131             strftime (datebuf, sizeof (datebuf), "%y/%m/%d",
132                 objdate2tm (dit->objdate));
133         fprintf (aa_stderr,
134             " dbn=%d dba=%d:%ld prox=%d sz=%ld date=%s key='%s'\n",
135             dit->dbn, dit->dba >> 24, (long)dit->dba & 0xffffffL,
136             dit->proximity, (long)dit->objsize,
137             datebuf, dit->reckey);
138         if (dit->abstractp)
139             if (dit->abstractp[0] != 0)
140                 fprintf (aa_stderr, "    abstract='%.50s'\n", dit->abstractp);
141     }
142     return;
143 }  /* print_dittolist() */
144
145 /************************************************/
146 /*                                              */
147 /*              get_hitlist_text                */
148 /*                                              */
149 /************************************************/
150 /* Converts dittolist into a single block of clean ascii text
151  * for use as a hitlist, or for printing out to hardcopy.
152  * Wraps lines intelligently to ensure no line is greater than maxlen.
153  * If maxlen == 0, no wrapping is performed.
154  * Returns static pointer to dynamically allocated buffer--
155  * if permanent copy is desired, caller must copy text
156  * to his own buffer before next call.
157  */
158 char           *get_hitlist_text (int maxlen)
159 {
160     static char    *text = NULL;
161     int             sofar;
162     char           *src, *targ, *eol;
163     DtSrResult     *dit;
164     char            sprintbuf[80];
165     size_t          mallocsz;
166
167     if (usrblk.dittocount <= 0L) {
168         sprintf (sprintbuf, catgets (dtsearch_catd, MS_misc, 96,
169                 "%s Hitlist is empty."), PROGNAME"96");
170         DtSearchAddMessage (sprintbuf);
171         return NULL;
172     }
173     if (maxlen > 0 && maxlen < DtSrMAX_DB_KEYSIZE + 7) {
174         sprintf (sprintbuf, PROGNAME "97 maxlen = %d is too small.", maxlen);
175         DtSearchAddMessage (sprintbuf);
176         return NULL;
177     }
178
179 /* Allocate enough memory for each item in ditto list */
180     mallocsz = usrblk.dittocount *
181         (usrblk.abstrbufsz + DtSrMAX_DB_KEYSIZE + 80L);
182     if (text != NULL)
183         free (text);
184     text = austext_malloc (mallocsz, PROGNAME "103", NULL);
185
186 /* Loop thru ditto list, creating text out of hitlist */
187     targ = text;
188     for (dit = usrblk.dittolist; dit != NULL; dit = dit->link) {
189         if (maxlen == 0) {
190             sprintf (targ, "%5d %-*s %s\n%n",
191                 dit->proximity,
192                 DtSrMAX_DB_KEYSIZE,
193                 dit->reckey,
194                 dit->abstractp,
195                 &sofar);
196             targ += sofar;
197         }
198         else    /* (maxlen > 0) */
199             /*
200              *  We may have more text than can fit on one line.
201              * Wrap lines to fit within maxlen. 
202              */
203         {
204             eol = targ + maxlen;
205             sprintf (targ, "%5d %-*s %n",
206                 dit->proximity,
207                 DtSrMAX_DB_KEYSIZE,
208                 dit->reckey,
209                 &sofar);
210             targ += sofar;
211             src = dit->abstractp;
212             for (;;) {
213                 while (targ < eol && *src != 0)
214                     *targ++ = *src++;
215                 *targ++ = '\n';
216                 if (*src == 0)
217                     break;
218                 eol = targ + maxlen;
219                 strcpy (targ, "         ");
220                 targ += 9;
221             }
222         }       /* end else (maxlen > 0) */
223     }   /* end loop on dittolist */
224
225     *targ = 0;  /* ...I don't know about you, but I always forget
226                  * this */
227
228     if (usrblk.debug & USRDBG_UTIL)
229         fprintf (aa_stderr, PROGNAME "160 "
230             "get_hitlist_text(): mallocsz=%ld textlen=%ld\n",
231             mallocsz, strlen (text));
232     return text;
233 }  /* get_hitlist_text() */
234
235
236 /****************************************/
237 /*                                      */
238 /*         print_usrblk_record          */
239 /*                                      */
240 /****************************************/
241 /* dumps out usrblk 'record' fields for debugging */
242 void    print_usrblk_record (char *prefix)
243 {
244     fprintf (aa_stderr, "%s usrblk_record(): dba=%ld:%ld objkey='%s'\n"
245         "  notesp=%p clearlen=%d cleartxt='%.30s'\n"
246         "  #hitw=%d hitw=%p abstr='%.24s'\n",
247         prefix,
248         (long)usrblk.dba >> 24, (long)usrblk.dba & 0xffffffL,
249         usrblk.objrec.or_objkey,
250         usrblk.notes,
251         (int)usrblk.clearlen,
252         NULLORSTR (usrblk.cleartext),
253         (int)usrblk.hitwcount,
254         usrblk.hitwords,
255         NULLORSTR (usrblk.abstrbuf));
256     return;
257 }  /* print_usrblk_record() */
258
259
260 /****************************************/
261 /*                                      */
262 /*         clear_usrblk_record          */
263 /*                                      */
264 /****************************************/
265 /* Clears and initializes all USRBLK fields that
266  * represent an opera record from vista, specifically
267  * objrec, notes, textblobs, clearlen, and cleartext.
268  * This function MUST NOT ALTER dba, hitwords, and hitwcount,
269  * because many callers require these things to remain untouched.
270  * If usrblk.objrec.or_objkey[0] == '\0',
271  * UI must presume that all record fields are invalid
272  * including usrblk.objfzkey and usrblk.abstract.
273  */
274 void            clear_usrblk_record (void)
275 {
276     usrblk.clearlen = 0;
277     if (usrblk.cleartext != NULL) {
278         free (usrblk.cleartext);
279         usrblk.cleartext = NULL;
280     }
281     free_llist (&usrblk.notes);
282     usrblk.objrec.or_objkey[0] = '\0';
283     if (usrblk.abstrbufsz > 0)
284         usrblk.abstrbuf[0] = 0;
285     if (usrblk.debug & USRDBG_RETRVL)
286         print_usrblk_record (PROGNAME "600 clear: ");
287     return;
288 }  /* clear_usrblk_record() */
289
290
291 /************************************************/
292 /*                                              */
293 /*               clear_hitwords                 */
294 /*                                              */
295 /************************************************/
296 /* clears hitwcount and array in usrblk */
297 void            clear_hitwords (void)
298 {
299     usrblk.hitwcount = 0;
300     if (usrblk.hitwords != NULL) {
301         free (usrblk.hitwords);
302         usrblk.hitwords = NULL;
303     }
304     return;
305 }  /* clear_hitwords() */
306
307
308 /*********************** DTSRUTIL.C ***********************/