• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1692
  • Last Modified:

How do I compare a variable of type LPTSTR against a String (returned by localtime)?

This is what I have:
int x       = _tcslen(NNew->Lst);
int y       = x - 1;
int z       = 1;
    for (LPTSTR R = &NNew->Lst[y]; (y >= 0) && (z <= 10); R--, y--, z++)
        if (! IsChNumeric(*R))
            break;
    *++R        = 0;
    y++;
TCHAR       szNum[_MAX_PATH];
    wsprintf(szNum, _T("%050lu"), NNew->GCounter);
    wsprintf(NNew->Nme, _T("%s%s"), NNew->Lst, &szNum[50 - (x - y)]);

I need to validate the first six digits of whatever is returned on this last line against the current date (returned by localtime). What is the easiest way to do this?



0
betsy05
Asked:
betsy05
  • 3
  • 2
1 Solution
 
SalteCommented:
The type LPTSTR depends on your project settings.

IF you do not use unicode then LPTSTR is a regular string and so you can compare it with a string using regular strcmp:

void do_something(LPTSTR x, const char * y)
{
   if (strcmp(x,y) == 0) { // they're equal.
   }
}

If you use unicode then LPTSTR is a wchar_t string and you need to convert your const char * string to wchar_t before you can compare them. When both are wchar_t you use wcscmp() to compare the strings.

What function you use to convert the const char * string to wchar_t depends on what kind of data is in the const char * string and what kind of data in the wchar_t string. Normally the LPTSTR string is used to store unicode data and if that is the case then you simply need to convert the const char * string to unicode. This is almost a piece of cake.

If the string is only pure ascii characters (all characters are in the range 0..127 and you don't use any of the extended characters such as ÆØÅæøåäÄàÀ etc then it is very simple:

wchar_t * ascii2unicode(wchar_t * dst, const char * src)
{
   wchar_t * d = dst;
   while ((*d++ = *src++) != 0);
   return dst;
}

This function works like strcpy in that it just assume the space is enough, so you must make sure that the wchar_t buffer has room for as many characters as there are in the string src + 1 (the L'\0' char).

If you do use any of those characters from the extended set (ANSI characters above 127) then you need to be more cautious since Unicode maps them to mostly the same position as they are but I believe there are exceptions, not 100% sure about that though, in any case you can get far by simply:

wchar_t * ascii2unicode(wchar_t * dst, const char * src)
{
   wchar_t * d = dst;
   while ((*d++ = (unsigned char)*src++) != 0);
   return dst;
}

In any case it is also quite possible that the windows function mbstowcs() will do all this for you and also take care of all possible codes etc.

You could check up mbstowcs and see if that function does what you want it to do.

Alf
0
 
jkrCommented:
Use '_tcscmp()' to do the comparation - it will use either UNICODE or ANSI, depending on your project settings.
0
 
SalteCommented:
jkr,

problem is that the other string to compare is const char string and not const TCHAR string.

betsy05 need a function:

int cmp(const TCHAR a[], const char b[])
{
   ....
}

function, the tcscmp() will only work if TCHAR happened to be char in which case it is the same as strcmp().

If TCHAR is wchar_t tcscmp() cannot be used - as far as I know.

In that case the mbstowcs() function can convert the const char string to const wchar_t string and wcscmp() can be used:

int cmp(const wchar_t a[], const char b[])
{
   int n = mbstowcs(NULL,b,0);
   wchar_t * p = new wchar_t[n + 1];
   mbstowcs(p,b,n+1);
   int res = wcscmp(a,p);
   delete [] p;
   return res;
}

Should do the compare if TCHAR is wchar_t. If TCHAR is char a regular strcmp() is enough.

Alf
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jkrCommented:
>>problem is that the other string to compare is const char string and not const TCHAR string

Ooops - too busy working here...
0
 
betsy05Author Commented:
Thanks Salte.
LPTSTR Nme is 50 characters long. I want to get the first 6 characters only (mmddyy) and then compare the last two characters (year) against tm_year (returned by localtime), if they are different, error out. If year is the same, then compare the first two characters against tm_mon, if greater than tm_mon, error out, and then check the two characters in the middle (dd) against tm_mday. I need to prevent users from entering future dates. I was thinking I could use something like the GregorianCalendar class in Java. Any ideas?
0
 
SalteCommented:
GregorianCalendar class isn't available in C++ but there are equivalents.

convert the string to a tm_struct and then convert that tm_struct to a time_t value would be the best way to go I guess.

If you try to compare the last two digits of year make sure you don't fall into the trap that you compare those two digits with tm_year.

tm_year is year - 1900, so in 2003 the value should be 103, not 3, so if you compare with the last two digits of year you're way off.

Better to compare the full four digits of year with tm_year + 1900. That is safer comparison.

Alf
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now