globing on windows in c / c++ language

Discussion in 'C Programming' started by Vipin, Dec 2, 2010.

  1. Vipin

    Vipin Guest

    I need the globing support on Windows for one of my application
    written in C / c++ language.
    Can any body point me to the help in that regards.

    Following code works well on UNIX but there is no <glob.h> or
    equivalent on windows in my opinion.
    Please share the code if anybody has written already.

    #include <stdio.h>
    #include <glob.h>


    int glob_err_handler(const char *path,int errno){
    switch(errno){
    case GLOB_ABORTED:
    printf("\nPermission denied on path %s",path);
    break;
    case GLOB_NOMATCH:
    printf("\nNo matching source files found.");
    break;
    case GLOB_NOSPACE:
    printf("\nOut of memory while trying to read matching source
    files.");
    break;
    default:
    printf("\n Unknown Error.");
    }
    return 0;
    }

    int main (int argc, char *argv[]){
    glob_t t;
    int retval;
    int flags;

    flags = 1;/* As per standards, do not use GLOB_APPEND on first call.*/

    retval = glob("./xs*/*.v",flags,glob_err_handler,&t);
    flags |=GLOB_APPEND;
    retval = glob("./*/*.v",flags,glob_err_handler,&t);

    printf("\n Printing the matching paths ... - %d ",retval);
    for(flags=0;flags < t.gl_pathc;flags++){
    printf("\n%s",t.gl_pathv[flags]);
    }

    globfree(&t);

    printf("\n\n");
    return 0;
    }
    Vipin, Dec 2, 2010
    #1
    1. Advertising

  2. Re: globing on windows in c / c++ language

    On Dec 2, 7:57 am, Vipin <> wrote:

    > Following code works well on UNIX but there is no <glob.h> or
    > equivalent on windows in my opinion.
    > Please share the code if anybody has written already.
    >


    This took quite a bit of getting right. The code was reviewed on
    comp.lang.c a couple of months ago.

    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <assert.h>

    static int chmatch(const char *target, const char *pat);

    /*
    wildcard matcher.
    Params: str - the target string
    pattern - pattern to match
    Returns: 1 if match, 0 if not.
    Notes: ? - match any character
    * - match zero or more characters
    [?], [*], escapes,
    [abc], match a, b or c.
    [A-Z] [0-9] [*-x], match range.
    [[] - match '['.
    [][abc] match ], [, a, b or c
    */
    int matchwild(const char *str, const char *pattern)
    {
    const char *target = str;
    const char *pat = pattern;
    int gobble;

    while( (gobble = chmatch(target, pat)) )
    {
    target++;
    pat += gobble;
    }
    if(*target == 0 && *pat == 0)
    return 1;
    else if(*pat == '*')
    {
    while(pat[1] == '*')
    pat++;
    if(pat[1] == 0)
    return 1;
    while(*target)
    if(matchwild(target++, pat+1))
    return 1;
    }
    return 0;
    }

    /*
    match a character.
    Parmas: target - target string
    pat - pattern string.
    Returns: number of pat character matched.
    Notes: means that a * in pat will return zero
    */
    static int chmatch(const char *target, const char *pat)
    {
    char *end, *ptr;

    if(*pat == '[' && (end = strchr(pat, ']')) )
    {
    /* treat close bracket following open bracket as character */
    if(end == pat + 1)
    {
    end = strchr(pat+2, ']');
    /* make "[]" with no close mismatch all */
    if(end == 0)
    return 0;
    }
    /* allow [A-Z] and like syntax */
    if(end - pat == 4 && pat[2] == '-' && pat[1] <= pat[3])
    if(*target >= pat[1] && *target <= pat[3])
    return 5;
    else
    return 0;

    /* search for character list contained within brackets */
    ptr = strchr(pat+1, *target);
    if(ptr != 0 && ptr < end)
    return end - pat + 1;
    else
    return 0;
    }

    if(*pat == '?' && *target != 0)
    return 1;

    if(*pat == '*')
    return 0;

    if(*target == 0 || *pat == 0)
    return 0;

    if(*target == *pat)
    return 1;

    return 0;
    }
    Malcolm McLean, Dec 2, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ed
    Replies:
    24
    Views:
    977
    Dimitri Maziuk
    Mar 27, 2006
  2. DaveInSidney
    Replies:
    0
    Views:
    401
    DaveInSidney
    May 9, 2005
  3. pabbu
    Replies:
    8
    Views:
    710
    Marc Boyer
    Nov 7, 2005
  4. Shravani
    Replies:
    8
    Views:
    780
    Bartc
    Mar 16, 2008
  5. Replies:
    28
    Views:
    1,148
Loading...

Share This Page