#if defined(WIN32)
#include <direct.h>
#include <io.h>
#endif /* WIN32 */
 
#include "FIL_CPA.h"
 
#define D_FIL_StructureDefine
#define D_FIL_Globals
 
 
#include "FIL_GF.h"
#include "FIL_Pub.h"
#include "FIL_Priv.h"
 
#include "MMG.h"
/**************************************************************************/
unsigned char FIL_fn_bDestroyRecurseDirectory(char *_szFileName)
{
 long hHandle;
 struct _finddata_t stFindData;
 char szFileName[_MAX_PATH];
 
 if ((hHandle=_findfirst(_szFileName,&stFindData))!=-1)
 {
  if (stFindData.attrib & _A_SUBDIR)
  {
   if (strcmp(stFindData.name,".")&&strcmp(stFindData.name,".."))
   {
    strcpy(szFileName,_szFileName);
    if (strrchr(szFileName,'*'))
     *strrchr(szFileName,'/') = 0;
    strcat(szFileName,"/*");
    FIL_fn_bDestroyRecurseDirectory(szFileName);
    _rmdir(_szFileName);
   }
  }
  else
  {
   unlink(_szFileName);
  }
 
  while (_findnext(hHandle,&stFindData)!=-1)
  {
   if (stFindData.attrib & _A_SUBDIR)
   {
    if (strcmp(stFindData.name,".")&&strcmp(stFindData.name,".."))
    {
     strcpy(szFileName,_szFileName);
     if (strrchr(szFileName,'*'))
      *strrchr(szFileName,'/') = 0;
     strcat(szFileName,"/");
     strcat(szFileName,stFindData.name);
     FIL_fn_bDestroyRecurseDirectory(szFileName);
    }
   }
   else
   {
    strcpy(szFileName,_szFileName);
    if (strrchr(szFileName,'*'))
     *strrchr(szFileName,'/') = 0;
    strcat(szFileName,"/");
    strcat(szFileName,stFindData.name);
    unlink(szFileName);
   }
  }
 
  _findclose(hHandle);
 }
 return(TRUE);
}
/**************************************************************************/
void FIL_TryToInsertFileNameInList(FIL_tdxHandleToFileNameList _hFileNameList,char *_szParentDir,struct _finddata_t *_p_stFindData,unsigned long ulAttrib)
{
 FIL_tdxHandleToFileNameListElement hFileNameListElement=NULL;
 
 if (  _p_stFindData->attrib&ulAttrib
     &&strcmp(_p_stFindData->name,".")
     &&strcmp(_p_stFindData->name,".."))
 {
  MMG_fn_vAddMemoryInfo (MMG_C_lTypeFile , MMG_C_lSubTypeMiscellaneousFile , NULL);
  hFileNameListElement = (FIL_tdxHandleToFileNameListElement)TMP_M_p_Malloc(sizeof(struct FIL_tdstFileNameListElement_));
 
  LST2_M_DynamicInitElement(hFileNameListElement);
  if (_szParentDir[0]!=0)
  {
   MMG_fn_vAddMemoryInfo (MMG_C_lTypeFile , MMG_C_lSubTypeMiscellaneousFile , NULL);
   hFileNameListElement->szFileName = (char*)TMP_M_p_Malloc(strlen(_szParentDir)+strlen(_p_stFindData->name)+2);
   sprintf(hFileNameListElement->szFileName,"%s\\%s",_szParentDir,_p_stFindData->name);
  }
  else
  {
   MMG_fn_vAddMemoryInfo (MMG_C_lTypeFile , MMG_C_lSubTypeMiscellaneousFile , NULL);
   hFileNameListElement->szFileName = (char*)TMP_M_p_Malloc(strlen(_p_stFindData->name)+1);
   sprintf(hFileNameListElement->szFileName,"%s",_p_stFindData->name);
  }
  LST2_M_DynamicAddTail(&_hFileNameList->hFileNameListAnchor,hFileNameListElement);
 }
}
/**************************************************************************/
void FIL_fn_vSearchInFile(FIL_tdxHandleToFileNameList _hFileNameList,char *_szFileName,unsigned long ulAttrib)
{
 long hHandle;
 struct _finddata_t stFindData;
 
 if ((hHandle=_findfirst(_szFileName,&stFindData))!=-1)
 {
  FIL_TryToInsertFileNameInList(_hFileNameList,"",&stFindData,ulAttrib);
  while (_findnext(hHandle,&stFindData)!=-1)
   FIL_TryToInsertFileNameInList(_hFileNameList,"",&stFindData,ulAttrib);
  _findclose(hHandle);
 }
}
/**************************************************************************/
void FIL_fn_vSearchRecurseInFile(FIL_tdxHandleToFileNameList _hFileNameList,char *_szParentDir,char *_szFileName,unsigned long ulAttrib)
{
 char szPath[_MAX_PATH];
 char szInternalFileName[_MAX_PATH];
 long hHandle;
 struct _finddata_t stFindData;
 
 strcpy(szInternalFileName,_szFileName);
 if (_szParentDir[0]!=0&&strrchr(szInternalFileName,'\\')!=NULL)
 {
  *(strrchr(szInternalFileName,'\\')+1) = 0;
  strcat(szInternalFileName,_szParentDir);
  strcat(szInternalFileName,"\\*.*");
 }
 else
 {
  *(strrchr(szInternalFileName,'\\')) = 0;
  strcat(szInternalFileName,"\\*.*");
 }
 
 if ((hHandle=_findfirst(szInternalFileName,&stFindData))!=-1)
 {
  if (  stFindData.attrib&_A_SUBDIR
      &&strcmp(stFindData.name,".")
      &&strcmp(stFindData.name,".."))
  {
   if (_szParentDir[0]!=0)
    sprintf(szPath,"%s\\%s",_szParentDir,stFindData.name);
   else
    sprintf(szPath,"%s",stFindData.name);
   FIL_fn_vSearchRecurseInFile(_hFileNameList,szPath,_szFileName,ulAttrib);
  }
  while (_findnext(hHandle,&stFindData)!=-1)
  {
   if (  stFindData.attrib&_A_SUBDIR
       &&strcmp(stFindData.name,".")
       &&strcmp(stFindData.name,".."))
   {
    if (_szParentDir[0]!=0)
     sprintf(szPath,"%s\\%s",_szParentDir,stFindData.name);
    else
     sprintf(szPath,"%s",stFindData.name);
    FIL_fn_vSearchRecurseInFile(_hFileNameList,szPath,_szFileName,ulAttrib);
   }
  }
  _findclose(hHandle);
 }
 
 strcpy(szInternalFileName,_szFileName);
 if (_szParentDir[0]!=0&&strrchr(szInternalFileName,'\\')!=NULL)
 {
  *(strrchr(szInternalFileName,'\\')+1) = 0;
  strcat(szInternalFileName,_szParentDir);
  strcat(szInternalFileName,strrchr(_szFileName,'\\'));
 }
 
 if ((hHandle=_findfirst(szInternalFileName,&stFindData))!=-1)
 {
  FIL_TryToInsertFileNameInList(_hFileNameList,_szParentDir,&stFindData,ulAttrib);
  while (_findnext(hHandle,&stFindData)!=-1)
  {
   FIL_TryToInsertFileNameInList(_hFileNameList,_szParentDir,&stFindData,ulAttrib);
  }
  _findclose(hHandle);
 }
}
/**************************************************************************/
