CMyString str;
str.format(3, "m_uNumAngularSlices = %d, m_uRotationPeriod = %ld, uRadiusClock = %lX", (int)m_uNumAngularSlices, (int)m_uRotationPeriod, (int)uRadiusClock);
If I call the format(...) function like this then:
The first call to getNextFormatType(...) will return
lpszFormatStr = ", m_uRotationPeriod = %ld, m_uRadiusClock = %lX"
rStrText = "m_uNumAngularSlices = "
rType = "%d"
The second call to getNextFormatType(...) will return
lpszFormatStr = ", m_uRadiusClock = %lX"
rStrText = ", m_uRotationPeriod = "
rType = "%ld"
And so on.
"lpszFormatStr = lpszEndType;"
This line of code seems to be the issue.
I can Serial.print(lpszEndType) and I get the expected string output in serial monitor.
But as soon as I try to return the string to the previous function in any way, shape or form it stuffs the program up and I get garbage in serial monitor.
I have tried this way with a reference parameter (char *).
I have tried with getNextFormatType returning the new pointer
I have tried by assigning the substring pointed to by lpszEndType to a CMyStringObject and returning the reduced format string as a reference parameter and a function return value.
All with the same result - if I try to return it to the previous function I get garbage in serial monitor.
If I comment out "lpszFormatStr = lpszEndType;" then I get sensible output in serial monitor.
If I comment out my previous line of rFormatStr = lpszEndType (where rFormatStr is a CMyString & aparameter )then I get sensible output in serial monitor.
If I uncomment rFormatStr = lpszEndType then I get garbage in serial monitor.
I am at a total loss. Why can't I do this seemingly simple operation?
void CMyString::getNextFormatType(char *&lpszFormatStr, CMyString &rStrText, CMyString &rType)
{
static char *lpszSartType = 0, *lpszEndType = 0;
int nTypeLen = 0, nPos = 0;
lpszSartType = strchr(lpszFormatStr, '%');
if (!(lpszEndType = strchr(lpszFormatStr, 'd')))
{
if (!(lpszEndType = strchr(lpszFormatStr, 'i')))
{
if (!(lpszEndType = strchr(lpszFormatStr, 'o')))
{
if (!(lpszEndType = strchr(lpszFormatStr, 'x')))
{
if (!(lpszEndType = strchr(lpszFormatStr, 'X')))
{
if (!(lpszEndType = strchr(lpszFormatStr, 'F')))
{
if (!(lpszEndType = strchr(lpszFormatStr, 'f')))
{
if (!(lpszEndType = strchr(lpszFormatStr, 'G')))
{
if (!(lpszEndType = strchr(lpszFormatStr, 'g')))
{
if (!(lpszEndType = strchr(lpszFormatStr, 'E')))
{
if (!(lpszEndType = strchr(lpszFormatStr, 'e')))
{
if (!(lpszEndType = strchr(lpszFormatStr, 'A')))
{
if (!(lpszEndType = strchr(lpszFormatStr, 'a')))
{
if (!(lpszEndType = strchr(lpszFormatStr, 'c')))
{
if (!(lpszEndType = strchr(lpszFormatStr, 's')))
{
if (!(lpszEndType = strchr(lpszFormatStr, 'p')))
{
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
if (lpszSartType)
{
nTypeLen = lpszEndType - lpszSartType + 1;
strncpy(rType.getBuffer(), lpszSartType, nTypeLen);
rType.getBuffer()[nTypeLen] = 0;
nPos = lpszSartType - lpszFormatStr;
strncpy(rStrText. getBuffer(), lpszFormatStr, nPos);
lpszEndType++;
lpszFormatStr = lpszEndType;
}
}
CMyString &CMyString::format(const int nNumParams, char *lpszFormat, const int nNum, ...)
{
int nI = 0, nPos = 0;
CMyString strType, strTxt, str;
strTemp.empty();
va_list arguments;
va_start(arguments, nNumParams);
for (nI = 0; nI < nNumParams; nI++)
{
getNextFormatType(lpszFormat, strTxt, strType);
Serial.print(lpszFormat);
Serial.print(", ");
Serial.print(strTxt);
Serial.print(", ");
Serial.println(strType);
Serial.print(", ");
Serial.println(nI);
Serial.println();
if (strType.getLength() > 0)
{
strTemp += strTxt;
sprintf(str.getBuffer(), strType, va_arg(arguments, int));
strTemp += str;
}
}
va_end(arguments);
return strTemp;
}