473,387 Members | 3,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
2 2122
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
1
by: Marcus | last post by:
I know how to fromat dates by weeks, as in Format(,"ww"). However, this displays the week as a number, such as 1, 2, 3, 4, to represent 1st week, 2nd week etc of the year. I am wondering if...
0
by: oneplace | last post by:
I have the following script that use for the following after i have made my search for a name in my database. I select the name and it bring up this from with all the information of the person. It...
9
by: Jeff Dege | last post by:
I've been programming in C++ for a good long while, but there are aspects of the language I've never needed, and hence never bothered to really learn. It's the curse of working on a developed...
6
by: andrey.vul | last post by:
The error is LNK2001: unresolved external symbol "protected: static bool solver<typename, typename, int, int, int, int, int>::solution" (<vc++ mangled name>) Code (sudoku.cpp): // Sudoku.cpp :...
0
by: hemadribabu | last post by:
hi , i have a textbox in my asp.net page, in this textbox some integer value will be displayed , Now i have to make that displayed integer value to be displayed in INR format , plstell me how to...
1
by: readnlearn | last post by:
hii all I created the Barcode Image with some Value .....But i want to know the value from the barcode image...How can i do it....any help...
2
by: cmdolcet69 | last post by:
If i format my date string to look like month,day,year when i look at the file created it will look like: Raw Data File 37 05 08 10 58 12 were the 37 comes from i dont know the 37 should be 09 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.