

#include <memory>

#include "drs/drs_type.h"
#include "drs/drs_setting.h"


DRS_DescriptorSerialize::DRS_DescriptorSerialize( byte *p)
{
  if( ! Extract(p)) throw AT_Constructor_Err(0);
}

DRS_DescriptorSerialize::DRS_DescriptorSerialize()
{
  Caption[0] = 0;
  Descr[0] = 0;
  ID[0] = 0;
}


DRS_DescriptorSerialize::DRS_DescriptorSerialize( AT_String sFile, AT_String sSection, AT_String key)
{
  DRS_FileSetting instr;
  GetPrivateProfileStringA( sSection, key, "", (LPSTR) instr, sizeof(DRS_FileSetting), (LPCSTR) (const char*) sFile);
  if( !std::strlen((const char*) instr)) throw DRSERR_MissingConfig( *this, sFile, sSection, key);
  Extract( instr);
}

DRS_DescriptorSerialize::DRS_DescriptorSerialize( AT_String sFile, AT_String sSection, DRS_Setting BaseKey)
{
  DRS_FileSetting instr;

  BaseKey += "Descr";
  GetPrivateProfileStringA( sSection, (LPCSTR) *BaseKey, "", (LPSTR) instr, sizeof(DRS_FileSetting), sFile);
  if( !std::strlen((const char*) instr)) throw DRSERR_MissingConfig( *this, sFile, sSection, *BaseKey);
  Extract( instr);
}

DRS_DescriptorSerialize::DRS_DescriptorSerialize( DRS_Descriptor const &rDescr)
{
  memcpy( Caption, rDescr.Caption, sizeof(Caption));
  memcpy( Descr, rDescr.Descr, sizeof(Descr));
  memcpy( ID, rDescr.ID, sizeof(ID));
}

DRS_DescriptorSerialize::DRS_DescriptorSerialize( DRS_DescriptorSerialize const &rDescr)
{
  memcpy( Caption, rDescr.Caption, sizeof(Caption));
  memcpy( Descr, rDescr.Descr, sizeof(Descr));
  memcpy( ID, rDescr.ID, sizeof(ID));
}

bool DRS_DescriptorSerialize::Serialise( byte *sSection, byte *sKey, byte *sFile)
{
  byte * sDescr = new byte[std::strlen(Caption) + std::strlen(Descr) + std::strlen(ID) + 5];
  wsprintfA( (LPSTR) sDescr, "%s::%s::%s", Caption, Descr, ID);
  return( WritePrivateProfileStringA( (LPCSTR) sSection, (LPCSTR) sKey, (LPCSTR) sDescr, (LPCSTR) sFile));
}

DRS_DescriptorSerialize const & DRS_DescriptorSerialize::Extract( byte *buf)
{
  Caption[0] = 0;
  Descr[0] = 0;
  ID[0] = 0;

  UTL_StringClean( buf);

  char *delim = strstr( (char *)buf, "::");
  if( delim)
    {
    *delim = 0;
    delim += 2;

    char *delim2 = strstr( (char *)delim, "::");
    if( delim2)
      {
      *delim2 = 0;
      delim2 += 2;
      std::strncpy( ID,delim2, sizeof( T_CaptionShort));
      }
    std::strncpy( Descr,delim, sizeof( T_CaptionShort));
    }
  std::strncpy( Caption, (const char*) buf, sizeof(T_Caption));
  return(*this);
}

#if 0
bool DRS_DescriptorSerialize::Deserialise( FILE *hFile)
{
  Caption[0] = 0;
  Descr[0] = 0;
  ID[0] = 0;

  byte buf[(2 * sizeof(T_Caption))+ 3];
  if ( ! fgets( buf, (2 * sizeof(T_Caption))+ 3, hFile))
    return( ERC_any);

  UTL_StringClean( buf);
  if( !std::strlen(buf)) return( ERC_any);

  byte *delim = std::strstr( (char *)buf, "::");
  if( delim)
    {
    *delim = 0;
    delim += 2;

    byte *delim2 = std::strstr( (char *)delim, "::");
    if( delim2)
      {
      *delim2 = 0;
      delim2 += 2;
      std::strcpy( ID,delim2);
      }
    std::strcpy( Descr,delim);
    }
  std::strcpy( Caption, buf);
  return(ERC_none);
}

#endif
