MQLArticles/Utils/File/FolderOps/Base.mqh
Nique_372 03cdb90103
2026-03-27 16:34:14 -05:00

241 lines
8.4 KiB
MQL5

//+------------------------------------------------------------------+
//| Base.mqh |
//| Copyright 2026, Niquel Mendoza. |
//| https://www.mql5.com/es/users/nique_372 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Niquel Mendoza."
#property link "https://www.mql5.com/es/users/nique_372"
#property strict
#ifndef MQLARTICLES_UTILS_FILE_FOLDEROPS_BASE_MQH
#define MQLARTICLES_UTILS_FILE_FOLDEROPS_BASE_MQH
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#include "..\\..\\StrMatch\\StrSimpleMatch.mqh"
#include "..\\..\\Basic.mqh"
#include "..\\\..\\..\\..\\fast_json\\fast_json.mqh"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CFolderOpsBase : public CLoggerBase
{
protected:
//---
string m_arr_clean_inlcluyed[];
int m_arr_clean_inlcluyed_size;
string m_arr_clean_excluyed[];
int m_arr_clean_excluyed_size;
public:
CFolderOpsBase(void);
~CFolderOpsBase(void) {}
//---
bool IsFileIncluyed(const string& json_file_name);
bool IsFileExcluyed(const string& json_file_name);
//---
// Size
__forceinline int SizeArrInclued() const { return m_arr_clean_inlcluyed_size; }
__forceinline int SizeArrExcluyed() const { return m_arr_clean_excluyed_size; }
// Resize
void SizeArrInclued(int size) { m_arr_clean_inlcluyed_size = ArrayResize(m_arr_clean_inlcluyed, size); }
void SizeArrExcluyed(int size) { m_arr_clean_excluyed_size = ArrayResize(m_arr_clean_excluyed, size); }
// Set val
void SetValIncluyed(int pos, const string& val) { m_arr_clean_inlcluyed[pos] = val; }
void SetValExluyed(int pos, const string& val) { m_arr_clean_excluyed[pos] = val; }
void SetValIncluyedNoRef(int pos, const string val) { m_arr_clean_inlcluyed[pos] = val; }
void SetValExluyedNoRef(int pos, const string val) { m_arr_clean_excluyed[pos] = val; }
// clean
void Clean();
//---
bool Load(const string& json_file_name, bool comon_flag, CJson& json_parser);
bool Save(const string& json_file_name, int extra_file_flags, CJsonBuilder& json_builder) const;
//---
void Summary() const;
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CFolderOpsBase::CFolderOpsBase(void)
{
m_arr_clean_inlcluyed_size = ArrayResize(m_arr_clean_inlcluyed, 0);
m_arr_clean_excluyed_size = ArrayResize(m_arr_clean_excluyed, 0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CFolderOpsBase::Load(const string &json_file_name, bool comon_flag, CJson &json_parser)
{
//---
::ResetLastError();
const int fh = FileOpen(json_file_name, FILE_READ | FILE_BIN | (comon_flag ? FILE_COMMON : 0), 0);
if(fh == INVALID_HANDLE)
{
LogError(StringFormat("Fallo al abrir el archivo = %s, ultimo error = %d", json_file_name, ::GetLastError()), FUNCION_ACTUAL);
return false;
}
//---
const ulong file_size = FileSize(fh);
string file_content = ::FileReadString(fh, int(file_size));
::FileClose(fh);
if(file_content.GetChar(0) == 0xFEFF) // check si el primero es boom
file_content.SetChar(0, ' ');
//---
if(!json_parser.Parse(file_content))
{
LogError(::StringFormat("Fallo al parsear json ultimo error = %s", ::EnumToString(EnumJsonError(json_parser.GetLastError()))), FUNCION_ACTUAL);
return false;
}
//--- Inclusion
if(!json_parser["incluyed"].IsValid())
{
m_arr_clean_inlcluyed_size = ArrayResize(m_arr_clean_inlcluyed, 0);
//---
LogWarning("No se ha encontrado el campo 'incluyed' en el json config, todos los archivos se omitiran para su uso", FUNCION_ACTUAL);
}
else
{
CJsonNode incluyed = json_parser["incluyed"];
//---
m_arr_clean_inlcluyed_size = ArrayResize(m_arr_clean_inlcluyed, incluyed.Size());
for(int i = 0; i < m_arr_clean_inlcluyed_size; i++)
{
m_arr_clean_inlcluyed[i] = incluyed[i].ToString();
}
}
//--- Exclusion
if(!json_parser["excluyed"].IsValid())
{
m_arr_clean_excluyed_size = ArrayResize(m_arr_clean_excluyed, 0);
//---
LogWarning("No se ha encontrado el campo 'excluyed' en el json config, ningun archivo se excluira para su uso", FUNCION_ACTUAL);
}
else
{
CJsonNode excluyed = json_parser["excluyed"];
//---
m_arr_clean_excluyed_size = ArrayResize(m_arr_clean_excluyed, excluyed.Size());
for(int i = 0; i < m_arr_clean_excluyed_size; i++)
{
m_arr_clean_excluyed[i] = excluyed[i].ToString();
}
}
//---
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CFolderOpsBase::Save(const string &json_file_name, int extra_file_flags, CJsonBuilder &json_builder) const
{
//---
::ResetLastError();
const int fh = FileOpen(json_file_name, FILE_WRITE | FILE_TXT | extra_file_flags, 0);
if(fh == INVALID_HANDLE)
{
LogError(StringFormat("Fallo al abrir el archivo = %s, ultimo error = %d", json_file_name, ::GetLastError()), FUNCION_ACTUAL);
return false;
}
//---
json_builder.Obj();
//--- Inclusion
json_builder.Key("incluyed").Arr();
for(int i = 0; i < m_arr_clean_inlcluyed_size; i++)
{
json_builder.Val(m_arr_clean_inlcluyed[i]);
}
json_builder.EndArr();
//--- Exclusion
json_builder.Key("excluyed").Arr();
for(int i = 0; i < m_arr_clean_excluyed_size; i++)
{
json_builder.Val(m_arr_clean_excluyed[i]);
}
json_builder.EndArr();
//---
json_builder.EndObj();
//---
::FileWriteString(fh, json_builder.Build());
::FileClose(fh);
//---
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CFolderOpsBase::IsFileExcluyed(const string & json_file_name)
{
for(int i = 0; i < m_arr_clean_excluyed_size; i++)
{
if(CSimpleStringMatch::SimpleMatchInString(json_file_name, m_arr_clean_excluyed[i]))
return true;
}
return false;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CFolderOpsBase::IsFileIncluyed(const string & json_file_name)
{
for(int i = 0; i < m_arr_clean_inlcluyed_size; i++)
{
if(CSimpleStringMatch::SimpleMatchInString(json_file_name, m_arr_clean_inlcluyed[i]))
return true;
}
return false;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CFolderOpsBase::Summary() const
{
Print("----- CFolderOpsBase info");
Print("Incluyed: ");
Print(ArrayStrToString(m_arr_clean_inlcluyed, " | "));
Print("Excluyed: ");
Print(ArrayStrToString(m_arr_clean_excluyed, " | "));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CFolderOpsBase::Clean(void)
{
m_arr_clean_inlcluyed_size = ArrayResize(m_arr_clean_inlcluyed, 0);
m_arr_clean_excluyed_size = ArrayResize(m_arr_clean_excluyed, 0);
}
#endif // MQLARTICLES_UTILS_FILE_FOLDEROPS_BASE_MQH
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+