home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / libfont / src / wfDlm.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.9 KB  |  326 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.  * Implements Dynamically loadable module manipulation
  21.  *
  22.  * dp Suresh <dp@netscape.com>
  23.  *
  24.  */
  25.  
  26.  
  27. #include "wfDlm.h"
  28. #include "libfont.h"
  29.  
  30. #ifdef NSPR20
  31. #ifdef XP_MAC
  32. #include "probslet.h"
  33. #else
  34. #include "obsolete/probslet.h"
  35. #endif
  36. #endif
  37.  
  38. wfDlm::wfDlm(const char *fname, const char *str)
  39. : m_state(0), m_filename(NULL), m_lib(NULL), m_dlmInterface(NULL)
  40. {
  41.     if (str && *str)
  42.     {
  43.         reconstruct(str);
  44.     }
  45.     else
  46.     {
  47.         // New file.
  48.         m_filename = CopyString(fname);
  49.         if (sync() < 0)
  50.         {
  51.             // Some error with this file.
  52.         }
  53.     }
  54. }
  55.  
  56.  
  57. int wfDlm::status(void)
  58. {
  59.     return(m_state);
  60. }
  61.  
  62. char *
  63. wfDlm::describe()
  64. {
  65.     char *buf;
  66.  
  67.     if (!m_filename || !*m_filename)
  68.     {
  69.         return (NULL);
  70.     }
  71.     
  72.     // NO I18N REQUIRED FOR THESE STRINGS
  73.     buf = PR_smprintf("%s;%lld;%lu;", m_filename, m_modifyTime, m_fileSize);
  74.     return (buf);
  75. }
  76.  
  77. int
  78. wfDlm::reconstruct(const char *str)
  79. {
  80.     char buf[1024];    // XXX this should be dynamic
  81.  
  82.     if (!str || !*str)
  83.     {
  84.         // Error. No string to reconstruct from.
  85.         m_state = -1;
  86.     }
  87.     else
  88.     {
  89.         str = wf_scanToken(str, buf, sizeof(buf), ";", 1);
  90.         m_filename = CopyString(buf);
  91.         if (isEmpty(m_filename))
  92.         {
  93.             m_state = -1;
  94.         }
  95.         if (*str) str++;
  96.  
  97.         str = wf_scanToken(str, buf, sizeof(buf), ";", 1);
  98.         if (*buf)
  99.         {
  100.             // XXX PRTime is 64 bit. This will result in data loss.
  101.             // XXX we will fix this once NSPR provides 64bit scanf
  102.             PRInt32 lo = atol(buf);
  103.             LL_I2L(m_modifyTime, lo);
  104.         }
  105.         WF_TRACEMSG(("NF: Modified time converted from string '%s' to %lld\n",
  106.             buf, m_modifyTime));
  107.         if (*str) str++;
  108.  
  109.         str = wf_scanToken(str, buf, sizeof(buf), ";", 1);
  110.         if (*buf)
  111.         {
  112.             // XXX fileSize is uint32. This should be ok.
  113.             m_fileSize = atol(buf);
  114.         }
  115.         m_state = 1;
  116.     }
  117.     return (m_state);
  118. }
  119.  
  120. wfDlm::~wfDlm()
  121. {
  122.     finalize();
  123. }
  124.  
  125.  
  126. int wfDlm::finalize(void)
  127. {
  128.     unload(1);        // this will reset m_lib and m_dlmInterface
  129.     m_state = 0;
  130.     if (m_filename)
  131.     {
  132.         delete[] m_filename;
  133.         m_filename = NULL;
  134.     }
  135.     return (0);
  136. }
  137.  
  138. //
  139. // Implementation of public methods
  140. //
  141.  
  142. const char *
  143. wfDlm::filename()
  144. {
  145.     return (m_filename);
  146. }
  147.  
  148.  
  149. int
  150. wfDlm::isChanged()
  151. {
  152.     PRFileInfo finfo;
  153.  
  154.     if (m_state == 0)
  155.         sync();
  156.  
  157.     if (m_state < 0)
  158.         return (m_state);
  159.     if (PR_GetFileInfo(m_filename, &finfo) == PR_FAILURE)
  160.         return (-1);
  161.     return (LL_NE(finfo.modifyTime, m_modifyTime) || finfo.size != m_fileSize);
  162. }
  163.  
  164.  
  165. int
  166. wfDlm::sync(void)
  167. {
  168.     PRFileInfo finfo;
  169.     if (!m_filename || !*m_filename ||
  170.         (PR_GetFileInfo(m_filename, &finfo) == PR_FAILURE))
  171.     {
  172.         m_state = -1;
  173.         return (m_state);
  174.     }
  175.  
  176.     m_state = 1;
  177.     m_modifyTime = finfo.modifyTime;
  178.     m_fileSize = finfo.size;
  179.     return (0);
  180. }
  181.  
  182. int wfDlm::load(void)
  183. {
  184.     if (!m_lib)
  185.     {
  186. #ifdef XP_MAC
  187.         const char *libPath = PR_GetLibraryPath();
  188.         char *libDir = CopyString(m_filename);
  189.         char *libName = strrchr(libDir, '/');
  190.         
  191.         if (libName != NULL)
  192.         {
  193.             libName[1] = '\0';
  194.             PR_SetLibraryPath(libDir);
  195.         }
  196.         
  197.         m_lib = PR_LoadLibrary(m_filename);
  198.         
  199.         if (libName != NULL)
  200.             PR_SetLibraryPath(libPath);
  201.         
  202.         delete[] libDir;
  203.  
  204. #else
  205.         m_lib = PR_LoadLibrary(m_filename);
  206. #endif
  207.         if (!m_lib)
  208.         {
  209.             m_state = -2;
  210.             return (-1);
  211.         }
  212.         nfdlm_OBJECT_CREATE_PROC *proc =
  213.           (nfdlm_OBJECT_CREATE_PROC *)findSymbol("dlmFactory_Create");
  214.         if (!proc)
  215.         {
  216.             WF_TRACEMSG(("NF: dlm.load(%s) couldn't find symbol dlmFactory_Create. Skipping dlm.", filename()));
  217.             unload(1);
  218.             m_state = -2;
  219.             return (-1);
  220.         }
  221.         m_dlmInterface = (*proc)(NULL);
  222.         
  223.         if (!m_dlmInterface)
  224.         {
  225.             WF_TRACEMSG(("NF: dlm.load(%s) Couldn't create m_dlmInterface object. Skipping dlm.", filename()));
  226.             unload();
  227.             m_state = -2;
  228.             return (-1);
  229.         }
  230.         else
  231.         {
  232.             WF_TRACEMSG(("NF: dlm.load(%s) successful.", filename()));
  233.         }
  234.     }
  235.     return (0);
  236. }
  237.  
  238. FARPROC wfDlm::findSymbol(const char *symbol)
  239. {
  240.     // Precondition
  241.     if (status() < 0)
  242.     {
  243.         return (NULL);
  244.     }
  245.  
  246.     if (!m_lib && load() < 0)
  247.     {
  248.         return (NULL);
  249.     }
  250.  
  251. #ifndef NSPR20
  252.     return PR_FindSymbol(symbol, m_lib);
  253. #else
  254.     return (FARPROC)PR_FindSymbol(m_lib, symbol);
  255. #endif /* NSPR20 */
  256. }
  257.  
  258. int wfDlm::unload(int force)
  259. {
  260.     int ret = 0;
  261.  
  262.     if (status() < 0)
  263.     {
  264.         return (-1);
  265.     }
  266.  
  267.     if (m_lib)
  268.     {
  269.         // Check if the dlm wants to be unloaded
  270.         if (m_dlmInterface != NULL)
  271.         {
  272.             if (force || nfdlm_OnUnload(m_dlmInterface, NULL) >= 0)
  273.             {
  274.                 nfdlm_release(m_dlmInterface, NULL);
  275.                 m_dlmInterface = NULL;
  276.             }
  277.         }
  278.         
  279.         if (m_dlmInterface == NULL)
  280.         {
  281.             ret = PR_UnloadLibrary(m_lib);
  282.             m_lib = NULL;
  283.             WF_TRACEMSG(("NF: dlm.unload(%s) returned %d.", filename(), ret));
  284.         }
  285.     }
  286.     return (ret);
  287. }
  288.  
  289. struct nffp *
  290. wfDlm::createDisplayerObject(struct nffbp *brokerDisplayerInterface)
  291. {
  292.     if (!m_lib)
  293.     {
  294.         load();
  295.     }
  296.  
  297.     if (status() < 0)
  298.     {
  299.         return (NULL);
  300.     }
  301.     
  302.     struct nffp *fp =
  303.       (struct nffp *)nfdlm_CreateObject(m_dlmInterface, &nffp_ID,
  304.                                         (void **)&brokerDisplayerInterface, 1,
  305.                                         NULL);
  306.     return (fp);
  307. }
  308.  
  309.  
  310. //
  311. // Static functions
  312. //
  313. int wfDlm::isEmpty(const char *filename)
  314. {
  315.     PRFileInfo finfo;
  316.     int ret = 0;
  317.     if (!filename || !*filename ||
  318.         (PR_GetFileInfo(filename, &finfo) == PR_FAILURE) ||
  319.         finfo.size == 0)
  320.     {
  321.         ret = 1;
  322.     }
  323.     return (ret);
  324. }
  325.  
  326.