home *** CD-ROM | disk | FTP | other *** search
/ Internet Publisher's Toolbox 2.0 / Internet Publisher's Toolbox.iso / internet / ntserver / wtsource / docid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-11  |  11.5 KB  |  389 lines

  1. /* WIDE AREA INFORMATION SERVER SOFTWARE:
  2.    No guarantees or restrictions.  See the readme file for the full standard
  3.    disclaimer.
  4.  *
  5.  * these are the methods for the DocID struct
  6.  * -brewster
  7.  *
  8.  * $Log: docid.c,v $
  9.  * Revision 1.1  1993/02/16  15:05:35  freewais
  10.  * Initial revision
  11.  *
  12.  * Revision 1.6  92/05/10  14:42:57  jonathan
  13.  * Made a little safer on NULL docid's and such.
  14.  * 
  15.  * Revision 1.5  92/04/01  17:07:25  jonathan
  16.  * Added copyDocID.
  17.  * 
  18.  * Revision 1.4  92/02/21  11:05:03  jonathan
  19.  * added RCSIdent
  20.  * 
  21.  * Revision 1.3  92/02/21  11:04:30  jonathan
  22.  * Added header, fixed $Log.
  23.  * 
  24.  * Revision 1.2  92/02/12  13:17:44  jonathan
  25.  * Added $Log so RCS will put the log message in the header
  26.  * 
  27. */
  28.  
  29. /* Copyright (c) CNIDR (see ../COPYRIGHT) */
  30.  
  31.  
  32. #ifndef lint
  33. static char *RCSid = "$Header: /usr/users/freewais/FreeWAIS-0.1/ir/docid.c,v 1.1 1993/02/16 15:05:35 freewais Exp $";
  34. #endif
  35.  
  36. #include <ctype.h>
  37. #include "docid.h"
  38. #include "irfileio.h"
  39. #include "cutil.h"
  40. #include "cdialect.h"
  41.  
  42. #ifdef WIN32
  43. #include <string.h>
  44. #endif
  45.  
  46. #define TESTDOC(doc) if((doc) == NULL) return NULL
  47.  
  48. /*---------------------------------------------------------------------------*/
  49.  
  50. /* get the server slot */
  51. any* GetServer(doc)
  52. DocID* doc;
  53. {
  54.   TESTDOC(doc);
  55.   if(NULL != doc->distributorServer)
  56.     return(doc->distributorServer);
  57.   else return(doc->originalServer);
  58. }
  59.  
  60. /*---------------------------------------------------------------------------*/
  61.  
  62. any* GetDatabase(doc)
  63. DocID* doc;
  64. {
  65.   TESTDOC(doc);
  66.   if(NULL != doc->distributorDatabase)
  67.     return(doc->distributorDatabase);
  68.   else return(doc->originalDatabase);
  69. }
  70.  
  71. /*---------------------------------------------------------------------------*/
  72.  
  73. any* GetLocalID(doc)
  74. DocID* doc;
  75. {
  76.   TESTDOC(doc);
  77.   if(NULL != doc->distributorLocalID)
  78.     return(doc->distributorLocalID);
  79.   else return(doc->originalLocalID);
  80. }
  81.  
  82. /*---------------------------------------------------------------------------*/
  83.  
  84. long GetCopyrightDisposition(doc)
  85. DocID* doc;
  86. {
  87.   if(doc)
  88.     return(doc->copyrightDisposition);
  89.   else return 0;
  90. }
  91.  
  92. /*---------------------------------------------------------------------------*/
  93.  
  94. /* returns FALSE if it loses, TRUE if it wins */
  95. long ReadDocID(doc,file)
  96. DocID* doc;
  97. FILE* file;
  98. {
  99.   char temp_string[MAX_SYMBOL_SIZE];
  100.   if(FALSE == CheckStartOfStruct("doc-id", file)){
  101.     return(FALSE);
  102.   }
  103.   doc->originalServer = NULL;
  104.   doc->originalDatabase = NULL;
  105.   doc->originalLocalID = NULL;
  106.   doc->distributorServer = NULL;
  107.   doc->distributorDatabase = NULL;
  108.   doc->distributorLocalID = NULL;
  109.   doc->copyrightDisposition = COPY_WITHOUT_RESTRICTION;
  110.         
  111.   while(TRUE){
  112.     long result = ReadSymbol(temp_string, file, 
  113.                              (long)MAX_SYMBOL_SIZE);
  114.                 
  115.     if(FALSE == result){ 
  116.       return(FALSE);
  117.     }
  118.     if(END_OF_STRUCT_OR_LIST == result){
  119.       break;
  120.     }
  121.     if(0 == strcmp(temp_string, ":original-server")){
  122.       any* an_any = (any*)s_malloc(sizeof(any));
  123.       ReadAny(an_any, file);
  124.       doc->originalServer = an_any;
  125.     }
  126.     else if(0 == strcmp(temp_string, ":original-database")){
  127.       any* an_any = (any*)s_malloc(sizeof(any));
  128.       ReadAny(an_any, file);
  129.       doc->originalDatabase = an_any;
  130.     }
  131.     else if(0 == strcmp(temp_string, ":original-local-id")){
  132.       any* an_any = (any*)s_malloc(sizeof(any));
  133.       ReadAny(an_any, file);
  134.       doc->originalLocalID = an_any;
  135.     }
  136.     else if(0 == strcmp(temp_string, ":distributor-server")){
  137.       any* an_any = (any*)s_malloc(sizeof(any));
  138.       ReadAny(an_any, file);
  139.       doc->distributorServer = an_any;
  140.     }
  141.     else if(0 == strcmp(temp_string, ":distributor-database")){
  142.       any* an_any = (any*)s_malloc(sizeof(any));
  143.       ReadAny(an_any, file);
  144.       doc->distributorDatabase = an_any;
  145.     }
  146.     else if(0 == strcmp(temp_string, ":distributor-local-id")){
  147.       any* an_any = (any*)s_malloc(sizeof(any));
  148.       ReadAny(an_any, file);
  149.       doc->distributorLocalID = an_any;
  150.     }
  151.     else if(0 == strcmp(temp_string, ":copyright-disposition"))
  152.       ReadLong(file,&doc->copyrightDisposition);
  153.     else{
  154.       SkipObject(file);
  155.     }
  156.   }
  157.   return(TRUE);
  158. }
  159.  
  160.  
  161. /*---------------------------------------------------------------------------*/
  162.  
  163. /* this writes a CDocID to a stream */
  164. long WriteDocID(doc,file)
  165. DocID* doc;
  166. FILE* file;
  167. {
  168.   WriteNewline(file);
  169.   WriteStartOfStruct("doc-id", file);
  170.   if (NULL != doc->originalServer){
  171.     WriteNewline(file);
  172.     WriteSymbol(":original-server", file); WriteAny(doc->originalServer, file);
  173.   }
  174.   if (NULL != doc->originalDatabase){
  175.     WriteNewline(file);
  176.     WriteSymbol(":original-database", file); WriteAny(doc->originalDatabase, file);
  177.   }
  178.   if (NULL != doc->originalLocalID){
  179.     WriteNewline(file);
  180.     WriteSymbol(":original-local-id", file); WriteAny(doc->originalLocalID, file);
  181.   }
  182.   if (NULL != doc->distributorServer){
  183.     WriteNewline(file);
  184.     WriteSymbol(":distributor-server", file); WriteAny(doc->distributorServer, file);
  185.   }
  186.   if (NULL != doc->distributorDatabase){
  187.     WriteNewline(file);
  188.     WriteSymbol(":distributor-database", file); 
  189.     WriteAny(doc->distributorDatabase, file);
  190.   }
  191.   if (NULL != doc->distributorLocalID){
  192.     WriteNewline(file);
  193.     WriteSymbol(":distributor-local-id", file); 
  194.     WriteAny(doc->distributorLocalID, file);
  195.   }
  196.   WriteNewline(file);
  197.   WriteSymbol(":copyright-disposition", file);
  198.   WriteLong(doc->copyrightDisposition, file);
  199.   return(WriteEndOfStruct(file));
  200. }
  201.  
  202. /*---------------------------------------------------------------------------*/
  203.  
  204.         static Boolean safeCmp _AP((any* a1,any* a2));
  205.  
  206.         static Boolean safeCmp(a1,a2)
  207.         any* a1;
  208.         any* a2;
  209.         {
  210.                 /* compare 2 any's, either of which may be NULL */
  211.                 if (a1 == NULL && a2 == NULL)
  212.                         return(true);
  213.                 else if (a1 == NULL || a2 == NULL)
  214.                         return(false);
  215.                 else if (a1->size != a2->size)
  216.                         return(false);
  217.                 else if (memcmp(a1->bytes,a2->bytes,(size_t)a1->size) == 0)
  218.                         return(true);
  219.                 else
  220.                         return(false);
  221.                 }
  222.  
  223. /*---------------------------------------------------------------------------*/
  224.  
  225. Boolean cmpDocIDs(d1,d2)
  226. DocID* d1;
  227. DocID* d2;
  228. {
  229.   if (safeCmp(d1->originalServer,d2->originalServer) &&
  230.       safeCmp(d1->originalDatabase,d2->originalDatabase) &&
  231.       safeCmp(d1->originalLocalID,d2->originalLocalID) &&
  232.       safeCmp(d1->distributorServer,d2->distributorServer) &&
  233.       safeCmp(d1->distributorDatabase,d2->distributorDatabase) &&
  234.       safeCmp(d1->distributorLocalID,d2->distributorLocalID) &&
  235.       d1->copyrightDisposition == d2->copyrightDisposition)
  236.     return(true);
  237.   else
  238.     return(false); 
  239. }
  240.         
  241. /*---------------------------------------------------------------------------*/
  242.  
  243. DocID* 
  244. makeDocID()
  245. {
  246.   return((DocID*)s_malloc((size_t)sizeof(DocID)));
  247. }
  248.  
  249. /*---------------------------------------------------------------------------*/
  250.  
  251. DocID*
  252. copyDocID(doc)
  253. DocID* doc;
  254. {
  255.   DocID* result = NULL;
  256.   if(doc != NULL && 
  257.      (result = makeDocID()) != NULL) {
  258.     result->originalServer = duplicateAny(doc->originalServer);
  259.     result->originalDatabase = duplicateAny(doc->originalDatabase);
  260.     result->originalLocalID = duplicateAny(doc->originalLocalID);
  261.     result->distributorServer = duplicateAny(doc->distributorServer);
  262.     result->distributorDatabase = duplicateAny(doc->distributorDatabase);
  263.     result->distributorLocalID = duplicateAny(doc->distributorLocalID);
  264.   }
  265.   return result;
  266. }
  267.  
  268. /*---------------------------------------------------------------------------*/
  269.  
  270. void freeDocID(doc)
  271. DocID* doc;
  272. {
  273.   if(doc) {
  274.     freeAny(doc->originalServer);
  275.     freeAny(doc->originalDatabase);
  276.     freeAny(doc->originalLocalID);
  277.     freeAny(doc->distributorServer);
  278.     freeAny(doc->distributorDatabase);
  279.     freeAny(doc->distributorLocalID);
  280.     s_free(doc);
  281.   }
  282. }
  283.  
  284. /*---------------------------------------------------------------------------*/
  285.  
  286. #define DT_OriginalServer       (data_tag)1
  287. #define DT_OriginalDatabase     (data_tag)2
  288. #define DT_OriginalLocalID      (data_tag)3
  289. #define DT_DistributorServer    (data_tag)4
  290. #define DT_DistributorDatabase  (data_tag)5
  291. #define DT_DistributorLocalID   (data_tag)6
  292. #define DT_CopyrightDispostion  (data_tag)7
  293.  
  294. DocID*
  295. docIDFromAny(rawDocID)
  296. any* rawDocID;
  297. /* read from a z3950 docid to a docid structure */
  298. {
  299.   DocID* docID;
  300.   char* buf;
  301.   
  302.   TESTDOC(rawDocID);
  303.  
  304.   buf = rawDocID->bytes;
  305.   docID = makeDocID();
  306. #ifdef WIN32
  307.   while ((buf - rawDocID->bytes) < (long)(rawDocID->size))
  308. #else
  309.   while ((buf - rawDocID->bytes) < rawDocID->size)
  310. #endif
  311.     { data_tag tag = peekTag(buf);
  312.       switch (tag)
  313.         { case DT_OriginalServer:
  314.             buf = readAny(&(docID->originalServer),buf);
  315.             break;
  316.           case DT_OriginalDatabase:
  317.             buf = readAny(&(docID->originalDatabase),buf);
  318.             break;
  319.           case DT_OriginalLocalID:
  320.             buf = readAny(&(docID->originalLocalID),buf);
  321.             break;
  322.           case DT_DistributorServer:
  323.             buf = readAny(&(docID->distributorServer),buf);
  324.             break;
  325.           case DT_DistributorDatabase:
  326.             buf = readAny(&(docID->distributorDatabase),buf);
  327.             break;
  328.           case DT_DistributorLocalID:
  329.             buf = readAny(&(docID->distributorLocalID),buf);
  330.             break;
  331.           case DT_CopyrightDispostion:
  332.             buf = readNum(&(docID->copyrightDisposition),buf);
  333.             break;
  334.           default:
  335.             freeDocID(docID);
  336.             return(NULL);
  337.           };
  338.     }
  339.   
  340.   return(docID);
  341. }
  342.  
  343. /*---------------------------------------------------------------------------*/
  344.  
  345. any*
  346. anyFromDocID(docID)
  347. DocID* docID;
  348. /* write a docid structure to a buffer in z3950 format */
  349. {
  350.   any* rawDocID = NULL;
  351.   char* buf = NULL;
  352.   char* data = NULL;
  353.   long size,bytesLeft;
  354.   
  355.   if (docID == NULL)
  356.     return(NULL);
  357.     
  358.   size = writtenAnySize(DT_OriginalServer,docID->originalServer);
  359.   size += writtenAnySize(DT_OriginalDatabase,docID->originalDatabase);
  360.   size += writtenAnySize(DT_OriginalLocalID,docID->originalLocalID);
  361.   size += writtenAnySize(DT_DistributorServer,docID->distributorServer);
  362.   size += writtenAnySize(DT_DistributorDatabase,docID->distributorDatabase);
  363.   size += writtenAnySize(DT_DistributorLocalID,docID->distributorLocalID);
  364.   size += writtenNumSize(DT_CopyrightDispostion,docID->copyrightDisposition);
  365.   
  366.   data = s_malloc((size_t)(sizeof(char) * size));
  367.   
  368.   buf = data;
  369.   bytesLeft = size;
  370.   
  371.   buf = writeAny(docID->originalServer,DT_OriginalServer,buf,&bytesLeft);
  372.   buf = writeAny(docID->originalDatabase,DT_OriginalDatabase,buf,&bytesLeft);
  373.   buf = writeAny(docID->originalLocalID,DT_OriginalLocalID,buf,&bytesLeft);
  374.   buf = writeAny(docID->distributorServer,DT_DistributorServer,buf,
  375.                  &bytesLeft);
  376.   buf = writeAny(docID->distributorDatabase,DT_DistributorDatabase,
  377.                  buf,&bytesLeft);
  378.   buf = writeAny(docID->distributorLocalID,DT_DistributorLocalID,buf,
  379.                  &bytesLeft);
  380.   buf = writeNum(docID->copyrightDisposition,DT_CopyrightDispostion,
  381.                  buf,&bytesLeft);
  382.   
  383.   rawDocID = makeAny(size,data);
  384.   
  385.   return(rawDocID);
  386. }
  387.  
  388. /*---------------------------------------------------------------------------*/
  389.