Solved

Function to compare wildcarded hostnames

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

Title # Comments Views Activity
how to understand recursion 12 224
Embarcadero C++ Builder XE2 TDateTime 8 70
FMX enumerated colours 2 98
GUI: DIalog Stacking and Popping in MS C++ 4 74
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

808 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