CFile

//+------------------------------------------------------------------+

//|                                                     MQL5File.mqh |

//|                        Copyright 2010, MetaQuotes Software Corp. |

//|                                        http://www.metaquotes.net |

//|                                              Revision 2010.02.08 |

//+------------------------------------------------------------------+

#include <Object.mqh>

//+------------------------------------------------------------------+

//| Class CFile.                                                     |

//| Appointment: Base class file.                                    |

//|              Derives from class CObject.                         |

//+------------------------------------------------------------------+

class CFile : public CObject

  {

protected:

   int               m_handle;             // file handle

   string            m_name;               // opened filename

   int               m_flags;              // opened file flags

public:

                     CFile();

                    ~CFile();

   //--- methods of access to protected data

   int               Handle()              { return(m_handle); };

   string            FileName()            { return(m_name);   };

   int               Flags()               { return(m_flags);  };

   void              SetUnicode(bool unicode);

   void              SetCommon(bool common);

   //--- general methods for working with files

   int               Open(const string file_name,int open_flags,short delimiter='\t');

   void              Close();

   void              Delete();

   void              Delete(const string file_name);

   bool              IsExist(const string file_name);

   bool              Copy(const string src_name,int common_flag,const string dst_name,int mode_flags);

   bool              Move(const string src_name,int common_flag,const string dst_name,int mode_flags);

   ulong             Size();

   ulong             Tell();

   void              Seek(long offset,ENUM_FILE_POSITION origin);

   void              Flush();

   bool              IsEnding();

   bool              IsLineEnding();

   //--- general methods of working with folders

   bool              FolderCreate(const string folder_name);

   bool              FolderDelete(const string folder_name);

   bool              FolderClean(const string folder_name);

   //--- general methods of finding files

   long              FileFindFirst(const string file_filter,string& returned_filename);

   bool              FileFindNext(long search_handle,string& returned_filename);

   void              FileFindClose(long search_handle);

  };

//+------------------------------------------------------------------+

//| Constructor CFile.                                               |

//| INPUT:  no.                                                      |

//| OUTPUT: no.                                                      |

//| REMARK: no.                                                      |

//+------------------------------------------------------------------+

void CFile::CFile()

  {

//--- initialize protected data

   m_handle=-1;

   m_name="";

//--- default text encoding - ANSI

   m_flags=FILE_ANSI;

  }

//+------------------------------------------------------------------+

//| Destructor CFile.                                                |

//| INPUT:  no.                                                      |

//| OUTPUT: no.                                                      |

//| REMARK: no.                                                      |

//+------------------------------------------------------------------+

void CFile::~CFile()

  {

   if(m_handle>=0) Close();

  }

//+------------------------------------------------------------------+

//| Set flag FILE_UNICODE.                                           |

//| INPUT:  unicode -new flag UNICODE.                               |

//| OUTPUT: no.                                                      |

//| REMARK: can not change the flag if the file is opened.           |

//+------------------------------------------------------------------+

void CFile::SetUnicode(bool unicode)

  {

//--- checking

   if(m_handle>=0) return;

//---

   if(unicode) m_flags|=FILE_UNICODE;

   else        m_flags^=FILE_UNICODE;

  }

//+------------------------------------------------------------------+

//| Set flag "Common Folder".                                        |

//| INPUT:  common -new flag "Common Folder".                        |

//| OUTPUT: no.                                                      |

//| REMARK: can not change the flag if the file is opened.           |

//+------------------------------------------------------------------+

void CFile::SetCommon(bool common)

  {

//--- checking

   if(m_handle>=0) return;

//---

   if(common) m_flags|=FILE_COMMON;

   else       m_flags^=FILE_COMMON;

  }

//+------------------------------------------------------------------+

//| Open file.                                                       |

//| INPUT:  file_name -filename,                                     |

//|         open_flags-open flags,                                   |

//|         delimiter -delimiter for CSV-file.                       |

//| OUTPUT: handle opened file, or -1.                               |

//| REMARK: no.                                                      |

//+------------------------------------------------------------------+

int CFile::Open(const string file_name,int open_flags,short delimiter)

  {

//--- checking

   if(m_handle>=0) Close();

//---

   if((open_flags&(FILE_BIN|FILE_CSV))==0) open_flags|=FILE_TXT;

   m_handle=FileOpen(file_name,open_flags|m_flags,delimiter);

   if(m_handle>=0)

     {

      //--- store options open file

      m_flags|=open_flags;

      m_name=file_name;

     }

//---

   return(m_handle);

  }

//+------------------------------------------------------------------+

//| Close file.                                                      |

//| INPUT:  no.                                                      |

//| OUTPUT: no.                                                      |

//| REMARK: no.                                                      |

//+------------------------------------------------------------------+

void CFile::Close()

  {

//--- checking

   if(m_handle<0) return;

//--- closing the file and reset all the variables in the initial state

   FileClose(m_handle);

   m_handle=-1;

   m_name="";

//--- reset all flags except the text

   m_flags&=FILE_ANSI|FILE_UNICODE;

  }

//+------------------------------------------------------------------+

//| Deleting an open file.                                           |

//| INPUT:  no.                                                      |

//| OUTPUT: no.                                                      |

//| REMARK: no.                                                      |

//+------------------------------------------------------------------+

void CFile::Delete()

  {

//--- checking

   if(m_handle<0) return;

//---

   string file_name=m_name;

   Close();

   FileDelete(file_name,m_flags&FILE_COMMON);

  }

//+------------------------------------------------------------------+

//| Deleting a file.                                                 |

//| INPUT:  file_name -filename.                                     |

//| OUTPUT: no.                                                      |

//| REMARK: used flag FILE_COMMON in m_flags.                        |

//+------------------------------------------------------------------+

void CFile::Delete(const string file_name)

  {

//--- checking

   if(file_name==m_name) Close();

//---

   FileDelete(file_name,m_flags&FILE_COMMON);

  }

//+------------------------------------------------------------------+

//| Check if file exists.                                            |

//| INPUT:  file_name -filename.                                     |

//| OUTPUT: true -if the file exists, otherwise false.               |

//| REMARK: used flag FILE_COMMON in m_flags.                        |

//+------------------------------------------------------------------+

bool CFile::IsExist(const string file_name)

  {

   return(FileIsExist(file_name,m_flags&FILE_COMMON));

  }

//+------------------------------------------------------------------+

//| Copying file.                                                    |

//| INPUT:  src_name    -source filename,                            |

//|         common_flag -source common flag,                         |

//|         dst_name    -destination filename,                       |

//|         mode_flags  -destination flags.                          |

//| OUTPUT: true-if successful, false otherwise.                     |

//| REMARK: no.                                                      |

//+------------------------------------------------------------------+

bool CFile::Copy(const string src_name,int common_flag,const string dst_name,int mode_flags)

  {

   return(FileCopy(src_name,common_flag,dst_name,mode_flags));

  }

//+------------------------------------------------------------------+

//| Move/rename file.                                                |

//| INPUT:  src_name    -source filename,                            |

//|         common_flag -source common flag,                         |

//|         dst_name    -destination filename,                       |

//|         mode_flags  -destination flags.                          |

//| OUTPUT: true-if successful, false otherwise.                     |

//| REMARK: no.                                                      |

//+------------------------------------------------------------------+

bool CFile::Move(const string src_name,int common_flag,const string dst_name,int mode_flags)

  {

   return(FileMove(src_name,common_flag,dst_name,mode_flags));

  }

//+------------------------------------------------------------------+

//| Get size opened file.                                            |

//| INPUT:  no.                                                      |

//| OUTPUT: size opened file.                                        |

//| REMARK: no.                                                      |

//+------------------------------------------------------------------+

ulong CFile::Size()

  {

//--- checking

   if(m_handle<0) return(ULONG_MAX);

//---

   return(FileSize(m_handle));

  }

//+------------------------------------------------------------------+

//| Get current position of file pointer.                            |

//| INPUT:  no.                                                      |

//| OUTPUT: current position of file pointer.                        |

//| REMARK: no.                                                      |

//+------------------------------------------------------------------+

ulong CFile::Tell()

  {

//--- checking

   if(m_handle<0) return(ULONG_MAX);

//---

   return(FileTell(m_handle));

  }

//+------------------------------------------------------------------+

//| Setting the position of file pointer.                            |

//| INPUT:  offset -offset in bytes,                                 |

//|         origin -base point for the offset.                       |

//| OUTPUT: no.                                                      |

//| REMARK: no.                                                      |

//+------------------------------------------------------------------+

void CFile::Seek(long offset,ENUM_FILE_POSITION origin)

  {

//--- checking

   if(m_handle<0) return;

//---

   FileSeek(m_handle,offset,origin);

  }

//+------------------------------------------------------------------+

//| Discharge data from the write buffer to disk.                    |

//| INPUT:  no.                                                      |

//| OUTPUT: no.                                                      |

//| REMARK: no.                                                      |

//+------------------------------------------------------------------+

void CFile::Flush()

  {

//--- checking

   if(m_handle<0) return;

//---

   FileFlush(m_handle);

  }

//+------------------------------------------------------------------+

//| Check the end of file.                                           |

//| INPUT:  no.                                                      |

//| OUTPUT: true -if end of file, false otherwise.                   |

//| REMARK: no.                                                      |

//+------------------------------------------------------------------+

bool CFile::IsEnding()

  {

//--- checking

   if(m_handle<0) return(false);

//---

   return(FileIsEnding(m_handle));

  }

//+------------------------------------------------------------------+

//| Check the end of string.                                         |

//| INPUT:  no.                                                      |

//| OUTPUT: true -end of string, false otherwise.                    |

//| REMARK: no.                                                      |

//+------------------------------------------------------------------+

bool CFile::IsLineEnding()

  {

//--- checking

   if(m_handle<0)            return(false);

   if((m_flags&FILE_BIN)!=0) return(false);

//---

   return(FileIsLineEnding(m_handle));

  }

//+------------------------------------------------------------------+

//| Create folder.                                                   |

//| INPUT:  folder_name-foldername.                                  |

//| OUTPUT: true -if OK, false otherwise.                            |

//| REMARK: used flag FILE_COMMON in m_flags.                        |

//+------------------------------------------------------------------+

bool CFile::FolderCreate(const string folder_name)

  {

   return(FolderCreate(folder_name,m_flags&FILE_COMMON));

  }

//+------------------------------------------------------------------+

//| Delete folder.                                                   |

//| INPUT:  folder_name-foldername.                                  |

//| OUTPUT: true -if OK, false otherwise.                            |

//| REMARK: used flag FILE_COMMON in m_flags.                        |

//+------------------------------------------------------------------+

bool CFile::FolderDelete(const string folder_name)

  {

   return(FolderDelete(folder_name,m_flags&FILE_COMMON));

  }

//+------------------------------------------------------------------+

//| Clean folder.                                                    |

//| INPUT:  folder_name-foldername.                                  |

//| OUTPUT: true -if OK, false otherwise.                            |

//| REMARK: used flag FILE_COMMON in m_flags.                        |

//+------------------------------------------------------------------+

bool CFile::FolderClean(const string folder_name)

  {

   return(FolderClean(folder_name,m_flags&FILE_COMMON));

  }

//+------------------------------------------------------------------+

//| Start find file.                                                 |

//| INPUT:  file_filter-find filter,                                 |

//|         returned_filename-reference to the string to return.     |

//| OUTPUT: find handle-if OK, otherwise -1.                         |

//| REMARK: used flag FILE_COMMON in m_flags.                        |

//+------------------------------------------------------------------+

long CFile::FileFindFirst(const string file_filter,string& returned_filename)

  {

   return(FileFindFirst(file_filter,returned_filename,m_flags&FILE_COMMON));

  }

//+------------------------------------------------------------------+

//| Continue file find.                                              |

//| INPUT:  search_handle-find handle,                               |

//|         returned_filename-reference to the string to return.     |

//| OUTPUT: true -if OK, false otherwise.                            |

//| REMARK: no.                                                      |

//+------------------------------------------------------------------+

bool CFile::FileFindNext(long search_handle,string& returned_filename)

  {

   return(FileFindNext(search_handle,returned_filename));

  }

//+------------------------------------------------------------------+

//| End file find.                                                   |

//| INPUT:  search_handle-find handle.                               |

//| OUTPUT: no.                                                      |

//| REMARK: no.                                                      |

//+------------------------------------------------------------------+

void CFile::FileFindClose(long search_handle)

  {

   FileFindClose(search_handle);

  }

//+------------------------------------------------------------------+