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

  1.  
  2. /* these are the methods for the DocID struct
  3.  * -brewster
  4.  */
  5.  
  6. #include <ctype.h>
  7. #include "docid.h"
  8. #include "irfileio.h"
  9. #include "cutil.h"
  10. #include "cdialect.h"
  11.  
  12. /*---------------------------------------------------------------------------*/
  13.  
  14. /* get the server slot */
  15. any* GetServer(doc)
  16. DocID* doc;
  17. {
  18.     if(NULL != doc->distributorServer)
  19.         return(doc->distributorServer);
  20.     else return(doc->originalServer);
  21. }
  22.  
  23. /*---------------------------------------------------------------------------*/
  24.  
  25. any* GetDatabase(doc)
  26. DocID* doc;
  27. {
  28.     if(NULL != doc->distributorDatabase)
  29.         return(doc->distributorDatabase);
  30.     else return(doc->originalDatabase);
  31. }
  32.  
  33. /*---------------------------------------------------------------------------*/
  34.  
  35. any* GetLocalID(doc)
  36. DocID* doc;
  37. {
  38.     if(NULL != doc->distributorLocalID)
  39.         return(doc->distributorLocalID);
  40.     else return(doc->originalLocalID);
  41. }
  42.  
  43. /*---------------------------------------------------------------------------*/
  44.  
  45. long GetCopyrightDisposition(doc)
  46. DocID* doc;
  47. {
  48.     return(doc->copyrightDisposition);
  49. }
  50.  
  51. /*---------------------------------------------------------------------------*/
  52.  
  53. /* returns FALSE if it loses, TRUE if it wins */
  54. long ReadDocID(doc,file)
  55. DocID* doc;
  56. FILE* file;
  57. {
  58.     char temp_string[MAX_SYMBOL_SIZE];
  59.     if(FALSE == CheckStartOfStruct("doc-id", file)){
  60.         return(FALSE);
  61.     }
  62.     doc->originalServer = NULL;
  63.     doc->originalDatabase = NULL;
  64.     doc->originalLocalID = NULL;
  65.     doc->distributorServer = NULL;
  66.     doc->distributorDatabase = NULL;
  67.     doc->distributorLocalID = NULL;
  68.     doc->copyrightDisposition = COPY_WITHOUT_RESTRICTION;
  69.     
  70.     while(TRUE){
  71.         long result = ReadSymbol(temp_string, file, 
  72.                      (long)MAX_SYMBOL_SIZE);
  73.         
  74.         if(FALSE == result){ 
  75.             return(FALSE);
  76.         }
  77.         if(END_OF_STRUCT_OR_LIST == result){
  78.             break;
  79.         }
  80.         if(0 == strcmp(temp_string, ":original-server")){
  81.             any* an_any = (any*)s_malloc(sizeof(any));
  82.             ReadAny(an_any, file);
  83.             doc->originalServer = an_any;
  84.         }
  85.         else if(0 == strcmp(temp_string, ":original-database")){
  86.             any* an_any = (any*)s_malloc(sizeof(any));
  87.             ReadAny(an_any, file);
  88.             doc->originalDatabase = an_any;
  89.         }
  90.         else if(0 == strcmp(temp_string, ":original-local-id")){
  91.             any* an_any = (any*)s_malloc(sizeof(any));
  92.             ReadAny(an_any, file);
  93.             doc->originalLocalID = an_any;
  94.         }
  95.         else if(0 == strcmp(temp_string, ":distributor-server")){
  96.             any* an_any = (any*)s_malloc(sizeof(any));
  97.             ReadAny(an_any, file);
  98.             doc->distributorServer = an_any;
  99.         }
  100.         else if(0 == strcmp(temp_string, ":distributor-database")){
  101.             any* an_any = (any*)s_malloc(sizeof(any));
  102.             ReadAny(an_any, file);
  103.             doc->distributorDatabase = an_any;
  104.         }
  105.         else if(0 == strcmp(temp_string, ":distributor-local-id")){
  106.             any* an_any = (any*)s_malloc(sizeof(any));
  107.             ReadAny(an_any, file);
  108.             doc->distributorLocalID = an_any;
  109.         }
  110.         else if(0 == strcmp(temp_string, ":copyright-disposition"))
  111.              ReadLong(file,&doc->copyrightDisposition);
  112.         else{
  113.           SkipObject(file);
  114.         }
  115.     }
  116.     return(TRUE);
  117. }
  118.  
  119.  
  120. /*---------------------------------------------------------------------------*/
  121.  
  122. /* this writes a CDocID to a stream */
  123. long WriteDocID(doc,file)
  124. DocID* doc;
  125. FILE* file;
  126. {
  127.     WriteNewline(file);
  128.     WriteStartOfStruct("doc-id", file);
  129.     if (NULL != doc->originalServer){
  130.         WriteNewline(file);
  131.         WriteSymbol(":original-server", file); WriteAny(doc->originalServer, file);
  132.         }
  133.     if (NULL != doc->originalDatabase){
  134.         WriteNewline(file);
  135.         WriteSymbol(":original-database", file); WriteAny(doc->originalDatabase, file);
  136.         }
  137.     if (NULL != doc->originalLocalID){
  138.         WriteNewline(file);
  139.         WriteSymbol(":original-local-id", file); WriteAny(doc->originalLocalID, file);
  140.         }
  141.     if (NULL != doc->distributorServer){
  142.         WriteNewline(file);
  143.         WriteSymbol(":distributor-server", file); WriteAny(doc->distributorServer, file);
  144.         }
  145.     if (NULL != doc->distributorDatabase){
  146.         WriteNewline(file);
  147.         WriteSymbol(":distributor-database", file); 
  148.         WriteAny(doc->distributorDatabase, file);
  149.         }
  150.     if (NULL != doc->distributorLocalID){
  151.         WriteNewline(file);
  152.         WriteSymbol(":distributor-local-id", file); 
  153.         WriteAny(doc->distributorLocalID, file);
  154.         }
  155.     WriteNewline(file);
  156.     WriteSymbol(":copyright-disposition", file);
  157.     WriteLong(doc->copyrightDisposition, file);
  158.     return(WriteEndOfStruct(file));
  159. }
  160.  
  161. /*---------------------------------------------------------------------------*/
  162.  
  163.     static Boolean safeCmp _AP((any* a1,any* a2));
  164.  
  165.     static Boolean safeCmp(a1,a2)
  166.         any* a1;
  167.         any* a2;
  168.         {
  169.         /* compare 2 any's, either of which may be NULL */
  170.         if (a1 == NULL && a2 == NULL)
  171.               return(true);
  172.         else if (a1 == NULL || a2 == NULL)
  173.               return(false);
  174.         else if (a1->size != a2->size)
  175.               return(false);
  176.         else if (memcmp(a1->bytes,a2->bytes,(size_t)a1->size) == 0)
  177.               return(true);
  178.         else
  179.             return(false);
  180.         }
  181.  
  182. /*---------------------------------------------------------------------------*/
  183.  
  184. Boolean cmpDocIDs(d1,d2)
  185. DocID* d1;
  186. DocID* d2;
  187. {
  188.     if (safeCmp(d1->originalServer,d2->originalServer) &&
  189.         safeCmp(d1->originalDatabase,d2->originalDatabase) &&
  190.         safeCmp(d1->originalLocalID,d2->originalLocalID) &&
  191.         safeCmp(d1->distributorServer,d2->distributorServer) &&
  192.         safeCmp(d1->distributorDatabase,d2->distributorDatabase) &&
  193.         safeCmp(d1->distributorLocalID,d2->distributorLocalID) &&
  194.         d1->copyrightDisposition == d2->copyrightDisposition)
  195.         return(true);
  196.     else
  197.         return(false); 
  198.     }
  199.     
  200. /*---------------------------------------------------------------------------*/
  201.  
  202. DocID* 
  203. makeDocID()
  204. {
  205.   return((DocID*)s_malloc((size_t)sizeof(DocID)));
  206. }
  207.  
  208. /*---------------------------------------------------------------------------*/
  209.  
  210. void freeDocID(doc)
  211. DocID* doc;
  212. {
  213.     freeAny(doc->originalServer);
  214.     freeAny(doc->originalDatabase);
  215.     freeAny(doc->originalLocalID);
  216.     freeAny(doc->distributorServer);
  217.     freeAny(doc->distributorDatabase);
  218.     freeAny(doc->distributorLocalID);
  219.     s_free(doc);
  220. }
  221.  
  222. /*---------------------------------------------------------------------------*/
  223.  
  224. #define DT_OriginalServer         (data_tag)1
  225. #define DT_OriginalDatabase     (data_tag)2
  226. #define DT_OriginalLocalID        (data_tag)3
  227. #define DT_DistributorServer    (data_tag)4
  228. #define DT_DistributorDatabase    (data_tag)5
  229. #define DT_DistributorLocalID    (data_tag)6
  230. #define DT_CopyrightDispostion    (data_tag)7
  231.  
  232. DocID*
  233. docIDFromAny(rawDocID)
  234. any* rawDocID;
  235. /* read from a z3950 docid to a docid structure */
  236. {
  237.   DocID* docID = makeDocID();
  238.   char* buf = rawDocID->bytes;
  239.   
  240.   while ((buf - rawDocID->bytes) < rawDocID->size)
  241.    { data_tag tag = peekTag(buf);
  242.      switch (tag)
  243.       { case DT_OriginalServer:
  244.           buf = readAny(&(docID->originalServer),buf);
  245.           break;
  246.         case DT_OriginalDatabase:
  247.           buf = readAny(&(docID->originalDatabase),buf);
  248.           break;
  249.         case DT_OriginalLocalID:
  250.           buf = readAny(&(docID->originalLocalID),buf);
  251.           break;
  252.         case DT_DistributorServer:
  253.           buf = readAny(&(docID->distributorServer),buf);
  254.           break;
  255.         case DT_DistributorDatabase:
  256.           buf = readAny(&(docID->distributorDatabase),buf);
  257.           break;
  258.         case DT_DistributorLocalID:
  259.           buf = readAny(&(docID->distributorLocalID),buf);
  260.           break;
  261.         case DT_CopyrightDispostion:
  262.           buf = readNum(&(docID->copyrightDisposition),buf);
  263.           break;
  264.         default:
  265.           freeDocID(docID);
  266.           return(NULL);
  267.       };
  268.    }
  269.   
  270.   return(docID);
  271. }
  272.  
  273. /*---------------------------------------------------------------------------*/
  274.  
  275. any*
  276. anyFromDocID(docID)
  277. DocID* docID;
  278. /* write a docid structure to a buffer in z3950 format */
  279. {
  280.   any* rawDocID = NULL;
  281.   char* buf = NULL;
  282.   char* data = NULL;
  283.   long size,bytesLeft;
  284.   
  285.   if (docID == NULL)
  286.     return(NULL);
  287.     
  288.   size = writtenAnySize(DT_OriginalServer,docID->originalServer);
  289.   size += writtenAnySize(DT_OriginalDatabase,docID->originalDatabase);
  290.   size += writtenAnySize(DT_OriginalLocalID,docID->originalLocalID);
  291.   size += writtenAnySize(DT_DistributorServer,docID->distributorServer);
  292.   size += writtenAnySize(DT_DistributorDatabase,docID->distributorDatabase);
  293.   size += writtenAnySize(DT_DistributorLocalID,docID->distributorLocalID);
  294.   size += writtenNumSize(DT_CopyrightDispostion,docID->copyrightDisposition);
  295.   
  296.   data = s_malloc((size_t)(sizeof(char) * size));
  297.   
  298.   buf = data;
  299.   bytesLeft = size;
  300.   
  301.   buf = writeAny(docID->originalServer,DT_OriginalServer,buf,&bytesLeft);
  302.   buf = writeAny(docID->originalDatabase,DT_OriginalDatabase,buf,&bytesLeft);
  303.   buf = writeAny(docID->originalLocalID,DT_OriginalLocalID,buf,&bytesLeft);
  304.   buf = writeAny(docID->distributorServer,DT_DistributorServer,buf,
  305.                  &bytesLeft);
  306.   buf = writeAny(docID->distributorDatabase,DT_DistributorDatabase,
  307.                  buf,&bytesLeft);
  308.   buf = writeAny(docID->distributorLocalID,DT_DistributorLocalID,buf,
  309.                  &bytesLeft);
  310.   buf = writeNum(docID->copyrightDisposition,DT_CopyrightDispostion,
  311.                  buf,&bytesLeft);
  312.   
  313.   rawDocID = makeAny(size,data);
  314.   
  315.   return(rawDocID);
  316. }
  317.  
  318. /*---------------------------------------------------------------------------*/
  319.  
  320.  
  321.