return n == std::string::npos ? path : path.substr(n + 1);
}
+std::string
+dir_name(const std::string& path)
+{
+ size_t n = path.rfind('/');
+#ifdef _WIN32
+ size_t n2 = path.rfind('\\');
+ if (n2 != std::string::npos && n2 > n) {
+ n = n2;
+ }
+#endif
+ if (n == std::string::npos) {
+ return ".";
+ }
+ return n == 0 ? "/" : path.substr(0, n);
+}
+
std::string
read_file(const std::string& path)
{
// Get base name of path.
std::string base_name(const std::string& path);
+// Get directory name of path.
+std::string dir_name(const std::string& path);
+
// Read file data as a string.
std::string read_file(const std::string& path);
CHECK(util::base_name("/foo/bar/f.txt") == "f.txt");
}
+TEST_CASE("util::dir_name")
+{
+ CHECK(util::dir_name("") == ".");
+ CHECK(util::dir_name(".") == ".");
+ CHECK(util::dir_name("foo") == ".");
+ CHECK(util::dir_name("/") == "/");
+ CHECK(util::dir_name("/foo") == "/");
+ CHECK(util::dir_name("/foo/bar/f.txt") == "/foo/bar");
+}
+
TEST_CASE("util::read_file and util::write_file")
{
util::write_file("test", "foo\nbar\n");