0

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);
}
2
  • The simplest way is certainly to overload the PrintXMLTag() function by declaring it as many times as possible types for val variable; function overloading is perfectly supported in C++ 5not in pure C though).
    – jfpoilpret
    Oct 30, 2014 at 6:19
  • Thanks all for the replies. Ive opted with simply converting the variables into type char before sending to the xml function. Seems to have done the trick, not sure why I didn't think of this prior to posting the question?
    – bassteq
    Oct 30, 2014 at 22:32

3 Answers 3

0

try using this example instead of iostream you can use iostream.h too..

#include <iostream>
using namespace std;

int sum (int a, int b)
{
  return a+b;
}

double sum (double a, double b)
{
  return a+b;
}

int main ()
{
  cout << sum (10,20) << '\n';
  cout << sum (1.0,1.5) << '\n';
  return 0;
}
0

take a look at: http://www.cplusplus.com/doc/tutorial/functions2/

template <typename SomeType>
SomeType sum (SomeType a, SomeType b)
{
  return a+b;
}

This appears to be what you want.

1
  • Not quite, because most types will need to be special-cased. Oct 30, 2014 at 17:27
0

If your goal is really to print the value from XML, you don't need to worry about types, as long as you believe the XML to be correctly formed. The value in the XML file is already character string just print it. The converting from string to variable (sprintf()) and back to string (Serial.print() or printf()) is unnecessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.