Advertisement
ind03

CsvParameter

Feb 13th, 2022
1,160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. // CsvParameter.h
  2.  
  3. #ifndef _CSVPARAMETER_h
  4. #define _CSVPARAMETER_h
  5.  
  6. #include <arduino.h>
  7.  
  8. class CsvParameter
  9. {
  10. private:
  11.     const char* p;
  12.     const char* delimiter;
  13.    
  14.  public:
  15.      CsvParameter ()
  16.          : p (nullptr), delimiter (0)
  17.      {
  18.      }
  19.  
  20.     // Assume values is a null terminated string.
  21.     CsvParameter (const char * values, const char *delimiter = ",")
  22.      : p (values), delimiter (delimiter)
  23.     {
  24.     }
  25.  
  26.     // Assume values is a MQTT command
  27.     CsvParameter (byte* values, unsigned length, const char* delimiter = ",")
  28.      : p (reinterpret_cast<const char*>(values)), delimiter (delimiter)
  29.     {  
  30.         // Assume the buffer is long enough.
  31.         values[length] = 0;
  32.     }
  33.  
  34.     void setDelimiter (const char* delimiter)
  35.     {
  36.         this->delimiter = delimiter;
  37.     }
  38.  
  39.     // _______________________________________________________________________________
  40.     // Valid only until the first call to next.
  41.     bool empty () const
  42.     {
  43.         return p == nullptr || *p == 0;
  44.     }
  45.  
  46.     char character ()
  47.     {
  48.         if (p == nullptr)
  49.             return 0;
  50.  
  51.         return *(p++);
  52.     }
  53.  
  54.     // _______________________________________________________________________________
  55.     // Normal calls for strtok.
  56.     const char* text () const
  57.     {
  58.         return p;
  59.     }
  60.  
  61.     const char *trim (const char *original)
  62.     {
  63.         if (original == nullptr)
  64.             return original;
  65.  
  66.         while (*original > 0 && *original <= ' ')
  67.             original++;
  68.  
  69.         return original;
  70.     }
  71.    
  72.     const char* rest ()
  73.     {
  74.         if ((p == nullptr) || (*p == 0))
  75.             return "";
  76.         return trim (strtok (const_cast<char*>(p), "\x00"));
  77.     }
  78.  
  79.     const char* next (const char* delimiter)
  80.     {
  81.         const char *act = strtok (const_cast<char*>(p), delimiter);
  82.         p = nullptr;
  83.         return trim (act);
  84.     }
  85.     const char* next ()
  86.     {
  87.         return next (delimiter);
  88.     }
  89.  
  90.     int nextBool ()
  91.     {
  92.         const auto act = next ();
  93.         return act == nullptr ? 0 : *act == '1';
  94.     }
  95.    
  96.     int nextInt ()
  97.     {
  98.         const auto act = next ();
  99.         return act == nullptr ? 0 : strtol (act, nullptr, 10);
  100.     }
  101.  
  102.     uint nextUInt (const char* delimiter)
  103.     {
  104.         const auto act = next (delimiter);
  105.         return act == nullptr ? 0 : strtoul (act, nullptr, 10);
  106.     }
  107.     uint nextUInt ()
  108.     {
  109.         return nextUInt (delimiter);
  110.     }
  111.  
  112.     long nextHex ()
  113.     {
  114.         const auto act = next ();
  115.         return act == nullptr ? 0 : strtoul (act, nullptr, 16);
  116.     }
  117.  
  118.     float nextFloat ()
  119.     {
  120.         const auto act = next ();
  121.         return act == nullptr ? NAN : strtof (act, nullptr);
  122.     }
  123. };
  124.  
  125. //════════════════════════════════════════════════════════════════════════════════
  126. class CsvCmd
  127. {
  128.     char cmd[32];
  129.     CsvParameter parameter;
  130.  
  131. public:
  132.     const char* getCmd () { return cmd; };
  133.     CsvParameter& getParameter () { return parameter; }
  134.  
  135. public:
  136.     explicit CsvCmd (const char *text)  // NOLINT(cppcoreguidelines-pro-type-member-init)
  137.     {
  138.         // Check for cmd without parameter.
  139.         const auto blankPos = strchr (text, ' ');
  140.         if (blankPos == nullptr)
  141.         {
  142.             strncpy (cmd, text, sizeof cmd);
  143.             parameter = CsvParameter ("");
  144.         }
  145.  
  146.         // or a cmd with blank and parameter
  147.         else
  148.         {
  149.             const auto pos = blankPos - text;
  150.             strncpy (cmd, text, pos);
  151.             cmd[pos + 1] = 0;
  152.             parameter = CsvParameter (blankPos + 1);
  153.         }
  154.     }
  155. };
  156.  
  157. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement