[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
a set of functions for Case-insensitive test
From: |
Benoît Rouits |
Subject: |
a set of functions for Case-insensitive test |
Date: |
Mon, 30 Jul 2001 12:15:35 +0200 |
Hello !
As i worked on some problems due to win32 CVS Clients with
case insensitive tests,
I made a small set of functions to test -Case insensitivly-
the presence of a file
in an Un*x server. I think it would be good to integrate it in
the parts of code
where there are stat() or other access to files, embedded by a
#ifdef CASE_INSENSITIVE
Or, if you don't want to manage win32 problems, tell about
this problem in the doc !
Cheers, Ben
(please CC replies to ben@fr.alcove.com since i unsubscribed
from cvs-bug, thanks.)
--
Benoit Rouits | Tel : (+33) 1 4922 7719
Free Software Engineer | http://www.alcove.com
/*give the directory and the filename extracted from the path*/
/* in : "path". out : "directory", "filename" (must be malloc'd) */
#define SEPARATOR '/'
#define MAXLEN 256
void basename(directory,filename,path)
char *directory;
char *filename;
char *path;
{
/* reverse a string */
/* in : "str". out : "rev" (must be malloc'd) */
void s_reverse(rev, str)
char *rev;
char *str;
{
int i;
for (i=strlen(str)-1;i>=0;i--)
{
rev[strlen(str)-i-1]=str[i];
}
rev[strlen(str)]='\0';
}
int i;
char rev_path[MAXLEN];
char rev_file[MAXLEN];
char rev_dir[MAXLEN];
/* first, we reverse the path string to parse */
s_reverse(rev_path,path);
i=0;
/* extract filename (reversed) */
while (rev_path[i] != SEPARATOR && i < strlen(rev_path))
{
rev_file[i]=rev_path[i];
i++;
}
rev_file[i]='\0';
s_reverse(filename, rev_file);
/* extract directory (reversed) */
while (i < strlen(rev_path))
{
rev_dir[i-strlen(rev_file)]=rev_path[i+1];
i++;
}
rev_dir[strlen(rev_path)-strlen(rev_file)]='\0';
s_reverse(directory, rev_dir);
}
/* gives "filename" that exists in "directory" with CASE INSENSITIVE TEST*/
/* in : "file", "directory". out :"return-fname" (must be malloc'd) */
/* return : 1 if filename exists (CASE INSENSITIVE), 0 if not */
int case_insensitive_exist(return_fname ,directory, file)
char *return_fname;
char *directory;
char *file;
{
int exist = 0;
struct dirent *d_ent; /* need of <dirent.h> */
DIR *dp;
/* Converts a string s into upper-case string dest. Need of <ctype.h> */
/* Warning : you must malloc "dest" ! */
void s_toupper(dest,s)
char *dest;
char *s;
{
int i;
for (i=0;i<=strlen(s);i++)
{
dest[i]=(char)toupper((int)s[i]);
}
}
/* open directory*/
dp=opendir(directory);
if (dp)
{
char upper_d_name[MAXLEN];
char upper_file[MAXLEN];
/* list directory */
while (d_ent=readdir(dp))
{ /* compare case-insensitive */
s_toupper(upper_d_name,d_ent->d_name);
s_toupper(upper_file, file);
if (!strcmp(upper_d_name,upper_file))
{
exist=1;
break;
}
}
}
if (exist)
{
strcpy (return_fname,d_ent->d_name);
return 1;
}
else
{
strcpy(return_fname,file);
return 0;
}
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- a set of functions for Case-insensitive test,
Benoît Rouits <=