MQLArticles/Utils/File/FolderOps/Base.mqh
257 líneas
9,3 KiB
MQL5
Nique_372
2026-03-27 09:40:26 -05:00
//+------------------------------------------------------------------+
//| 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
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Nique_372
2026-10-22 21:57:02 -05:00
#include "Def.mqh"
Nique_372
2026-03-27 09:40:26 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Nique_372
2026-10-22 21:57:02 -05:00
// MQLARTICLES_FOLDEROPS_ENABLE_LOGS
Nique_372
2026-06-03 18:52:51 -05:00
namespace TSN
{
Nique_372
2026-10-22 21:57:02 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CFolderOpsBase
Nique_372
2026-03-27 09:40:26 -05:00
{
protected:
string m_arr_clean_inlcluyed[];
int m_arr_clean_inlcluyed_size;
string m_arr_clean_excluyed[];
int m_arr_clean_excluyed_size;
Nique_372
2026-10-22 21:57:02 -05:00
//---
ENUM_FILE_FOLDEOPS_ERR m_last_err;
ENUM_FILE_FOLDEROPS_WARN m_warns[FILEFOLDEROPS_MAX_WARNS];
int m_warns_s;
Nique_372
2026-03-27 09:40:26 -05:00
public:
CFolderOpsBase(void);
~CFolderOpsBase(void) {}
Nique_372
2026-03-27 16:34:14 -05:00
//---
Nique_372
2026-09-02 15:54:33 -05:00
bool IsFileIncluyed(const string& file_name);
bool IsFileExcluyed(const string& file_name);
Nique_372
2026-03-27 16:34:14 -05:00
Nique_372
2026-03-27 09:40:26 -05:00
//---
Nique_372
2026-03-27 15:29:57 -05:00
// Size
Nique_372
2026-03-27 09:40:26 -05:00
__forceinline int SizeArrInclued() const { return m_arr_clean_inlcluyed_size; }
__forceinline int SizeArrExcluyed() const { return m_arr_clean_excluyed_size; }
Nique_372
2026-03-27 15:29:57 -05:00
// Resize
Nique_372
2026-03-27 09:40:26 -05:00
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); }
Nique_372
2026-03-27 15:29:57 -05:00
Nique_372
2026-04-30 15:56:25 -05:00
//
void SetArrIncluyed(const string& str, ushort sep = '|');
void SetArrExcluyed(const string& str, ushort sep = '|');
Nique_372
2026-03-27 15:29:57 -05:00
// Set val
Nique_372
2026-03-27 16:34:14 -05:00
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; }
Nique_372
2026-03-27 15:29:57 -05:00
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();
//---
Nique_372
2026-06-03 18:52:51 -05:00
bool Load(const string& json_file_name, bool comon_flag, CJsonParser& json_parser);
Nique_372
2026-10-22 21:57:02 -05:00
bool Save(const string& json_file_name, int extra_file_flags, CJsonBuilder& json_builder);
Nique_372
2026-03-27 15:29:57 -05:00
//---
void Summary() const;
Nique_372
2026-10-22 21:57:02 -05:00
//---
__forceinline ENUM_FILE_FOLDEOPS_ERR LastErr() const { return m_last_err; }
__forceinline int TotalWarns() const { return m_warns_s; }
__forceinline ENUM_FILE_FOLDEROPS_WARN Warn(const int i) const { return m_warns[i]; }
Nique_372
2026-03-27 09:40:26 -05:00
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CFolderOpsBase::CFolderOpsBase(void)
Nique_372
2026-10-22 21:57:02 -05:00
: m_arr_clean_inlcluyed_size(0), m_arr_clean_excluyed_size(0), m_last_err(FILE_FOLDEOPS_ERR_NOT), m_warns_s(0)
Nique_372
2026-03-27 09:40:26 -05:00
{
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Nique_372
2026-06-03 18:52:51 -05:00
bool CFolderOpsBase::Load(const string &json_file_name, bool comon_flag, CJsonParser &json_parser)
Nique_372
2026-03-27 15:29:57 -05:00
{
Nique_372
2026-10-22 21:57:02 -05:00
//---
m_warns_s = 0;
Nique_372
2026-03-27 15:29:57 -05:00
//---
Nique_372
2026-07-12 22:32:11 -05:00
const int ext = json_parser.AssingFile(json_file_name, comon_flag);
if(!json_parser.AutoParseByFileExt(ext))
Nique_372
2026-03-27 15:29:57 -05:00
{
Nique_372
2026-10-22 21:57:02 -05:00
m_last_err = FILE_FOLDEOPS_ERR_PARSE_JSON;
Nique_372
2026-03-27 15:29:57 -05:00
return false;
}
//---
Nique_372
2026-06-03 18:52:51 -05:00
CJsonNode root = json_parser.GetRoot();
Nique_372
2026-03-27 15:29:57 -05:00
//--- Inclusion
Nique_372
2026-06-03 18:52:51 -05:00
CJsonNode incluyed = root["incluyed"];
if(!incluyed.IsValid())
Nique_372
2026-03-27 15:29:57 -05:00
{
m_arr_clean_inlcluyed_size = ArrayResize(m_arr_clean_inlcluyed, 0);
Nique_372
2026-10-22 21:57:02 -05:00
FILEFOLDESOPS_PUSH_WARN(FILE_FOLDEROPS_WARN_INCLUED_CAMP_NOT_FOUND)
#ifdef MQLARTICLES_FOLDEROPS_ENABLE_LOGS
FastLog(FUNCION_ACTUAL, WARNING_TEXT, "No se ha encontrado el campo 'incluyed' en el json config, todos los archivos se omitiran para su uso");
#endif // MQLARTICLES_FOLDEROPS_ENABLE_LOGS
Nique_372
2026-03-27 15:29:57 -05:00
}
else
{
Nique_372
2026-10-22 21:57:02 -05:00
m_arr_clean_inlcluyed_size = incluyed.ToArray(m_arr_clean_inlcluyed);
Nique_372
2026-03-27 15:29:57 -05:00
}
//--- Exclusion
Nique_372
2026-06-03 18:52:51 -05:00
CJsonNode excluyed = root["excluyed"];
if(!excluyed.IsValid())
Nique_372
2026-03-27 15:29:57 -05:00
{
m_arr_clean_excluyed_size = ArrayResize(m_arr_clean_excluyed, 0);
Nique_372
2026-10-22 21:57:02 -05:00
FILEFOLDESOPS_PUSH_WARN(FILE_FOLDEROPS_WARN_EXCLUYED_CAMP_NOT_FOUND)
#ifdef MQLARTICLES_FOLDEROPS_ENABLE_LOGS
FastLog(FUNCION_ACTUAL, WARNING_TEXT, "No se ha encontrado el campo 'excluyed' en el json config, ningun archivo se excluira para su uso");
#endif // MQLARTICLES_FOLDEROPS_ENABLE_LOGS
Nique_372
2026-03-27 15:29:57 -05:00
}
else
{
Nique_372
2026-10-22 21:57:02 -05:00
m_arr_clean_excluyed_size = excluyed.ToArray(m_arr_clean_excluyed);
Nique_372
2026-03-27 15:29:57 -05:00
}
//---
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Nique_372
2026-10-22 21:57:02 -05:00
bool CFolderOpsBase::Save(const string &json_file_name, int extra_file_flags, CJsonBuilder &json_builder)
Nique_372
2026-03-27 15:29:57 -05:00
{
//---
::ResetLastError();
const int fh = FileOpen(json_file_name, FILE_WRITE | FILE_TXT | extra_file_flags, 0);
if(fh == INVALID_HANDLE)
{
Nique_372
2026-10-22 21:57:02 -05:00
m_last_err = FILE_FOLDEOPS_ERR_OPENFILE;
Nique_372
2026-03-27 15:29:57 -05:00
return false;
}
//---
json_builder.Obj();
Nique_372
2026-03-27 16:34:14 -05:00
Nique_372
2026-03-27 15:29:57 -05:00
//--- Inclusion
json_builder.Key("incluyed").Arr();
for(int i = 0; i < m_arr_clean_inlcluyed_size; i++)
Nique_372
2026-06-03 18:52:51 -05:00
json_builder.ValS(m_arr_clean_inlcluyed[i]);
Nique_372
2026-03-27 15:29:57 -05:00
json_builder.EndArr();
//--- Exclusion
json_builder.Key("excluyed").Arr();
for(int i = 0; i < m_arr_clean_excluyed_size; i++)
Nique_372
2026-06-03 18:52:51 -05:00
json_builder.ValS(m_arr_clean_excluyed[i]);
Nique_372
2026-03-27 15:29:57 -05:00
json_builder.EndArr();
//---
json_builder.EndObj();
//---
::FileWriteString(fh, json_builder.Build());
::FileClose(fh);
//---
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Nique_372
2026-09-02 15:54:33 -05:00
bool CFolderOpsBase::IsFileExcluyed(const string & file_name)
Nique_372
2026-03-27 09:40:26 -05:00
{
for(int i = 0; i < m_arr_clean_excluyed_size; i++)
{
Nique_372
2026-09-02 15:54:33 -05:00
if(CSimpleStringMatch::SimpleMatchInString(file_name, m_arr_clean_excluyed[i]))
Nique_372
2026-03-27 09:40:26 -05:00
return true;
}
return false;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Nique_372
2026-09-02 15:54:33 -05:00
bool CFolderOpsBase::IsFileIncluyed(const string & file_name)
Nique_372
2026-03-27 09:40:26 -05:00
{
for(int i = 0; i < m_arr_clean_inlcluyed_size; i++)
{
Nique_372
2026-09-02 15:54:33 -05:00
if(CSimpleStringMatch::SimpleMatchInString(file_name, m_arr_clean_inlcluyed[i]))
Nique_372
2026-03-27 09:40:26 -05:00
return true;
}
return false;
}
Nique_372
2026-04-30 15:56:25 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CFolderOpsBase::SetArrExcluyed(const string &str, ushort sep)
{
m_arr_clean_excluyed_size = StringSplit(str, sep, m_arr_clean_excluyed);
if(m_arr_clean_excluyed_size <= -1)
m_arr_clean_excluyed_size = 0;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Nique_372
2026-06-03 18:52:51 -05:00
void CFolderOpsBase::SetArrIncluyed(const string &str, ushort sep)
Nique_372
2026-04-30 15:56:25 -05:00
{
m_arr_clean_inlcluyed_size = StringSplit(str, sep, m_arr_clean_inlcluyed);
if(m_arr_clean_inlcluyed_size <= -1)
m_arr_clean_inlcluyed_size = 0;
}
Nique_372
2026-03-27 15:29:57 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CFolderOpsBase::Summary() const
{
Print("----- CFolderOpsBase info");
Nique_372
2026-06-03 18:52:51 -05:00
Print("Incluyed: [", m_arr_clean_inlcluyed_size, "]");
Nique_372
2026-10-22 21:57:02 -05:00
Print(ArrayStrToString(m_arr_clean_inlcluyed, "|", m_arr_clean_inlcluyed_size));
Print("Excluyed: [", m_arr_clean_excluyed_size, "]");
Print(ArrayStrToString(m_arr_clean_excluyed, "|", m_arr_clean_excluyed_size));
Nique_372
2026-03-27 15:29:57 -05:00
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CFolderOpsBase::Clean(void)
{
Nique_372
2026-10-22 21:57:02 -05:00
m_arr_clean_inlcluyed_size = 0;
m_arr_clean_excluyed_size = 0;
Nique_372
2026-03-27 15:29:57 -05:00
}
Nique_372
2026-06-03 18:52:51 -05:00
}
Nique_372
2026-03-27 09:40:26 -05:00
#endif // MQLARTICLES_UTILS_FILE_FOLDEROPS_BASE_MQH
//+------------------------------------------------------------------+
Nique_372
2026-03-27 15:29:57 -05:00
//+------------------------------------------------------------------+