mirror of
https://github.com/dlang/phobos.git
synced 2025-05-01 07:30:33 +03:00
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:
parent
ad796a88a2
commit
1036d05646
1 changed files with 37 additions and 1 deletions
38
std/file.d
38
std/file.d
|
@ -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);
|
auto result = cast(S) read(name);
|
||||||
std.utf.validate(result);
|
std.utf.validate(result);
|
||||||
|
@ -1199,6 +1199,17 @@ void mkdir(string pathname)
|
||||||
cenforce(std.c.linux.linux.mkdir(toStringz(pathname), 0777) == 0, 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.
|
* Remove directory.
|
||||||
*/
|
*/
|
||||||
|
@ -1208,6 +1219,31 @@ void rmdir(string pathname)
|
||||||
cenforce(std.c.linux.linux.rmdir(toStringz(pathname)) == 0, 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.
|
* Get current directory.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue