This code contains the following errors:
1. The return value type is a boolean value. Use type
bool
(and the values
true
and
false
, rather than
TRUE
and
FALSE
). If the return value is passed to a system function, you may need to use
BOOL
instead
2. the variables
iStrlen
and
I
are used as subscript into an array. They should be defined as
std::size_t
. You can use
SIZE_T
instead, which is the MS-Version of the same type, but their inability to define and use types properly has already been sufficiently documented by the fact they needed to introduce the
*_PTR
types - so why bother with unreliable type definitions when you can use standardized types?
3. Literals of type
TCHAR
need to be defined using the
_T()
macro, not
T()
. You are missing the leading underscores.
Fixed those types and values for you:
bool GetShortPath( THAR *szFullPath, TCHAR *pszShortPath, std::size_t iStrlen) {
if(!lstrlen(pszFullPath))
{
return false; }
TCHAR *p, *q;
p = pszFullPath, q = pszShortPath;
std::size_t I = 0; while(*p)
{
*q = *p;
p++,q++;
}
while(*p != _T('\\')) {
if( I < iStrlen)
{
*q = *p;
p++, q++,I++;
}
else
{
return false; }
}
*q = _T('\0'); return true;}
Please note that you should always use the correct type - else you might just be using no type at all and program in Assembler!
Don't use the MS types unless you really, really must (i. e. you're interfacing to system functions that require them, or using a proprietary type definition such as TCHAR).