Changed Boolean function signatures (e.g. exists) to return bool instead of int. Got rid of some gotos. Added the readText, lastModified, mkdirRecurse, and rmdirRecurse functions.

This commit is contained in:
Andrei Alexandrescu 2008-05-16 21:07:50 +00:00
parent ad796a88a2
commit 1036d05646

View file

@ -940,7 +940,7 @@ void[] read(string name)
*
*/
S readText(S)(in string name)
S readText(S = string)(in string name)
{
auto result = cast(S) read(name);
std.utf.validate(result);
@ -1199,6 +1199,17 @@ void mkdir(string pathname)
cenforce(std.c.linux.linux.mkdir(toStringz(pathname), 0777) == 0, pathname);
}
/****************************************************
* Make directory and all parent directories as needed.
*/
void mkdirRecurse(string pathname)
{
invariant left = dirname(pathname);
exists(left) || mkdirRecurse(left);
mkdir(pathname);
}
/****************************************************
* Remove directory.
*/
@ -1208,6 +1219,31 @@ void rmdir(string pathname)
cenforce(std.c.linux.linux.rmdir(toStringz(pathname)) == 0, pathname);
}
/****************************************************
Remove directory and all of its content and subdirectories,
recursively.
*/
void rmdirRecurse(string pathname)
{
// all children, recursively depth-first
foreach (DirEntry e; dirEntries(pathname, SpanMode.depth))
{
e.isdir ? rmdir(e.name) : remove(e.name);
}
// the dir itself
rmdir(pathname);
}
unittest
{
auto d = "/tmp/deleteme/a/b/c/d/e/f/g";
enforce(collectException(mkdir(d)));
mkdirRecurse(d);
rmdirRecurse("/tmp/deleteme");
enforce(!exists("/tmp/deleteme"));
}
/****************************************************
* Get current directory.
*/