//----------------------------------------------------------------------------//
//    module ThrLoadData.cpp                                                  //
//                                                                            //
//    Thread function: Calls TData::Load() in its Execute function            //
//                                                                            //
//    copyright (c) 2003 by Lars Haendel                                      //
//    home: www.newty.de                                                      //
//                                                                            //
//    This program is free software and can be used under the terms of the    //
//    GNU licence. See header file for further information and disclaimer.    //
//                                                                            //
//----------------------------------------------------------------------------//


#include <vcl.h>
#pragma hdrstop

#include "ThrLoadData.h"
#pragma package(smart_init)

#include "exception.h"         // due to:  TExceptionU

//----------------------------------------------------------------------------------------------------------------------
// constructor
__fastcall ThrLoadData::ThrLoadData(const bool& CreateSuspended, TData*const& _data, const AnsiString& _szFilename,
   void (*_FinishFunc)(const bool&), const bool*const& _f_Stop/*=NULL*/, const TDataData*const& _ddata/*=NULL*/)
      : TThread(CreateSuspended)
{
   data            = _data;
   ddata           = _ddata;
   szFilename      = _szFilename;
   FinishFunc      = _FinishFunc;      // callback function when finished
   f_Stop          = _f_Stop;          // stop flag

   Priority        = tpNormal;         // set thread's properties
   OnTerminate     = &OnFinish;
   FreeOnTerminate = true;
   f_Error = false;

   f_IsWithOutput = true;              // ini to true
}


//----------------------------------------------------------------------------------------------------------------------
// execute function gets called when thread object is created. Calls TData::Load()
void __fastcall ThrLoadData::Execute()
{
   try
   {
      data->Load(szFilename.c_str(), f_Stop, ddata);      // load data file using TData::Load()

      // if given: set output column and variable types and throw error if they cannot be set
      if(ddata)
         f_IsWithOutput = data->MakeCompatible(ddata);
   }
   catch(TExceptionU excp)                // exception handling 'type U' exception
   {
      f_Error = true;                           // set error flag
      strcpy(szText, excp.GetErrorText());      // copy error message ...
      Synchronize(MessageBox);                  // ... and display it in a message box
   }
   catch(TExceptionAB excp)               // exception handling 'type AB' exception
   {
      f_Error = true;                           // set error flag
      strcpy(szText, excp.GetErrorText());      // copy error message ...
      Synchronize(MessageBox);                  // ... and display it in a message box
   }
   catch(...)                             // all other exceptions
   {
      f_Error = true;                           // set error flag
      strcpy(szText, "C++ Exception ;-)");      // copy error message ...
      Synchronize(MessageBox);                  // ... and display it in a message box
   }
};


//----------------------------------------------------------------------------------------------------------------------
// call when loading is finished
void __fastcall ThrLoadData::OnFinish(TObject*)
{
   try
   {
      (*FinishFunc)(f_Error);  // make callback to specified finish function
   }
   catch(TExceptionU excp)                // exception handling 'type U' exception
   {
      strcpy(szText, excp.GetErrorText());      // copy error message ...
      Synchronize(MessageBox);                  // ... and display it in a message box
   }
   catch(TExceptionAB excp)               // exception handling 'type AB' exception
   {
      strcpy(szText, excp.GetErrorText());      // copy error message ...
      Synchronize(MessageBox);                  // ... and display it in a message box
   }
   catch(...)                             // all other exceptions
   {
      strcpy(szText, "C++ Exception ;-)");      // copy error message ...
      Synchronize(MessageBox);                  // ... and display it in a message box
   }
}

//----------------------------------------------------------------------------------------------------------------------
// display error message
void __fastcall ThrLoadData::MessageBox()
{
   char szCaption[256];
   sprintf(szCaption, "Error loading %s", szFilename.c_str());
   Application->MessageBox(szText, szCaption, MB_OK | MB_ICONERROR);
}