Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CsvParameter.h
- #ifndef _CSVPARAMETER_h
- #define _CSVPARAMETER_h
- #include <arduino.h>
- class CsvParameter
- {
- private:
- const char* p;
- const char* delimiter;
- public:
- CsvParameter ()
- : p (nullptr), delimiter (0)
- {
- }
- // Assume values is a null terminated string.
- CsvParameter (const char * values, const char *delimiter = ",")
- : p (values), delimiter (delimiter)
- {
- }
- // Assume values is a MQTT command
- CsvParameter (byte* values, unsigned length, const char* delimiter = ",")
- : p (reinterpret_cast<const char*>(values)), delimiter (delimiter)
- {
- // Assume the buffer is long enough.
- values[length] = 0;
- }
- void setDelimiter (const char* delimiter)
- {
- this->delimiter = delimiter;
- }
- // _______________________________________________________________________________
- // Valid only until the first call to next.
- bool empty () const
- {
- return p == nullptr || *p == 0;
- }
- char character ()
- {
- if (p == nullptr)
- return 0;
- return *(p++);
- }
- // _______________________________________________________________________________
- // Normal calls for strtok.
- const char* text () const
- {
- return p;
- }
- const char *trim (const char *original)
- {
- if (original == nullptr)
- return original;
- while (*original > 0 && *original <= ' ')
- original++;
- return original;
- }
- const char* rest ()
- {
- if ((p == nullptr) || (*p == 0))
- return "";
- return trim (strtok (const_cast<char*>(p), "\x00"));
- }
- const char* next (const char* delimiter)
- {
- const char *act = strtok (const_cast<char*>(p), delimiter);
- p = nullptr;
- return trim (act);
- }
- const char* next ()
- {
- return next (delimiter);
- }
- int nextBool ()
- {
- const auto act = next ();
- return act == nullptr ? 0 : *act == '1';
- }
- int nextInt ()
- {
- const auto act = next ();
- return act == nullptr ? 0 : strtol (act, nullptr, 10);
- }
- uint nextUInt (const char* delimiter)
- {
- const auto act = next (delimiter);
- return act == nullptr ? 0 : strtoul (act, nullptr, 10);
- }
- uint nextUInt ()
- {
- return nextUInt (delimiter);
- }
- long nextHex ()
- {
- const auto act = next ();
- return act == nullptr ? 0 : strtoul (act, nullptr, 16);
- }
- float nextFloat ()
- {
- const auto act = next ();
- return act == nullptr ? NAN : strtof (act, nullptr);
- }
- };
- //════════════════════════════════════════════════════════════════════════════════
- class CsvCmd
- {
- char cmd[32];
- CsvParameter parameter;
- public:
- const char* getCmd () { return cmd; };
- CsvParameter& getParameter () { return parameter; }
- public:
- explicit CsvCmd (const char *text) // NOLINT(cppcoreguidelines-pro-type-member-init)
- {
- // Check for cmd without parameter.
- const auto blankPos = strchr (text, ' ');
- if (blankPos == nullptr)
- {
- strncpy (cmd, text, sizeof cmd);
- parameter = CsvParameter ("");
- }
- // or a cmd with blank and parameter
- else
- {
- const auto pos = blankPos - text;
- strncpy (cmd, text, pos);
- cmd[pos + 1] = 0;
- parameter = CsvParameter (blankPos + 1);
- }
- }
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement