By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,420 Members | 1,130 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,420 IT Pros & Developers. It's quick & easy.

where can i get the pure c++ code of the implementataion of CString::Fromat()?

P: n/a
here is my code, as follows:
void TString::FormatV(const char *pszFormat, va_list argList)
{
va_list argListSave = argList;

// make a guess at the maximum length of the resulting string
size_t nMaxLen = 0;
for(const char *psz = pszFormat; *psz != '\0'; psz = _tcsinc(psz))
{
// handle '%' character, but watch out for '%%'
if (*psz != '%' || *(psz = _tcsinc(psz)) == '%')
{
nMaxLen += _tclen(psz);
continue;
}

size_t nItemLen = 0;
// handle '%' character with format
size_t nWidth = 0;

for(; *psz != '\0'; psz = _tcsinc(psz))
{
// check for valid flags
if(*psz == '#')
{
nMaxLen += 2; // for '0x'
}
else if(*psz == '*')
{
nWidth = va_arg(argList, int);
}
else if(*psz == '-' ||
*psz == '+' ||
*psz == '0' ||
*psz == ' ')
{
;
}
else // hit non-flag character
{
break;
}
}

// get width and skip it
if(nWidth == 0)
{
// width indicated by
nWidth = _ttoi(psz);
for (; *psz != '\0' && _istdigit(*psz); psz = _tcsinc(psz))
{
;
}
}

TASSERT(nWidth >= 0);

size_t nPrecision = 0;
if(*psz == '.')
{
// skip past '.' separator (width.precision)
psz = _tcsinc(psz);

// get precision and skip it
if (*psz == '*')
{
nPrecision = va_arg(argList, int);
psz = _tcsinc(psz);
}
else
{
nPrecision = _ttoi(psz);
for (; *psz != '\0' && _istdigit(*psz); psz = _tcsinc(psz))
{
;
}
}

TASSERT(nPrecision >= 0);
}

// should be on type modifier or specifier
switch (*psz)
{
// modifiers that affect size
case 'h':
case 'l':
case 'F':
case 'N':
case 'L':
psz = _tcsinc(psz);
break;
}

// now should be on specifier
switch(*psz)
{
// single characters
case 'c':
case 'C':
nItemLen = 2;
va_arg(argList, int);
break;

// strings
case 's':
case 'S':
const char *pstrNextArg = va_arg(argList, const char*);

if(pstrNextArg == NULL)
{
nItemLen = 6; // "(null)"
}
else
{
nItemLen = ::strlen(pstrNextArg);
nItemLen = max(1, nItemLen);
}
break;
}

// adjust nItemLen for strings
if(nItemLen != 0)
{
nItemLen = max(nItemLen, nWidth);
if (nPrecision != 0)
{
nItemLen = min(nItemLen, nPrecision);
}
}
else
{
switch(*psz)
{
// integers
case 'd':
case 'i':
case 'u':
case 'x':
case 'X':
case 'o':
va_arg(argList, int);
nItemLen = 32;
nItemLen = max(nItemLen, nWidth + nPrecision);
break;

case 'e':
case 'f':
case 'g':
case 'G':
va_arg(argList, double);
nItemLen = 128;
nItemLen = max(nItemLen, nWidth + nPrecision);
break;

case 'p':
va_arg(argList, void*);
nItemLen = 32;
nItemLen = max(nItemLen, nWidth + nPrecision);
break;

// no output
case 'n':
va_arg(argList, int*);
break;

default:
TASSERT(FALSE); // unknown formatting option
}

}

// adjust nMaxLen for output nItemLen
nMaxLen += nItemLen;
}

if(!AllocBeforeWrite(nMaxLen))
{
return;
}

::sprintf(m_pchData, pszFormat, argListSave);
}

it is difficult for me to write it, unfortunately, it does not work well. :(
is anyone willing to rewrite it?

my email: wenming_hu2002 at hotmail.com

Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
CString is not a standard C++ class. Your question is offtopic here. Try
asking on microsoft.public.vc.mfc on msnews.microsoft.com news server.
Jul 22 '05 #2

P: n/a
"Wenming Hu" <we************@hotmail.com> wrote in message
news:ce***********@mail.cn99.com...
here is my code, as follows: [quite complicated calculations for buffer size] ::sprintf(m_pchData, pszFormat, argListSave);
}

it is difficult for me to write it, unfortunately, it does not work well. :( is anyone willing to rewrite it?


Why do all this work to attempt to compute the length of the string,
you should rely on functions available in your system library.

C99 has the function snprintf() that will return the length of the
output string if an empty (or too small) output buffer is provided.
While this function is not in the 98 C++ standard,
it is likely to be available on many platforms.

In MS Visual Studio, _snprintf has a different/incompatible behavior,
but the function _vscprintf can be used instead.

hth-Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.