//+------------------------------------------------------------------+
//|                                             PersistentParser.mqh |
//|                 Lightweight Persistent Key-Value Storage Engine  |
//+------------------------------------------------------------------+
#ifndef PERSISTENT_PARSER_MQH
#define PERSISTENT_PARSER_MQH

//+------------------------------------------------------------------+
//| CPersistentParser                                                |
//| Responsible for parsing individual lines of the flat file into   |
//| key-value pairs. Handles whitespace trimming, comment skipping,  |
//| empty-line detection, and delimiter validation.                  |
//+------------------------------------------------------------------+
class CPersistentParser
  {
private:
   static string     TrimWhitespace(const string s);

public:
                     CPersistentParser(void);
                    ~CPersistentParser(void);

   static bool       ParseLine(const string line,
                               string &out_key,
                               string &out_value);

   static bool       IsCommentOrEmpty(const string line);
   static bool       IsValidKey(const string key);
  };

//+------------------------------------------------------------------+
//| Constructor                                                      |
//+------------------------------------------------------------------+
CPersistentParser::CPersistentParser(void)
  {
  }

//+------------------------------------------------------------------+
//| Destructor                                                       |
//+------------------------------------------------------------------+
CPersistentParser::~CPersistentParser(void)
  {
  }

//+------------------------------------------------------------------+
//| Return true if line is empty or starts with '#'                  |
//+------------------------------------------------------------------+
bool CPersistentParser::IsCommentOrEmpty(const string line)
  {
   string trimmed = TrimWhitespace(line);
//--- skip empty lines
   if(StringLen(trimmed) == 0)
      return(true);
//--- skip comment lines
   if(trimmed[0] == '#')
      return(true);
   return(false);
  }

//+------------------------------------------------------------------+
//| Return true if key contains no '=' character and is non-empty    |
//+------------------------------------------------------------------+
bool CPersistentParser::IsValidKey(const string key)
  {
   if(StringLen(key) == 0)
      return(false);
   if(StringFind(key, "=") >= 0)
      return(false);
   return(true);
  }

//+------------------------------------------------------------------+
//| Parse a single line into key and value components                |
//+------------------------------------------------------------------+
bool CPersistentParser::ParseLine(const string line,
                                  string &out_key,
                                  string &out_value)
  {
   out_key   = "";
   out_value = "";

//--- skip comments and blank lines
   if(IsCommentOrEmpty(line))
      return(false);

//--- locate the first '=' delimiter
   int eq_pos = StringFind(line, "=");
   if(eq_pos < 1)
      return(false); // No delimiter or empty key

//--- extract key as substring before '='
   out_key = StringSubstr(line, 0, eq_pos);
   out_key = TrimWhitespace(out_key);

//--- extract value as everything after '='
   out_value = StringSubstr(line, eq_pos + 1);
   out_value = TrimWhitespace(out_value);

//--- validate that the extracted key is well-formed
   if(!IsValidKey(out_key))
     {
      out_key   = "";
      out_value = "";
      return(false);
     }

   return(true);
  }

//+------------------------------------------------------------------+
//| Remove leading and trailing whitespace from a string             |
//+------------------------------------------------------------------+
string CPersistentParser::TrimWhitespace(const string s)
  {
   string result = s;
   int    len    = StringLen(result);

//--- trim leading whitespace
   int start = 0;
   while(start < len && (result[start] == ' ' || result[start] == '\t'))
      start++;

//--- trim trailing whitespace and carriage returns
   int end = len - 1;
   while(end >= start && (result[end] == ' '  || result[end] == '\t' ||
                          result[end] == '\r' || result[end] == '\n'))
      end--;

   if(start > end)
      return("");

   return(StringSubstr(result, start, end - start + 1));
  }

#endif // PERSISTENT_PARSER_MQH
//+------------------------------------------------------------------+