dtsrutil.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 libraries 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 <stdlib.h>
  70. #include <sys/stat.h>
  71. #define MS_misc 1
  72. /********#define DEBUG_CLEARREC*********/
  73. /**********#define DEBUG_FAX***********/
  74. #define PROGNAME "DTSRUTIL"
  75. /************************************************/
  76. /* */
  77. /* print_stems */
  78. /* */
  79. /************************************************/
  80. /* For debugging. Prints passed stems array to aa_stderr. */
  81. void print_stems (int stemcount, void *stems, char *prefix)
  82. {
  83. int i;
  84. UCHAR *cptr;
  85. if (stemcount <= 0) {
  86. fprintf (aa_stderr, "%s Stems array is empty.\n", prefix);
  87. fflush (aa_stderr);
  88. return;
  89. }
  90. fprintf (aa_stderr, "%s stemct = %d:\n", prefix, stemcount);
  91. for (i=0; i<stemcount; i++) {
  92. cptr = (UCHAR *) stems + (i * DtSrMAXWIDTH_HWORD);
  93. if (i == 3 || i == 6)
  94. fputc ('\n', aa_stderr);
  95. fprintf (aa_stderr, " #%d:'%c%s'",
  96. i, (*cptr < 32) ? '~' : *cptr, cptr + 1);
  97. }
  98. fputc ('\n', aa_stderr);
  99. fflush (aa_stderr);
  100. return;
  101. } /* print_stems() */
  102. /************************************************/
  103. /* */
  104. /* print_dittolist */
  105. /* */
  106. /************************************************/
  107. /* Only for debugging in oe. Dumps first 10 list nodes. */
  108. void print_dittolist (DtSrResult * dittolist, char *prefix)
  109. {
  110. DtSrResult *dit;
  111. char datebuf[48];
  112. int maxcount = 10;
  113. if (prefix == NULL)
  114. prefix = "HITLIST";
  115. if (dittolist == NULL) {
  116. fprintf (aa_stderr, "%s Dittolist is empty.\n", prefix);
  117. return;
  118. }
  119. fprintf (aa_stderr, "%s First %d hits on dittolist at %p:\n",
  120. prefix, maxcount, (void *) dittolist);
  121. for (dit = dittolist; dit != NULL && maxcount-- > 0; dit = dit->link) {
  122. if (dit->objdate == 0)
  123. strcpy (datebuf, "0");
  124. else
  125. strftime (datebuf, sizeof (datebuf), "%y/%m/%d",
  126. objdate2tm (dit->objdate));
  127. fprintf (aa_stderr,
  128. " dbn=%d dba=%d:%ld prox=%d sz=%ld date=%s key='%s'\n",
  129. dit->dbn, dit->dba >> 24, (long)dit->dba & 0xffffffL,
  130. dit->proximity, (long)dit->objsize,
  131. datebuf, dit->reckey);
  132. if (dit->abstractp)
  133. if (dit->abstractp[0] != 0)
  134. fprintf (aa_stderr, " abstract='%.50s'\n", dit->abstractp);
  135. }
  136. return;
  137. } /* print_dittolist() */
  138. /************************************************/
  139. /* */
  140. /* get_hitlist_text */
  141. /* */
  142. /************************************************/
  143. /* Converts dittolist into a single block of clean ascii text
  144. * for use as a hitlist, or for printing out to hardcopy.
  145. * Wraps lines intelligently to ensure no line is greater than maxlen.
  146. * If maxlen == 0, no wrapping is performed.
  147. * Returns static pointer to dynamically allocated buffer--
  148. * if permanent copy is desired, caller must copy text
  149. * to his own buffer before next call.
  150. */
  151. char *get_hitlist_text (int maxlen)
  152. {
  153. static char *text = NULL;
  154. int sofar;
  155. char *src, *targ, *eol;
  156. DtSrResult *dit;
  157. char sprintbuf[80];
  158. size_t mallocsz;
  159. if (usrblk.dittocount <= 0L) {
  160. sprintf (sprintbuf, CATGETS(dtsearch_catd, MS_misc, 96,
  161. "%s Hitlist is empty."), PROGNAME"96");
  162. DtSearchAddMessage (sprintbuf);
  163. return NULL;
  164. }
  165. if (maxlen > 0 && maxlen < DtSrMAX_DB_KEYSIZE + 7) {
  166. sprintf (sprintbuf, PROGNAME "97 maxlen = %d is too small.", maxlen);
  167. DtSearchAddMessage (sprintbuf);
  168. return NULL;
  169. }
  170. /* Allocate enough memory for each item in ditto list */
  171. mallocsz = usrblk.dittocount *
  172. (usrblk.abstrbufsz + DtSrMAX_DB_KEYSIZE + 80L);
  173. if (text != NULL)
  174. free (text);
  175. text = austext_malloc (mallocsz, PROGNAME "103", NULL);
  176. /* Loop thru ditto list, creating text out of hitlist */
  177. targ = text;
  178. for (dit = usrblk.dittolist; dit != NULL; dit = dit->link) {
  179. if (maxlen == 0) {
  180. sprintf (targ, "%5d %-*s %s\n%n",
  181. dit->proximity,
  182. DtSrMAX_DB_KEYSIZE,
  183. dit->reckey,
  184. dit->abstractp,
  185. &sofar);
  186. targ += sofar;
  187. }
  188. else /* (maxlen > 0) */
  189. /*
  190. * We may have more text than can fit on one line.
  191. * Wrap lines to fit within maxlen.
  192. */
  193. {
  194. eol = targ + maxlen;
  195. sprintf (targ, "%5d %-*s %n",
  196. dit->proximity,
  197. DtSrMAX_DB_KEYSIZE,
  198. dit->reckey,
  199. &sofar);
  200. targ += sofar;
  201. src = dit->abstractp;
  202. for (;;) {
  203. while (targ < eol && *src != 0)
  204. *targ++ = *src++;
  205. *targ++ = '\n';
  206. if (*src == 0)
  207. break;
  208. eol = targ + maxlen;
  209. strcpy (targ, " ");
  210. targ += 9;
  211. }
  212. } /* end else (maxlen > 0) */
  213. } /* end loop on dittolist */
  214. *targ = 0; /* ...I don't know about you, but I always forget
  215. * this */
  216. if (usrblk.debug & USRDBG_UTIL)
  217. fprintf (aa_stderr, PROGNAME "160 "
  218. "get_hitlist_text(): mallocsz=%lu textlen=%lu\n",
  219. (unsigned long) mallocsz, (unsigned long) strlen (text));
  220. return text;
  221. } /* get_hitlist_text() */
  222. /****************************************/
  223. /* */
  224. /* print_usrblk_record */
  225. /* */
  226. /****************************************/
  227. /* dumps out usrblk 'record' fields for debugging */
  228. void print_usrblk_record (char *prefix)
  229. {
  230. fprintf (aa_stderr, "%s usrblk_record(): dba=%ld:%ld objkey='%s'\n"
  231. " notesp=%p clearlen=%d cleartxt='%.30s'\n"
  232. " #hitw=%d hitw=%p abstr='%.24s'\n",
  233. prefix,
  234. (long)usrblk.dba >> 24, (long)usrblk.dba & 0xffffffL,
  235. usrblk.objrec.or_objkey,
  236. (void *) usrblk.notes,
  237. (int)usrblk.clearlen,
  238. NULLORSTR (usrblk.cleartext),
  239. (int)usrblk.hitwcount,
  240. (void *) usrblk.hitwords,
  241. NULLORSTR (usrblk.abstrbuf));
  242. return;
  243. } /* print_usrblk_record() */
  244. /****************************************/
  245. /* */
  246. /* clear_usrblk_record */
  247. /* */
  248. /****************************************/
  249. /* Clears and initializes all USRBLK fields that
  250. * represent an opera record from vista, specifically
  251. * objrec, notes, textblobs, clearlen, and cleartext.
  252. * This function MUST NOT ALTER dba, hitwords, and hitwcount,
  253. * because many callers require these things to remain untouched.
  254. * If usrblk.objrec.or_objkey[0] == '\0',
  255. * UI must presume that all record fields are invalid
  256. * including usrblk.objfzkey and usrblk.abstract.
  257. */
  258. void clear_usrblk_record (void)
  259. {
  260. usrblk.clearlen = 0;
  261. if (usrblk.cleartext != NULL) {
  262. free (usrblk.cleartext);
  263. usrblk.cleartext = NULL;
  264. }
  265. free_llist (&usrblk.notes);
  266. usrblk.objrec.or_objkey[0] = '\0';
  267. if (usrblk.abstrbufsz > 0)
  268. usrblk.abstrbuf[0] = 0;
  269. if (usrblk.debug & USRDBG_RETRVL)
  270. print_usrblk_record (PROGNAME "600 clear: ");
  271. return;
  272. } /* clear_usrblk_record() */
  273. /************************************************/
  274. /* */
  275. /* clear_hitwords */
  276. /* */
  277. /************************************************/
  278. /* clears hitwcount and array in usrblk */
  279. void clear_hitwords (void)
  280. {
  281. usrblk.hitwcount = 0;
  282. if (usrblk.hitwords != NULL) {
  283. free (usrblk.hitwords);
  284. usrblk.hitwords = NULL;
  285. }
  286. return;
  287. } /* clear_hitwords() */
  288. /*********************** DTSRUTIL.C ***********************/