//#include <mem.h>
#include <stdlib.h>

//#include "drs_appl.h"
#include "drs/drs_srch.h"
//#include "drs_indexlist.h"

// **************************************************************************
// DRS_SearchFieldDef & DRS_SearchField
// **************************************************************************
DRS_SearchField::DRS_SearchField( DRS_Database const &rDB, AT_String fp, AT_String sSection, DRS_Setting BaseKey)
                   : iFieldCount(0)
{

  DRS_FileSetting sSetting;

  DRS_Setting key = BaseKey;

      //Descriptor
      key += "Descr";
      GetPrivateProfileStringA( (LPCSTR) sSection, (LPCSTR) *key, "", (LPSTR) sSetting, sizeof(DRS_FileSetting), fp);
      Descr.Extract(sSetting);

      //SearchField Definition
      //FieldIDs
      key = BaseKey;
      key += "IDCount";
      if( ! (iFieldCount = GetPrivateProfileIntA( (LPCSTR) sSection, (LPCSTR) *key, 0, fp)))
          throw AT_Corrupt_Err( *this, *key);

      //FieldIDs = new AT_FieldID[iFieldCount];
      DataFields.resize(iFieldCount, NULL);

      uint iFieldID;
      for( uint j = 0; j < iFieldCount; j++)
        {
        key = BaseKey;
        key += "ID";
        key += j;
        if( (iFieldID = GetPrivateProfileIntA( (LPCSTR) sSection, (LPCSTR) *key, -1, fp)) == (uint)-1)
          throw AT_Corrupt_Err( *this, *key);
        DataFields[j] = & rDB.Field(iFieldID);
        }

      //IndexList
      key = BaseKey;
      key += "IdxListType";
      uint iIndexListType = GetPrivateProfileIntA( (LPCSTR) sSection, (LPCSTR) *key, MAX_UINT, fp);
      switch( iIndexListType)
        {
        case INDEXLISTTYPE_ENUMERATED:
                apIndexList.reset( new DRS_IndexListEnumerated( rDB, fp, sSection, BaseKey));
                break;
//        case INDEXLISTTYPE_MAPPED:
//                apIndexList.reset( new DRS_IndexListMapped( rDB, fp, sSection, BaseKey));
//                break;
        default:
                throw AT_Corrupt_Err( *this, *key);
        }

}

DRS_Field const & DRS_SearchField::DataField( uint iFieldID) const
{
  if( iFieldID >= iFieldCount)
    throw AT_Bounds_Err( uint, (AT_String)"Search field data field", iFieldID);

  return(* DataFields[iFieldID]);
}


