Add files via upload

This commit is contained in:
amirghadiri1987
2025-02-07 19:08:31 +03:30
committed by GitHub
parent c33ac55f2f
commit a9340e5d4e
85 changed files with 15214 additions and 0 deletions
+292
View File
@@ -0,0 +1,292 @@
//+------------------------------------------------------------------+
//| File.mqh |
//| Copyright 2000-2024, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#include <Object.mqh>
//+------------------------------------------------------------------+
//| Class CFile. |
//| Purpose: Base class of file operations. |
//| Derives from class CObject. |
//+------------------------------------------------------------------+
class CFile : public CObject
{
protected:
int m_handle; // handle of file
string m_name; // name of opened file
int m_flags; // flags of opened file
public:
CFile(void);
~CFile(void);
//--- methods of access to protected data
int Handle(void) const { return(m_handle); };
string FileName(void) const { return(m_name); };
int Flags(void) const { return(m_flags); };
void SetUnicode(const bool unicode);
void SetCommon(const bool common);
//--- general methods for working with files
int Open(const string file_name,int open_flags,const short delimiter='\t');
void Close(void);
void Delete(void);
ulong Size(void);
ulong Tell(void);
void Seek(const long offset,const ENUM_FILE_POSITION origin);
void Flush(void);
bool IsEnding(void);
bool IsLineEnding(void);
//--- general methods for working with files
void Delete(const string file_name,const int common_flag=0);
bool IsExist(const string file_name,const int common_flag=0);
bool Copy(const string src_name,const int common_flag,const string dst_name,const int mode_flags);
bool Move(const string src_name,const int common_flag,const string dst_name,const int mode_flags);
//--- 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(const long search_handle,string &returned_filename);
void FileFindClose(const long search_handle);
};
//+------------------------------------------------------------------+
//| Constructor |
//+------------------------------------------------------------------+
CFile::CFile(void) : m_handle(INVALID_HANDLE),
m_name(""),
m_flags(FILE_ANSI)
{
}
//+------------------------------------------------------------------+
//| Destructor |
//+------------------------------------------------------------------+
CFile::~CFile(void)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
Close();
}
//+------------------------------------------------------------------+
//| Set the FILE_UNICODE flag |
//+------------------------------------------------------------------+
void CFile::SetUnicode(const bool unicode)
{
//--- check handle
if(m_handle==INVALID_HANDLE)
{
if(unicode)
m_flags|=FILE_UNICODE;
else
m_flags&=~FILE_UNICODE;
}
}
//+------------------------------------------------------------------+
//| Set the "Common Folder" flag |
//+------------------------------------------------------------------+
void CFile::SetCommon(const bool common)
{
//--- check handle
if(m_handle==INVALID_HANDLE)
{
if(common)
m_flags|=FILE_COMMON;
else
m_flags&=~FILE_COMMON;
}
}
//+------------------------------------------------------------------+
//| Open the file |
//+------------------------------------------------------------------+
int CFile::Open(const string file_name,int open_flags,const short delimiter)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
Close();
//--- action
if((open_flags &(FILE_BIN|FILE_CSV))==0)
open_flags|=FILE_TXT;
//--- open
m_handle=FileOpen(file_name,open_flags|m_flags,delimiter);
if(m_handle!=INVALID_HANDLE)
{
//--- store options of the opened file
m_flags|=open_flags;
m_name=file_name;
}
//--- result
return(m_handle);
}
//+------------------------------------------------------------------+
//| Close the file |
//+------------------------------------------------------------------+
void CFile::Close(void)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
{
//--- closing the file and resetting all the variables to the initial state
FileClose(m_handle);
m_handle=INVALID_HANDLE;
m_name="";
//--- reset all flags except the text
m_flags&=FILE_ANSI|FILE_UNICODE;
}
}
//+------------------------------------------------------------------+
//| Deleting an open file |
//+------------------------------------------------------------------+
void CFile::Delete(void)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
{
string file_name=m_name;
int common_flag=m_flags&FILE_COMMON;
//--- close before deleting
Close();
//--- delete
FileDelete(file_name,common_flag);
}
}
//+------------------------------------------------------------------+
//| Get size of opened file |
//+------------------------------------------------------------------+
ulong CFile::Size(void)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileSize(m_handle));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Get current position of pointer in file |
//+------------------------------------------------------------------+
ulong CFile::Tell(void)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileTell(m_handle));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Set position of pointer in file |
//+------------------------------------------------------------------+
void CFile::Seek(const long offset,const ENUM_FILE_POSITION origin)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
FileSeek(m_handle,offset,origin);
}
//+------------------------------------------------------------------+
//| Flush data from the file buffer of input-output to disk |
//+------------------------------------------------------------------+
void CFile::Flush(void)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
FileFlush(m_handle);
}
//+------------------------------------------------------------------+
//| Detect the end of file |
//+------------------------------------------------------------------+
bool CFile::IsEnding(void)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileIsEnding(m_handle));
//--- failure
return(false);
}
//+------------------------------------------------------------------+
//| Detect the end of string |
//+------------------------------------------------------------------+
bool CFile::IsLineEnding(void)
{
//--- checking
if(m_handle!=INVALID_HANDLE)
if((m_flags&FILE_BIN)==0)
return(FileIsLineEnding(m_handle));
//--- failure
return(false);
}
//+------------------------------------------------------------------+
//| Deleting a file |
//+------------------------------------------------------------------+
void CFile::Delete(const string file_name,const int common_flag)
{
//--- checking
if(file_name==m_name)
{
int flag=m_flags&FILE_COMMON;
if(flag==common_flag)
Close();
}
//--- delete
FileDelete(file_name,common_flag);
}
//+------------------------------------------------------------------+
//| Check if file exists |
//+------------------------------------------------------------------+
bool CFile::IsExist(const string file_name,const int common_flag)
{
return(FileIsExist(file_name,common_flag));
}
//+------------------------------------------------------------------+
//| Copying file |
//+------------------------------------------------------------------+
bool CFile::Copy(const string src_name,const int common_flag,const string dst_name,const int mode_flags)
{
return(FileCopy(src_name,common_flag,dst_name,mode_flags));
}
//+------------------------------------------------------------------+
//| Move/rename file |
//+------------------------------------------------------------------+
bool CFile::Move(const string src_name,const int common_flag,const string dst_name,const int mode_flags)
{
return(FileMove(src_name,common_flag,dst_name,mode_flags));
}
//+------------------------------------------------------------------+
//| Create folder |
//+------------------------------------------------------------------+
bool CFile::FolderCreate(const string folder_name)
{
return(::FolderCreate(folder_name,m_flags));
}
//+------------------------------------------------------------------+
//| Delete folder |
//+------------------------------------------------------------------+
bool CFile::FolderDelete(const string folder_name)
{
return(::FolderDelete(folder_name,m_flags));
}
//+------------------------------------------------------------------+
//| Clean folder |
//+------------------------------------------------------------------+
bool CFile::FolderClean(const string folder_name)
{
return(::FolderClean(folder_name,m_flags));
}
//+------------------------------------------------------------------+
//| Start search of files |
//+------------------------------------------------------------------+
long CFile::FileFindFirst(const string file_filter,string &returned_filename)
{
return(::FileFindFirst(file_filter,returned_filename,m_flags));
}
//+------------------------------------------------------------------+
//| Continue search of files |
//+------------------------------------------------------------------+
bool CFile::FileFindNext(const long search_handle,string &returned_filename)
{
return(::FileFindNext(search_handle,returned_filename));
}
//+------------------------------------------------------------------+
//| End search of files |
//+------------------------------------------------------------------+
void CFile::FileFindClose(const long search_handle)
{
::FileFindClose(search_handle);
}
//+------------------------------------------------------------------+
+183
View File
@@ -0,0 +1,183 @@
//+------------------------------------------------------------------+
//| FileBMP.mqh |
//| Copyright 2000-2024, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#include <Object.mqh>
//+------------------------------------------------------------------+
//| Bitmap headers |
//+------------------------------------------------------------------+
struct BITMAPFILEHEADER
{
ushort bfType;
uint bfSize;
ushort bfReserved1;
ushort bfReserved2;
uint bfOffBits;
};
struct BITMAPINFOHEADER
{
uint biSize;
int biWidth;
int biHeight;
ushort biPlanes;
ushort biBitCount;
uint biCompression;
uint biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
uint biClrUsed;
uint biClrImportant;
};
#define BM 0x4D42
//+------------------------------------------------------------------+
//| Class CFileBMP |
//| Purpose: Special class to read and write bmp file |
//| Derives from class CObject. |
//+------------------------------------------------------------------+
class CFileBMP : public CObject
{
protected:
int m_handle;
BITMAPFILEHEADER m_file_header;
BITMAPINFOHEADER m_info_header;
public:
CFileBMP(void);
~CFileBMP(void);
int OpenWrite(const string file_name,bool common_flag=false);
int OpenRead(const string file_name,bool common_flag=false);
int Write32BitsArray(uint& uint_array[],const int width,const int height);
int Read32BitsArray(uint& uint_array[],int& width,int& height);
void Close(void);
};
//+------------------------------------------------------------------+
//| Constructor |
//+------------------------------------------------------------------+
CFileBMP::CFileBMP(void) : m_handle(INVALID_HANDLE)
{
ZeroMemory(m_file_header);
ZeroMemory(m_info_header);
}
//+------------------------------------------------------------------+
//| Destructor |
//+------------------------------------------------------------------+
CFileBMP::~CFileBMP(void)
{
Close();
}
//+------------------------------------------------------------------+
//| Open the file |
//+------------------------------------------------------------------+
int CFileBMP::OpenWrite(const string file_name,bool common_flag)
{
Close();
//--- action
int open_flags=FILE_BIN|FILE_WRITE|FILE_SHARE_READ|FILE_SHARE_WRITE;
if(common_flag)
open_flags|=FILE_COMMON;
//--- open
m_handle=FileOpen(file_name,open_flags);
//--- result
return(m_handle);
}
//+------------------------------------------------------------------+
//| Open the file |
//+------------------------------------------------------------------+
int CFileBMP::OpenRead(const string file_name,bool common_flag)
{
Close();
//--- action
int open_flags=FILE_BIN|FILE_READ|FILE_SHARE_READ|FILE_SHARE_WRITE;
if(common_flag)
open_flags|=FILE_COMMON;
//--- open
m_handle=FileOpen(file_name,open_flags);
//--- check bmp headers
if(m_handle!=INVALID_HANDLE)
{
uint fileheader_size=FileReadStruct(m_handle,m_file_header);
uint infoheader_size=FileReadStruct(m_handle,m_info_header);
//--- it should be a simple 32-bit bmp
if(fileheader_size!=sizeof(m_file_header) ||
infoheader_size!=sizeof(m_info_header) ||
m_file_header.bfType!=BM ||
m_file_header.bfOffBits!=sizeof(m_file_header)+sizeof(m_info_header) ||
m_info_header.biBitCount!=32 ||
m_info_header.biClrUsed!=0)
Close();
}
//--- result
return(m_handle);
}
//+------------------------------------------------------------------+
//| Write the file |
//+------------------------------------------------------------------+
int CFileBMP::Write32BitsArray(uint& uint_array[],const int width,const int height)
{
if(m_handle==INVALID_HANDLE)
return(-1);
//--- check size
int size=width*height;
if(size==0)
return(0);
if(size<0)
size=-size;
if(ArraySize(uint_array)<size)
return(-2);
//--- prepare headers
ZeroMemory(m_file_header);
ZeroMemory(m_info_header);
m_file_header.bfType=BM;
m_file_header.bfSize=sizeof(m_file_header)+sizeof(m_info_header)+size*sizeof(uint);
m_file_header.bfOffBits=sizeof(m_file_header)+sizeof(m_info_header);
m_info_header.biSize=sizeof(m_info_header);
m_info_header.biWidth=width;
m_info_header.biHeight=height;
m_info_header.biPlanes=1;
m_info_header.biBitCount=32;
m_info_header.biSizeImage=size*32;
//--- write bmp-file
FileSeek(m_handle,0,SEEK_SET);
FileWriteStruct(m_handle,m_file_header);
FileWriteStruct(m_handle,m_info_header);
uint written=FileWriteArray(m_handle,uint_array,0,size);
//--- bytes written
return((int)written*sizeof(uint));
}
//+------------------------------------------------------------------+
//| Read the file |
//+------------------------------------------------------------------+
int CFileBMP::Read32BitsArray(uint& uint_array[],int& width,int& height)
{
if(m_handle==INVALID_HANDLE)
return(-1);
//--- store dimensions from header
width=m_info_header.biWidth;
height=m_info_header.biHeight;
//--- check size
int size=width*height;
if(size==0)
return(0);
if(size<0)
size=-size;
//--- read bmp-file
FileSeek(m_handle,sizeof(m_file_header)+sizeof(m_info_header),SEEK_SET);
uint read=FileReadArray(m_handle,uint_array,0,size);
//--- bytes read
return((int)read*sizeof(uint));
}
//+------------------------------------------------------------------+
//| Close the file |
//+------------------------------------------------------------------+
void CFileBMP::Close(void)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
{
FileClose(m_handle);
m_handle=INVALID_HANDLE;
}
}
//+------------------------------------------------------------------+
+517
View File
@@ -0,0 +1,517 @@
//+------------------------------------------------------------------+
//| FileBin.mqh |
//| Copyright 2000-2024, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#include "File.mqh"
//+------------------------------------------------------------------+
//| Class CFileBin |
//| Purpose: Class of operations with binary files |
//| Derives from class CFile |
//+------------------------------------------------------------------+
class CFileBin : public CFile
{
public:
CFileBin(void);
~CFileBin(void);
//--- methods for working with files
int Open(const string file_name,const int open_flags);
//--- methods for writing data
uint WriteChar(const char value);
uint WriteShort(const short value);
uint WriteInteger(const int value);
uint WriteLong(const long value);
uint WriteFloat(const float value);
uint WriteDouble(const double value);
uint WriteString(const string value);
uint WriteString(const string value,const int size);
uint WriteCharArray(const char &array[],const int start_item=0,const int items_count=WHOLE_ARRAY);
uint WriteShortArray(const short& array[],const int start_item=0,const int items_count=WHOLE_ARRAY);
uint WriteIntegerArray(const int& array[],const int start_item=0,const int items_count=WHOLE_ARRAY);
uint WriteLongArray(const long &array[],const int start_item=0,const int items_count=WHOLE_ARRAY);
uint WriteFloatArray(const float &array[],const int start_item=0,const int items_count=WHOLE_ARRAY);
uint WriteDoubleArray(const double &array[],const int start_item=0,const int items_count=WHOLE_ARRAY);
template<typename T>
uint WriteArray(T &array[],const int start_item=0,const int items_count=WHOLE_ARRAY);
template<typename T>
uint WriteStruct(T &data);
bool WriteObject(CObject *object);
template<typename T>
uint WriteEnum(const T value) { return(WriteInteger((int)value)); }
//--- methods for reading data
bool ReadChar(char &value);
bool ReadShort(short &value);
bool ReadInteger(int &value);
bool ReadLong(long &value);
bool ReadFloat(float &value);
bool ReadDouble(double &value);
bool ReadString(string &value);
bool ReadString(string &value,const int size);
uint ReadCharArray(char &array[],const int start_item=0,const int items_count=WHOLE_ARRAY);
uint ReadShortArray(short& array[],const int start_item=0,const int items_count=WHOLE_ARRAY);
uint ReadIntegerArray(int& array[],const int start_item=0,const int items_count=WHOLE_ARRAY);
uint ReadLongArray(long &array[],const int start_item=0,const int items_count=WHOLE_ARRAY);
uint ReadFloatArray(float &array[],const int start_item=0,const int items_count=WHOLE_ARRAY);
uint ReadDoubleArray(double &array[],const int start_item=0,const int items_count=WHOLE_ARRAY);
template<typename T>
uint ReadArray(T &array[],const int start_item=0,const int items_count=WHOLE_ARRAY);
template<typename T>
uint ReadStruct(T &data);
bool ReadObject(CObject *object);
template<typename T>
bool ReadEnum(T &value);
};
//+------------------------------------------------------------------+
//| Constructor |
//+------------------------------------------------------------------+
CFileBin::CFileBin(void)
{
}
//+------------------------------------------------------------------+
//| Destructor |
//+------------------------------------------------------------------+
CFileBin::~CFileBin(void)
{
}
//+------------------------------------------------------------------+
//| Opening a binary file |
//+------------------------------------------------------------------+
int CFileBin::Open(const string file_name,const int open_flags)
{
return(CFile::Open(file_name,open_flags|FILE_BIN));
}
//+------------------------------------------------------------------+
//| Write a variable of char or uchar type |
//+------------------------------------------------------------------+
uint CFileBin::WriteChar(const char value)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteInteger(m_handle,value,sizeof(char)));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write a variable of short or ushort type |
//+------------------------------------------------------------------+
uint CFileBin::WriteShort(const short value)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteInteger(m_handle,value,sizeof(short)));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write a variable of int or uint type |
//+------------------------------------------------------------------+
uint CFileBin::WriteInteger(const int value)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteInteger(m_handle,value,sizeof(int)));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write a variable of long or ulong type |
//+------------------------------------------------------------------+
uint CFileBin::WriteLong(const long value)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteLong(m_handle,value));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write a variable of float type |
//+------------------------------------------------------------------+
uint CFileBin::WriteFloat(const float value)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteFloat(m_handle,value));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write a variable of double type |
//+------------------------------------------------------------------+
uint CFileBin::WriteDouble(const double value)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteDouble(m_handle,value));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write a variable of string type |
//+------------------------------------------------------------------+
uint CFileBin::WriteString(const string value)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
{
//--- size of string
int size=StringLen(value);
//--- write
if(FileWriteInteger(m_handle,size)==sizeof(int))
return(FileWriteString(m_handle,value,size));
}
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write a part of string |
//+------------------------------------------------------------------+
uint CFileBin::WriteString(const string value,const int size)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteString(m_handle,value,size));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write array variables of type char or uchar |
//+------------------------------------------------------------------+
uint CFileBin::WriteCharArray(const char &array[],const int start_item,const int items_count)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteArray(m_handle,array,start_item,items_count));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write an array of variables of short or ushort type |
//+------------------------------------------------------------------+
uint CFileBin::WriteShortArray(const short &array[],const int start_item,const int items_count)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteArray(m_handle,array,start_item,items_count));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write an array of variables of int or uint type |
//+------------------------------------------------------------------+
uint CFileBin::WriteIntegerArray(const int &array[],const int start_item,const int items_count)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteArray(m_handle,array,start_item,items_count));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write an array of variables of long or ulong type |
//+------------------------------------------------------------------+
uint CFileBin::WriteLongArray(const long &array[],const int start_item,const int items_count)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteArray(m_handle,array,start_item,items_count));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write an array of variables of float type |
//+------------------------------------------------------------------+
uint CFileBin::WriteFloatArray(const float &array[],const int start_item,const int items_count)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteArray(m_handle,array,start_item,items_count));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write an array of variables of double type |
//+------------------------------------------------------------------+
uint CFileBin::WriteDoubleArray(const double &array[],const int start_item,const int items_count)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteArray(m_handle,array,start_item,items_count));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write an array of variables of any type |
//+------------------------------------------------------------------+
template<typename T>
uint CFileBin::WriteArray(T &array[],const int start_item=0,const int items_count=WHOLE_ARRAY)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteArray(m_handle,array,start_item,items_count));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write an structure |
//+------------------------------------------------------------------+
template<typename T>
uint CFileBin::WriteStruct(T &data)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteStruct(m_handle,data));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write data of an instance of the CObject class |
//+------------------------------------------------------------------+
bool CFileBin::WriteObject(CObject *object)
{
//--- check handle & object
if(m_handle!=INVALID_HANDLE)
if(CheckPointer(object))
return(object.Save(m_handle));
//--- failure
return(false);
}
//+------------------------------------------------------------------+
//| Read a variable of char or uchar type |
//+------------------------------------------------------------------+
bool CFileBin::ReadChar(char &value)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
{
ResetLastError();
value=(char)FileReadInteger(m_handle,sizeof(char));
return(GetLastError()==0);
}
//--- failure
return(false);
}
//+------------------------------------------------------------------+
//| Read a variable of short or ushort type |
//+------------------------------------------------------------------+
bool CFileBin::ReadShort(short &value)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
{
ResetLastError();
value=(short)FileReadInteger(m_handle,sizeof(short));
return(GetLastError()==0);
}
//--- failure
return(false);
}
//+------------------------------------------------------------------+
//| Read a variable of int or uint type |
//+------------------------------------------------------------------+
bool CFileBin::ReadInteger(int &value)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
{
ResetLastError();
value=FileReadInteger(m_handle,sizeof(int));
return(GetLastError()==0);
}
//--- failure
return(false);
}
//+------------------------------------------------------------------+
//| Read a variable of long or ulong type |
//+------------------------------------------------------------------+
bool CFileBin::ReadLong(long &value)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
{
ResetLastError();
value=FileReadLong(m_handle);
return(GetLastError()==0);
}
//--- failure
return(false);
}
//+------------------------------------------------------------------+
//| Read a variable of float type |
//+------------------------------------------------------------------+
bool CFileBin::ReadFloat(float &value)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
{
ResetLastError();
value=FileReadFloat(m_handle);
return(GetLastError()==0);
}
//--- failure
return(false);
}
//+------------------------------------------------------------------+
//| Read a variable of double type |
//+------------------------------------------------------------------+
bool CFileBin::ReadDouble(double &value)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
{
ResetLastError();
value=FileReadDouble(m_handle);
return(GetLastError()==0);
}
//--- failure
return(false);
}
//+------------------------------------------------------------------+
//| Read a variable of string type |
//+------------------------------------------------------------------+
bool CFileBin::ReadString(string &value)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
{
ResetLastError();
int size=FileReadInteger(m_handle);
if(GetLastError()==0)
{
value=FileReadString(m_handle,size);
return(size==StringLen(value));
}
}
//--- failure
return(false);
}
//+------------------------------------------------------------------+
//| Read a part of string |
//+------------------------------------------------------------------+
bool CFileBin::ReadString(string &value,const int size)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
{
value=FileReadString(m_handle,size);
return(size==StringLen(value));
}
//--- failure
return(false);
}
//+------------------------------------------------------------------+
//| Read an array of variables of char or uchar type |
//+------------------------------------------------------------------+
uint CFileBin::ReadCharArray(char &array[],const int start_item,const int items_count)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileReadArray(m_handle,array,start_item,items_count));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Read an array of variables of short or ushort type |
//+------------------------------------------------------------------+
uint CFileBin::ReadShortArray(short &array[],const int start_item,const int items_count)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileReadArray(m_handle,array,start_item,items_count));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Read an array of variables of int or uint type |
//+------------------------------------------------------------------+
uint CFileBin::ReadIntegerArray(int &array[],const int start_item,const int items_count)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileReadArray(m_handle,array,start_item,items_count));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Read an array of variables of long or ulong type |
//+------------------------------------------------------------------+
uint CFileBin::ReadLongArray(long &array[],const int start_item,const int items_count)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileReadArray(m_handle,array,start_item,items_count));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Read an array of variables of float type |
//+------------------------------------------------------------------+
uint CFileBin::ReadFloatArray(float &array[],const int start_item,const int items_count)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileReadArray(m_handle,array,start_item,items_count));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Read an array of variables of double type |
//+------------------------------------------------------------------+
uint CFileBin::ReadDoubleArray(double &array[],const int start_item,const int items_count)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileReadArray(m_handle,array,start_item,items_count));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Read an array of variables of any type |
//+------------------------------------------------------------------+
template<typename T>
uint CFileBin::ReadArray(T &array[],const int start_item=0,const int items_count=WHOLE_ARRAY)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileReadArray(m_handle,array,start_item,items_count));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Read an structure |
//+------------------------------------------------------------------+
template<typename T>
uint CFileBin::ReadStruct(T &data)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileReadStruct(m_handle,data));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Read data of an instance of the CObject class |
//+------------------------------------------------------------------+
bool CFileBin::ReadObject(CObject *object)
{
//--- check handle & object
if(m_handle!=INVALID_HANDLE)
if(CheckPointer(object))
return(object.Load(m_handle));
//--- failure
return(false);
}
//+------------------------------------------------------------------+
//| Read a variable of an enumeration type |
//+------------------------------------------------------------------+
template<typename T>
bool CFileBin::ReadEnum(T &value)
{
int val;
if(!ReadInteger(val))
return(false);
//---
value=(T)val;
return(true);
}
//+------------------------------------------------------------------+
+347
View File
@@ -0,0 +1,347 @@
//+------------------------------------------------------------------+
//| FilePipe.mqh |
//| Copyright 2000-2024, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#include "File.mqh"
//+------------------------------------------------------------------+
//| Class CFilePipe |
//| Purpose: Class of operations with binary files |
//| Derives from class CFile |
//+------------------------------------------------------------------+
class CFilePipe : public CFile
{
public:
CFilePipe(void);
~CFilePipe(void);
//--- methods for working with files
int Open(const string file_name,const int open_flags);
//--- wait for incoming data
bool WaitForRead(const ulong size);
//--- methods for writing data
template<typename T>
uint WriteInteger(const T value);
uint WriteLong(const long value);
uint WriteFloat(const float value);
uint WriteDouble(const double value);
uint WriteString(const string value);
uint WriteString(const string value,const int size);
template<typename T>
uint WriteArray(T &array[],const int start_item=0,const int items_count=WHOLE_ARRAY);
template<typename T>
uint WriteStruct(T &data);
bool WriteObject(CObject *object);
template<typename T>
uint WriteEnum(const T value) { return(WriteInteger((int)value)); }
//--- methods for reading data
template<typename T>
bool ReadInteger(T &value);
bool ReadLong(long &value);
bool ReadFloat(float &value);
bool ReadDouble(double &value);
bool ReadString(string &value);
bool ReadString(string &value,const int size);
template<typename T>
uint ReadArray(T &array[],const int start_item=0,const int items_count=WHOLE_ARRAY);
template<typename T>
uint ReadStruct(T &data);
bool ReadObject(CObject *object);
template<typename T>
bool ReadEnum(T &value);
};
//+------------------------------------------------------------------+
//| Constructor |
//+------------------------------------------------------------------+
CFilePipe::CFilePipe(void)
{
}
//+------------------------------------------------------------------+
//| Destructor |
//+------------------------------------------------------------------+
CFilePipe::~CFilePipe(void)
{
}
//+------------------------------------------------------------------+
//| Opening a binary file |
//+------------------------------------------------------------------+
int CFilePipe::Open(const string file_name,const int open_flags)
{
return(CFile::Open(file_name,open_flags|FILE_BIN));
}
//+------------------------------------------------------------------+
//| Wait for incoming data |
//+------------------------------------------------------------------+
bool CFilePipe::WaitForRead(const ulong size)
{
//--- check handle and stop flag
while(m_handle!=INVALID_HANDLE && !IsStopped())
{
//--- enought data?
if(FileSize(m_handle)>=size)
return(true);
//--- wait a little
Sleep(1);
}
//--- failure
return(false);
}
//+------------------------------------------------------------------+
//| Write a variable of integer types |
//+------------------------------------------------------------------+
template<typename T>
uint CFilePipe::WriteInteger(const T value)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteInteger(m_handle,value,sizeof(T)));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write a variable of long or ulong type |
//+------------------------------------------------------------------+
uint CFilePipe::WriteLong(const long value)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteLong(m_handle,value));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write a variable of float type |
//+------------------------------------------------------------------+
uint CFilePipe::WriteFloat(const float value)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteFloat(m_handle,value));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write a variable of double type |
//+------------------------------------------------------------------+
uint CFilePipe::WriteDouble(const double value)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteDouble(m_handle,value));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write a variable of string type |
//+------------------------------------------------------------------+
uint CFilePipe::WriteString(const string value)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
{
//--- size of string
int size=StringLen(value);
//--- write
if(FileWriteInteger(m_handle,size)==sizeof(int))
return(FileWriteString(m_handle,value,size));
}
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write a part of string |
//+------------------------------------------------------------------+
uint CFilePipe::WriteString(const string value,const int size)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteString(m_handle,value,size));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write an array of variables of any type |
//+------------------------------------------------------------------+
template<typename T>
uint CFilePipe::WriteArray(T &array[],const int start_item=0,const int items_count=WHOLE_ARRAY)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteArray(m_handle,array,start_item,items_count));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write an structure |
//+------------------------------------------------------------------+
template<typename T>
uint CFilePipe::WriteStruct(T &data)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteStruct(m_handle,data));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Write data of an instance of the CObject class |
//+------------------------------------------------------------------+
bool CFilePipe::WriteObject(CObject *object)
{
//--- check handle & object
if(m_handle!=INVALID_HANDLE)
if(CheckPointer(object))
return(object.Save(m_handle));
//--- failure
return(false);
}
//+------------------------------------------------------------------+
//| Read a variable of integer types |
//+------------------------------------------------------------------+
template<typename T>
bool CFilePipe::ReadInteger(T &value)
{
//--- check for data
if(WaitForRead(sizeof(T)))
{
ResetLastError();
value=FileReadInteger(m_handle,sizeof(T));
return(GetLastError()==0);
}
//--- failure
return(false);
}
//+------------------------------------------------------------------+
//| Read a variable of long or ulong type |
//+------------------------------------------------------------------+
bool CFilePipe::ReadLong(long &value)
{
//--- check handle
if(WaitForRead(sizeof(long)))
{
ResetLastError();
value=FileReadLong(m_handle);
return(GetLastError()==0);
}
//--- failure
return(false);
}
//+------------------------------------------------------------------+
//| Read a variable of float type |
//+------------------------------------------------------------------+
bool CFilePipe::ReadFloat(float &value)
{
//--- check for data
if(WaitForRead(sizeof(float)))
{
ResetLastError();
value=FileReadFloat(m_handle);
return(GetLastError()==0);
}
//--- failure
return(false);
}
//+------------------------------------------------------------------+
//| Read a variable of double type |
//+------------------------------------------------------------------+
bool CFilePipe::ReadDouble(double &value)
{
//--- check for data
if(WaitForRead(sizeof(double)))
{
ResetLastError();
value=FileReadDouble(m_handle);
return(GetLastError()==0);
}
//--- failure
return(false);
}
//+------------------------------------------------------------------+
//| Read a variable of string type |
//+------------------------------------------------------------------+
bool CFilePipe::ReadString(string &value)
{
//--- check for data
if(WaitForRead(sizeof(int)))
{
ResetLastError();
int size=FileReadInteger(m_handle);
if(GetLastError()==0)
{
//--- check for data
if(WaitForRead(size))
{
value=FileReadString(m_handle,size);
return(size==StringLen(value));
}
}
}
//--- failure
return(false);
}
//+------------------------------------------------------------------+
//| Read a part of string |
//+------------------------------------------------------------------+
bool CFilePipe::ReadString(string &value,const int size)
{
//--- check for data
if(WaitForRead(size))
{
value=FileReadString(m_handle,size);
return(size==StringLen(value));
}
//--- failure
return(false);
}
//+------------------------------------------------------------------+
//| Read an array of variables of any type |
//+------------------------------------------------------------------+
template<typename T>
uint CFilePipe::ReadArray(T &array[],const int start_item=0,const int items_count=WHOLE_ARRAY)
{
//--- calculate size
uint size=ArraySize(array);
if(items_count!=WHOLE_ARRAY) size=items_count;
//--- check for data
if(WaitForRead(size*sizeof(T)))
return(FileReadArray(m_handle,array,start_item,items_count));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Read an structure |
//+------------------------------------------------------------------+
template<typename T>
uint CFilePipe::ReadStruct(T &data)
{
//--- check for data
if(WaitForRead(sizeof(T)))
return(FileReadStruct(m_handle,data));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Read data of an instance of the CObject class |
//+------------------------------------------------------------------+
bool CFilePipe::ReadObject(CObject *object)
{
//--- check for object & data
if(CheckPointer(object))
if(WaitForRead(sizeof(int))) // only 4 bytes!
return(object.Load(m_handle));
//--- failure
return(false);
}
//+------------------------------------------------------------------+
//| Read a variable of an enumeration type |
//+------------------------------------------------------------------+
template<typename T>
bool CFilePipe::ReadEnum(T &value)
{
int val;
if(!ReadInteger(val))
return(false);
//---
value=(T)val;
return(true);
}
//+------------------------------------------------------------------+
+64
View File
@@ -0,0 +1,64 @@
//+------------------------------------------------------------------+
//| FileTxt.mqh |
//| Copyright 2000-2024, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#include "File.mqh"
//+------------------------------------------------------------------+
//| Class CFileTxt |
//| Purpose: Class of operations with text files. |
//| Derives from class CFile. |
//+------------------------------------------------------------------+
class CFileTxt : public CFile
{
public:
CFileTxt(void);
~CFileTxt(void);
//--- methods for working with files
int Open(const string file_name,const int open_flags);
//--- methods to access data
uint WriteString(const string value);
string ReadString(void);
};
//+------------------------------------------------------------------+
//| Constructor |
//+------------------------------------------------------------------+
CFileTxt::CFileTxt(void)
{
}
//+------------------------------------------------------------------+
//| Destructor |
//+------------------------------------------------------------------+
CFileTxt::~CFileTxt(void)
{
}
//+------------------------------------------------------------------+
//| Open the text file |
//+------------------------------------------------------------------+
int CFileTxt::Open(const string file_name,const int open_flags)
{
return(CFile::Open(file_name,open_flags|FILE_TXT));
}
//+------------------------------------------------------------------+
//| Writing string to file |
//+------------------------------------------------------------------+
uint CFileTxt::WriteString(const string value)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileWriteString(m_handle,value));
//--- failure
return(0);
}
//+------------------------------------------------------------------+
//| Reading string from file |
//+------------------------------------------------------------------+
string CFileTxt::ReadString(void)
{
//--- check handle
if(m_handle!=INVALID_HANDLE)
return(FileReadString(m_handle));
//--- failure
return("");
}
//+------------------------------------------------------------------+