added extra checks, use r-value for strings

This commit is contained in:
proddy
2020-10-19 23:17:19 +02:00
parent ef7a415742
commit c507195b39
2 changed files with 29 additions and 30 deletions

View File

@@ -136,7 +136,10 @@ char * Helpers::render_boolean(char * result, bool value) {
} }
// depending on format render a number or a string // depending on format render a number or a string
char * Helpers::render_enum(char * result, const std::vector<std::string> value, const int8_t no) { char * Helpers::render_enum(char * result, const std::vector<std::string> & value, const uint8_t no) {
if (no >= value.size()) {
return nullptr; // out of bounds
}
if (bool_format() == BOOL_FORMAT_ONOFF) { if (bool_format() == BOOL_FORMAT_ONOFF) {
strcpy(result, value[no].c_str()); strcpy(result, value[no].c_str());
} else if (bool_format() == BOOL_FORMAT_TRUEFALSE) { } else if (bool_format() == BOOL_FORMAT_TRUEFALSE) {
@@ -211,6 +214,7 @@ char * Helpers::render_value(char * result, const float value, const uint8_t for
char * ret = result; char * ret = result;
int32_t whole = (int32_t)value; int32_t whole = (int32_t)value;
itoa(result, whole, 10); itoa(result, whole, 10);
while (*result != '\0') { while (*result != '\0') {
@@ -458,7 +462,7 @@ bool Helpers::value2bool(const char * v, bool & value) {
} }
// checks to see if a string is member of a vector and return the index, also allow true/false for on/off // checks to see if a string is member of a vector and return the index, also allow true/false for on/off
bool Helpers::value2enum(const char * v, uint8_t & value, const std::vector<std::string> strs) { bool Helpers::value2enum(const char * v, uint8_t & value, const std::vector<std::string> & strs) {
if ((v == nullptr) || (strlen(v) == 0)) { if ((v == nullptr) || (strlen(v) == 0)) {
return false; return false;
} }

View File

@@ -31,9 +31,6 @@ namespace emsesp {
class Helpers { class Helpers {
public: public:
static char * hextoa(char * result, const uint8_t value);
static std::string data_to_hex(const uint8_t * data, const uint8_t length);
static char * render_value(char * result, const float value, const uint8_t format); // format is the precision static char * render_value(char * result, const float value, const uint8_t format); // format is the precision
static char * render_value(char * result, const uint8_t value, const uint8_t format); static char * render_value(char * result, const uint8_t value, const uint8_t format);
static char * render_value(char * result, const int8_t value, const uint8_t format); static char * render_value(char * result, const int8_t value, const uint8_t format);
@@ -41,10 +38,11 @@ class Helpers {
static char * render_value(char * result, const uint32_t value, const uint8_t format); static char * render_value(char * result, const uint32_t value, const uint8_t format);
static char * render_value(char * result, const int16_t value, const uint8_t format); static char * render_value(char * result, const int16_t value, const uint8_t format);
static char * render_value(char * result, const char * value, uint8_t format); static char * render_value(char * result, const char * value, uint8_t format);
static char * render_boolean(char * result, bool value); static char * render_boolean(char * result, bool value);
static char * render_enum(char * result, const std::vector<std::string> value, const int8_t no); static char * render_enum(char * result, const std::vector<std::string> & value, const uint8_t no);
static char * hextoa(char * result, const uint8_t value);
static std::string data_to_hex(const uint8_t * data, const uint8_t length);
static char * smallitoa(char * result, const uint8_t value); static char * smallitoa(char * result, const uint8_t value);
static char * smallitoa(char * result, const uint16_t value); static char * smallitoa(char * result, const uint16_t value);
static char * itoa(char * result, int32_t value, const uint8_t base = 10); static char * itoa(char * result, int32_t value, const uint8_t base = 10);
@@ -52,6 +50,19 @@ class Helpers {
static uint16_t atoint(const char * value); static uint16_t atoint(const char * value);
static bool check_abs(const int32_t i); static bool check_abs(const int32_t i);
static double round2(double value); static double round2(double value);
static std::string toLower(std::string const & s);
static bool hasValue(const uint8_t & v, const uint8_t isBool = 0);
static bool hasValue(const int8_t & v);
static bool hasValue(const int16_t & v);
static bool hasValue(const uint16_t & v);
static bool hasValue(const uint32_t & v);
static bool value2number(const char * v, int & value);
static bool value2float(const char * v, float & value);
static bool value2bool(const char * v, bool & value);
static bool value2string(const char * v, std::string & value);
static bool value2enum(const char * v, uint8_t & value, const std::vector<std::string> & strs);
static void bool_format(uint8_t bool_format) { static void bool_format(uint8_t bool_format) {
bool_format_ = bool_format; bool_format_ = bool_format;
@@ -65,22 +76,6 @@ class Helpers {
static char * ultostr(char * ptr, uint32_t value, const uint8_t base); static char * ultostr(char * ptr, uint32_t value, const uint8_t base);
#endif #endif
static bool hasValue(const uint8_t & v, const uint8_t isBool = 0);
static bool hasValue(const int8_t & v);
static bool hasValue(const int16_t & v);
static bool hasValue(const uint16_t & v);
static bool hasValue(const uint32_t & v);
static std::string toLower(std::string const & s);
static bool value2number(const char * v, int & value);
static bool value2float(const char * v, float & value);
static bool value2bool(const char * v, bool & value);
static bool value2string(const char * v, std::string & value);
static bool value2enum(const char * v, uint8_t & value, const std::vector<std::string> strs);
private: private:
static uint8_t bool_format_; static uint8_t bool_format_;
}; };