home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / rdf / src / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  10.0 KB  |  514 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /* 
  20.    This file implements utility routines for the rdf data model.
  21.    For more information on this file, contact rjc or guha 
  22.    For more information on RDF, look at the RDF section of www.mozilla.org
  23. */
  24.  
  25. #include "utils.h"
  26. #include "nlcstore.h"
  27.  
  28.  
  29.     /* globals */
  30. PRBool rdfDBInited = 0;
  31. PLHashTable*  resourceHash = 0;
  32. RDFT gRemoteStore = 0;
  33. RDFT gSessionDB = 0;
  34.  
  35.     /* externs */
  36. extern    char    *profileDirURL;
  37.  
  38.  
  39.  
  40. RDF_Resource
  41. getMCFFrtop (char* furl)
  42. {
  43.   char* url = getBaseURL(furl);
  44.   RDF_Resource r;
  45.   r = RDF_GetResource(NULL, url, 1);
  46.   freeMem(url);
  47.   return r;
  48. }
  49.  
  50.  
  51.  
  52. RDFFile
  53. makeRDFFile (char* url, RDF_Resource top, PRBool localp)
  54. {
  55.   RDFFile ans = (RDFFile)getMem(sizeof(struct RDF_FileStruct));
  56.   /*  ans->rdf = rdf; */
  57.   ans->url = getBaseURL(url);
  58.   ans->top = top;
  59.   ans->localp = localp;
  60.   initRDFFile(ans);
  61.   return ans;
  62. }
  63.  
  64.  
  65.  
  66. void
  67. initRDFFile (RDFFile ans)
  68. {
  69.   char* url = ans->url;
  70.   ans->rtop = getMCFFrtop(url);
  71.   ans->line = (char*)getMem(RDF_BUF_SIZE);
  72.   ans->currentSlot = (char*)getMem(100);
  73.   ans->resourceList = (RDF_Resource*)getMem(200);
  74.   ans->assertionList = (Assertion*)getMem(400);
  75.   ans->resourceListSize = 50;
  76.   ans->assertionListSize = 100;
  77.   ans->holdOver = (char*)getMem(RDF_BUF_SIZE);
  78.   ans->depth = 1;
  79.   ans->lastItem = ans->stack[0] = ans->top;
  80.   ans->locked = ans->localp;
  81.   ans->lineSize = LINE_SIZE;
  82.   ans->tv = true;
  83. }
  84.  
  85.  
  86.  
  87. void
  88. addToResourceList (RDFFile f, RDF_Resource u)
  89. {
  90.   if (f->resourceListSize == f->resourceCount) {
  91.     RDF_Resource* newResourceList = (RDF_Resource*)getMem(4*(f->resourceListSize + 50));
  92.     RDF_Resource* old = f->resourceList;
  93.     memcpy((char*)newResourceList, (char*)f->resourceList, 4*f->resourceListSize);
  94.     f->resourceList = newResourceList;
  95.     f->resourceListSize = f->resourceListSize + 50;
  96.     freeMem(old);
  97.   }
  98.   *(f->resourceList + f->resourceCount++) = u;
  99. }
  100.  
  101.  
  102.  
  103. void
  104. addToAssertionList (RDFFile f, Assertion as)
  105. {
  106.   if (f->assertionListSize == f->assertionCount) {
  107.     Assertion* newAssertionList = (Assertion*)getMem(4*(f->assertionListSize + 50));
  108.     Assertion* old = f->assertionList;
  109.     memcpy((char*)newAssertionList, (char*)f->assertionList, 4*f->assertionListSize);
  110.     f->assertionList = newAssertionList;
  111.     f->assertionListSize = f->assertionListSize + 50;
  112.     freeMem(old);
  113.   }
  114.   *(f->assertionList + f->assertionCount++) = as;
  115. }
  116.  
  117.  
  118.  
  119. void
  120. ht_fprintf(PRFileDesc *file, const char *fmt, ...) 
  121. {
  122.     va_list ap;
  123.     char *buf;
  124.     va_start(ap, fmt);
  125.     buf = PR_smprintf(fmt, ap);
  126.     va_end(ap);
  127.     if(buf) {
  128.           PR_Write(file, buf, strlen(buf));
  129.     free(buf);
  130.     }
  131. }
  132.  
  133.  
  134.  
  135. void
  136. ht_rjcprintf(PRFileDesc *file, const char *fmt, const char *data)
  137. {
  138.     char    *buf;
  139.  
  140.     buf = PR_smprintf(fmt, data);
  141.     if(buf) {
  142.         PR_Write(file, buf, strlen(buf));
  143.         free(buf);
  144.     }
  145. }
  146.  
  147.  
  148.  
  149. char *
  150. makeDBURL(char* name)
  151. {
  152.   char        *ans;
  153.   size_t        s;
  154.   
  155.   if (profileDirURL == NULL) return NULL;
  156.   if ((ans = (char*) getMem(strlen(profileDirURL) + strlen(name) + 3)) != NULL) {
  157.     s = strlen(profileDirURL);
  158.     memcpy(ans, profileDirURL, s);
  159.     if (ans[s-1] != '/') {
  160.       ans[s++] = '/';
  161.     }
  162.     memcpy(&ans[s], name, strlen(name));
  163.     
  164. #ifdef    XP_WIN
  165.     if (ans[9] == '|') ans[9] = ':';
  166. #endif
  167.   }
  168.   return(ans);
  169. }
  170.  
  171.  
  172.  
  173. PLHashNumber
  174. idenHash (const void *key)
  175. {
  176.     return (PLHashNumber)key;
  177. }
  178.  
  179.  
  180.  
  181. int
  182. idenEqual (const void *v1, const void *v2)
  183. {
  184.     return (v1 == v2);
  185. }
  186.  
  187.  
  188.  
  189. PRBool
  190. inverseTV (PRBool tv)
  191. {
  192.   if (tv == true) {
  193.     return false;
  194.   } else return true;
  195. }
  196.  
  197.  
  198.  
  199. char *
  200. append2Strings (const char* str1, const char* str2)
  201. {
  202.   int32 l1 = strlen(str1);
  203.   int32 len = l1 + strlen(str2);
  204.   char* ans = (char*) getMem(len+1);
  205.   memcpy(ans, str1, l1);
  206.   memcpy(&ans[l1], str2, len-l1);
  207.   return ans;
  208. }
  209.  
  210.  
  211.  
  212. void
  213. stringAppendBase (char* dest, const char* addition)
  214. {
  215.   int32 l1 = strlen(dest);
  216.   int32 l2 = strlen(addition);
  217.   int32 l3 = charSearch('#', addition);
  218.   if (l3 != -1) l2 = l3;
  219.   memcpy(&dest[l1], addition, l2);
  220. }
  221.  
  222.  
  223.  
  224. void
  225. stringAppend (char* dest, const char* addition)
  226. {
  227.   int32 l1 = strlen(dest);
  228.   int32 l2 = strlen(addition);
  229.   memcpy(&dest[l1], addition, l2);
  230. }
  231.  
  232.  
  233.  
  234. int16
  235. charSearch (const char c, const char* data)
  236. {
  237.   char* ch = strchr(data, c);
  238.  
  239.   if (ch) {
  240.     return (ch - data);
  241.   } else {
  242.     return -1;
  243.   }
  244.  
  245. }
  246.  
  247.  
  248.  
  249. PRBool
  250. endsWith (const char* pattern, const char* uuid)
  251. {
  252.   short l1 = strlen(pattern);
  253.   short l2 = strlen(uuid);
  254.   short index;
  255.   
  256.   if (l2 < l1) return false;
  257.   
  258.   for (index = 1; index <= l1; index++) {
  259.     if (pattern[l1-index] != uuid[l2-index]) return false;
  260.   }
  261.   
  262.   return true;
  263. }
  264.  
  265.  
  266.  
  267. PR_PUBLIC_API(PRBool)
  268. startsWith (const char* pattern, const char* uuid)
  269. {
  270.   short l1 = strlen(pattern);
  271.   short l2 = strlen(uuid);
  272.   if (l2 < l1) return false;
  273.   return strncmp(pattern, uuid, l1)  == 0;
  274. }
  275.  
  276.  
  277.  
  278. PRBool
  279. substring (const char* pattern, const char* data)
  280. {
  281.   char *p = strstr(data, pattern);
  282.   return p != NULL;
  283. }
  284.  
  285.  
  286.  
  287. int16
  288. revCharSearch (const char c, const char* data)
  289. {
  290.   char *p = strrchr(data, c);
  291.   return p ? p-data : -1;
  292. }
  293.  
  294.  
  295.  
  296. PRBool
  297. urlEquals (const char* url1, const char* url2)
  298. {
  299.   int16 n1 = charSearch('#', url1);
  300.   int16 n2 = charSearch('#',  url2);
  301.   if ((n1 == -1) && (n2 == -1)) {    
  302.     return (strcmp(url1, url2) == 0);
  303.   } else if ((n2 == -1) && (n1 > 0)) {
  304.     return ((strlen(url2) == (size_t)(n1)) && (strncmp(url1, url2, n1) == 0));
  305.   } else if ((n1 == -1) && (size_t) (n2 > 0)) {
  306.     return ((strlen(url1) == (size_t)(n2)) && (strncmp(url1, url2, n2) == 0));
  307.   } else return 0;
  308. }
  309.  
  310.  
  311.  
  312. PRBool
  313. isSeparator (RDF_Resource r)
  314. {
  315.   return startsWith("separator", resourceID(r));
  316. }
  317.  
  318.  
  319.  
  320. char *
  321. getBaseURL (const char* url)
  322. {
  323.   int n = charSearch('#' , url);
  324.   char* ans;
  325.   if (n == -1) return copyString(url);
  326.   if (n == 0) return NULL;
  327.   ans = getMem(n+1);
  328.   memcpy(ans, url, n);
  329.   return ans;
  330. }
  331.  
  332.  
  333.  
  334. void
  335. setContainerp (RDF_Resource r, PRBool val)
  336. {
  337.   if (val) {
  338.     r->flags |= CONTAINER_FLAG;
  339.   } else {
  340.     r->flags &= (~CONTAINER_FLAG);
  341.   }
  342. }
  343.  
  344.  
  345.  
  346. PRBool
  347. containerp (RDF_Resource r)
  348. {
  349.   return (r->flags & CONTAINER_FLAG);
  350. }
  351.  
  352.  
  353.  
  354. void
  355. setLockedp (RDF_Resource r, PRBool val)
  356. {
  357.   if (val) {
  358.     r->flags |= LOCKED_FLAG;
  359.   } else {
  360.     r->flags &= (~LOCKED_FLAG);
  361.   }
  362. }
  363.  
  364.  
  365.  
  366. PRBool
  367. lockedp (RDF_Resource r)
  368. {
  369.   return (r->flags & LOCKED_FLAG);
  370. }
  371.  
  372.  
  373.  
  374. uint8
  375. resourceType (RDF_Resource r)
  376. { return r->type;
  377. }
  378.  
  379.  
  380.  
  381. void
  382. setResourceType (RDF_Resource r, uint8 val)
  383. {
  384.   r->type = val;
  385. }
  386.  
  387.  
  388.  
  389. char *
  390. resourceID(RDF_Resource r)
  391. {
  392.   return r->url;
  393. }
  394.  
  395.  
  396.  
  397. char *
  398. makeResourceName (RDF_Resource node)
  399. {
  400.   char* name;
  401.   name =  resourceID(node);
  402.   if (startsWith("http:", name)) return copyString(&name[7]);
  403.   if (startsWith("file:", name)) return copyString(&name[8]);
  404.   return copyString(name);
  405. }
  406.  
  407.  
  408.  
  409. PR_PUBLIC_API(char *)
  410. RDF_GetResourceName(RDF rdf, RDF_Resource node)
  411. {
  412.   char* name = RDF_GetSlotValue(rdf, node, gCoreVocab->RDF_name, RDF_STRING_TYPE, false, true);
  413.   if (name != NULL) return name;
  414.   return makeResourceName(node);
  415. }
  416.  
  417.  
  418.  
  419. PR_PUBLIC_API(RDF_Resource)
  420. RDFUtil_GetFirstInstance (RDF_Resource type, char* defaultURL)
  421. {
  422.   RDF_Resource bmk = nlocalStoreGetSlotValue(gLocalStore, type,
  423.                          gCoreVocab->RDF_instanceOf,
  424.                          RDF_RESOURCE_TYPE, true, true);
  425.   if (bmk == NULL) {
  426.     bmk = RDF_GetResource(NULL, defaultURL, 1);
  427.     nlocalStoreAssert(gLocalStore, bmk, gCoreVocab->RDF_instanceOf, 
  428.               type, RDF_RESOURCE_TYPE, 1);
  429.   }
  430.   return bmk;
  431. }
  432.  
  433.  
  434.  
  435. PR_PUBLIC_API(void)
  436. RDFUtil_SetFirstInstance (RDF_Resource type, RDF_Resource item)
  437. {
  438.   RDF_Resource bmk = nlocalStoreGetSlotValue(gLocalStore, type,
  439.                          gCoreVocab->RDF_instanceOf,
  440.                          RDF_RESOURCE_TYPE, true, true);
  441.   if (bmk) {
  442.     nlocalStoreUnassert(gLocalStore, bmk,  gCoreVocab->RDF_instanceOf, 
  443.             type,     RDF_RESOURCE_TYPE);
  444.   }
  445.   if (item) {
  446.     nlocalStoreAssert(gLocalStore, item, gCoreVocab->RDF_instanceOf, 
  447.             type, RDF_RESOURCE_TYPE, true);
  448.   }
  449. }
  450.  
  451.  
  452.  
  453. PR_PUBLIC_API(RDF_Resource)
  454. RDFUtil_GetQuickFileFolder()
  455. {
  456.   return RDFUtil_GetFirstInstance(gNavCenter->RDF_BookmarkFolderCategory, "NC:Bookmarks");
  457. }
  458.  
  459.  
  460.  
  461. PR_PUBLIC_API(void)
  462. RDFUtil_SetQuickFileFolder(RDF_Resource container)
  463. {
  464.   RDFUtil_SetFirstInstance(gNavCenter->RDF_BookmarkFolderCategory, container);
  465. }
  466.  
  467.  
  468.  
  469. PR_PUBLIC_API(RDF_Resource)
  470. RDFUtil_GetPTFolder()
  471. {
  472.  return RDFUtil_GetFirstInstance(gNavCenter->RDF_PersonalToolbarFolderCategory, "PersonalToolbar");
  473. }
  474.  
  475.  
  476.  
  477. PR_PUBLIC_API(void)
  478. RDFUtil_SetPTFolder(RDF_Resource container)
  479. {
  480.   RDFUtil_SetFirstInstance( gNavCenter->RDF_PersonalToolbarFolderCategory, container);
  481. }
  482.  
  483.  
  484.  
  485. PR_PUBLIC_API(RDF_Resource)
  486. RDFUtil_GetNewBookmarkFolder()
  487. {
  488.   return RDFUtil_GetFirstInstance(gNavCenter->RDF_NewBookmarkFolderCategory, "NC:Bookmarks");
  489. }
  490.  
  491.  
  492.  
  493. PR_PUBLIC_API(void)
  494. RDFUtil_SetNewBookmarkFolder(RDF_Resource container)
  495. {
  496.   RDFUtil_SetFirstInstance(gNavCenter->RDF_NewBookmarkFolderCategory, container);
  497. }
  498.  
  499.  
  500.  
  501. PR_PUBLIC_API(RDF_Resource)
  502. RDFUtil_GetDefaultSelectedView()
  503. {
  504.   return RDFUtil_GetFirstInstance(gNavCenter->RDF_DefaultSelectedView, "selectedView");
  505. }
  506.  
  507.  
  508.  
  509. PR_PUBLIC_API(void)
  510. RDFUtil_SetDefaultSelectedView(RDF_Resource container)
  511. {
  512.   RDFUtil_SetFirstInstance(gNavCenter->RDF_DefaultSelectedView, container);
  513. }
  514.