///////////////////////////////////////////////////////////////////////////////
/// @file deShared.hpp
///
/// @brief deShared Lib Header
///
/// @author Hootie
///
/// This file is the intellectual property of Novus Delta, LLC.. Usage of the
/// contents of this file is subject to the Destiny3D Member License which
/// can be found at http://www.destiny3d.com.  Any other usage is prohibited.
///
/// This file is distributed "AS IS" without warranty of any kind.  Novus
/// Delta, LLC. does not guarantee the fitness of the contents of this file
/// for any particular purpose.
///
/// Copyright (C) 2001-2003 Novus Delta, LLC. All Rights Reserved.
///
/// <hr>
///                                 Change History
/// <hr>
///
/// @date Oct 2001
/// @author Hootie
/// @remarks Rearchitecture II
///
///////////////////////////////////////////////////////////////////////////////

#ifndef DESHARED_HPP
#define DESHARED_HPP



//=================================================================================
// Includes
//=================================================================================

#include "deGlobalTypes.hpp"

#ifdef USING_DESTINY3D
#ifdef _DEBUG
// no static link
#pragma comment(lib, "deSharedd")
#else
#pragma comment(lib, "deShared")
#endif //_DEBUG
#endif //USING_DESTINY3D


//=================================================================================
// Classes Declared
//=================================================================================

class deRegistry;
class deWinFile;
class deConfigFile;
class deDynamicArray;
class deDynamicList;



//=================================================================================
// deRegistry Definitions
//=================================================================================

//=================================================================================
// Registry Root Key Enums
//=================================================================================

typedef enum
{
   DEREGISTRY_CLASSES_ROOT,
   DEREGISTRY_CURRENT_USER,
   DEREGISTRY_LOCAL_MACHINE,
   DEREGISTRY_USERS
} deRegistryRootKey;



//=================================================================================
// deRegistry Class
//
// Simple Win32 Registry Class
//=================================================================================
typedef struct HKEY__* HKEY;

class deRegistry
{
public:
   deRegistry();

   deBoolean SetRoot(const deRegistryRootKey RootKey);
   deBoolean ReadString(const char *Key, const char *ValueName, char *Buffer, int *BufferSize);
   deBoolean WriteString(const char *Key, const char *ValueName, const char *Buffer, const int BufferSize);
   deBoolean ReadInt(const char *Key, const char *ValueName, int *IntValue);
   deBoolean WriteInt(const char *Key, const char *ValueName, const int IntValue);
   LONG GetLastReturnCode() { return LastReturnCode; }

private:
   HKEY CurrentRootKey;
   HKEY CurrentOpenKey;
   LONG LastReturnCode;
};



//=================================================================================
// deWinFile Definitions
//=================================================================================

//=================================================================================
// File Access Enums
//=================================================================================

typedef enum
{
   DEWINFILE_READONLY,
   DEWINFILE_WRITEONLY,
   DEWINFILE_READWRITE
} deWinFileAccessType;



//=================================================================================
// File Disposition Enums
//=================================================================================

typedef enum
{
   DEWINFILE_NEW,
   DEWINFILE_EXISTS
} deWinFileDisposition;



//=================================================================================
// deWinFile Class
//
// Simple Win32 File Class
//=================================================================================

class deWinFile
{
public:
   deWinFile();
   ~deWinFile();

   deBoolean Open(const char *FileName,
                  const deWinFileAccessType FileAccessType,
                  const deWinFileDisposition FileDispositionType);
   deBoolean Close(void);
   unsigned int Read(void *Buffer, unsigned int BufferSize);
   unsigned int Write(void *Buffer, unsigned int BufferSize);
   unsigned int GetSize(void);

private:
   HANDLE CurrentOpenFile;
};



//=================================================================================
// deConfigFile Definitions
//=================================================================================

//=================================================================================
// deConfigFile Class
//
// Non-deFile Config File Class
//=================================================================================

class deConfigFile
{
public:
   deConfigFile(char *FullFilePath);
   ~deConfigFile();

   deBoolean GetCreationResult(void) { return CreationResult; }
   void SetCurrentEntry(unsigned int Entry) { CurrentEntry = Entry; } 
   char *FindKeyValue(const char *KeyName);
   char *FindNextKeyValue(const char *KeyName);
   deBoolean GetFirstKeyValue(char **KeyName, char **KeyValue);
   deBoolean GetNextKeyValue(char **KeyName, char **KeyValue);
   deBoolean GetPreviousKeyValue(char **KeyName, char **KeyValue);

private:
   unsigned int LoadFileBuffer(char *FullFilePath);
   deBoolean ParseFileBuffer(void);


   deBoolean CreationResult;
   void *FileBuffer;
   deDynamicArray *ConfigKeys;
   deDynamicArray *ConfigValues;
   int TotalConfigEntries;
   int CurrentEntry;
};



//=================================================================================
// deArray Definitions
//=================================================================================

//=================================================================================
// deArray Class
//
// Simple Dynamic Array Class
//=================================================================================

class deDynamicArray
{
public:
   deDynamicArray(const unsigned int Size, const unsigned int Count);
   deDynamicArray(deDynamicArray &deDynamicArraySource);
   ~deDynamicArray();

   deBoolean GetCreationResult(void) { return CreationResult; }
   void SetArrayGrowthRate(unsigned int GrowthRate) { ArrayGrowthRate = GrowthRate; }
   unsigned int GetElementSize(void) { return ElementSize; }
   unsigned int GetElementCount(void) { return ElementCount; }
   void *GetElement(unsigned int Index);
   deBoolean SetElement(void *Element, unsigned int Index);

private:
   deBoolean GrowArray(unsigned int SizeIncrease);

   deBoolean CreationResult;
   unsigned int ElementSize;
   unsigned int ElementCount;
   void  *Array;
   unsigned int ArrayGrowthRate;
};



//=================================================================================
// deDynamicList Definitions
//=================================================================================

//=================================================================================
// deDynamicList Clear Type
//=================================================================================

typedef enum
{
   DEDYNAMICLIST_NOCLEAR,
   DEDYNAMICLIST_CLEAR
} deDynamicListClearType;



//=================================================================================
// deDynamicList Class
//
// Dynamic Linked List Class
//=================================================================================

class deDynamicList
{
public:
   deDynamicList(const unsigned int Size,
                 const unsigned int Count,
                 const unsigned int Growth,
                 const deDynamicListClearType ClearType);
   deDynamicList(deDynamicList &deDynamicListSource);
   ~deDynamicList();

   deBoolean GetCreationResult(void) { return CreationResult; }
   deBoolean AddElement(void *Element);
   deBoolean InsertElementBefore(void *Element);
   deBoolean InsertElementAfter(void *Element);
   deBoolean DeleteElement(void);
   void *GetFirstElement(void);
   void *GetNextElement(void);
   void *GetPreviousElement(void);
   void *GetLastElement(void);

private:
   void *FindOpenSlot(void);
   deBoolean GrowList(unsigned int SizeIncrease);

   deBoolean CreationResult;
   void *List;
   void *FirstElement;
   void *LastElement;
   void *CurrentElement;
   unsigned int ElementSize;
   unsigned int ListEntrySize;
   unsigned int GrowthRate;
   unsigned int ListSize;
   unsigned int CurrentElementCount;
   unsigned int ElementOffset;
   unsigned int PrevOffset;
   unsigned int NextOffset;
   deDynamicListClearType ListClearType;
};




#endif  //End DESHARED_HPP

