home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / Programming / Source / WAIS / ir / wprot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-02  |  67.0 KB  |  2,436 lines

  1. /* WIDE AREA INFORMATION SERVER SOFTWARE:
  2.    No guarantees or restrictions.  See the readme file for the full standard
  3.    disclaimer.    
  4.   
  5.    3.26.90    Harry Morris, morris@think.com
  6.    3.30.90  Harry Morris 
  7.    -    removed chunk code from WAISSearchAPDU,
  8.    -    added makeWAISQueryType1Query() and readWAISType1Query() which replace
  9.    makeWAISQueryTerms() and makeWAISQueryDocs().
  10.    4.11.90  HWM - generalized conditional includes (see c-dialect.h)
  11.    - renamed makeWAISType1Query() to makeWAISTextQuery()
  12.    renamed readWAISType1Query() to readWAISTextQuery()
  13.    5.29.90  TS - fixed bug in makeWAISQueryDocs
  14.    added CSTFreeWAISFoo functions
  15. */
  16.  
  17. #define _C_WAIS_protocol_
  18.  
  19. /*    This file implements the Z39.50 extensions required for WAIS 
  20. */
  21.  
  22. #include "wprot.h"
  23. #include "cutil.h"
  24. #include "panic.h"
  25. #include <string.h> /* for memmove */
  26.  
  27. #ifndef ANSI_LIKE
  28. #include "ustubs.h"
  29. #endif
  30.  
  31. /* very rough estimates of the size of an object */
  32. #define DefWAISInitResponseSize        (size_t)200
  33. #define DefWAISSearchSize            (size_t)3000
  34. #define DefWAISSearchResponseSize    (size_t)6000
  35. #define DefWAISPresentSize            (size_t)1000
  36. #define DefWAISPresentResponseSize    (size_t)6000
  37. #define DefWAISDocHeaderSize        (size_t)500
  38. #define DefWAISShortHeaderSize        (size_t)200
  39. #define DefWAISLongHeaderSize        (size_t)800
  40. #define DefWAISDocTextSize            (size_t)6000
  41. #define DefWAISDocHeadlineSize        (size_t)500
  42. #define DefWAISDocCodeSize            (size_t)500
  43.  
  44. #define RESERVE_SPACE_FOR_WAIS_HEADER(len)    \
  45.      if (*len > 0)                            \
  46.          *len -= header_len;
  47.  
  48. static char* writeUserInfoHeader _AP((data_tag tag,long infoSize,    
  49.                       long estHeaderSize,char* buffer,
  50.                       long* len));
  51.  
  52. static unsigned long userInfoTagSize _AP((data_tag tag,
  53.                       unsigned long length));
  54.  
  55. /*----------------------------------------------------------------------*/
  56.  
  57. char*
  58. writeInitInfo(init,buffer,len)
  59. InitAPDU* init;
  60. char* buffer;
  61. long* len;
  62. {
  63.   char *userInfo, *buf;
  64.   /* The WAIS protocol doesn't use init info - does now! JG*/
  65.  
  66.   /* older servers barf on this.  Someday we can use it... 
  67.   userInfo = (char *)init->UserInformationField; */
  68.   userInfo = NULL;
  69.  
  70.   if(userInfo != NULL) {
  71.     buf = writeString(userInfo, DT_UserInformationField, buffer, len);
  72.     return(buf);
  73.   }
  74.   else return buffer;
  75. }
  76.  
  77. /*----------------------------------------------------------------------*/
  78.  
  79. static char* readUserInfoHeader _AP((data_tag* tag,unsigned long* num,
  80.                      char* buffer));
  81.  
  82. char*
  83. readInitInfo(info,buffer)
  84. void** info;
  85. char* buffer;
  86. {
  87.   /* The WAIS protocol doesn't use init info */
  88.   readString((char **)info, buffer);
  89.   return buffer;
  90. }
  91.  
  92. /*----------------------------------------------------------------------*/
  93.  
  94. static unsigned long
  95. userInfoTagSize(tag,length)
  96. data_tag tag;
  97. unsigned long length;
  98. /* return the number of bytes required to write the user info tag and
  99.    length 
  100.  */
  101. {
  102.   unsigned long size;
  103.  
  104.   /* calculate bytes required to represent tag.  max tag is 16K */
  105.   size = writtenCompressedIntSize(tag);
  106.   size += writtenCompressedIntSize(length);
  107.       
  108.   return(size);
  109. }   
  110.  
  111. /*----------------------------------------------------------------------*/
  112.  
  113. static char*
  114. writeUserInfoHeader(tag,infoSize,estHeaderSize,buffer,len)
  115. data_tag tag;
  116. long infoSize;
  117. long estHeaderSize;
  118. char* buffer;
  119. long* len;
  120. /* write the tag and size, making sure the info fits.  return the true end
  121.    of the info (after adjustment) note that the argument infoSize includes
  122.    estHeaderSize.  Note that the argument len is the number of bytes remaining
  123.    in the buffer.  Since we write the tag and size at the begining of the
  124.    buffer (in space that we reserved) we don't want to pass len the calls which
  125.    do that writing. 
  126.  */
  127. {
  128.   long dummyLen = 100;        /* plenty of space for a tag and size */
  129.   char* buf = buffer;
  130.   long realSize = infoSize - estHeaderSize;
  131.   long realHeaderSize = userInfoTagSize(tag,realSize);
  132.  
  133.   if (buffer == NULL || *len == 0)
  134.     return(NULL);
  135.   
  136.   /* write the tag */
  137.   buf = writeTag(tag,buf,&dummyLen);
  138.   
  139.   /* see if the if the header size was correct. if not,
  140.      we have to shift the info to fit the real header size */
  141.   if (estHeaderSize != realHeaderSize)
  142.     {                /* make sure there is enough space */
  143.       CHECK_FOR_SPACE_LEFT(realHeaderSize - estHeaderSize,len);
  144.       memmove(buffer + realHeaderSize,buffer + estHeaderSize,(size_t)(realSize));
  145.     }
  146.    
  147.   /* write the size */
  148.   writeCompressedInteger(realSize,buf,&dummyLen);
  149.   
  150.   /* return the true end of buffer */
  151.   return(buffer + realHeaderSize + realSize);
  152. }
  153.  
  154. /*----------------------------------------------------------------------*/
  155.  
  156. static char*
  157. readUserInfoHeader(tag,num,buffer)
  158. data_tag* tag;
  159. unsigned long* num;
  160. char* buffer;
  161. /* read the tag and size */
  162. {
  163.   char* buf = buffer;
  164.   buf = readTag(tag,buf);
  165.   buf = readCompressedInteger(num,buf);
  166.   return(buf); 
  167. }
  168.  
  169. /*----------------------------------------------------------------------*/
  170.  
  171. WAISInitResponse* 
  172. makeWAISInitResponse(chunkCode,
  173.              chunkIDLen,
  174.              chunkMarker,
  175.              highlightMarker,
  176.              deHighlightMarker,
  177.              newLineChars)
  178. long chunkCode;
  179. long chunkIDLen;
  180. char* chunkMarker;
  181. char* highlightMarker;
  182. char* deHighlightMarker;
  183. char* newLineChars;
  184. /* create a WAIS init response object */
  185. {
  186.   WAISInitResponse* init = (WAISInitResponse*)s_malloc((size_t)sizeof(WAISInitResponse));
  187.  
  188.   init->ChunkCode = chunkCode;    /* note: none are copied! */
  189.   init->ChunkIDLength = chunkIDLen;
  190.   init->ChunkMarker = chunkMarker;
  191.   init->HighlightMarker = highlightMarker;
  192.   init->DeHighlightMarker = deHighlightMarker;
  193.   init->NewlineCharacters = newLineChars;
  194.   
  195.   return(init);
  196. }
  197.  
  198. /*----------------------------------------------------------------------*/
  199.  
  200. void 
  201. freeWAISInitResponse(init)
  202. WAISInitResponse* init;
  203. /* free an object made with makeWAISInitResponse */
  204. {
  205.   s_free(init->ChunkMarker);
  206.   s_free(init->HighlightMarker);
  207.   s_free(init->DeHighlightMarker);
  208.   s_free(init->NewlineCharacters);
  209.   s_free(init);
  210. }
  211.  
  212. /*----------------------------------------------------------------------*/
  213.  
  214. char*
  215. writeInitResponseInfo(init,buffer,len)
  216. InitResponseAPDU* init;
  217. char* buffer;
  218. long* len;
  219. /* write an init response object */
  220. {
  221.   unsigned long header_len = userInfoTagSize(DT_UserInformationLength,
  222.                          DefWAISInitResponseSize);
  223.   char* buf = buffer + header_len;
  224.   WAISInitResponse* info = (WAISInitResponse*)init->UserInformationField;
  225.   unsigned long size;
  226.   
  227.   RESERVE_SPACE_FOR_WAIS_HEADER(len);
  228.     
  229.   buf = writeNum(info->ChunkCode,DT_ChunkCode,buf,len);
  230.   buf = writeNum(info->ChunkIDLength,DT_ChunkIDLength,buf,len);
  231.   buf = writeString(info->ChunkMarker,DT_ChunkMarker,buf,len);
  232.   buf = writeString(info->HighlightMarker,DT_HighlightMarker,buf,len);
  233.   buf = writeString(info->DeHighlightMarker,DT_DeHighlightMarker,buf,len);
  234.   buf = writeString(info->NewlineCharacters,DT_NewlineCharacters,buf,len);
  235.   
  236.   /* now write the header and size */
  237.   size = buf - buffer; 
  238.   buf = writeUserInfoHeader(DT_UserInformationLength,size,header_len,buffer,len);
  239.   
  240.   return(buf);
  241. }
  242.  
  243. /*----------------------------------------------------------------------*/
  244.  
  245. char*
  246. readInitResponseInfo(info,buffer)
  247. void** info;
  248. char* buffer;
  249. /* read an init response object */
  250. {
  251.   char* buf = buffer;
  252.   unsigned long size; 
  253.   unsigned long headerSize;
  254.   data_tag tag;
  255.   long chunkCode,chunkIDLen;
  256.   char* chunkMarker = NULL;
  257.   char* highlightMarker = NULL;
  258.   char* deHighlightMarker = NULL;
  259.   char* newLineChars = NULL;
  260.   
  261.   chunkCode = chunkIDLen = UNUSED;
  262.   
  263.   buf = readUserInfoHeader(&tag,&size,buf);
  264.   headerSize = buf - buffer;
  265.     
  266.   while (buf < (buffer + size + headerSize))
  267.     { data_tag tag = peekTag(buf);
  268.       switch (tag)
  269.     { case DT_ChunkCode:
  270.         buf = readNum(&chunkCode,buf);
  271.         break;
  272.       case DT_ChunkIDLength:
  273.         buf = readNum(&chunkIDLen,buf);
  274.         break;
  275.       case DT_ChunkMarker:
  276.         buf = readString(&chunkMarker,buf);
  277.         break;
  278.       case DT_HighlightMarker:
  279.         buf = readString(&highlightMarker,buf);
  280.         break;
  281.       case DT_DeHighlightMarker:
  282.         buf = readString(&deHighlightMarker,buf);
  283.         break;
  284.       case DT_NewlineCharacters:
  285.         buf = readString(&newLineChars,buf);
  286.         break;
  287.       default:
  288.         s_free(highlightMarker);
  289.         s_free(deHighlightMarker);
  290.         s_free(newLineChars);
  291.         REPORT_READ_ERROR(buf);
  292.         break;
  293.       }
  294.     }
  295.         
  296.   *info = (void *)makeWAISInitResponse(chunkCode,chunkIDLen,chunkMarker,
  297.                    highlightMarker,deHighlightMarker,
  298.                    newLineChars);
  299.   return(buf);
  300. }
  301.  
  302. /*----------------------------------------------------------------------*/
  303.  
  304. WAISSearch* 
  305. makeWAISSearch(seedWords,
  306.            docs,
  307.            textList,
  308.            dateFactor,
  309.            beginDateRange,
  310.            endDateRange,
  311.            maxDocsRetrieved)
  312. char* seedWords;
  313. DocObj** docs;
  314. char** textList;
  315. long dateFactor;
  316. char* beginDateRange;
  317. char* endDateRange;
  318. long maxDocsRetrieved;
  319.  
  320. /* create a type 3 query object */
  321.   WAISSearch* query = (WAISSearch*)s_malloc((size_t)sizeof(WAISSearch));
  322.  
  323.   query->SeedWords = seedWords;    /* not copied! */
  324.   query->Docs = docs;        /* not copied! */
  325.   query->TextList = textList;    /* not copied! */
  326.   query->DateFactor = dateFactor;
  327.   query->BeginDateRange = beginDateRange;
  328.   query->EndDateRange = endDateRange;
  329.   query->MaxDocumentsRetrieved = maxDocsRetrieved;
  330.   
  331.   return(query);
  332. }
  333.  
  334. /*----------------------------------------------------------------------*/
  335.  
  336. void 
  337. freeWAISSearch(query)
  338. WAISSearch* query;
  339.  
  340. /* destroy an object made with makeWAISSearch() */
  341. {
  342.   void* ptr = NULL;
  343.   long i;
  344.   
  345.   s_free(query->SeedWords);
  346.   
  347.   if (query->Docs != NULL)
  348.     for (i = 0,ptr = (void *)query->Docs[i]; ptr != NULL; ptr = (void *)query->Docs[++i])
  349.       freeDocObj((DocObj*)ptr);
  350.   s_free(query->Docs);
  351.    
  352.   if (query->TextList != NULL)    /* XXX revisit when textlist is fully defined */
  353.     for (i = 0,ptr = (void *)query->TextList[i]; ptr != NULL; ptr = (void *)query->TextList[++i])
  354.       s_free(ptr);
  355.   s_free(query->TextList);
  356.  
  357.   s_free(query->BeginDateRange);
  358.   s_free(query->EndDateRange);
  359.   s_free(query);
  360. }
  361.  
  362. /*----------------------------------------------------------------------*/
  363.  
  364. DocObj* 
  365. makeDocObjUsingWholeDocument(docID,type)
  366. any* docID;
  367. char* type;
  368.  
  369. /* construct a document object using byte chunks - only for use by
  370.    servers */
  371. {
  372.   DocObj* doc = (DocObj*)s_malloc((size_t)sizeof(DocObj));
  373.   doc->DocumentID = docID;        /* not copied! */
  374.   doc->Type = type;        /* not copied! */
  375.   doc->ChunkCode = CT_document;
  376.   return(doc);
  377. }
  378.  
  379. /*----------------------------------------------------------------------*/
  380.  
  381. DocObj* 
  382. makeDocObjUsingLines(docID,type,start,end)
  383. any* docID;
  384. char* type;
  385. long start;
  386. long end;
  387.  
  388. /* construct a document object using line chunks - only for use by
  389.    servers */
  390. {
  391.   DocObj* doc = (DocObj*)s_malloc((size_t)sizeof(DocObj));
  392.   doc->ChunkCode = CT_line;
  393.   doc->DocumentID = docID;        /* not copied */
  394.   doc->Type = type;        /* not copied! */
  395.   doc->ChunkStart.Pos = start;
  396.   doc->ChunkEnd.Pos = end;
  397.   return(doc);
  398. }
  399.  
  400. /*----------------------------------------------------------------------*/
  401.  
  402.  
  403. DocObj* 
  404. makeDocObjUsingBytes(docID,type,start,end)
  405. any* docID;
  406. char* type;
  407. long start;
  408. long end;
  409.  
  410. /* construct a document object using byte chunks - only for use by
  411.    servers */
  412. {
  413.   DocObj* doc = (DocObj*)s_malloc((size_t)sizeof(DocObj));
  414.   doc->ChunkCode = CT_byte;
  415.   doc->DocumentID = docID;        /* not copied */
  416.   doc->Type = type;        /* not copied! */
  417.   doc->ChunkStart.Pos = start;
  418.   doc->ChunkEnd.Pos = end;
  419.   return(doc);
  420. }
  421.  
  422. /*----------------------------------------------------------------------*/
  423.  
  424. DocObj* 
  425. makeDocObjUsingParagraphs(docID,type,start,end)
  426. any* docID;
  427. char* type;
  428. any* start;
  429. any* end;
  430.  
  431. /* construct a document object using byte chunks - only for use by
  432.    servers */
  433. {
  434.   DocObj* doc = (DocObj*)s_malloc((size_t)sizeof(DocObj));
  435.   doc->ChunkCode = CT_paragraph;
  436.   doc->DocumentID = docID;        /* not copied */
  437.   doc->Type = type;
  438.   doc->ChunkStart.ID = start; 
  439.   doc->ChunkEnd.ID = end; 
  440.   return(doc);
  441. }
  442.  
  443. /*----------------------------------------------------------------------*/
  444.  
  445. void
  446. freeDocObj(doc)
  447. DocObj* doc;
  448.  
  449. /* free a docObj */
  450. {
  451.   freeAny(doc->DocumentID);
  452.   s_free(doc->Type);
  453.   if (doc->ChunkCode == CT_paragraph)
  454.     { freeAny(doc->ChunkStart.ID);
  455.       freeAny(doc->ChunkEnd.ID);
  456.     }
  457.   s_free(doc);
  458. }
  459.  
  460. /*----------------------------------------------------------------------*/
  461.  
  462. static char* writeDocObj _AP((DocObj* doc,char* buffer,long* len));
  463.  
  464. static char*
  465. writeDocObj(doc,buffer,len)
  466. DocObj* doc;
  467. char* buffer;
  468. long* len;
  469.  
  470. /* write as little as we can about the doc obj */
  471. {
  472.   char* buf = buffer;
  473.   
  474.   /* we alwasy have to write the id, but its tag depends on if its a chunk */
  475.   if (doc->ChunkCode == CT_document)
  476.     buf = writeAny(doc->DocumentID,DT_DocumentID,buf,len);
  477.   else
  478.     buf = writeAny(doc->DocumentID,DT_DocumentIDChunk,buf,len);
  479.   
  480.   if (doc->Type != NULL)
  481.     buf = writeString(doc->Type,DT_TYPE,buf,len);
  482.   
  483.   switch (doc->ChunkCode)
  484.     { case CT_document:
  485.     /* do nothing - there is no chunk data */
  486.     break;
  487.       case CT_byte:
  488.       case CT_line:
  489.     buf = writeNum(doc->ChunkCode,DT_ChunkCode,buf,len);
  490.     buf = writeNum(doc->ChunkStart.Pos,DT_ChunkStartID,buf,len);
  491.     buf = writeNum(doc->ChunkEnd.Pos,DT_ChunkEndID,buf,len);
  492.     break;
  493.       case CT_paragraph:
  494.     buf = writeNum(doc->ChunkCode,DT_ChunkCode,buf,len);
  495.     buf = writeAny(doc->ChunkStart.ID,DT_ChunkStartID,buf,len);
  496.     buf = writeAny(doc->ChunkEnd.ID,DT_ChunkEndID,buf,len);
  497.     break;
  498.       default:
  499.     panic("Implementation error: unknown chuck type %ld",
  500.           doc->ChunkCode);
  501.     break;
  502.       }
  503.    
  504.   return(buf);
  505. }
  506.  
  507. /*----------------------------------------------------------------------*/
  508.  
  509. static char* readDocObj _AP((DocObj** doc,char* buffer));
  510.  
  511. static char*
  512. readDocObj(doc,buffer)
  513. DocObj** doc;
  514. char* buffer;
  515.  
  516. /* read whatever we have about the new document */
  517. {
  518.   char* buf = buffer;
  519.   data_tag tag;
  520.   
  521.   *doc = (DocObj*)s_malloc((size_t)sizeof(DocObj));
  522.   
  523.   tag = peekTag(buf);
  524.   buf = readAny(&((*doc)->DocumentID),buf);
  525.   
  526.   if (tag == DT_DocumentID)
  527.     { (*doc)->ChunkCode = CT_document;
  528.       tag = peekTag(buf);
  529.       if (tag == DT_TYPE)    /* XXX depends on DT_TYPE != what comes next */
  530.     buf = readString(&((*doc)->Type),buf);
  531.       /* ChunkStart and ChunkEnd are undefined */
  532.     }
  533.   else if (tag == DT_DocumentIDChunk)
  534.     { Boolean readParagraphs = false; /* for cleanup */
  535.       tag = peekTag(buf);
  536.       if (tag == DT_TYPE)    /* XXX depends on DT_TYPE != CT_FOO */
  537.     buf = readString(&((*doc)->Type),buf);
  538.       buf = readNum(&((*doc)->ChunkCode),buf);
  539.       switch ((*doc)->ChunkCode)
  540.     { case CT_byte:
  541.       case CT_line:
  542.         buf = readNum(&((*doc)->ChunkStart.Pos),buf);
  543.         buf = readNum(&((*doc)->ChunkEnd.Pos),buf);
  544.         break;
  545.       case CT_paragraph:
  546.         readParagraphs = true;
  547.         buf = readAny(&((*doc)->ChunkStart.ID),buf);
  548.         buf = readAny(&((*doc)->ChunkEnd.ID),buf);
  549.         break;
  550.       default:
  551.         freeAny((*doc)->DocumentID);
  552.         if (readParagraphs)
  553.           { freeAny((*doc)->ChunkStart.ID);
  554.         freeAny((*doc)->ChunkEnd.ID);
  555.           }
  556.         s_free(doc);
  557.         REPORT_READ_ERROR(buf);
  558.         break;
  559.       }
  560.     }
  561.   else
  562.     { freeAny((*doc)->DocumentID);
  563.       s_free(*doc);
  564.       REPORT_READ_ERROR(buf);
  565.     }
  566.   return(buf);  
  567. }
  568.  
  569. /*----------------------------------------------------------------------*/
  570.  
  571. char* 
  572. writeSearchInfo(query,buffer,len)
  573. SearchAPDU* query;
  574. char* buffer;
  575. long* len;
  576.  
  577. /* write out a WAIS query (type 1 or 3) */
  578. {
  579.   if (strcmp(query->QueryType,QT_TextRetrievalQuery) == 0)
  580.     { return(writeAny((any*)query->Query,DT_Query,buffer,len));
  581.     }
  582.   else
  583.     { unsigned long header_len = userInfoTagSize(DT_UserInformationLength,
  584.                          DefWAISSearchSize); 
  585.       char* buf = buffer + header_len;
  586.       WAISSearch* info = (WAISSearch*)query->Query;
  587.       unsigned long size;
  588.       long i;
  589.   
  590.       RESERVE_SPACE_FOR_WAIS_HEADER(len);
  591.        
  592.       buf = writeString(info->SeedWords,DT_SeedWords,buf,len);
  593.  
  594.       if (info->Docs != NULL)
  595.       { for (i = 0; info->Docs[i] != NULL; i++)
  596.       { buf = writeDocObj(info->Docs[i],buf,len);
  597.       }
  598.     }
  599.    
  600.       /* XXX text list */
  601.  
  602.       buf = writeNum(info->DateFactor,DT_DateFactor,buf,len);
  603.       buf = writeString(info->BeginDateRange,DT_BeginDateRange,buf,len);
  604.       buf = writeString(info->EndDateRange,DT_EndDateRange,buf,len);
  605.       buf = writeNum(info->MaxDocumentsRetrieved,DT_MaxDocumentsRetrieved,buf,len);
  606.   
  607.       /* now write the header and size */
  608.       size = buf - buffer; 
  609.       buf = writeUserInfoHeader(DT_UserInformationLength,size,header_len,buffer,len);
  610.    
  611.       return(buf);
  612.     }
  613. }
  614.  
  615. /*----------------------------------------------------------------------*/
  616.  
  617. char* 
  618. readSearchInfo(info,buffer)
  619. void** info;
  620. char* buffer;
  621.  
  622. /* read a WAIS query (type 1 or 3) */
  623. {
  624.   data_tag type = peekTag(buffer);
  625.   if (type == DT_Query)        /* this is a type 1 query */
  626.     { char* buf = buffer;
  627.       any* query = NULL;
  628.       buf = readAny(&query,buf);
  629.       *info = (void *)query;
  630.       return(buf);
  631.     }
  632.   else                /* a type 3 query */
  633.     { char* buf = buffer;
  634.       unsigned long size; 
  635.       unsigned long headerSize;
  636.       data_tag tag;
  637.       char* seedWords = NULL;
  638.       char* beginDateRange = NULL;
  639.       char* endDateRange = NULL;
  640.       long dateFactor,maxDocsRetrieved;
  641.       char** textList = NULL; 
  642.       DocObj** docIDs = NULL;
  643.       DocObj* doc = NULL;
  644.       long docs = 0;
  645.       long i;
  646.       void* ptr = NULL;
  647.  
  648.       dateFactor = maxDocsRetrieved = UNUSED;
  649.   
  650.       buf = readUserInfoHeader(&tag,&size,buf);
  651.       headerSize = buf - buffer;
  652.   
  653.       while (buf < (buffer + size + headerSize))
  654.     { data_tag tag = peekTag(buf);
  655.       switch (tag)
  656.         { case DT_SeedWords:
  657.         buf = readString(&seedWords,buf);
  658.         break;
  659.           case DT_DocumentID:
  660.           case DT_DocumentIDChunk:
  661.         if (docIDs == NULL) /* create a new doc list */
  662.           { docIDs = (DocObj**)s_malloc((size_t)sizeof(DocObj*) * 2);
  663.           }
  664.         else        /* grow the doc list */
  665.           { docIDs = (DocObj**)s_realloc((char*)docIDs,(size_t)(sizeof(DocObj*) * (docs + 2)));
  666.           }
  667.         buf = readDocObj(&doc,buf);
  668.         if (buf == NULL) 
  669.           { s_free(seedWords);
  670.             s_free(beginDateRange);
  671.             s_free(endDateRange);
  672.             if (docIDs != NULL)
  673.               for (i = 0,ptr = (void *)docIDs[i]; ptr != NULL; ptr = (void *)docIDs[++i])
  674.             freeDocObj((DocObj*)ptr);
  675.             s_free(docIDs);
  676.             /* XXX should also free textlist when it is fully defined */
  677.           }
  678.         RETURN_ON_NULL(buf);
  679.         docIDs[docs++] = doc; /* put it in the list */
  680.         docIDs[docs] = NULL;
  681.         break;
  682.           case DT_TextList:
  683.         /* XXX */
  684.         break;
  685.           case DT_DateFactor:
  686.         buf = readNum(&dateFactor,buf);
  687.         break;
  688.           case DT_BeginDateRange:
  689.         buf = readString(&beginDateRange,buf);
  690.         break;
  691.           case DT_EndDateRange:
  692.         buf = readString(&endDateRange,buf);
  693.         break;
  694.           case DT_MaxDocumentsRetrieved:
  695.         buf = readNum(&maxDocsRetrieved,buf);
  696.         break;
  697.           default:
  698.         s_free(seedWords);
  699.         s_free(beginDateRange);
  700.         s_free(endDateRange);
  701.         if (docIDs != NULL)
  702.           for (i = 0,ptr = (void *)docIDs[i]; ptr != NULL; ptr = (void *)docIDs[++i])
  703.             freeDocObj((DocObj*)ptr);
  704.         s_free(docIDs);
  705.         /* XXX should also free textlist when it is fully defined */
  706.         REPORT_READ_ERROR(buf);
  707.         break;
  708.           }
  709.     }
  710.         
  711.       *info = (void *)makeWAISSearch(seedWords,docIDs,textList,
  712.                      dateFactor,beginDateRange,endDateRange,
  713.                      maxDocsRetrieved);
  714.       return(buf);
  715.     }
  716. }
  717.  
  718. /*----------------------------------------------------------------------*/
  719.  
  720. WAISDocumentHeader*
  721. makeWAISDocumentHeader(docID,
  722.                versionNumber,
  723.                score,
  724.                bestMatch,
  725.                docLen,
  726.                lines,
  727.                types,
  728.                source,
  729.                date,
  730.                headline,
  731.                originCity)
  732. any* docID;
  733. long versionNumber;
  734. long score;
  735. long bestMatch;
  736. long docLen;
  737. long lines;
  738. char** types;
  739. char* source;
  740. char* date;
  741. char* headline;
  742. char* originCity;
  743.  
  744. /* construct a standard document header, note that no fields are copied!
  745.    if the application needs to save these fields, it should copy them,
  746.    or set the field in this object to NULL before freeing it.
  747.  */
  748. {
  749.   WAISDocumentHeader* header = 
  750.     (WAISDocumentHeader*)s_malloc((size_t)sizeof(WAISDocumentHeader));
  751.  
  752.   header->DocumentID = docID;
  753.   header->VersionNumber = versionNumber;
  754.   header->Score = score;
  755.   header->BestMatch = bestMatch;
  756.   header->DocumentLength = docLen;
  757.   header->Lines = lines;
  758.   header->Types = types;
  759.   header->Source = source;
  760.   header->Date = date;
  761.   header->Headline = headline;
  762.   header->OriginCity = originCity;
  763.   
  764.   return(header);
  765. }
  766.  
  767. /*----------------------------------------------------------------------*/
  768.  
  769. void
  770. freeWAISDocumentHeader(header)
  771. WAISDocumentHeader* header;
  772.  
  773. {
  774.   freeAny(header->DocumentID);
  775.   doList((void**)header->Types,fs_free); /* can't use the macro here ! */
  776.   s_free(header->Types);
  777.   s_free(header->Source);
  778.   s_free(header->Date);
  779.   s_free(header->Headline);
  780.   s_free(header->OriginCity);
  781.   s_free(header);
  782. }
  783.  
  784. /*----------------------------------------------------------------------*/
  785.  
  786. char*
  787. writeWAISDocumentHeader(header,buffer,len)
  788. WAISDocumentHeader* header;
  789. char* buffer;
  790. long* len;
  791. {
  792.   unsigned long header_len = userInfoTagSize(DT_DocumentHeaderGroup ,
  793.                          DefWAISDocHeaderSize);
  794.   char* buf = buffer + header_len;
  795.   unsigned long size;
  796.   
  797.   RESERVE_SPACE_FOR_WAIS_HEADER(len);
  798.    
  799.   buf = writeAny(header->DocumentID,DT_DocumentID,buf,len);
  800.   buf = writeNum(header->VersionNumber,DT_VersionNumber,buf,len);
  801.   buf = writeNum(header->Score,DT_Score,buf,len);
  802.   buf = writeNum(header->BestMatch,DT_BestMatch,buf,len);
  803.   buf = writeNum(header->DocumentLength,DT_DocumentLength,buf,len);
  804.   buf = writeNum(header->Lines,DT_Lines,buf,len);
  805.   if (header->Types != NULL)
  806.     { long size;
  807.       char* ptr = NULL;
  808.       long i;
  809.       buf = writeTag(DT_TYPE_BLOCK,buf,len);
  810.       for (i = 0,size = 0,ptr = header->Types[i]; ptr != NULL; ptr = header->Types[++i])
  811.     { long typeSize = strlen(ptr);
  812.       size += writtenTagSize(DT_TYPE);
  813.       size += writtenCompressedIntSize(typeSize);
  814.       size += typeSize; 
  815.     }
  816.       buf = writeCompressedInteger((unsigned long)size,buf,len);
  817.       for (i = 0,ptr = header->Types[i]; ptr != NULL; ptr = header->Types[++i])
  818.     buf = writeString(ptr,DT_TYPE,buf,len);
  819.     }
  820.   buf = writeString(header->Source,DT_Source,buf,len);
  821.   buf = writeString(header->Date,DT_Date,buf,len);
  822.   buf = writeString(header->Headline,DT_Headline,buf,len);
  823.   buf = writeString(header->OriginCity,DT_OriginCity,buf,len);
  824.   
  825.   /* now write the header and size */
  826.   size = buf - buffer; 
  827.   buf = writeUserInfoHeader(DT_DocumentHeaderGroup,size,header_len,buffer,len);
  828.  
  829.   return(buf);
  830. }
  831.  
  832. /*----------------------------------------------------------------------*/
  833.  
  834. char*
  835. readWAISDocumentHeader(header,buffer)
  836. WAISDocumentHeader** header;
  837. char* buffer;
  838. {
  839.   char* buf = buffer;
  840.   unsigned long size; 
  841.   unsigned long headerSize;
  842.   data_tag tag;
  843.   any* docID = NULL;
  844.   long versionNumber,score,bestMatch,docLength,lines;
  845.   char** types = NULL;
  846.   char *source = NULL;
  847.   char *date = NULL;
  848.   char *headline = NULL;
  849.   char *originCity = NULL;
  850.   
  851.   versionNumber = score = bestMatch = docLength = lines = UNUSED;
  852.   
  853.   buf = readUserInfoHeader(&tag,&size,buf);
  854.   headerSize = buf - buffer;
  855.     
  856.   while (buf < (buffer + size + headerSize))
  857.     { data_tag tag = peekTag(buf);
  858.       switch (tag)
  859.     { case DT_DocumentID:
  860.         buf = readAny(&docID,buf);
  861.         break;
  862.       case DT_VersionNumber:
  863.         buf = readNum(&versionNumber,buf);
  864.         break;
  865.       case DT_Score:
  866.         buf = readNum(&score,buf);
  867.         break;
  868.       case DT_BestMatch:
  869.         buf = readNum(&bestMatch,buf);
  870.         break;
  871.       case DT_DocumentLength:
  872.         buf = readNum(&docLength,buf);
  873.         break;
  874.       case DT_Lines:
  875.         buf = readNum(&lines,buf);
  876.         break;
  877.       case DT_TYPE_BLOCK:
  878.         { unsigned long size = -1;
  879.           long numTypes = 0;
  880.           buf = readTag(&tag,buf);
  881.           buf = readCompressedInteger(&size,buf);
  882.           while (size > 0)
  883.         { char* type = NULL;
  884.           char* originalBuf = buf;
  885.           buf = readString(&type,buf);
  886.           types = (char**)s_realloc(types,(size_t)(sizeof(char*) * (numTypes + 2)));
  887.           types[numTypes++] = type;
  888.           types[numTypes] = NULL;
  889.           size -= (buf - originalBuf);
  890.         }
  891.         }
  892.       case DT_Source:
  893.         buf = readString(&source,buf);
  894.         break;
  895.       case DT_Date:
  896.         buf = readString(&date,buf);
  897.         break;
  898.       case DT_Headline:
  899.         buf = readString(&headline,buf);
  900.         break;
  901.       case DT_OriginCity:
  902.         buf = readString(&originCity,buf);
  903.         break;
  904.       default:
  905.         freeAny(docID);
  906.         s_free(source);
  907.         s_free(date);
  908.         s_free(headline);
  909.         s_free(originCity);
  910.         REPORT_READ_ERROR(buf);
  911.         break;
  912.       }
  913.     }
  914.         
  915.   *header = makeWAISDocumentHeader(docID,versionNumber,score,bestMatch,
  916.                    docLength,lines,types,source,date,headline,
  917.                    originCity);
  918.   return(buf);
  919. }
  920.  
  921. /*----------------------------------------------------------------------*/
  922.  
  923. WAISDocumentShortHeader*
  924. makeWAISDocumentShortHeader(docID,
  925.                 versionNumber,
  926.                 score,
  927.                 bestMatch,
  928.                 docLen,
  929.                 lines)
  930. any* docID;
  931. long versionNumber;
  932. long score;
  933. long bestMatch;
  934. long docLen;
  935. long lines;
  936. /* construct a short document header, note that no fields are copied!
  937.    if the application needs to save these fields, it should copy them,
  938.    or set the field in this object to NULL before freeing it.
  939.  */
  940. {
  941.   WAISDocumentShortHeader* header = 
  942.     (WAISDocumentShortHeader*)s_malloc((size_t)sizeof(WAISDocumentShortHeader));
  943.  
  944.   header->DocumentID = docID;
  945.   header->VersionNumber = versionNumber;
  946.   header->Score = score;
  947.   header->BestMatch = bestMatch;
  948.   header->DocumentLength = docLen;
  949.   header->Lines = lines;
  950.   
  951.   return(header);
  952. }
  953.  
  954. /*----------------------------------------------------------------------*/
  955.  
  956. void
  957. freeWAISDocumentShortHeader(header)
  958. WAISDocumentShortHeader* header;
  959. {
  960.   freeAny(header->DocumentID);
  961.   s_free(header);
  962. }
  963.  
  964. /*----------------------------------------------------------------------*/
  965.  
  966. char*
  967. writeWAISDocumentShortHeader(header,buffer,len)
  968. WAISDocumentShortHeader* header;
  969. char* buffer;
  970. long* len;
  971. {
  972.   unsigned long header_len = userInfoTagSize(DT_DocumentShortHeaderGroup ,
  973.                          DefWAISShortHeaderSize);
  974.   char* buf = buffer + header_len;
  975.   unsigned long size;
  976.   
  977.   RESERVE_SPACE_FOR_WAIS_HEADER(len);
  978.    
  979.   buf = writeAny(header->DocumentID,DT_DocumentID,buf,len);
  980.   buf = writeNum(header->VersionNumber,DT_VersionNumber,buf,len);
  981.   buf = writeNum(header->Score,DT_Score,buf,len);
  982.   buf = writeNum(header->BestMatch,DT_BestMatch,buf,len);
  983.   buf = writeNum(header->DocumentLength,DT_DocumentLength,buf,len);
  984.   buf = writeNum(header->Lines,DT_Lines,buf,len);
  985.   
  986.   /* now write the header and size */
  987.   size = buf - buffer; 
  988.   buf = writeUserInfoHeader(DT_DocumentShortHeaderGroup,size,header_len,buffer,len);
  989.  
  990.   return(buf);
  991. }
  992.  
  993. /*----------------------------------------------------------------------*/
  994.  
  995. char*
  996. readWAISDocumentShortHeader(header,buffer)
  997. WAISDocumentShortHeader** header;
  998. char* buffer;
  999. {
  1000.   char* buf = buffer;
  1001.   unsigned long size; 
  1002.   unsigned long headerSize;
  1003.   data_tag tag;
  1004.   any* docID = NULL;
  1005.   long versionNumber,score,bestMatch,docLength,lines;
  1006.   
  1007.   versionNumber = score = bestMatch = docLength = lines = UNUSED;
  1008.   
  1009.   buf = readUserInfoHeader(&tag,&size,buf);
  1010.   headerSize = buf - buffer;
  1011.     
  1012.   while (buf < (buffer + size + headerSize))
  1013.     { data_tag tag = peekTag(buf);
  1014.       switch (tag)
  1015.     { case DT_DocumentID:
  1016.         buf = readAny(&docID,buf);
  1017.         break;
  1018.       case DT_VersionNumber:
  1019.         buf = readNum(&versionNumber,buf);
  1020.         break;
  1021.       case DT_Score:
  1022.         buf = readNum(&score,buf);
  1023.         break;
  1024.       case DT_BestMatch:
  1025.         buf = readNum(&bestMatch,buf);
  1026.         break;
  1027.       case DT_DocumentLength:
  1028.         buf = readNum(&docLength,buf);
  1029.         break;
  1030.       case DT_Lines:
  1031.         buf = readNum(&lines,buf);
  1032.         break;
  1033.       default:
  1034.         freeAny(docID);
  1035.         REPORT_READ_ERROR(buf);
  1036.         break;
  1037.       }
  1038.     }
  1039.         
  1040.   *header = makeWAISDocumentShortHeader(docID,versionNumber,score,bestMatch,
  1041.                     docLength,lines);
  1042.   return(buf);
  1043. }
  1044.  
  1045. /*----------------------------------------------------------------------*/
  1046.  
  1047. WAISDocumentLongHeader*
  1048. makeWAISDocumentLongHeader(docID,
  1049.                versionNumber,
  1050.                score,
  1051.                bestMatch,
  1052.                docLen,
  1053.                lines,
  1054.                types,
  1055.                source,
  1056.                date,
  1057.                headline,
  1058.                originCity,
  1059.                stockCodes,
  1060.                companyCodes,
  1061.                industryCodes)
  1062. any* docID;
  1063. long versionNumber;
  1064. long score;
  1065. long bestMatch;
  1066. long docLen;
  1067. long lines;
  1068. char** types;
  1069. char* source;
  1070. char* date;
  1071. char* headline;
  1072. char* originCity;
  1073. char* stockCodes;
  1074. char* companyCodes;
  1075. char* industryCodes;
  1076. /* construct a long document header, note that no fields are copied!
  1077.    if the application needs to save these fields, it should copy them,
  1078.    or set the field in this object to NULL before freeing it.
  1079.  */
  1080. {
  1081.   WAISDocumentLongHeader* header = 
  1082.     (WAISDocumentLongHeader*)s_malloc((size_t)sizeof(WAISDocumentLongHeader));
  1083.  
  1084.   header->DocumentID = docID;
  1085.   header->VersionNumber = versionNumber;
  1086.   header->Score = score;
  1087.   header->BestMatch = bestMatch;
  1088.   header->DocumentLength = docLen;
  1089.   header->Lines = lines;
  1090.   header->Types = types;
  1091.   header->Source = source;
  1092.   header->Date = date;
  1093.   header->Headline = headline;
  1094.   header->OriginCity = originCity;
  1095.   header->StockCodes = stockCodes;
  1096.   header->CompanyCodes = companyCodes;
  1097.   header->IndustryCodes = industryCodes;
  1098.   
  1099.   return(header);
  1100. }
  1101.  
  1102. /*----------------------------------------------------------------------*/
  1103.  
  1104. void
  1105. freeWAISDocumentLongHeader(header)
  1106. WAISDocumentLongHeader* header;
  1107. {
  1108.   freeAny(header->DocumentID);
  1109.   doList((void**)header->Types,fs_free); /* can't use the macro here! */
  1110.   s_free(header->Source);
  1111.   s_free(header->Date);
  1112.   s_free(header->Headline);
  1113.   s_free(header->OriginCity);
  1114.   s_free(header->StockCodes);
  1115.   s_free(header->CompanyCodes);
  1116.   s_free(header->IndustryCodes);
  1117.   s_free(header);
  1118. }
  1119.  
  1120. /*----------------------------------------------------------------------*/
  1121.  
  1122. char*
  1123. writeWAISDocumentLongHeader(header,buffer,len)
  1124. WAISDocumentLongHeader* header;
  1125. char* buffer;
  1126. long* len;
  1127. {
  1128.   unsigned long header_len = userInfoTagSize(DT_DocumentLongHeaderGroup ,
  1129.                          DefWAISLongHeaderSize);
  1130.   char* buf = buffer + header_len;
  1131.   unsigned long size;
  1132.   
  1133.   RESERVE_SPACE_FOR_WAIS_HEADER(len);
  1134.    
  1135.   buf = writeAny(header->DocumentID,DT_DocumentID,buf,len);
  1136.   buf = writeNum(header->VersionNumber,DT_VersionNumber,buf,len);
  1137.   buf = writeNum(header->Score,DT_Score,buf,len);
  1138.   buf = writeNum(header->BestMatch,DT_BestMatch,buf,len);
  1139.   buf = writeNum(header->DocumentLength,DT_DocumentLength,buf,len);
  1140.   buf = writeNum(header->Lines,DT_Lines,buf,len);
  1141.   if (header->Types != NULL)
  1142.     { long size;
  1143.       char* ptr = NULL;
  1144.       long i;
  1145.       buf = writeTag(DT_TYPE_BLOCK,buf,len);
  1146.       for (i = 0,size = 0,ptr = header->Types[i]; ptr != NULL; ptr = header->Types[++i])
  1147.     { long typeSize = strlen(ptr);
  1148.       size += writtenTagSize(DT_TYPE);
  1149.       size += writtenCompressedIntSize(typeSize);
  1150.       size += typeSize; 
  1151.     }
  1152.       buf = writeCompressedInteger((unsigned long)size,buf,len);
  1153.       for (i = 0,ptr = header->Types[i]; ptr != NULL; ptr = header->Types[++i])
  1154.     buf = writeString(ptr,DT_TYPE,buf,len);
  1155.     }
  1156.   buf = writeString(header->Source,DT_Source,buf,len);
  1157.   buf = writeString(header->Date,DT_Date,buf,len);
  1158.   buf = writeString(header->Headline,DT_Headline,buf,len);
  1159.   buf = writeString(header->OriginCity,DT_OriginCity,buf,len);
  1160.   buf = writeString(header->StockCodes,DT_StockCodes,buf,len);
  1161.   buf = writeString(header->CompanyCodes,DT_CompanyCodes,buf,len);
  1162.   buf = writeString(header->IndustryCodes,DT_IndustryCodes,buf,len);
  1163.   
  1164.   /* now write the header and size */
  1165.   size = buf - buffer; 
  1166.   buf = writeUserInfoHeader(DT_DocumentLongHeaderGroup,size,header_len,buffer,len);
  1167.  
  1168.   return(buf);
  1169. }
  1170.  
  1171. /*----------------------------------------------------------------------*/
  1172.  
  1173. char*
  1174. readWAISDocumentLongHeader(header,buffer)
  1175. WAISDocumentLongHeader** header;
  1176. char* buffer;
  1177. {
  1178.   char* buf = buffer;
  1179.   unsigned long size; 
  1180.   unsigned long headerSize;
  1181.   data_tag tag;
  1182.   any* docID;
  1183.   long versionNumber,score,bestMatch,docLength,lines;
  1184.   char **types;
  1185.   char *source,*date,*headline,*originCity,*stockCodes,*companyCodes,*industryCodes;
  1186.   
  1187.   docID = NULL;
  1188.   versionNumber = score = bestMatch = docLength = lines = UNUSED;
  1189.   types = NULL;
  1190.   source = date = headline = originCity = stockCodes = companyCodes = industryCodes = NULL;
  1191.   
  1192.   buf = readUserInfoHeader(&tag,&size,buf);
  1193.   headerSize = buf - buffer;
  1194.     
  1195.   while (buf < (buffer + size + headerSize))
  1196.     { data_tag tag = peekTag(buf);
  1197.       switch (tag)
  1198.     { case DT_DocumentID:
  1199.         buf = readAny(&docID,buf);
  1200.         break;
  1201.       case DT_VersionNumber:
  1202.         buf = readNum(&versionNumber,buf);
  1203.         break;
  1204.       case DT_Score:
  1205.         buf = readNum(&score,buf);
  1206.         break;
  1207.       case DT_BestMatch:
  1208.         buf = readNum(&bestMatch,buf);
  1209.         break;
  1210.       case DT_DocumentLength:
  1211.         buf = readNum(&docLength,buf);
  1212.         break;
  1213.       case DT_Lines:
  1214.         buf = readNum(&lines,buf);
  1215.         break;
  1216.       case DT_TYPE_BLOCK:
  1217.         { unsigned long size = -1;
  1218.           long numTypes = 0;
  1219.           buf = readTag(&tag,buf);
  1220.           readCompressedInteger(&size,buf);
  1221.           while (size > 0)
  1222.         { char* type = NULL;
  1223.           char* originalBuf = buf;
  1224.           buf = readString(&type,buf);
  1225.           types = (char**)s_realloc(types,(size_t)(sizeof(char*) * (numTypes + 2)));
  1226.           types[numTypes++] = type;
  1227.           types[numTypes] = NULL;
  1228.           size -= (buf - originalBuf);
  1229.         }
  1230.         }
  1231.       case DT_Source:
  1232.         buf = readString(&source,buf);
  1233.         break;
  1234.       case DT_Date:
  1235.         buf = readString(&date,buf);
  1236.         break;
  1237.       case DT_Headline:
  1238.         buf = readString(&headline,buf);
  1239.         break;
  1240.       case DT_OriginCity:
  1241.         buf = readString(&originCity,buf);
  1242.         break;
  1243.       case DT_StockCodes:
  1244.         buf = readString(&stockCodes,buf);
  1245.         break;
  1246.       case DT_CompanyCodes:
  1247.         buf = readString(&companyCodes,buf);
  1248.         break;
  1249.       case DT_IndustryCodes:
  1250.         buf = readString(&industryCodes,buf);
  1251.         break;
  1252.       default:
  1253.         freeAny(docID);
  1254.         s_free(source);
  1255.         s_free(date);
  1256.         s_free(headline);
  1257.         s_free(originCity);
  1258.         s_free(stockCodes);
  1259.         s_free(companyCodes);
  1260.         s_free(industryCodes);
  1261.         REPORT_READ_ERROR(buf);
  1262.         break;
  1263.       }
  1264.     }
  1265.         
  1266.   *header = makeWAISDocumentLongHeader(docID,versionNumber,score,bestMatch,
  1267.                        docLength,lines,types,source,date,headline,
  1268.                        originCity,stockCodes,companyCodes,
  1269.                        industryCodes);
  1270.   return(buf);
  1271. }
  1272.  
  1273. /*----------------------------------------------------------------------*/
  1274.  
  1275. WAISSearchResponse*
  1276. makeWAISSearchResponse(seedWordsUsed,
  1277.                docHeaders,
  1278.                shortHeaders,
  1279.                longHeaders,
  1280.                text,
  1281.                headlines,
  1282.                codes,
  1283.                diagnostics)
  1284. char* seedWordsUsed;
  1285. WAISDocumentHeader** docHeaders;
  1286. WAISDocumentShortHeader** shortHeaders;
  1287. WAISDocumentLongHeader** longHeaders;
  1288. WAISDocumentText** text;
  1289. WAISDocumentHeadlines** headlines;
  1290. WAISDocumentCodes** codes;
  1291. diagnosticRecord** diagnostics;
  1292. {
  1293.   WAISSearchResponse* response = (WAISSearchResponse*)s_malloc((size_t)sizeof(WAISSearchResponse));
  1294.   
  1295.   response->SeedWordsUsed = seedWordsUsed;
  1296.   response->DocHeaders = docHeaders;
  1297.   response->ShortHeaders = shortHeaders;
  1298.   response->LongHeaders = longHeaders;
  1299.   response->Text = text;
  1300.   response->Headlines = headlines;
  1301.   response->Codes = codes;
  1302.   response->Diagnostics = diagnostics;
  1303.   
  1304.   return(response);
  1305. }
  1306.  
  1307. /*----------------------------------------------------------------------*/
  1308.  
  1309. void
  1310. freeWAISSearchResponse(response)
  1311. WAISSearchResponse* response;
  1312. {
  1313.   void* ptr = NULL;
  1314.   long i;
  1315.  
  1316.   s_free(response->SeedWordsUsed);
  1317.  
  1318.   if (response->DocHeaders != NULL)
  1319.     for (i = 0,ptr = (void *)response->DocHeaders[i]; ptr != NULL; ptr = (void *)response->DocHeaders[++i])
  1320.       freeWAISDocumentHeader((WAISDocumentHeader*)ptr);
  1321.   s_free(response->DocHeaders);
  1322.    
  1323.   if (response->ShortHeaders != NULL)
  1324.     for (i = 0,ptr = (void *)response->ShortHeaders[i]; ptr != NULL; ptr = (void *)response->ShortHeaders[++i])
  1325.       freeWAISDocumentShortHeader((WAISDocumentShortHeader*)ptr);
  1326.   s_free(response->ShortHeaders);
  1327.    
  1328.   if (response->LongHeaders != NULL)
  1329.     for (i = 0,ptr = (void *)response->LongHeaders[i]; ptr != NULL; ptr = (void *)response->LongHeaders[++i])
  1330.       freeWAISDocumentLongHeader((WAISDocumentLongHeader*)ptr);
  1331.   s_free(response->LongHeaders);
  1332.    
  1333.   if (response->Text != NULL)
  1334.     for (i = 0,ptr = (void *)response->Text[i]; ptr != NULL; ptr = (void *)response->Text[++i])
  1335.       freeWAISDocumentText((WAISDocumentText*)ptr);
  1336.   s_free(response->Text);
  1337.    
  1338.   if (response->Headlines != NULL)
  1339.     for (i = 0,ptr = (void *)response->Headlines[i]; ptr != NULL; ptr = (void *)response->Headlines[++i])
  1340.       freeWAISDocumentHeadlines((WAISDocumentHeadlines*)ptr);
  1341.   s_free(response->Headlines);
  1342.    
  1343.   if (response->Codes != NULL)
  1344.     for (i = 0,ptr = (void *)response->Codes[i]; ptr != NULL; ptr = (void *)response->Codes[++i])
  1345.       freeWAISDocumentCodes((WAISDocumentCodes*)ptr);
  1346.   s_free(response->Codes);
  1347.    
  1348.   if (response->Diagnostics != NULL)
  1349.     for (i = 0,ptr = (void *)response->Diagnostics[i]; ptr != NULL; ptr = (void *)response->Diagnostics[++i])
  1350.       freeDiag((diagnosticRecord*)ptr);
  1351.   s_free(response->Diagnostics);
  1352.   
  1353.   s_free(response);
  1354. }
  1355.  
  1356. /*----------------------------------------------------------------------*/
  1357.  
  1358. char* 
  1359. writeSearchResponseInfo(query,buffer,len)
  1360. SearchResponseAPDU* query;
  1361. char* buffer;
  1362. long* len;
  1363. {
  1364.   unsigned long header_len = userInfoTagSize(DT_UserInformationLength,
  1365.                          DefWAISSearchResponseSize);
  1366.   char* buf = buffer + header_len;
  1367.   WAISSearchResponse* info = (WAISSearchResponse*)query->DatabaseDiagnosticRecords;
  1368.   unsigned long size;
  1369.   void* header = NULL;
  1370.   long i;
  1371.   
  1372.   RESERVE_SPACE_FOR_WAIS_HEADER(len);
  1373.   
  1374.   buf = writeString(info->SeedWordsUsed,DT_SeedWordsUsed,buf,len);
  1375.   
  1376.   /* write out all the headers */
  1377.   if (info->DocHeaders != NULL)
  1378.     { for (i = 0,header = (void *)info->DocHeaders[i]; header != NULL; header = (void *)info->DocHeaders[++i])
  1379.     buf = writeWAISDocumentHeader((WAISDocumentHeader*)header,buf,len);
  1380.       }
  1381.    
  1382.   if (info->ShortHeaders != NULL)
  1383.     { for (i = 0,header = (void *)info->ShortHeaders[i]; header != NULL; header = (void *)info->ShortHeaders[++i])
  1384.     buf = writeWAISDocumentShortHeader((WAISDocumentShortHeader*)header,buf,len);
  1385.       }
  1386.  
  1387.   if (info->LongHeaders != NULL)
  1388.     { for (i = 0,header = (void *)info->LongHeaders[i]; header != NULL; header = (void *)info->LongHeaders[++i])
  1389.     buf = writeWAISDocumentLongHeader((WAISDocumentLongHeader*)header,buf,len);
  1390.       }
  1391.  
  1392.   if (info->Text != NULL)
  1393.     { for (i = 0,header = (void *)info->Text[i]; header != NULL; header = (void *)info->Text[++i])
  1394.     buf = writeWAISDocumentText((WAISDocumentText*)header,buf,len);
  1395.       }
  1396.  
  1397.   if (info->Headlines != NULL)
  1398.     { for (i = 0,header = (void *)info->Headlines[i]; header != NULL; header = (void *)info->Headlines[++i])
  1399.     buf = writeWAISDocumentHeadlines((WAISDocumentHeadlines*)header,buf,len);
  1400.       }
  1401.  
  1402.   if (info->Codes != NULL)
  1403.     { for (i = 0,header = (void *)info->Codes[i]; header != NULL;header = (void *)info->Codes[++i])
  1404.     buf = writeWAISDocumentCodes((WAISDocumentCodes*)header,buf,len);
  1405.       }
  1406.  
  1407.   if (info->Diagnostics != NULL)
  1408.     { for (i = 0, header = (void *)info->Diagnostics[i]; header != NULL; header = (void *)info->Diagnostics[++i])
  1409.     buf = writeDiag((diagnosticRecord*)header,buf,len);
  1410.       }
  1411.    
  1412.   /* now write the header and size */
  1413.   size = buf - buffer; 
  1414.   buf = writeUserInfoHeader(DT_UserInformationLength,size,header_len,buffer,len);
  1415.   
  1416.   return(buf);
  1417. }
  1418.  
  1419. /*----------------------------------------------------------------------*/
  1420.  
  1421. static void
  1422. cleanUpWaisSearchResponse _AP((char* buf,char* seedWordsUsed,
  1423.                    WAISDocumentHeader** docHeaders,
  1424.                    WAISDocumentShortHeader** shortHeaders,
  1425.                    WAISDocumentLongHeader** longHeaders,
  1426.                    WAISDocumentText** text,
  1427.                    WAISDocumentHeadlines** headlines,
  1428.                    WAISDocumentCodes** codes,
  1429.                    diagnosticRecord**diags));
  1430.  
  1431. static void
  1432. cleanUpWaisSearchResponse (buf,seedWordsUsed,docHeaders,shortHeaders,
  1433.                longHeaders,text,headlines,codes,diags)
  1434. char* buf;
  1435. char* seedWordsUsed;
  1436. WAISDocumentHeader** docHeaders;
  1437. WAISDocumentShortHeader** shortHeaders;
  1438. WAISDocumentLongHeader** longHeaders;
  1439. WAISDocumentText** text;
  1440. WAISDocumentHeadlines** headlines;
  1441. WAISDocumentCodes** codes;
  1442. diagnosticRecord** diags;
  1443. /* if buf is NULL, we have just gotten a read error, and need to clean up 
  1444.    any state we have built.  If not, then everything is going fine, and
  1445.    we should just hang loose
  1446.  */
  1447. {
  1448.   void* ptr = NULL;
  1449.   long i;
  1450.  
  1451.   if (buf == NULL)                        
  1452.    { s_free(seedWordsUsed);                
  1453.      if (docHeaders != NULL)                
  1454.        for (i = 0,ptr = (void *)docHeaders[i]; ptr != NULL; 
  1455.         ptr = (void *)docHeaders[++i])        
  1456.      freeWAISDocumentHeader((WAISDocumentHeader*)ptr);    
  1457.      s_free(docHeaders);                
  1458.      if (shortHeaders != NULL)    
  1459.        for (i = 0,ptr = (void *)shortHeaders[i]; ptr != NULL;
  1460.         ptr = (void *)shortHeaders[++i])    
  1461.      freeWAISDocumentShortHeader((WAISDocumentShortHeader*)ptr);
  1462.      s_free(shortHeaders);                        
  1463.      if (longHeaders != NULL)                
  1464.        for (i = 0,ptr = (void *)longHeaders[i]; ptr != NULL; 
  1465.         ptr = (void *)longHeaders[++i])    
  1466.      freeWAISDocumentLongHeader((WAISDocumentLongHeader*)ptr);
  1467.      s_free(longHeaders);                
  1468.      if (text != NULL)                    
  1469.        for (i = 0,ptr = (void *)text[i]; ptr != NULL; ptr = (void *)text[++i])
  1470.      freeWAISDocumentText((WAISDocumentText*)ptr);    
  1471.      s_free(text);                    
  1472.      if (headlines != NULL)                    
  1473.        for (i = 0,ptr = (void *)headlines[i]; ptr != NULL;
  1474.         ptr = (void *)headlines[++i])        
  1475.      freeWAISDocumentHeadlines((WAISDocumentHeadlines*)ptr);    
  1476.      s_free(headlines);                        
  1477.      if (codes != NULL)                     
  1478.        for (i = 0,ptr = (void *)codes[i]; ptr != NULL; 
  1479.         ptr = (void *)codes[++i])                
  1480.      freeWAISDocumentCodes((WAISDocumentCodes*)ptr);     
  1481.      s_free(codes);                    
  1482.      if (diags != NULL)                          
  1483.        for (i = 0,ptr = (void *)diags[i]; ptr != NULL; 
  1484.         ptr = (void *)diags[++i])     
  1485.      freeDiag((diagnosticRecord*)ptr);         
  1486.      s_free(diags);
  1487.    }
  1488. }
  1489.  
  1490. /*----------------------------------------------------------------------*/
  1491.  
  1492. char*
  1493. readSearchResponseInfo(info,buffer)
  1494. void** info;
  1495. char* buffer;
  1496. {
  1497.   char* buf = buffer;
  1498.   unsigned long size; 
  1499.   unsigned long headerSize;
  1500.   data_tag tag;
  1501.   void* header = NULL;
  1502.   WAISDocumentHeader** docHeaders = NULL;
  1503.   WAISDocumentShortHeader** shortHeaders = NULL;
  1504.   WAISDocumentLongHeader** longHeaders = NULL;
  1505.   WAISDocumentText** text = NULL;
  1506.   WAISDocumentHeadlines** headlines = NULL;
  1507.   WAISDocumentCodes** codes = NULL;
  1508.   long numDocHeaders,numLongHeaders,numShortHeaders,numText,numHeadlines;
  1509.   long numCodes;
  1510.   char* seedWordsUsed = NULL;
  1511.   diagnosticRecord** diags = NULL;
  1512.   diagnosticRecord* diag = NULL;
  1513.   long numDiags = 0;
  1514.   
  1515.   numDocHeaders = numLongHeaders = numShortHeaders = numText = numHeadlines = numCodes = 0;
  1516.   
  1517.   buf = readUserInfoHeader(&tag,&size,buf);
  1518.   headerSize = buf - buffer;
  1519.     
  1520.   while (buf < (buffer + size + headerSize))
  1521.    { data_tag tag = peekTag(buf);
  1522.      switch (tag)
  1523.       { case DT_SeedWordsUsed:
  1524.             buf = readString(&seedWordsUsed,buf);
  1525.             break;
  1526.           case DT_DatabaseDiagnosticRecords:
  1527.             if (diags == NULL) /* create a new diag list */
  1528.              { diags = (diagnosticRecord**)s_malloc((size_t)sizeof(diagnosticRecord*) * 2);
  1529.              }
  1530.             else /* grow the diag list */
  1531.              { diags = (diagnosticRecord**)s_realloc((char*)diags,(size_t)(sizeof(diagnosticRecord*) * (numDiags + 2)));
  1532.              }
  1533.             buf = readDiag(&diag,buf);
  1534.             diags[numDiags++] = diag; /* put it in the list */
  1535.             diags[numDiags] = NULL;
  1536.             break;
  1537.           case DT_DocumentHeaderGroup:
  1538.             if (docHeaders == NULL) /* create a new header list */
  1539.              { docHeaders = (WAISDocumentHeader**)s_malloc((size_t)sizeof(WAISDocumentHeader*) * 2);
  1540.              }
  1541.             else /* grow the doc list */
  1542.              { docHeaders = (WAISDocumentHeader**)s_realloc((char*)docHeaders,(size_t)(sizeof(WAISDocumentHeader*) * (numDocHeaders + 2)));
  1543.              }
  1544.             buf = readWAISDocumentHeader((WAISDocumentHeader**)&header,buf);
  1545.           cleanUpWaisSearchResponse(buf,seedWordsUsed,docHeaders,shortHeaders,longHeaders,text,headlines,codes,diags);
  1546.             RETURN_ON_NULL(buf);
  1547.             docHeaders[numDocHeaders++] = 
  1548.             (WAISDocumentHeader*)header; /* put it in the list */
  1549.             docHeaders[numDocHeaders] = NULL;
  1550.             break;
  1551.           case DT_DocumentShortHeaderGroup:
  1552.             if (shortHeaders == NULL) /* create a new header list */
  1553.              { shortHeaders = (WAISDocumentShortHeader**)s_malloc((size_t)sizeof(WAISDocumentShortHeader*) * 2);
  1554.              }
  1555.             else /* grow the doc list */
  1556.              { shortHeaders = (WAISDocumentShortHeader**)s_realloc((char*)shortHeaders,(size_t)(sizeof(WAISDocumentShortHeader*) * (numShortHeaders + 2)));
  1557.              }
  1558.             buf = readWAISDocumentShortHeader((WAISDocumentShortHeader**)&header,buf);
  1559.           cleanUpWaisSearchResponse(buf,seedWordsUsed,docHeaders,shortHeaders,longHeaders,text,headlines,codes,diags);
  1560.             RETURN_ON_NULL(buf);
  1561.             shortHeaders[numShortHeaders++] = 
  1562.             (WAISDocumentShortHeader*)header; /* put it in the list */
  1563.             shortHeaders[numShortHeaders] = NULL;
  1564.             break;
  1565.           case DT_DocumentLongHeaderGroup:
  1566.             if (longHeaders == NULL) /* create a new header list */
  1567.              { longHeaders = (WAISDocumentLongHeader**)s_malloc((size_t)sizeof(WAISDocumentLongHeader*) * 2);
  1568.              }
  1569.             else /* grow the doc list */
  1570.              { longHeaders = (WAISDocumentLongHeader**)s_realloc((char*)longHeaders,(size_t)(sizeof(WAISDocumentLongHeader*) * (numLongHeaders + 2)));
  1571.              }
  1572.             buf = readWAISDocumentLongHeader((WAISDocumentLongHeader**)&header,buf);
  1573.           cleanUpWaisSearchResponse(buf,seedWordsUsed,docHeaders,shortHeaders,longHeaders,text,headlines,codes,diags);
  1574.             RETURN_ON_NULL(buf);
  1575.             longHeaders[numLongHeaders++] = 
  1576.             (WAISDocumentLongHeader*)header; /* put it in the list */
  1577.             longHeaders[numLongHeaders] = NULL;
  1578.             break;
  1579.         case DT_DocumentTextGroup:
  1580.             if (text == NULL) /* create a new list */
  1581.              { text = (WAISDocumentText**)s_malloc((size_t)sizeof(WAISDocumentText*) * 2);
  1582.              }
  1583.             else /* grow the list */
  1584.              { text = (WAISDocumentText**)s_realloc((char*)text,(size_t)(sizeof(WAISDocumentText*) * (numText + 2)));
  1585.              }
  1586.             buf = readWAISDocumentText((WAISDocumentText**)&header,buf);
  1587.           cleanUpWaisSearchResponse(buf,seedWordsUsed,docHeaders,shortHeaders,longHeaders,text,headlines,codes,diags);
  1588.             RETURN_ON_NULL(buf);
  1589.             text[numText++] = 
  1590.             (WAISDocumentText*)header; /* put it in the list */
  1591.             text[numText] = NULL;
  1592.             break;
  1593.           case DT_DocumentHeadlineGroup:
  1594.             if (headlines == NULL) /* create a new list */
  1595.              { headlines = (WAISDocumentHeadlines**)s_malloc((size_t)sizeof(WAISDocumentHeadlines*) * 2);
  1596.              }
  1597.             else /* grow the list */
  1598.              { headlines = (WAISDocumentHeadlines**)s_realloc((char*)headlines,(size_t)(sizeof(WAISDocumentHeadlines*) * (numHeadlines + 2)));
  1599.              }
  1600.             buf = readWAISDocumentHeadlines((WAISDocumentHeadlines**)&header,buf);
  1601.           cleanUpWaisSearchResponse(buf,seedWordsUsed,docHeaders,shortHeaders,longHeaders,text,headlines,codes,diags);
  1602.             RETURN_ON_NULL(buf);
  1603.             headlines[numHeadlines++] = 
  1604.             (WAISDocumentHeadlines*)header; /* put it in the list */
  1605.             headlines[numHeadlines] = NULL;
  1606.             break;
  1607.           case DT_DocumentCodeGroup:
  1608.             if (codes == NULL) /* create a new list */
  1609.              { codes = (WAISDocumentCodes**)s_malloc((size_t)sizeof(WAISDocumentCodes*) * 2);
  1610.              }
  1611.             else /* grow the list */
  1612.              { codes = (WAISDocumentCodes**)s_realloc((char*)codes,(size_t)(sizeof(WAISDocumentCodes*) * (numCodes + 2)));
  1613.              }
  1614.             buf = readWAISDocumentCodes((WAISDocumentCodes**)&header,buf);
  1615.           cleanUpWaisSearchResponse(buf,seedWordsUsed,docHeaders,shortHeaders,longHeaders,text,headlines,codes,diags);
  1616.             RETURN_ON_NULL(buf);
  1617.             codes[numCodes++] = 
  1618.             (WAISDocumentCodes*)header; /* put it in the list */
  1619.             codes[numCodes] = NULL;
  1620.             break;
  1621.         default:
  1622.           cleanUpWaisSearchResponse(buf,seedWordsUsed,docHeaders,shortHeaders,longHeaders,text,headlines,codes,diags);
  1623.           REPORT_READ_ERROR(buf);
  1624.           break;
  1625.       }
  1626.    }
  1627.         
  1628.   *info = (void *)makeWAISSearchResponse(seedWordsUsed,docHeaders,shortHeaders,
  1629.                  longHeaders,text,headlines,codes,diags);
  1630.   
  1631.   return(buf);
  1632. }
  1633.  
  1634. /*----------------------------------------------------------------------*/
  1635.  
  1636. WAISDocumentText*
  1637. makeWAISDocumentText(docID,versionNumber,documentText)
  1638. any* docID;
  1639. long versionNumber;
  1640. any* documentText;
  1641. {
  1642.   WAISDocumentText* docText = (WAISDocumentText*)s_malloc((size_t)sizeof(WAISDocumentText));
  1643.  
  1644.   docText->DocumentID = docID;
  1645.   docText->VersionNumber = versionNumber;
  1646.   docText->DocumentText = documentText;
  1647.   
  1648.   return(docText);
  1649. }
  1650.  
  1651. /*----------------------------------------------------------------------*/
  1652.  
  1653. void 
  1654. freeWAISDocumentText(docText)
  1655. WAISDocumentText* docText;
  1656. {
  1657.   freeAny(docText->DocumentID);
  1658.   freeAny(docText->DocumentText);
  1659.   s_free(docText);
  1660. }
  1661.  
  1662. /*----------------------------------------------------------------------*/
  1663.  
  1664. char* 
  1665. writeWAISDocumentText(docText,buffer,len)
  1666. WAISDocumentText* docText;
  1667. char* buffer;
  1668. long* len;
  1669. {
  1670.   unsigned long header_len = userInfoTagSize(DT_DocumentTextGroup,
  1671.                                             DefWAISDocTextSize);
  1672.   char* buf = buffer + header_len;
  1673.   unsigned long size;
  1674.   
  1675.   RESERVE_SPACE_FOR_WAIS_HEADER(len);
  1676.  
  1677.   buf = writeAny(docText->DocumentID,DT_DocumentID,buf,len);
  1678.   buf = writeNum(docText->VersionNumber,DT_VersionNumber,buf,len);
  1679.   buf = writeAny(docText->DocumentText,DT_DocumentText,buf,len);
  1680.   
  1681.   /* now write the header and size */
  1682.   size = buf - buffer; 
  1683.   buf = writeUserInfoHeader(DT_DocumentTextGroup,size,header_len,buffer,len);
  1684.  
  1685.   return(buf);
  1686. }
  1687.  
  1688. /*----------------------------------------------------------------------*/
  1689.  
  1690. char* 
  1691. readWAISDocumentText(docText,buffer)
  1692. WAISDocumentText** docText;
  1693. char* buffer;
  1694. {
  1695.   char* buf = buffer;
  1696.   unsigned long size; 
  1697.   unsigned long headerSize;
  1698.   data_tag tag;
  1699.   any *docID,*documentText;
  1700.   long versionNumber;
  1701.   
  1702.   docID = documentText = NULL;
  1703.   versionNumber = UNUSED;
  1704.   
  1705.   buf = readUserInfoHeader(&tag,&size,buf);
  1706.   headerSize = buf - buffer;
  1707.     
  1708.   while (buf < (buffer + size + headerSize))
  1709.    { data_tag tag = peekTag(buf);
  1710.      switch (tag)
  1711.       { case DT_DocumentID:
  1712.             buf = readAny(&docID,buf);
  1713.             break;
  1714.           case DT_VersionNumber:
  1715.             buf = readNum(&versionNumber,buf);
  1716.             break;
  1717.           case DT_DocumentText:
  1718.             buf = readAny(&documentText,buf);
  1719.             break;
  1720.         default:
  1721.           freeAny(docID);
  1722.           freeAny(documentText);
  1723.           REPORT_READ_ERROR(buf);
  1724.           break;
  1725.       }
  1726.    }
  1727.         
  1728.   *docText = makeWAISDocumentText(docID,versionNumber,documentText);
  1729.   return(buf);
  1730. }
  1731.  
  1732. /*----------------------------------------------------------------------*/
  1733.  
  1734. WAISDocumentHeadlines*
  1735. makeWAISDocumentHeadlines(docID,
  1736.               versionNumber,
  1737.               source,
  1738.               date,
  1739.               headline,
  1740.               originCity)
  1741. any* docID;
  1742. long versionNumber;
  1743. char* source;
  1744. char* date;
  1745. char* headline;
  1746. char* originCity;
  1747. {
  1748.   WAISDocumentHeadlines* docHeadline =
  1749.     (WAISDocumentHeadlines*)s_malloc((size_t)sizeof(WAISDocumentHeadlines));
  1750.  
  1751.   docHeadline->DocumentID = docID;
  1752.   docHeadline->VersionNumber = versionNumber;
  1753.   docHeadline->Source = source;
  1754.   docHeadline->Date = date;
  1755.   docHeadline->Headline = headline;
  1756.   docHeadline->OriginCity = originCity;
  1757.   
  1758.   return(docHeadline);
  1759. }
  1760.  
  1761. /*----------------------------------------------------------------------*/
  1762.  
  1763. void 
  1764. freeWAISDocumentHeadlines(docHeadline)
  1765. WAISDocumentHeadlines* docHeadline;
  1766. {
  1767.   freeAny(docHeadline->DocumentID);
  1768.   s_free(docHeadline->Source);
  1769.   s_free(docHeadline->Date);
  1770.   s_free(docHeadline->Headline);
  1771.   s_free(docHeadline->OriginCity);
  1772.   s_free(docHeadline);
  1773. }
  1774.  
  1775. /*----------------------------------------------------------------------*/
  1776.  
  1777. char* 
  1778. writeWAISDocumentHeadlines(docHeadline,buffer,len)
  1779. WAISDocumentHeadlines* docHeadline;
  1780. char* buffer;
  1781. long* len;
  1782. {
  1783.   unsigned long header_len = userInfoTagSize(DT_DocumentHeadlineGroup,
  1784.                                             DefWAISDocHeadlineSize);
  1785.   char* buf = buffer + header_len;
  1786.   unsigned long size;
  1787.   
  1788.   RESERVE_SPACE_FOR_WAIS_HEADER(len);
  1789.  
  1790.   buf = writeAny(docHeadline->DocumentID,DT_DocumentID,buf,len);
  1791.   buf = writeNum(docHeadline->VersionNumber,DT_VersionNumber,buf,len);
  1792.   buf = writeString(docHeadline->Source,DT_Source,buf,len);
  1793.   buf = writeString(docHeadline->Date,DT_Date,buf,len);
  1794.   buf = writeString(docHeadline->Headline,DT_Headline,buf,len);
  1795.   buf = writeString(docHeadline->OriginCity,DT_OriginCity,buf,len);
  1796.   
  1797.   /* now write the header and size */
  1798.   size = buf - buffer; 
  1799.   buf = writeUserInfoHeader(DT_DocumentHeadlineGroup,size,header_len,buffer,len);
  1800.  
  1801.   return(buf);
  1802. }
  1803.  
  1804. /*----------------------------------------------------------------------*/
  1805.  
  1806. char* 
  1807. readWAISDocumentHeadlines(docHeadline,buffer)
  1808. WAISDocumentHeadlines** docHeadline;
  1809. char* buffer;
  1810. {
  1811.   char* buf = buffer;
  1812.   unsigned long size; 
  1813.   unsigned long headerSize;
  1814.   data_tag tag;
  1815.   any* docID;
  1816.   long versionNumber;
  1817.   char *source,*date,*headline,*originCity;
  1818.   
  1819.   docID = NULL;
  1820.   versionNumber = UNUSED;
  1821.   source = date = headline = originCity = NULL;
  1822.   
  1823.   buf = readUserInfoHeader(&tag,&size,buf);
  1824.   headerSize = buf - buffer;
  1825.     
  1826.   while (buf < (buffer + size + headerSize))
  1827.    { data_tag tag = peekTag(buf);
  1828.      switch (tag)
  1829.       { case DT_DocumentID:
  1830.             buf = readAny(&docID,buf);
  1831.             break;
  1832.           case DT_VersionNumber:
  1833.             buf = readNum(&versionNumber,buf);
  1834.             break;
  1835.           case DT_Source:
  1836.             buf = readString(&source,buf);
  1837.             break;
  1838.           case DT_Date:
  1839.             buf = readString(&date,buf);
  1840.             break;
  1841.           case DT_Headline:
  1842.             buf = readString(&headline,buf);
  1843.             break;
  1844.           case DT_OriginCity:
  1845.             buf = readString(&originCity,buf);
  1846.             break;
  1847.         default:
  1848.           freeAny(docID);
  1849.           s_free(source);
  1850.           s_free(date);
  1851.           s_free(headline);
  1852.           s_free(originCity);
  1853.           REPORT_READ_ERROR(buf);
  1854.           break;
  1855.       }
  1856.    }
  1857.         
  1858.   *docHeadline = makeWAISDocumentHeadlines(docID,versionNumber,source,date,
  1859.                                              headline,originCity);
  1860.   return(buf);
  1861. }
  1862.  
  1863. /*----------------------------------------------------------------------*/
  1864.  
  1865. WAISDocumentCodes*
  1866. makeWAISDocumentCodes(docID,
  1867.               versionNumber,
  1868.               stockCodes,
  1869.               companyCodes,
  1870.               industryCodes)
  1871. any* docID;
  1872. long versionNumber;
  1873. char* stockCodes;
  1874. char* companyCodes;
  1875. char* industryCodes;
  1876. {
  1877.   WAISDocumentCodes* docCodes = (WAISDocumentCodes*)s_malloc((size_t)sizeof(WAISDocumentCodes));
  1878.  
  1879.   docCodes->DocumentID = docID;
  1880.   docCodes->VersionNumber = versionNumber;
  1881.   docCodes->StockCodes = stockCodes;
  1882.   docCodes->CompanyCodes = companyCodes;
  1883.   docCodes->IndustryCodes = industryCodes;
  1884.   
  1885.   return(docCodes);
  1886. }
  1887.  
  1888. /*----------------------------------------------------------------------*/
  1889.  
  1890. void 
  1891. freeWAISDocumentCodes(docCodes)
  1892. WAISDocumentCodes* docCodes;
  1893. {
  1894.   freeAny(docCodes->DocumentID);
  1895.   s_free(docCodes->StockCodes);
  1896.   s_free(docCodes->CompanyCodes);
  1897.   s_free(docCodes->IndustryCodes);
  1898.   s_free(docCodes);
  1899. }
  1900.  
  1901. /*----------------------------------------------------------------------*/
  1902.  
  1903. char* 
  1904. writeWAISDocumentCodes(docCodes,buffer,len)
  1905. WAISDocumentCodes* docCodes;
  1906. char* buffer;
  1907. long* len;
  1908. {
  1909.   unsigned long header_len = userInfoTagSize(DT_DocumentCodeGroup ,
  1910.                                             DefWAISDocCodeSize);
  1911.   char* buf = buffer + header_len;
  1912.   unsigned long size;
  1913.   
  1914.   RESERVE_SPACE_FOR_WAIS_HEADER(len);
  1915.  
  1916.   buf = writeAny(docCodes->DocumentID,DT_DocumentID,buf,len);
  1917.   buf = writeNum(docCodes->VersionNumber,DT_VersionNumber,buf,len);
  1918.   buf = writeString(docCodes->StockCodes,DT_StockCodes,buf,len);
  1919.   buf = writeString(docCodes->CompanyCodes,DT_CompanyCodes,buf,len);
  1920.   buf = writeString(docCodes->IndustryCodes,DT_IndustryCodes,buf,len);
  1921.   
  1922.   /* now write the header and size */
  1923.   size = buf - buffer; 
  1924.   buf = writeUserInfoHeader(DT_DocumentCodeGroup,size,header_len,buffer,len);
  1925.  
  1926.   return(buf);
  1927. }
  1928.  
  1929. /*----------------------------------------------------------------------*/
  1930.  
  1931. char* 
  1932. readWAISDocumentCodes(docCodes,buffer)
  1933. WAISDocumentCodes** docCodes;
  1934. char* buffer;
  1935. {
  1936.   char* buf = buffer;
  1937.   unsigned long size; 
  1938.   unsigned long headerSize;
  1939.   data_tag tag;
  1940.   any* docID;
  1941.   long versionNumber;
  1942.   char *stockCodes,*companyCodes,*industryCodes;
  1943.   
  1944.   docID = NULL;
  1945.   versionNumber = UNUSED;
  1946.   stockCodes = companyCodes = industryCodes = NULL;
  1947.   
  1948.   buf = readUserInfoHeader(&tag,&size,buf);
  1949.   headerSize = buf - buffer;
  1950.     
  1951.   while (buf < (buffer + size + headerSize))
  1952.    { data_tag tag = peekTag(buf);
  1953.      switch (tag)
  1954.       { case DT_DocumentID:
  1955.             buf = readAny(&docID,buf);
  1956.             break;
  1957.           case DT_VersionNumber:
  1958.             buf = readNum(&versionNumber,buf);
  1959.             break;
  1960.           case DT_StockCodes:
  1961.             buf = readString(&stockCodes,buf);
  1962.             break;
  1963.           case DT_CompanyCodes:
  1964.             buf = readString(&companyCodes,buf);
  1965.             break;
  1966.           case DT_IndustryCodes:
  1967.             buf = readString(&industryCodes,buf);
  1968.             break;
  1969.         default:
  1970.           freeAny(docID);
  1971.           s_free(stockCodes);
  1972.           s_free(companyCodes);
  1973.           s_free(industryCodes);
  1974.           REPORT_READ_ERROR(buf);
  1975.           break;
  1976.       }
  1977.    }
  1978.         
  1979.   *docCodes = makeWAISDocumentCodes(docID,versionNumber,stockCodes,
  1980.                                       companyCodes,industryCodes);
  1981.   return(buf);
  1982. }
  1983.  
  1984. /*----------------------------------------------------------------------*/
  1985.  
  1986. char* 
  1987. writePresentInfo(present,buffer,len)
  1988. PresentAPDU* present;
  1989. char* buffer;
  1990. long* len;
  1991. {
  1992.   /* The WAIS protocol doesn't use present info */
  1993.   return(buffer);
  1994. }
  1995.  
  1996. /*----------------------------------------------------------------------*/
  1997.  
  1998. char* 
  1999. readPresentInfo(info,buffer)
  2000. void** info;
  2001. char* buffer;
  2002. {
  2003.   /* The WAIS protocol doesn't use present info */
  2004.   *info = NULL;
  2005.   return(buffer);
  2006. }
  2007.  
  2008. /*----------------------------------------------------------------------*/
  2009.  
  2010. char* 
  2011. writePresentResponseInfo(response,buffer,len)
  2012. PresentResponseAPDU* response;
  2013. char* buffer;
  2014. long* len;
  2015. {
  2016.   /* The WAIS protocol doesn't use presentResponse info */
  2017.   return(buffer);
  2018. }
  2019.  
  2020. /*----------------------------------------------------------------------*/
  2021.  
  2022. char* 
  2023. readPresentResponseInfo(info,buffer)
  2024. void** info;
  2025. char* buffer;
  2026. {
  2027.   /* The WAIS protocol doesn't use presentResponse info */
  2028.   *info = NULL;
  2029.   return(buffer);
  2030. }
  2031.  
  2032. /*----------------------------------------------------------------------*/
  2033.  
  2034. /* support for type 1 queries */
  2035.  
  2036. /* new use values (for the chunk types) */
  2037. #define    BYTE        "wb"
  2038. #define    LINE        "wl"
  2039. #define    PARAGRAPH    "wp"
  2040. #define DATA_TYPE    "wt"
  2041.  
  2042. /* WAIS supports the following semantics for type 1 queries:
  2043.        
  2044.      1.  retrieve the header/codes from a document:
  2045.  
  2046.             System_Control_Number = docID
  2047.             Data Type = type (optional)
  2048.             And
  2049.  
  2050.      2.  retrieve a fragment of the text of a document:
  2051.  
  2052.             System_Control_Number = docID
  2053.             Data Type = type (optional)
  2054.             And
  2055.             Chunk >= start
  2056.             And
  2057.             Chunk < end
  2058.             And
  2059.  
  2060.            Information from multiple documents may be requested by using 
  2061.            groups of the above joined by:
  2062.  
  2063.             OR
  2064.  
  2065.            ( XXX does an OR come after every group but the first, or do they
  2066.               all come at the end? )
  2067.               
  2068.         ( XXX return type could be in the element set)
  2069. */
  2070.  
  2071. static query_term** makeWAISQueryTerms _AP((DocObj** docs));
  2072.    
  2073. static query_term**
  2074. makeWAISQueryTerms(docs)
  2075. DocObj** docs;
  2076. /* given a null terminated list of docObjs, construct the appropriate
  2077.    query of the form given above
  2078.  */
  2079. {
  2080.   query_term** terms = NULL;
  2081.   long numTerms = 0;
  2082.   DocObj* doc = NULL;
  2083.   long i;
  2084.  
  2085.   if (docs == NULL)
  2086.     return((query_term**)NULL);
  2087.  
  2088.   terms = (query_term**)s_malloc((size_t)(sizeof(query_term*) * 1));
  2089.   terms[numTerms] = NULL;
  2090.  
  2091.   /* loop through the docs making terms for them all */
  2092.   for (i = 0,doc = docs[i]; doc != NULL; doc = docs[++i])
  2093.     { any* type = NULL;
  2094.  
  2095.       if (doc->Type != NULL)
  2096.     type = stringToAny(doc->Type);
  2097.  
  2098.       if (doc->ChunkCode == CT_document) /* a whole document */
  2099.     { terms = (query_term**)s_realloc((char*)terms,
  2100.                       (size_t)(sizeof(query_term*) * 
  2101.                            (numTerms + 3 + 1)));
  2102.       terms[numTerms++] = makeAttributeTerm(SYSTEM_CONTROL_NUMBER,
  2103.                         EQUAL,IGNORE,IGNORE,
  2104.                         IGNORE,IGNORE,doc->DocumentID);
  2105.       if (type != NULL)
  2106.        { terms[numTerms++] = makeAttributeTerm(DATA_TYPE,EQUAL,
  2107.                            IGNORE,IGNORE,IGNORE,
  2108.                            IGNORE,type);
  2109.          terms[numTerms++] = makeOperatorTerm(AND);
  2110.            }
  2111.       terms[numTerms] = NULL;
  2112.     }
  2113.       else            /* a document fragment */
  2114.     {    char chunk_att[ATTRIBUTE_SIZE];
  2115.         any* startChunk = NULL;
  2116.         any* endChunk = NULL;
  2117.  
  2118.         terms = (query_term**)s_realloc((char*)terms,
  2119.                         (size_t)(sizeof(query_term*) * 
  2120.                              (numTerms + 7 + 1)));
  2121.  
  2122.         switch (doc->ChunkCode)
  2123.           { case CT_byte:
  2124.             case CT_line:
  2125.               { char start[20],end[20];
  2126.             (doc->ChunkCode == CT_byte) ?
  2127.               strncpy(chunk_att,BYTE,ATTRIBUTE_SIZE) :
  2128.             strncpy(chunk_att,LINE,ATTRIBUTE_SIZE);    
  2129.             sprintf(start,"%ld",doc->ChunkStart.Pos);
  2130.             startChunk = stringToAny(start);
  2131.             sprintf(end,"%ld",doc->ChunkEnd.Pos);
  2132.             endChunk = stringToAny(end);
  2133.               }
  2134.               break;
  2135.             case CT_paragraph:
  2136.               strncpy(chunk_att,PARAGRAPH,ATTRIBUTE_SIZE);
  2137.               startChunk = doc->ChunkStart.ID;
  2138.               endChunk = doc->ChunkEnd.ID;
  2139.               break;
  2140.             default:
  2141.               /* error */
  2142.               break;
  2143.             }
  2144.  
  2145.         terms[numTerms++] = makeAttributeTerm(SYSTEM_CONTROL_NUMBER,
  2146.                               EQUAL,IGNORE,IGNORE,
  2147.                               IGNORE,
  2148.                               IGNORE,doc->DocumentID);
  2149.         if (type != NULL)
  2150.          { terms[numTerms++] = makeAttributeTerm(DATA_TYPE,EQUAL,IGNORE,
  2151.                              IGNORE,IGNORE,IGNORE,
  2152.                              type);
  2153.            terms[numTerms++] = makeOperatorTerm(AND);
  2154.          }
  2155.         terms[numTerms++] = makeAttributeTerm(chunk_att,
  2156.                               GREATER_THAN_OR_EQUAL,
  2157.                               IGNORE,IGNORE,IGNORE, 
  2158.                               IGNORE,
  2159.                               startChunk);
  2160.         terms[numTerms++] = makeOperatorTerm(AND);
  2161.         terms[numTerms++] = makeAttributeTerm(chunk_att,LESS_THAN,
  2162.                               IGNORE,IGNORE,IGNORE,
  2163.                               IGNORE,
  2164.                               endChunk);
  2165.         terms[numTerms++] = makeOperatorTerm(AND);
  2166.         terms[numTerms] = NULL;
  2167.  
  2168.         if (doc->ChunkCode == CT_byte || doc->ChunkCode == CT_line)
  2169.           { freeAny(startChunk);
  2170.             freeAny(endChunk);
  2171.           }
  2172.           }
  2173.       
  2174.       freeAny(type);
  2175.       
  2176.      if (i != 0) /* multiple independent queries, need a disjunction */
  2177.     { terms = (query_term**)s_realloc((char*)terms,
  2178.                       (size_t)(sizeof(query_term*) * 
  2179.                            (numTerms + 1 + 1)));
  2180.       terms[numTerms++] = makeOperatorTerm(OR);
  2181.       terms[numTerms] = NULL;
  2182.     }
  2183.     }
  2184.  
  2185.   return(terms);
  2186. }
  2187.  
  2188. /*----------------------------------------------------------------------*/
  2189.  
  2190. static DocObj** makeWAISQueryDocs _AP((query_term** terms));
  2191.  
  2192. static DocObj** 
  2193. makeWAISQueryDocs(terms)
  2194. query_term** terms;
  2195. /* given a list of terms in the form given above, convert them to 
  2196.    DocObjs.
  2197.  */
  2198. {
  2199.   query_term* docTerm = NULL;
  2200.   query_term* fragmentTerm = NULL;
  2201.   DocObj** docs = NULL;
  2202.   DocObj* doc = NULL;
  2203.   long docNum,termNum;
  2204.  
  2205.   docNum = termNum = 0;
  2206.   
  2207.   docs = (DocObj**)s_malloc((size_t)(sizeof(DocObj*) * 1));
  2208.   docs[docNum] = NULL;
  2209.  
  2210.   /* translate the terms into DocObjs */
  2211.   while (true)
  2212.     {          
  2213.       query_term* typeTerm = NULL;
  2214.       char* type = NULL;
  2215.       long startTermOffset;
  2216.  
  2217.       docTerm = terms[termNum];
  2218.      
  2219.       if (docTerm == NULL)
  2220.     break;            /* we're done converting */;
  2221.  
  2222.       typeTerm = terms[termNum + 1]; /* get the lead Term if it exists */
  2223.  
  2224.       if (strcmp(typeTerm->Use,DATA_TYPE) == 0)    /* we do have a type */
  2225.        { startTermOffset = 3;    
  2226.      type = anyToString(typeTerm->Term);
  2227.        }
  2228.       else                        /* no type */
  2229.        { startTermOffset = 1;
  2230.      typeTerm = NULL;
  2231.      type = NULL;
  2232.        }
  2233.  
  2234.       /* grow the doc list */
  2235.       docs = (DocObj**)s_realloc((char*)docs,(size_t)(sizeof(DocObj*) * 
  2236.                               (docNum + 1 + 1)));
  2237.  
  2238.       /* figure out what kind of docObj to build - and build it */
  2239.       fragmentTerm = terms[termNum + startTermOffset];
  2240.       if (fragmentTerm != NULL && fragmentTerm->TermType == TT_Attribute)
  2241.     {            /* build a document fragment */
  2242.       query_term* startTerm = fragmentTerm;
  2243.       query_term* endTerm = terms[termNum + startTermOffset + 2]; 
  2244.  
  2245.       if (strcmp(startTerm->Use,BYTE) == 0) /* a byte chunk */
  2246.         doc = makeDocObjUsingBytes(duplicateAny(docTerm->Term),
  2247.                        type,
  2248.                        anyToLong(startTerm->Term),
  2249.                        anyToLong(endTerm->Term));
  2250.       else if (strcmp(startTerm->Use,LINE) == 0) /* a line chunk */
  2251.         doc = makeDocObjUsingLines(duplicateAny(docTerm->Term),
  2252.                        type,
  2253.                        anyToLong(startTerm->Term),
  2254.                        anyToLong(endTerm->Term));
  2255.       else            /* a paragraph chunk */
  2256.         doc = makeDocObjUsingParagraphs(duplicateAny(docTerm->Term),
  2257.                         type,
  2258.                         duplicateAny(startTerm->Term),
  2259.                         duplicateAny(endTerm->Term));
  2260.       termNum += (startTermOffset + 4);    /* point to next term */
  2261.     }
  2262.       else            /* build a full document */
  2263.     { 
  2264.       doc = makeDocObjUsingWholeDocument(duplicateAny(docTerm->Term),
  2265.                          type);
  2266.       termNum += startTermOffset;    /* point to next term */
  2267.     }
  2268.      
  2269.       docs[docNum++] = doc;    /* insert the new document */
  2270.      
  2271.       docs[docNum] = NULL;    /* keep the doc list terminated */
  2272.  
  2273.      
  2274.       if (terms[termNum] != NULL)
  2275.     termNum++; /* skip the OR operator it necessary */
  2276.       else
  2277.     break; /* we are done */
  2278.     }
  2279.  
  2280.   return(docs);
  2281. }
  2282.  
  2283. /*----------------------------------------------------------------------*/
  2284.  
  2285. any* 
  2286. makeWAISTextQuery(docs)
  2287. DocObj** docs;
  2288. /* given a list of DocObjs, return an any whose contents is the corresponding
  2289.    type 1 query
  2290.  */
  2291. {
  2292.   any *buf = NULL;
  2293.   query_term** terms = NULL;
  2294.   
  2295.   terms = makeWAISQueryTerms(docs);
  2296.   buf = writeQuery(terms);
  2297.   
  2298.   doList((void**)terms,freeTerm);
  2299.   s_free(terms);
  2300.   
  2301.   return(buf);
  2302. }
  2303.  
  2304. /*----------------------------------------------------------------------*/
  2305.  
  2306. DocObj** 
  2307. readWAISTextQuery(buf)
  2308. any* buf;
  2309. /* given an any whose contents are type 1 queries of the WAIS sort, 
  2310.    construct a list of the corresponding DocObjs
  2311.  */
  2312. {
  2313.   query_term** terms = NULL;
  2314.   DocObj** docs = NULL;
  2315.   
  2316.   terms = readQuery(buf);
  2317.   docs = makeWAISQueryDocs(terms);
  2318.   
  2319.   doList((void**)terms,freeTerm);
  2320.   s_free(terms);
  2321.   
  2322.   return(docs);
  2323. }
  2324.  
  2325. /*----------------------------------------------------------------------*/
  2326. /* Customized free WAIS object routines:                                */
  2327. /*                                                                      */
  2328. /*   This set of procedures is for applications to free a WAIS object   */
  2329. /*   which was made with makeWAISFOO.                                   */
  2330. /*   Each procedure frees only the memory that was allocated in its     */
  2331. /*   associated makeWAISFOO routine, thus it's not necessary for the    */
  2332. /*   caller to assign nulls to the pointer fields of the WAIS object.  */
  2333. /*----------------------------------------------------------------------*/
  2334.  
  2335. void 
  2336. CSTFreeWAISInitResponse(init)
  2337. WAISInitResponse* init;
  2338. /* free an object made with makeWAISInitResponse */
  2339. {
  2340.   s_free(init);
  2341. }
  2342.  
  2343. /*----------------------------------------------------------------------*/
  2344.  
  2345. void 
  2346. CSTFreeWAISSearch(query)
  2347. WAISSearch* query;
  2348. /* destroy an object made with makeWAISSearch() */
  2349.   s_free(query);
  2350. }
  2351.  
  2352. /*----------------------------------------------------------------------*/
  2353.  
  2354. void
  2355. CSTFreeDocObj(doc)
  2356. DocObj* doc;
  2357. /* free a docObj */
  2358.     s_free(doc);
  2359. }
  2360.  
  2361. /*----------------------------------------------------------------------*/
  2362.  
  2363. void
  2364. CSTFreeWAISDocumentHeader(header)
  2365. WAISDocumentHeader* header;
  2366.     s_free(header);
  2367. }
  2368.  
  2369. /*----------------------------------------------------------------------*/
  2370.  
  2371. void
  2372. CSTFreeWAISDocumentShortHeader(header)
  2373. WAISDocumentShortHeader* header;
  2374.   s_free(header);
  2375. }
  2376. /*----------------------------------------------------------------------*/
  2377.  
  2378. void
  2379. CSTFreeWAISDocumentLongHeader(header)
  2380. WAISDocumentLongHeader* header;
  2381. {
  2382.   s_free(header);
  2383. }
  2384.  
  2385. /*----------------------------------------------------------------------*/
  2386.  
  2387. void
  2388. CSTFreeWAISSearchResponse(response)
  2389. WAISSearchResponse* response;
  2390.   s_free(response);
  2391. }
  2392.  
  2393. /*----------------------------------------------------------------------*/
  2394.  
  2395. void 
  2396. CSTFreeWAISDocumentText(docText)
  2397. WAISDocumentText* docText;
  2398.   s_free(docText);
  2399. }
  2400.  
  2401. /*----------------------------------------------------------------------*/
  2402.  
  2403. void 
  2404. CSTFreeWAISDocumentHeadlines(docHeadline)
  2405. WAISDocumentHeadlines* docHeadline;
  2406.   s_free(docHeadline);
  2407. }
  2408.  
  2409. /*----------------------------------------------------------------------*/
  2410.  
  2411. void 
  2412. CSTFreeWAISDocumentCodes(docCodes)
  2413. WAISDocumentCodes* docCodes;
  2414. {
  2415.   s_free(docCodes);
  2416. }
  2417.  
  2418. /*----------------------------------------------------------------------*/
  2419.  
  2420. void 
  2421. CSTFreeWAISTextQuery(query)
  2422. any* query;
  2423. {
  2424.    freeAny(query);
  2425. }
  2426.  
  2427. /*----------------------------------------------------------------------*/
  2428.