Solved

Function to compare wildcarded hostnames

Posted on 2004-04-08
4
336 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
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 150 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 100 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now