home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / db02_src.zip / nserver.cc < prev    next >
C/C++ Source or Header  |  1993-11-05  |  4KB  |  173 lines

  1. /**************************************************************************
  2.  * Source Id :
  3.  *
  4.  * $Id: nserver.cc,v 1.18 1993/11/04 14:57:06 kevinl Exp $
  5.  *-------------------------------------------------------------------------
  6.  * Project Notes :
  7.  *
  8.  *  Diamond Base
  9.  *  ============
  10.  *      A solid database implementation, spurred on by the continuing
  11.  *  Metal (Lead) Base saga.
  12.  *
  13.  *  Project Team :
  14.  *        A. Davison
  15.  *        K. Lentin
  16.  *        D. Platt
  17.  *
  18.  *    Project Commenced : 05-02-1993
  19.  *
  20.  *-------------------------------------------------------------------------
  21.  *  Module Notes :
  22.  *
  23.  *    Name Server Class : This will return the path leading to the database
  24.  *   files related a relation by its name.
  25.  *
  26.  *  Original Author :
  27.  *        Andy
  28.  *
  29.  *-------------------------------------------------------------------------
  30.  * Revision History:
  31.  *
  32.  * $Log: nserver.cc,v $
  33. // Revision 1.18  1993/11/04  14:57:06  kevinl
  34. // Added allowAll and isOpen and took care of unnamed relations "" being
  35. // added to the array
  36. //
  37. // Revision 1.17  1993/10/18  11:21:08  kevinl
  38. // Allow use of "" as a database config file
  39. //
  40. // Revision 1.16  1993/07/11  08:46:18  kevinl
  41. // Minor compile time thingos
  42. //
  43. // Revision 1.15  1993/07/11  08:19:14  kevinl
  44. // Stopped NameServer exiting when config file is empty/missing
  45. //
  46. // Revision 1.14  1993/06/23  05:21:22  kevinl
  47. // Mallocs are now in angular brackets
  48. //
  49. // Revision 1.13  1993/05/26  01:01:39  kevinl
  50. // MALLOC_H_MISSING
  51. //
  52. // Revision 1.12  1993/05/11  14:44:50  kevinl
  53. // Added version number output
  54. //
  55. // Revision 1.11  1993/05/06  04:00:19  kevinl
  56. // SASC define for malloc.h
  57. //
  58. // Revision 1.10  1993/05/03  01:33:03  kevinl
  59. // Cosmetic (mainly) changes for CC
  60. //
  61. // Revision 1.9  1993/04/27  07:03:31  kevinl
  62. // Comments
  63. //
  64. // Revision 1.8  1993/04/15  04:21:52  kevinl
  65. // Moved malloc.h
  66. //
  67. // Revision 1.7  1993/04/07  08:29:46  kevinl
  68. // NameServer now displays correct dbc filename when it's not found
  69. //
  70. // Revision 1.6  1993/03/29  13:09:23  kevinl
  71. // Now outputs requested relation name not array pointer
  72. //
  73. // Revision 1.5  1993/03/29  08:20:09  darrenp
  74. // Added new malloc library support
  75. //
  76. // Revision 1.4  1993/03/25  22:21:12  davison
  77. // queryName now takes a const char*.
  78. //
  79. // Revision 1.3  1993/03/20  15:11:47  davison
  80. // Now returns a name ID, so that only one dbobject for each relation is
  81. // available at any time.
  82. //
  83. // Revision 1.2  1993/02/22  05:39:54  davison
  84. // Fixed RCS Header.
  85. //
  86.  **************************************************************************/
  87.  
  88. #include <stdlib.h>
  89. #ifndef MALLOC_H_MISSING
  90. #include <malloc.h>
  91. #endif
  92. #include <nserver.h>
  93. #include <iostream.h>
  94. #include <fstream.h>
  95.  
  96. char* TNameServer::verStr(void)
  97. {
  98.     return "$Id: nserver.cc,v 1.18 1993/11/04 14:57:06 kevinl Exp $";
  99. }
  100.  
  101. TNameServer::TNameServer(char* name)
  102. {
  103.     nsOpen = false;
  104.     char dbcName[200];
  105.     if (name)
  106.         strcpy(dbcName,name);
  107.     else
  108.         strcpy(dbcName,"ns.dbc"); // Default: (n)ame(s)erver.(d)ata(b)ase (c)onfig
  109.  
  110.     allowAll=false;
  111.     if (!strlen(dbcName)) {
  112.         NumNames=0;
  113.         allowAll = true;
  114.         nsOpen = true;
  115.         return;
  116.     }
  117.  
  118.     ifstream is(dbcName);
  119.  
  120.     if (!is.good()) {
  121.         dbErr(db_nfound, "database configuration file");
  122.         NumNames = 0;
  123.         return;
  124.     }
  125.  
  126.     // File is of the form:
  127.     // relation=path
  128.     // .
  129.     // .
  130.     for(int i=0;is.good()&&i<MAX_RELATIONS;i++) {
  131.         is.getline(RelName[i],40,'=');
  132.         is.getline(PathName[i],200,'\n');
  133.     }
  134.     NumNames = i;
  135.     nsOpen = true;
  136. }
  137.  
  138. // Fetch a path for a particular relation database.
  139.  
  140. char* TNameServer::queryName(const char *name, long& nameId)
  141. {
  142.     int i;
  143.     char* data=0;
  144.  
  145.     for(i=0;i<NumNames;i++) 
  146.     {
  147.         if (!strcmp(RelName[i],name)) 
  148.         {
  149.             data = new char[strlen(PathName[i])+1];
  150.             strcpy(data,PathName[i]);
  151.             nameId = (long)i;
  152.             break;
  153.         }
  154.     }
  155.     if (!data) {
  156.         if (allowAll)
  157.         {
  158.             strcpy(RelName[i=NumNames],name);
  159.             strcpy(PathName[NumNames],".");
  160.             NumNames++;
  161.             data = new char[strlen(PathName[i])+1];
  162.             strcpy(data,PathName[i]);
  163.             nameId = (long)i;
  164.         }
  165.         else
  166.         {
  167.             cerr << "Relation name server lookup failed for '" << name <<"'" << endl;
  168.             exit(1);
  169.         }
  170.     }
  171.     return data;
  172. }
  173.