diff --git a/fbsh.cc b/fbsh.cc index 58f0ee3..4add649 100644 --- a/fbsh.cc +++ b/fbsh.cc @@ -9,9 +9,27 @@ #include #include #include +#include #include "fbsh.h" +/* + * implementation of the function atoi and atof but with additional check for NULL pointer + * atoi_check will return fallback value on error + * atof will return NAN + */ +double atof_check (char *s) { + if (s != NULL) return (atof(s)); + return NAN; +} + + +int atoi_check (char *s, int failnumber) { + if (s != NULL) return (atoi(s)); + return failnumber; +} + + std::string generateMD5 (char* input, int size) { int i; MD5_CTX md5; @@ -484,10 +502,10 @@ int FBSmartHome::GetHKR(std::string ain, FBSmartHomeHKR *hkr) { xmlNodePtr xmlc = xmlchild->children; while(xmlc) { if (!xmlStrcmp(xmlc->name, (const xmlChar *)"celsius")) { - hkr->temp = (atof ((char *) xmlNodeListGetString(xmldoc, xmlc->children, 1))) / 10.0; + hkr->temp = (atof_check((char *) xmlNodeListGetString(xmldoc, xmlc->children, 1))) / 10.0; } else if (!xmlStrcmp(xmlc->name, (const xmlChar *)"offset")) { - hkr->offset = atoi ((char *) xmlNodeListGetString(xmldoc, xmlc->children, 1)); + hkr->offset = atoi_check ((char *) xmlNodeListGetString(xmldoc, xmlc->children, 1), -999); } xmlc = xmlc->next; @@ -499,19 +517,19 @@ int FBSmartHome::GetHKR(std::string ain, FBSmartHomeHKR *hkr) { xmlNodePtr xmlc = xmlchild->children; while(xmlc) { if (!xmlStrcmp(xmlc->name, (const xmlChar *)"tist")) { - hkr->temp_cur = atof ((char *) xmlNodeListGetString(xmldoc, xmlc->children, 1)); + hkr->temp_cur = atof_check ((char *) xmlNodeListGetString(xmldoc, xmlc->children, 1)); hkr->temp_cur = hkr->temp_cur / 2.0; } else if (!xmlStrcmp(xmlc->name, (const xmlChar *)"tsoll")) { - hkr->temp_set = atof ((char *) xmlNodeListGetString(xmldoc, xmlc->children, 1)); + hkr->temp_set = atof_check ((char *) xmlNodeListGetString(xmldoc, xmlc->children, 1)); hkr->temp_set = hkr->temp_set / 2.0; } else if (!xmlStrcmp(xmlc->name, (const xmlChar *)"absenk")) { - hkr->temp_l = atof ((char *) xmlNodeListGetString(xmldoc, xmlc->children, 1)); + hkr->temp_l = atof_check ((char *) xmlNodeListGetString(xmldoc, xmlc->children, 1)); hkr->temp_l = hkr->temp_l / 2.0; } else if (!xmlStrcmp(xmlc->name, (const xmlChar *)"komfort")) { - hkr->temp_h = atof ((char *) xmlNodeListGetString(xmldoc, xmlc->children, 1)); + hkr->temp_h = atof_check ((char *) xmlNodeListGetString(xmldoc, xmlc->children, 1)); hkr->temp_h = hkr->temp_h / 2.0; } @@ -560,9 +578,9 @@ int FBSmartHome::GetHKR(std::string ain, FBSmartHomeHKR *hkr) { xmlattr = xmlc->properties; while (xmlattr) { if (!xmlStrcmp(xmlattr->name, (const xmlChar *)"count")) - count = atoi((char *) xmlNodeListGetString(xmldoc, xmlattr->children, 1)); + count = atoi_check((char *) xmlNodeListGetString(xmldoc, xmlattr->children, 1), -999); else if (!xmlStrcmp(xmlattr->name, (const xmlChar *)"grid")) - hkr-> statsgrid = atoi((char *) xmlNodeListGetString(xmldoc, xmlattr->children, 1)); + hkr-> statsgrid = atoi_check((char *) xmlNodeListGetString(xmldoc, xmlattr->children, 1), -999); xmlattr = xmlattr->next; } @@ -572,7 +590,7 @@ int FBSmartHome::GetHKR(std::string ain, FBSmartHomeHKR *hkr) { t = (char*)txt.c_str(); for (int i = 0; t != NULL && i < count; i++) { if (*t == ',') t++; - f = atoi (t); + f = atoi_check (t, 0); hkr->stats.push_back(f); t = strchr(t, ','); } @@ -707,11 +725,11 @@ std::list *FBSmartHome::GetColorDefaults (std::string FBSmartHomeLightDefaults def = { -1, -1, -1 }; while (xmlattr) { if (!xmlStrcmp(xmlattr->name, (const xmlChar *)"hue")) - def.hue = atoi((char *) xmlNodeListGetString(xmldoc, xmlattr->children, 1)); + def.hue = atoi_check((char *) xmlNodeListGetString(xmldoc, xmlattr->children, 1), 0); else if (!xmlStrcmp(xmlattr->name, (const xmlChar *)"sat")) - def.sat = atoi((char *) xmlNodeListGetString(xmldoc, xmlattr->children, 1)); + def.sat = atoi_check((char *) xmlNodeListGetString(xmldoc, xmlattr->children, 1), 0); else if (!xmlStrcmp(xmlattr->name, (const xmlChar *)"val")) - def.val = atoi((char *) xmlNodeListGetString(xmldoc, xmlattr->children, 1)); + def.val = atoi_check((char *) xmlNodeListGetString(xmldoc, xmlattr->children, 1), 0); xmlattr = xmlattr->next; } if (def.hue != -1) lst->push_back(def);