Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   globing on windows in c / c++ language (http://www.velocityreviews.com/forums/t739310-globing-on-windows-in-c-c-language.html)

Vipin 12-02-2010 05:57 AM

globing on windows in c / c++ language
 
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;
}

Malcolm McLean 12-02-2010 03:04 PM

Re: globing on windows in c / c++ language
 
On Dec 2, 7:57*am, Vipin <sh.vi...@gmail.com> 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;
}


All times are GMT. The time now is 04:36 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.