Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Function to compare wildcarded hostnames

Posted on 2004-04-08
4
Medium Priority
?
345 Views
Last Modified: 2010-04-01
Does anybody know of a function (or feels like writing one) that can compare a host name to a wildcarded specification, for example:

*.company.com should match a.company.com but not company.com
*any.com should match company.com or a.company.com but not company1.com
*any*.com should match a.company1.com
*boston*.*any*.com should match 23boston.company1.com

etc.

The wildcarding doesn't need to be more extensive than *, that is, no [1..9] or aything like that is required.

Regular expressions might do the trick although the overhead seems high for something so simple, and I'd like this to be as light-weight as possible.
0
Comment
Question by:wayside
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 600 total points
ID: 10786929
If you are on windows, check 'PathMatchSpec()' (http://msdn.microsoft.com/library/en-us/shellcc/platform/shell/reference/shlwapi/path/pathmatchspec.asp) - it seems to match your criteria (no pun intended :o)
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 400 total points
ID: 10790132
Try this, i adopted it from my own string class to std::string

#include <string>
#include <iostream>
using namespace std;

// @mfunc compare with wildcard string
bool compareWildcard(const string& str, const string& mask)
{
    int     i1   = 0;
    int     i2   = 0;
    int     len1 = str.length();
    int     len2 = mask.length();
    string  next;
    char    c1, c2;

    while (i1 < len1 && i2 < len2)
    {
        c1 = str[i1];
        c2 = mask[i2];
        switch (c2)
        {
        case '*':
           
            while (++i2 < len2 && ((c2 = mask[i2]) == '?' || c2 == '*'))
            {
                if (c2 == '*')
                    continue;
                if (++i1 == len1)
                    return false;
                c1 = str[i1];
            }
            if (i2 >= len2)
                return true;

            while ((i1 = str.find(c2, i1)) != string::npos)
            {
                next = str.substr(i1);
                if (compareWildcard(next, mask.substr(i2)))
                    return true;
                i1++;
            }
            return false;
        case '?':
            break;
        default:
            if (c1 != c2)
                return false;
        }
        if (++i1 == len1)
        {
            if (++i2 == len2)
                return true;
            next = mask.substr(i2);
                  int pos;
                  while ((pos = next.find('*')) != string::npos)
                 next.erase(pos, 1);
            return next.empty();
        }
        if (++i2 == len2)
            return false;
    }
    return true;
}


void main(int narg, char* pszArgs[])
{
    if (narg  != 3)
        return;
    string str = pszArgs[1];
    string msk = pszArgs[2];
    if (compareWildcard(str, msk))
        cout << str << " matches " << msk << endl;
    else
        cout << str << " doesn't match " << msk << endl;

}

Regards, Alex


0
 
LVL 14

Author Comment

by:wayside
ID: 10790849
Thanks for the replies, I will check both of these out.
0
 
LVL 14

Author Comment

by:wayside
ID: 10813598
jkr - PathMatchSpec() seems to work for every test case I've given it. Thanks for letting me know about it, I had never come across that one before.

Alex - that's a nifty function, thanks. I've squirrelled it away in case I ever need to port to another platform.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question