Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > globing on windows in c / c++ language

Reply
Thread Tools

globing on windows in c / c++ language

 
 
Vipin
Guest
Posts: n/a
 
      12-02-2010
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;
}
 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      12-02-2010
On Dec 2, 7:57*am, Vipin <(E-Mail Removed)> 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;
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
writing a compiler for Monster language using C language Shravani C Programming 8 03-16-2008 09:36 PM
A language-agnostic language Ed Java 24 03-27-2006 08:19 PM
c is a low-level language or neither low level nor high level language pabbu C Programming 8 11-07-2005 03:05 PM
Using a Scripting Language as Your Scripting Language DaveInSidney Python 0 05-09-2005 03:13 AM
Python is the best and most popular general purpose scripting language; the universal scripting language Ron Stephens Python 23 04-12-2004 05:32 PM



Advertisments