[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 23/27: Use gnulib for basename / dirname
From: |
Andy Wingo |
Subject: |
[Guile-commits] 23/27: Use gnulib for basename / dirname |
Date: |
Mon, 25 Jul 2016 09:53:46 +0000 (UTC) |
wingo pushed a commit to branch master
in repository guile.
commit 1f14900a07ce5542060522a032c99dca99e41dee
Author: Andy Wingo <address@hidden>
Date: Sun Jul 17 08:02:33 2016 +0200
Use gnulib for basename / dirname
* libguile/filesys.c (scm_dirname, scm_basename): Rewrite to use
gnulib's dirname-lgpl.
---
libguile/filesys.c | 86 +++++++++++++++++++---------------------------------
1 file changed, 32 insertions(+), 54 deletions(-)
diff --git a/libguile/filesys.c b/libguile/filesys.c
index 879a72d..c4f2653 100644
--- a/libguile/filesys.c
+++ b/libguile/filesys.c
@@ -35,6 +35,7 @@
#endif
#include <alloca.h>
+#include <dirname.h>
#include <stdlib.h>
#include <stdio.h>
@@ -1546,31 +1547,22 @@ SCM_DEFINE (scm_dirname, "dirname", 1, 0, 0,
"component, @code{.} is returned.")
#define FUNC_NAME s_scm_dirname
{
- long int i;
- unsigned long int len;
+ char *c_filename, *c_dirname;
+ SCM res;
- SCM_VALIDATE_STRING (1, filename);
-
- len = scm_i_string_length (filename);
+ scm_dynwind_begin (0);
+ c_filename = scm_to_utf8_string (filename);
+ scm_dynwind_free (c_filename);
- i = len - 1;
+ c_dirname = mdir_name (c_filename);
+ if (!c_dirname)
+ SCM_SYSERROR;
+ scm_dynwind_free (c_dirname);
- while (i >= 0 && is_file_name_separator (scm_c_string_ref (filename, i)))
- --i;
- while (i >= 0 && !is_file_name_separator (scm_c_string_ref (filename, i)))
- --i;
- while (i >= 0 && is_file_name_separator (scm_c_string_ref (filename, i)))
- --i;
+ res = scm_from_utf8_string (c_dirname);
+ scm_dynwind_end ();
- if (i < 0)
- {
- if (len > 0 && is_file_name_separator (scm_c_string_ref (filename, 0)))
- return scm_c_substring (filename, 0, 1);
- else
- return scm_dot_string;
- }
- else
- return scm_c_substring (filename, 0, i + 1);
+ return res;
}
#undef FUNC_NAME
@@ -1582,42 +1574,28 @@ SCM_DEFINE (scm_basename, "basename", 1, 1, 0,
"@var{filename}, it is removed also.")
#define FUNC_NAME s_scm_basename
{
- int i, j, len, end;
+ char *c_filename, *c_last_component;
+ SCM res;
- SCM_VALIDATE_STRING (1, filename);
- len = scm_i_string_length (filename);
+ scm_dynwind_begin (0);
+ c_filename = scm_to_utf8_string (filename);
+ scm_dynwind_free (c_filename);
- if (SCM_UNBNDP (suffix))
- j = -1;
- else
- {
- SCM_VALIDATE_STRING (2, suffix);
- j = scm_i_string_length (suffix) - 1;
- }
- i = len - 1;
- while (i >= 0 && is_file_name_separator (scm_c_string_ref (filename, i)))
- --i;
- end = i;
- while (i >= 0 && j >= 0
- && (scm_i_string_ref (filename, i)
- == scm_i_string_ref (suffix, j)))
- {
- --i;
- --j;
- }
- if (j == -1)
- end = i;
- while (i >= 0 && !is_file_name_separator (scm_c_string_ref (filename, i)))
- --i;
- if (i == end)
- {
- if (len > 0 && is_file_name_separator (scm_c_string_ref (filename, 0)))
- return scm_c_substring (filename, 0, 1);
- else
- return scm_dot_string;
- }
+ c_last_component = last_component (c_filename);
+ if (!c_last_component)
+ res = filename;
else
- return scm_c_substring (filename, i+1, end+1);
+ res = scm_from_utf8_string (c_last_component);
+ scm_dynwind_end ();
+
+ if (!SCM_UNBNDP (suffix) &&
+ scm_is_true (scm_string_suffix_p (suffix, filename,
+ SCM_UNDEFINED, SCM_UNDEFINED,
+ SCM_UNDEFINED, SCM_UNDEFINED)))
+ res = scm_c_substring
+ (res, 0, scm_c_string_length (res) - scm_c_string_length (suffix));
+
+ return res;
}
#undef FUNC_NAME
- [Guile-commits] 12/27: Add popen feature, (continued)
- [Guile-commits] 12/27: Add popen feature, Andy Wingo, 2016/07/25
- [Guile-commits] 10/27: Rename win32-uname.[ch] to posix-w32.[ch], Andy Wingo, 2016/07/25
- [Guile-commits] 16/27: Update uname implementation in posix-w32, Andy Wingo, 2016/07/25
- [Guile-commits] 22/27: Gnulib: Add dirname-lgpl., Andy Wingo, 2016/07/25
- [Guile-commits] 14/27: Fix MinGW build error, Andy Wingo, 2016/07/25
- [Guile-commits] 06/27: getaffinity, setaffinity docstring cleanup, Andy Wingo, 2016/07/25
- [Guile-commits] 13/27: Update NEWS, Andy Wingo, 2016/07/25
- [Guile-commits] 26/27: Fix --without-threads against threaded BDW-GC, Andy Wingo, 2016/07/25
- [Guile-commits] 08/27: Factor start_child out of open_process, Andy Wingo, 2016/07/25
- [Guile-commits] 21/27: doc: Add unquote and unquote-splicing examples., Andy Wingo, 2016/07/25
- [Guile-commits] 23/27: Use gnulib for basename / dirname,
Andy Wingo <=
- [Guile-commits] 15/27: Fix unused static variables in net_db.c, Andy Wingo, 2016/07/25
- [Guile-commits] 25/27: Reimplement null-threads as inline functions, Andy Wingo, 2016/07/25
- [Guile-commits] 18/27: Allow mkstemp! to have optional "mode" argument, Andy Wingo, 2016/07/25
- [Guile-commits] 20/27: Untabify posix-w32.c, Andy Wingo, 2016/07/25
- [Guile-commits] 11/27: Add POSIX shims for MinGW, Andy Wingo, 2016/07/25
- [Guile-commits] 19/27: Improve process handling on MS-Windows, Andy Wingo, 2016/07/25