I'm trying to create a function that prints out an XML tag with a value. I have the code below, but kind of stuck. I'd like to be able to call the function, and pass a value that could be a float, int, string or char. Appreciate any help.
char PingTime[6] = "3ms";
char PingText[60] = "ping reply blah blah";
float humidity = "40.56";
int DigialPin = 0;
PrintXMLTag(cl,"pingtime",PingTime);
PrintXMLTag(cl,"PingText",PingText);
PrintXMLTag(cl,"humidity",humidity);
PrintXMLTag(cl,"DigialPin",DigialPin);
void PrintXMLTag_DEC(EthernetClient cl, const char* tag, NotSure?? val)
{
char tmp [100];
StrClear(tmp,100);
// If val type is float ----------------------
int d1 = val; // Get the integer part
float f2 = val - d1; // Get fractional part
int d2 = trunc(f2 * 10000);// Turn into integer
sprintf(tmp,"<%s>%d.%02d</%s>\n",tag,d1,d2,tag);
// end float
// If val type is string ----------------------
// If val type is int ----------------------
// If val type is char ----------------------
cl.print(tmp);
}
Update, now converting the type to char before calling the PrintXML function resolves my initial issue. If I use the code below it seems to chew up a lot of memory, is there something obviously wrong? As a workaround I'm just printing the XML tags using cl.print and the memory usage is stable.
void PrintXMLTag(EthernetClient cl, char* tag, char* val)
{
char tmp [100];
sprintf(tmp,"<%s>%s</%s>\n",tag,val,tag);
cl.print(tmp);
StrClear(tmp,100);
}
PrintXMLTag()
function by declaring it as many times as possible types forval
variable; function overloading is perfectly supported in C++ 5not in pure C though).