home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / xpcom / tests / RegFactory.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  2.9 KB  |  110 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. #include <iostream.h>
  20. #include "plstr.h"
  21. #include "prlink.h"
  22. #include "nsRepository.h"
  23.  
  24. static PRBool gUnreg = PR_FALSE;
  25.  
  26. PRLibrary *GetLib(const char *path) {
  27.   return PR_LoadLibrary(path);
  28. }
  29.  
  30. int Register(const char *path) 
  31. {
  32.   int res = NS_OK;
  33.   PRLibrary *instance = GetLib(path);
  34.   if (instance != NULL) {
  35.     nsRegisterProc proc = (nsRegisterProc) PR_FindSymbol(instance,
  36.                                                          "NSRegisterSelf");
  37.     if (proc != NULL) {
  38.       res = proc(path);
  39.     }
  40.     PR_UnloadLibrary(instance);
  41.   } else {
  42.     res = NS_ERROR_FACTORY_NOT_LOADED;
  43.   }
  44.  
  45.   return res;
  46. }
  47.  
  48. int Unregister(const char *path) 
  49. {
  50.   int res = NS_OK;
  51.   PRLibrary *instance = GetLib(path);
  52.   if (instance != NULL) {
  53.     nsUnregisterProc proc = (nsUnregisterProc) PR_FindSymbol(instance,
  54.                                                            "NSUnregisterSelf");
  55.     if (proc != NULL) {
  56.       res = proc(path);
  57.     }
  58.     PR_UnloadLibrary(instance);
  59.   } else {
  60.     res = NS_ERROR_FACTORY_NOT_LOADED;
  61.   }
  62.  
  63.   return res;
  64. }
  65.  
  66. int ProcessArgs(int argc, char *argv[])
  67. {
  68.   int i = 1;
  69.   nsresult res;
  70.  
  71.   while (i < argc) {
  72.     if (argv[i][0] == '-') {
  73.       int j;
  74.       for (j = 1; argv[i][j] != '\0'; j++) {
  75.         switch (argv[i][j]) {
  76.         case 'u':
  77.           gUnreg = PR_TRUE;
  78.           break;
  79.         default:
  80.           cerr << "Unknown option '" << argv[i][j] << "'\n";
  81.         }
  82.       }
  83.       i++;
  84.     } else {
  85.       if (gUnreg == PR_TRUE) {
  86.         res = Unregister(argv[i]);
  87.         if (NS_SUCCEEDED(res)) {
  88.           cout << "Successfully unregistered: " << argv[i] << "\n";
  89.         } else {
  90.           cerr << "Unregister failed (" << res << "): " << argv[i] << "\n";
  91.         }
  92.       } else {
  93.         res = Register(argv[i]);
  94.         if (NS_SUCCEEDED(res)) {
  95.           cout << "Successfully registered: " << argv[i] << "\n";
  96.         } else {
  97.           cerr << "Register failed (" << res << "): " << argv[i] << "\n";
  98.         }
  99.       }
  100.       i++;
  101.     }
  102.   }
  103.   return 0;
  104. }
  105.  
  106. int main(int argc, char *argv[])
  107. {
  108.   return ProcessArgs(argc, argv);
  109. }
  110.