/**************************************************************************
 * Source Id :
 *
 * $Id: nserver.cpp,v 1.3 2001/04/20 10:15:01 georgetasker Exp $
 *-------------------------------------------------------------------------
 * Project Notes :
 *
 *  Diamond Base
 *  ============
 *      A solid database implementation, spurred on by the continuing
 *  Metal (Lead) Base saga.
 *
 *  Project Team :
 *        A. Davison
 *        K. Lentin
 *        D. Platt
 *
 *	Project Commenced : 05-02-1993
 *
 *-------------------------------------------------------------------------
 *  Module Notes :
 *
 *    Name Server Class : This will return the path leading to the database
 *   files related a relation by its name.
 *
 *  Original Author :
 *		Andy
 *
 **************************************************************************/

// FOR NOW::
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#include <stdlib.h>

#include <diamond/dbconf.h>

#if HAVE_MALLOC_H && !MALLOC_H_INCLUDED
extern "C" {
#include <malloc.h>
}
#define MALLOC_H_INCLUDED 1
#endif
#include <diamond/nserver.h>
#include <iostream.h>
#include <fstream.h>
#include <diamond/dberr.h>

const char*
TNameServer::verStr(void) const
{
	return "$Id: nserver.cpp,v 1.3 2001/04/20 10:15:01 georgetasker Exp $";
}

TNameServer::TNameServer()
{
	NumNames=0;
	allowAll = false;
	nsOpen = false;
	dbErr(db_ok);
	return;
}

int TNameServer::loadConfig(const char* dbcName)
{
	if (strcmp(dbcName,"") == 0)
	{
		NumNames = 0;
		allowAll = true;
		nsOpen = true;
		dbErr(db_ok);
		return 0;
	}
	ifstream is(dbcName);

	if (!is.good()) {
		dbErr(db_nfound, "database configuration file");
		NumNames = 0;
		return db_badName;
	}

	// File is of the form:
	// relation path
	// .
	// .

	dbString relName, pathName;
	while(is.good()) 
	{
		is >> relName >> pathName;
		char c;
		do {
			c = (char) is.get();
			if (!is.good() || c == '\n')
				break;
			else if (!isspace(c))
			{       
				//
				// Not good practice for library to write to standard i/o - need a better
				// way of reporting this.  It should probably be an error.
				//
				// cerr << "Warning: spurious non whitespace on end of line in config file" << endl;
				//cerr << " character seen was #" << (int)c << endl;
			}
		} while (isspace(c));
                              
		if (relName != dbString("") && pathName != dbString(""))
		{
			if (!tree.insert(new NameNode(pathName, relName)))
			{
				// Insert failed. What to do ?
				dbErr(db_alreadyregistered,(char*)relName);
				return db_alreadyregistered;
			} 
			relName = "";
			pathName = "";
			NumNames++;
		}
	}
	nsOpen = true;
	dbErr(db_ok);
	return 0;
}
// Fetch a path for a particular relation database.

char* TNameServer::queryName(const char *name, int32& nameId)
{
	NameNode  info("",name);
	DBBSTNode* node = tree.lookup((DBBSTNode*)&info);
	if (!node) {
		if (allowAll) {
			tree.insert(node = new NameNode(".", name));
		} else {
			return 0;
		}
	}
	nameId = (int32)node;
	return (char*)((NameNode*)node)->pathName;
}

///////////////////////////////////////////////////////////////////////////
// Revision History:
//
// $Log: nserver.cpp,v $
// Revision 1.3  2001/04/20 10:15:01  georgetasker
// Cygwin compilation fixes, compilation speed enhancements
// Code modified to get rid of some compiler warnings
//
// Revision 1.2  2001/04/19 21:12:54  georgetasker
// cygwin compilation fixes
//
// Revision 1.1  2001/04/04 12:05:49  aleksandras
// forgotten diamond .cpps
//
// Revision 1.36  1996/08/20  14:58:49  daz
// MSVC NT change.
//
// Revision 1.35  1996/03/05  22:04:29  dmb
// removed ^M.
//
// Revision 1.34  1996/03/05  21:42:35  dmb
// Preliminary modifications to get Dos/Windows 0.4 portability.
//
// Revision 1.33  1996/02/01  23:16:19  dmb
// int32.
//
// Revision 1.32  1995/10/04  02:18:16  davison
// iFixed the "spurious whitespace" message by checking for EOF
// properly.
//
// Revision 1.31  1995/03/08 00:40:28  kevinl
// Oops. curt-n-paste strikes again. s/cout/is/
//
// Revision 1.30  1994/12/22  03:57:15  kevinl
// OK, so I lied. The previous fix should have been compiled before I checked
// it in. char c moved out of the while loop
//
// Revision 1.29  1994/12/13  06:48:41  kevinl
// Fixed parsing. Well, did something to it then.
//
// Revision 1.28  1994/12/12  03:50:58  kevinl
// Major parsing problems in config file (almost) fixed
//
// Revision 1.27  1994/05/15  04:58:56  kevinl
// Type fixups.
//
// Revision 1.26  1994/02/10  00:22:33  davison
// Fixed return codes fdor or loadConfig
//
// Revision 1.25  1994/02/03  06:01:42  kevinl
// Moved include files to diamond/ and fixed HAVE_MALLOC_H
//
// Revision 1.24  1994/01/28  04:34:14  davison
// No longer returns a copy of the path requested, only a pointer to it's
// string
//
// Revision 1.23  1994/01/28  04:26:04  davison
// Fixed the "return a copy" problem in queryName.
//
// Revision 1.22  1994/01/24  03:02:42  davison
// NameServer now uses a binary search tree and requires a call to
// loadConfig to be initialised.
//
// Revision 1.21  1993/12/23  22:58:48  kevinl
// dbErr uses dbString, autoconf stuff, moved logs, added const and size_t
//
// Revision 1.20  1993/11/09  04:12:09  kevinl
// Fixed includes
// moved logs
//
// Revision 1.19  1993/11/09  01:31:32  kevinl
// Might fix the error reporting
//
// Revision 1.18  1993/11/04  14:57:06  kevinl
// Added allowAll and isOpen and took care of unnamed relations "" being
// added to the array
//
// Revision 1.17  1993/10/18  11:21:08  kevinl
// Allow use of "" as a database config file
//
// Revision 1.16  1993/07/11  08:46:18  kevinl
// Minor compile time thingos
//
// Revision 1.15  1993/07/11  08:19:14  kevinl
// Stopped NameServer exiting when config file is empty/missing
//
// Revision 1.14  1993/06/23  05:21:22  kevinl
// Mallocs are now in angular brackets
//
// Revision 1.13  1993/05/26  01:01:39  kevinl
// MALLOC_H_MISSING
//
// Revision 1.12  1993/05/11  14:44:50  kevinl
// Added version number output
//
// Revision 1.11  1993/05/06  04:00:19  kevinl
// SASC define for malloc.h
//
// Revision 1.10  1993/05/03  01:33:03  kevinl
// Cosmetic (mainly) changes for CC
//
// Revision 1.9  1993/04/27  07:03:31  kevinl
// Comments
//
// Revision 1.8  1993/04/15  04:21:52  kevinl
// Moved malloc.h
//
// Revision 1.7  1993/04/07  08:29:46  kevinl
// NameServer now displays correct dbc filename when it's not found
//
// Revision 1.6  1993/03/29  13:09:23  kevinl
// Now outputs requested relation name not array pointer
//
// Revision 1.5  1993/03/29  08:20:09  darrenp
// Added new malloc library support
//
// Revision 1.4  1993/03/25  22:21:12  davison
// queryName now takes a const char*.
//
// Revision 1.3  1993/03/20  15:11:47  davison
// Now returns a name ID, so that only one dbobject for each relation is
// available at any time.
//
// Revision 1.2  1993/02/22  05:39:54  davison
// Fixed RCS Header.
//
///////////////////////////////////////////////////////////////////////////
