Libraries: File Mapping without DLL - page 9

 

Thanks to the author for the library!

I made functions for transferring any data. The script below shows their work on the example of ticks

#include <MemMapLib.mqh>
#include <TypeToBytes.mqh>

// Allocates memory of specified length for data 
template <typename T>
bool GetFileMemory( CMemMapFile* &FileMemory, const int Amount, const string FileName = "Local\\test" )
{
  FileMemory = new CMemMapFile;
    
  return(FileMemory.Open(FileName, sizeof(T) * Amount + sizeof(int) + HEAD_MEM, modeCreate) == 0);
}

// Writes data to memory
template <typename T>
void DataSave( CMemMapFile* FileMemory, const T &Data[], const bool FromBegin = true  )
{
  const int Size = ArraySize(Data) * sizeof(T);
  uchar Bytes[];
  
  _ArrayCopy(Bytes, _R(Size).Bytes);              // Recorded the quantity 
  _ArrayCopy(Bytes, _R(Data).Bytes, sizeof(int)); // Recorded the data

  if (FromBegin)
    FileMemory.Seek(0, SEEK_SET);

  FileMemory.Write(Bytes, ArraySize(Bytes)); // Dumped everything into memory
  
  return;
}

// Reads data from memory
template <typename T>
int DataLoad( CMemMapFile* FileMemory, T &Data[], const bool FromBegin = true )
{
  if (FromBegin)
    FileMemory.Seek(0, SEEK_SET);

  uchar Bytes[];
          
  FileMemory.Read(Bytes, sizeof(int));  // Read the amount of data from memory 
  FileMemory.Read(Bytes, _R(Bytes)[0]); // Got the data itself

  _ArrayCopy(Data, Bytes);              // Dumped the data into an array
  
  return(ArraySize(Data));
}

#define  AMOUNT 1000

#define  TOSTRING(A) #A + " = " + (string)(A) + " "

// Example of tick transmission
void OnStart()
{  
  CMemMapFile* FileMemory;
  
  if (GetFileMemory<MqlTick>(FileMemory, AMOUNT))
  {
    MqlTick Ticks4Save[];    
    CopyTicks(_Symbol, Ticks4Save, COPY_TICKS_INFO, 0, AMOUNT);
    
    DataSave(FileMemory, Ticks4Save);
    
    MqlTick Ticks4Load[];    
    
    if (DataLoad(FileMemory, Ticks4Load) > 0)    
      Print(TOSTRING((_R(Ticks4Save) == Ticks4Load)) +
            TOSTRING(ArraySize(Ticks4Save)) +
            TOSTRING(ArraySize(Ticks4Load)));
     
    FileMemory.Close();   
  }
  
  delete FileMemory;
}


Result

(_R(Ticks4Save)==Ticks4Load) = true ArraySize(Ticks4Save) = 1000 ArraySize(Ticks4Load) = 1000
 

Version update

1.02 - changed copying of structures to union

Files:
MemMapLib.mqh  31 kb
 
o_o:

Version update

1.02 - changed copying of structures to union

Replaced the file and published
 
o_o:
Maybe you didn't take into account that here in the class the first bytes are allocated for the header, where the file size is stored?

Could you please tell me if I'm doing it right?

I want to write/read double. Since mql4 does not know how to copy structures into each other, I did otherwise.

In your script there were no such calls with such types of parameters and I added them:

	// 64
        long memcpy(double &Destination[], uchar &Source[], int Length);
        long memcpy(uchar &Destination[], double &Source[], int Length);

And this code worked without errors:

void OnStart()
  {
   CMemMapFile hmem;
   long err=hmem.Open("Local\\test",111,modeCreate);

   double src[5]={1.2,3.4,5.6,7.8,9.0};
   int cnt=sizeof(double)*ArraySize(src);
   uchar data[5*8];
   memcpy(data,src,cnt);
   err=hmem.Write(data,ArraySize(data));



   ArrayInitialize(data,0);
   hmem.Seek(0,SEEK_SET);
   err=hmem.Read(data,ArraySize(data));

   ArrayInitialize(src,0);
   memcpy(src,data,cnt);
   
   hmem.Close();
  }


1. Can I use memcpy with such parameter types? If "it's all wrong, let's do it again" no, then how to write/read double at all?

2. Will this work for 32 bits?

 
Denis Lysenko:

I want to write/read double. Since mql4 does not know how to copy structures into each other, I did otherwise.

now you can copy structures with the help of union


1. Can I use memcpy with such types of parameters?

2. Will this work for 32 bits?

yes

 
Customer:

If last trade loss they raise lot


2 trades per sesion but open and close example open 8.00 GBP /usd and closed 16 .00 gbp /usd

 
o_o:

structures can now be copied using union


For some reason the previous comment has been erased.

   union dbl_u
     {
      double            value;
     };
   union uchar_u
     {
      uchar              value[8];
     };
     
   dbl_u dbl;
   uchar_u chr;
   
   dbl = chr;
It returns: '=' - illegal operation use
 
Denis Lysenko:

For some reason the previous comment has been erased.

It gives: '=' - illegal operation use
Read the manual. Unions do not use this way.
 

Please help me to understand why the following code does not work:

#include <MemMapLib.mqh>

//------------------------------------------------------------------ OnStart
void OnStart()
{
        CMemMapFile hmem;
        long err=hmem.Open("Local\\test",111,modeCreate);
        
        uchar data[],data2[];
        StringToCharArray("0.12243;0.44565;1.32452",data);
        err=hmem.Write(data,ArraySize(data));

   ArrayResize(data2,ArraySize(data));
        ArrayInitialize(data2,0);
        hmem.Seek(0,SEEK_SET);
        err=hmem.Read(data2,ArraySize(data2));
        
        Print("Read result=",CharArrayToString(data2));
        
        hmem.Close();
        
   Print("New cycle of open memory file");
   err=hmem.Open("Local\\test",111,modeOpen);
   Print("err1=",err);  
        
        ArrayInitialize(data2,0);
        hmem.Seek(0,SEEK_SET);
        err=hmem.Read(data2,ArraySize(data2));
        Print("err2=",err);     
        
        Print("after reading data2=",CharArrayToString(data2));
        
        hmem.Close();   
                
}

The result of the code:

2017.09.20 11:13:54.981 memmap_MY_02_question EURUSD,H1: after reading data2=
2017.09.20 11:13:54.981 memmap_MY_02_question EURUSD,H1: err2=-1
2017.09.20 11:13:54.981 memmap_MY_02_question EURUSD,H1: err1=0
2017.09.20 11:13:54.981 memmap_MY_02_question EURUSD,H1: New cycle of open memory file
2017.09.20 11:13:54.981 memmap_MY_02_question EURUSD,H1: Read result=0.12243;0.44565;1.32452

Why can't I read the contents of a file in memory after reopening it?

I run the script in MT4 terminal.

 
Иван:

Why can't I read the contents of a file in memory after reopening it?

At least you don't try to read a file in memory after restarting the computer...