Solved

Function to compare wildcarded hostnames

Posted on 2004-04-08
4
341 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

724 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