/**************************/
/* Written by David Yates */
/* Copyright Atos 2013    */
/**************************/

#if !defined(AFX_DATABASEMGR_H__9773ADBC_FCB3_4432_AB7A_3B4F83AE186F__INCLUDED_)
#define AFX_DATABASEMGR_H__9773ADBC_FCB3_4432_AB7A_3B4F83AE186F__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#ifdef _DEBUG
//#pragma warning( disable : 4786) 
#endif

#include "database.h"
#include <windows.h>
#include <map>
using namespace std;


class databaseMgr  
{
public:	// construction
	databaseMgr( const char * path);
	databaseMgr();
	virtual ~databaseMgr();

public:	// members
	void setPath( const char * path );
	const char* getName(unsigned int device, unsigned int db, unsigned int index);
	bool getName(unsigned int device, unsigned int db, unsigned int index, char * name, unsigned int len);

	unsigned int getIndex(unsigned int device, unsigned int db, const char* name, unsigned int start = 0, unsigned int end = 0);
	unsigned int getIndex(unsigned int device, unsigned int db, const char* name, bool caseInsensitive, bool substring);
	bool setName(unsigned int device, unsigned int db, unsigned int index, const char * name, bool writeToFile = true, bool *changed = 0);

	bool exists(unsigned int device, unsigned int db, unsigned int start, unsigned int end);
	unsigned int numLoaded(void);
	const char * path( void );
	bool createDatabase(	const char* name, 
		unsigned int device,
		unsigned int sizeDB0,
		unsigned int sizeDB1,
		unsigned int sizeDB2 = 0,
		unsigned int sizeDB3 = 0,
		unsigned int sizeDB4 = 0,
		unsigned int sizeDB5 = 0,
		unsigned int sizeDB6 = 0,
		unsigned int sizeDB7 = 0,
		unsigned int sizeDB8 = 0,
		unsigned int sizeDB9 = 0);
	unsigned int size(unsigned int device, unsigned int db);
	unsigned int exists(unsigned int device, unsigned int db);
	void clear(unsigned int device = 0);
	bool dump(void);
	bool loadDump(unsigned int device, unsigned int db);
private:	// variables
	map< unsigned long long, database* > dbs;
//	map< int, int > lastTimeUsed;		// use this to determine whether to drop the last used database to make room
	char m_path[ MAX_PATH ];

private:	// members
	database * doCreate(unsigned int device, unsigned int db);
	int WritePrivateProfileInt(const char* section, const char* entry, unsigned int value, const char* filename);
	database * getDatabaseObject(unsigned int device, unsigned int db, bool create = true);
};

#endif // !defined(AFX_DATABASEMGR_H__9773ADBC_FCB3_4432_AB7A_3B4F83AE186F__INCLUDED_)
