forked from nique_372/MQLArticles
64 lines
No EOL
5.4 KiB
MQL5
64 lines
No EOL
5.4 KiB
MQL5
//+------------------------------------------------------------------+
| |
//| StringToArray.mqh |
| |
//| Copyright 2025, Niquel Mendoza. |
| |
//| https://www.mql5.com |
| |
//+------------------------------------------------------------------+
| |
#property copyright "Copyright 2025, Niquel Mendoza."
| |
#property link "https://www.mql5.com"
| |
#property strict
| |
| |
#ifndef MQLARTICLES_UTILS_FA_STRINGTOARRAY_MQH
| |
#define MQLARTICLES_UTILS_FA_STRINGTOARRAY_MQH
| |
| |
//+------------------------------------------------------------------+
| |
//| Macro para generar funciones dentro del namespace |
| |
//+------------------------------------------------------------------+
| |
class StrTo
| |
{
| |
public:
| |
#define DEFINE_STRING_TO_ARRAY(TYPE, CONVERT_FUNC) \
| |
static bool CstArray(TYPE &array_receptor[], const string &cadena, ushort cadena_separator = ',', bool trim = false) \
| |
{ \
| |
string result[]; \
| |
ResetLastError(); \
| |
int num = StringSplit(cadena, cadena_separator, result); \
| |
if(num == -1) \
| |
return false; \
| |
\
| |
if(ArrayResize(array_receptor, ArraySize(result)) == -1) \
| |
return false; \
| |
\
| |
for(int i = 0; i < ArraySize(array_receptor); i++) \
| |
{ \
| |
if(trim)\
| |
{ \
| |
StringTrimLeft(result[i]); \
| |
StringTrimRight(result[i]); \
| |
} \
| |
array_receptor[i] = CONVERT_FUNC; \
| |
} \
| |
return true; \
| |
}
| |
| |
| |
| |
// Generar funciones sobrecargadas para cada tipo
| |
DEFINE_STRING_TO_ARRAY(string, result[i])
| |
DEFINE_STRING_TO_ARRAY(int, (int)StringToInteger(result[i]))
| |
DEFINE_STRING_TO_ARRAY(long, StringToInteger(result[i]))
| |
DEFINE_STRING_TO_ARRAY(short, (short)StringToInteger(result[i]))
| |
DEFINE_STRING_TO_ARRAY(char, (char)StringToInteger(result[i]))
| |
DEFINE_STRING_TO_ARRAY(bool, (StringToInteger(result[i]) != 0))
| |
DEFINE_STRING_TO_ARRAY(uint, (uint)StringToInteger(result[i]))
| |
DEFINE_STRING_TO_ARRAY(ulong, (ulong)StringToInteger(result[i]))
| |
DEFINE_STRING_TO_ARRAY(uchar, (uchar)StringToInteger(result[i]))
| |
DEFINE_STRING_TO_ARRAY(ushort, (ushort)StringToInteger(result[i]))
| |
DEFINE_STRING_TO_ARRAY(double, StringToDouble(result[i]))
| |
DEFINE_STRING_TO_ARRAY(float, (float)StringToDouble(result[i]))
| |
DEFINE_STRING_TO_ARRAY(datetime, StringToTime(result[i]))
| |
DEFINE_STRING_TO_ARRAY(color, StringToColor(result[i]))
| |
| |
#undef DEFINE_STRING_TO_ARRAY
| |
};
| |
//+------------------------------------------------------------------+
| |
#endif // MQLARTICLES_UTILS_FA_STRINGTOARRAY_MQH |