[patch libiberty include]: Add additional helper functions for directory-separator searching

Kai Tietz
2011-03-08T10:56:57Z

[-- Attachment #1: Type: text/plain, Size: 1130 bytes --]

Hello,

This patch introduce directory-separator search routines to libiberty.
IMHO filename_cmp.c is suiteable for those functions, but if there are
objections about that I can move it into a separate source-file. It
helps to avoid a commonly used pattern about dir-separator searching
in code, which requires #if-conditions to check if DOS paths are used
and introduces additional internal variables.

the pattern

const char *filename = strrchr (xloc.file, '/');
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
const char *filename2 = strrchr (xloc.file, '\\');

if (!filename || (filename2 && filename2 > filename))
filename = filename2;

can be written by this patch as
const char *filename = filename_dirrchr (xloc.file);

ChangeLog include/

2011-03-08 Kai Tietz

* filenames.h (filename_dirchr): New prototype.
(filename_dirrchr): Likewise.

ChangeLog libiberty/

2011-03-08 Kai Tietz

* filename_cmp.c (filename_dirchr): New function.
(filename_dirrchr): Likewise.
* functions.texi: Regenerated.

Tested for x86_64-pc-linux-gnu and x86_64-w64-mingw32. Ok for apply?

Regards,
Kai

[-- Attachment #2: libiberty_dirsep.txt --]
[-- Type: text/plain, Size: 3386 bytes --]

Index: gcc/include/filenames.h
===================================================================
--- gcc.orig/include/filenames.h 2011-02-28 19:16:35.000000000 +0100
+++ gcc/include/filenames.h 2011-03-08 11:11:10.909109700 +0100
@@ -75,6 +75,8 @@ extern int filename_cmp (const char *s1,

extern int filename_ncmp (const char *s1, const char *s2,
size_t n);
+extern char *filename_dirchr (const char *p);
+extern char *filename_dirrchr (const char *p);

#ifdef __cplusplus
}
Index: gcc/libiberty/filename_cmp.c
===================================================================
--- gcc.orig/libiberty/filename_cmp.c 2011-02-28 19:16:35.000000000 +0100
+++ gcc/libiberty/filename_cmp.c 2011-03-08 11:43:32.797198100 +0100
@@ -125,3 +125,70 @@ filename_ncmp (const char *s1, const cha
return 0;
#endif
}
+
+/*
+
+@deftypefn Extension int filename_dirchr (const char *@var{p})
+
+The returned value is similar to what @code{strchr} would return for
+searching for a directory separator.
+
+This function does not normalize file name. However, it does handle
+the fact that on DOS-like file systems, forward and backward slashes
+are directory separators.
+
+@end deftypefn
+
+*/
+
+char *
+filename_dirchr (const char *p)
+{
+ char *r;
+#ifdef HAVE_DOS_BASED_FILE_SYSTEM
+ char *r2;
+#endif
+ if (!p)
+ return NULL;
+ r = strchr (p, '/');
+#ifdef HAVE_DOS_BASED_FILE_SYSTEM
+ r2 = strchr (p, '\\');
+ if (!r || (r2 && r2 < r))
+ r = r2;
+#endif
+ return r;
+}
+
+/*
+
+@deftypefn Extension int filename_dirrchr (const char *@var{p})
+
+The returned value is similar to what @code{strrchr} would return for
+searching for a directory separator.
+
+This function does not normalize file name. However, it does handle
+the fact that on DOS-like file systems, forward and backward slashes
+are directory separators.
+
+@end deftypefn
+
+*/
+
+char *
+filename_dirrchr (const char *p)
+{
+ char *r;
+#ifdef HAVE_DOS_BASED_FILE_SYSTEM
+ char *r2;
+#endif
+
+ if (!p)
+ return NULL;
+ r = strrchr (p, '/');
+#ifdef HAVE_DOS_BASED_FILE_SYSTEM
+ r2 = strrchr (p, '\\');
+ if (!r || (r2 && r2 > r))
+ r = r2;
+#endif
+ return r;
+}
Index: gcc/libiberty/functions.texi
===================================================================
--- gcc.orig/libiberty/functions.texi 2011-02-28 19:16:35.000000000 +0100
+++ gcc/libiberty/functions.texi 2011-03-08 11:43:42.314406700 +0100
@@ -296,6 +296,30 @@ and backward slashes are equal.

@end deftypefn

+@c filename_cmp.c:131
+@deftypefn Extension int filename_dirchr (const char *@var{p})
+
+The returned value is similar to what @code{strchr} would return for
+searching for a directory separator.
+
+This function does not normalize file name. However, it does handle
+the fact that on DOS-like file systems, forward and backward slashes
+are directory separators.
+
+@end deftypefn
+
+@c filename_cmp.c:164
+@deftypefn Extension int filename_dirrchr (const char *@var{p})
+
+The returned value is similar to what @code{strrchr} would return for
+searching for a directory separator.
+
+This function does not normalize file name. However, it does handle
+the fact that on DOS-like file systems, forward and backward slashes
+are directory separators.
+
+@end deftypefn
+
@c filename_cmp.c:81
@deftypefn Extension int filename_ncmp (const char *@var{s1}, const char *@var{s2}, size_t @var{n})