Solved

Function to compare wildcarded hostnames

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

792 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