Add base::make_all_directories function

This commit is contained in:
David Capello 2014-08-23 20:26:59 -03:00
parent fb68969ba5
commit 87857f5a95
5 changed files with 33 additions and 4 deletions

View File

@ -1,5 +1,5 @@
# Aseprite
# Copyright (C) 2001-2013 David Capello
# Copyright (C) 2001-2014 David Capello
add_library(app-lib
app.cpp

View File

@ -1,5 +1,5 @@
# Aseprite Base Library
# Copyright (c) 2001-2013 David Capello
# Copyright (c) 2001-2014 David Capello
include(CheckCSourceCompiles)

View File

@ -9,9 +9,36 @@
#endif
#include "base/fs.h"
#include "base/split_string.h"
#ifdef _WIN32
#include "base/fs_win32.h"
#else
#include "base/fs_unix.h"
#endif
namespace base {
void make_all_directories(const std::string& path)
{
std::vector<std::string> parts;
split_string(path, parts, "/\\");
std::string intermediate;
for (const std::string& component : parts) {
if (component.empty()) {
if (intermediate.empty())
intermediate += "/";
continue;
}
intermediate = join_path(intermediate, component);
if (is_file(intermediate))
throw std::runtime_error("Error creating directory (a component is a file name)");
else if (!is_directory(intermediate))
make_directory(intermediate);
}
}
} // namespace base

View File

@ -23,6 +23,7 @@ namespace base {
void remove_readonly_attr(const std::string& path);
void make_directory(const std::string& path);
void make_all_directories(const std::string& path);
void remove_directory(const std::string& path);
std::string get_app_path();

View File

@ -6,8 +6,10 @@
#include <stdexcept>
#include <windows.h>
#include <shlobj.h>
#include <sys/stat.h>
#include "base/path.h"
#include "base/string.h"
#include "base/win32_exception.h"
@ -66,7 +68,6 @@ void make_directory(const std::string& path)
throw Win32Exception("Error creating directory");
}
void remove_directory(const std::string& path)
{
BOOL result = ::RemoveDirectory(from_utf8(path).c_str());
@ -86,7 +87,7 @@ std::string get_app_path()
std::string get_temp_path()
{
TCHAR buffer[MAX_PATH+1];
DWORD result = GetTempPath(sizeof(buffer)/sizeof(TCHAR), buffer);
DWORD result = ::GetTempPath(sizeof(buffer)/sizeof(TCHAR), buffer);
return to_utf8(buffer);
}