Add file-system functions in base.

This commit is contained in:
David Capello 2012-08-16 11:57:25 -03:00
parent e976005cdd
commit dc9a2ced94
7 changed files with 230 additions and 0 deletions

View File

@ -20,6 +20,7 @@ add_library(base-lib
convert_to.cpp
errno_string.cpp
exception.cpp
fs.cpp
mem_utils.cpp
memory.cpp
memory_dump.cpp
@ -30,6 +31,7 @@ add_library(base-lib
sha1_rfc3174.c
split_string.cpp
string.cpp
temp_dir.cpp
thread.cpp
trim_string.cpp
version.cpp)

15
src/base/fs.cpp Normal file
View File

@ -0,0 +1,15 @@
// ASEPRITE base library
// Copyright (C) 2001-2012 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#include "config.h"
#include "base/fs.h"
#ifdef _WIN32
#include "base/fs_win32.h"
#else
#include "base/fs_unix.h"
#endif

24
src/base/fs.h Normal file
View File

@ -0,0 +1,24 @@
// ASEPRITE base library
// Copyright (C) 2001-2012 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#ifndef BASE_FS_H_INCLUDED
#define BASE_FS_H_INCLUDED
#include "base/string.h"
namespace base {
bool file_exists(const string& path);
bool directory_exists(const string& path);
void make_directory(const string& path);
void remove_directory(const string& path);
string get_temp_path();
}
#endif

54
src/base/fs_unix.h Normal file
View File

@ -0,0 +1,54 @@
// ASEPRITE base library
// Copyright (C) 2001-2012 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdexcept>
namespace base {
bool file_exists(const string& path)
{
struct stat sts;
return (stat(path.c_str(), &sts) == 0 && S_ISREG(sts.st_mode)) ? true: false;
}
bool directory_exists(const string& path)
{
struct stat sts;
return (stat(path.c_str(), &sts) == 0 && S_ISDIR(sts.st_mode)) ? true: false;
}
void make_directory(const string& path)
{
int result = mkdir(path.c_str(), 0777);
if (result < 0) {
// TODO add errno into the exception
throw runtime_error("Error creating directory");
}
}
void remove_directory(const string& path)
{
int result = rmdir(path.c_str());
if (result != 0) {
// TODO add errno into the exception
throw runtime_error("Error removing directory");
}
}
string get_temp_path()
{
char* tmpdir = getenv("TMPDIR");
if (tmpdir)
return tmpdir;
else
return "/tmp";
}
}

55
src/base/fs_win32.h Normal file
View File

@ -0,0 +1,55 @@
// ASEPRITE base library
// Copyright (C) 2001-2012 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#include <windows.h>
#include <stdexcept>
namespace base {
bool file_exists(const string& path)
{
DWORD attr = ::GetFileAttributes(path.c_str());
// GetFileAttributes returns INVALID_FILE_ATTRIBUTES in case of
// fail.
return ((attr != INVALID_FILE_ATTRIBUTES) &&
!(attr & FILE_ATTRIBUTE_DIRECTORY));
}
bool directory_exists(const string& path)
{
DWORD attr = ::GetFileAttributes(path.c_str());
return ((attr != INVALID_FILE_ATTRIBUTES) &&
((attr & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY));
}
void make_directory(const string& path)
{
BOOL result = ::CreateDirectory(path.c_str(), NULL);
if (result == 0) {
// TODO add GetLastError() value into the exception
throw std::runtime_error("Error creating directory");
}
}
void remove_directory(const string& path)
{
BOOL result = ::RemoveDirectory(path.c_str());
if (result == 0) {
// TODO add GetLastError() value into the exception
throw std::runtime_error("Error removing directory");
}
}
string get_temp_path()
{
TCHAR buffer[MAX_PATH+1];
DWORD result = GetTempPath(sizeof(buffer)/sizeof(TCHAR), buffer);
return string(buffer);
}
}

51
src/base/temp_dir.cpp Normal file
View File

@ -0,0 +1,51 @@
// ASEPRITE base library
// Copyright (C) 2001-2012 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#include "config.h"
#include "base/temp_dir.h"
#include "base/convert_to.h"
#include "base/fs.h"
#include "base/path.h"
#include <cstdlib>
namespace base {
TempDir::TempDir()
{
}
TempDir::TempDir(const string& appName)
{
for (int i=(std::rand()%0xffff); ; ++i) {
m_path = join_path(get_temp_path(),
appName + convert_to<string>(i));
if (!directory_exists(m_path)) {
make_directory(m_path);
break;
}
}
}
TempDir::~TempDir()
{
if (!m_path.empty())
remove_directory(m_path);
}
void TempDir::attach(const string& path)
{
if (!m_path.empty())
remove_directory(m_path);
ASSERT(directory_exists(path));
m_path = path;
}
}

29
src/base/temp_dir.h Normal file
View File

@ -0,0 +1,29 @@
// ASEPRITE base library
// Copyright (C) 2001-2012 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#ifndef BASE_TEMP_DIR_H_INCLUDED
#define BASE_TEMP_DIR_H_INCLUDED
#include "base/string.h"
namespace base {
class TempDir {
public:
TempDir();
TempDir(const string& appName);
~TempDir();
void attach(const string& path);
const string& path() const { return m_path; }
private:
string m_path;
};
}
#endif