My application is perfoming "domain name to ip" and "ip to domain name" resolving.
To reduce request overwok would be useful to check domain name correctness before sending request right in application. To avoid user mistakes in typing domain name.

So we can write a C++ function to check if string is a real domain name or it is a simple string with abracadabra:


Code:
BOOL isValidDomainName(wchar_t* dName);
function takes Unicode string and return true; if it contains correct domain name, like example.com or return false; if it not.

How we can implement simple checkup?
Let me think. Valid domain name is differ from ordinary string:

1. domain name can't start with '-' sign
2. domain name can contains only 'a-z' characters, '0-9' digits, and '-' sign.
(No special or control characters).
3. domain name ends with TLD part like .com, .net, .org, .name, .museum and etc.
4. domain name can be multi level like (i.am.correct.com)

I was trying to write my own version of isValidDomainName() but it did not works (

PHP Code:
BOOL isValidDomainName(wchar_tdName)
{
    
wchar_twszTLD;
    
wchar_tpNULL//Context pointer to next token

    
wszTLD=wcstok_s(dName_T("."), &p); 
    if(
wszTLD==NULL && wcscmp(dName,_T("localhost"))!=NULL) return false// no dots and no localhost string
    
else
    {
        do {
            
wszTLD=wcstok_s(NULL_T("."), &p);            
            if(!
wcscmp(wszTLD,_T("com"))) return true;
            else return 
false// there are dots but no com TLD string found
                        
        
}while(wszTLD);        
    }
    
// domain can't start with '-' sign
    
if(dName[0]=='-')return false;

P.S. it's C++ code, not php. Simply I could not find tag for C++ code. Tag for better visual perception.
Maybe your version of this function will be more successful and effective. Any ideas?