2013-01-04 16:01:50 +01:00
|
|
|
// Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
//
|
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2013-05-05 16:54:33 +02:00
|
|
|
#include "libaegisub/access.h"
|
2013-01-04 16:01:50 +01:00
|
|
|
#include "libaegisub/fs.h"
|
2013-05-05 16:54:33 +02:00
|
|
|
#include "libaegisub/io.h"
|
2013-01-04 16:01:50 +01:00
|
|
|
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <fnmatch.h>
|
2014-03-21 22:37:21 +01:00
|
|
|
#include <istream>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
namespace bfs = boost::filesystem;
|
|
|
|
|
|
|
|
namespace agi { namespace fs {
|
|
|
|
std::string ShortName(path const& p) {
|
|
|
|
return p.string();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Touch(path const& file) {
|
|
|
|
CreateDirectory(file.parent_path());
|
|
|
|
|
|
|
|
int fd = open(file.c_str(), O_CREAT | O_APPEND | O_WRONLY, 0644);
|
|
|
|
if (fd >= 0) {
|
|
|
|
futimes(fd, nullptr);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-05 16:54:33 +02:00
|
|
|
void Copy(fs::path const& from, fs::path const& to) {
|
|
|
|
acs::CheckFileRead(from);
|
|
|
|
CreateDirectory(to.parent_path());
|
|
|
|
acs::CheckDirWrite(to.parent_path());
|
|
|
|
|
2014-03-21 22:37:21 +01:00
|
|
|
auto in = io::Open(from, true);
|
2013-05-05 16:54:33 +02:00
|
|
|
io::Save(to).Get() << in->rdbuf();
|
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
struct DirectoryIterator::PrivData {
|
|
|
|
boost::system::error_code ec;
|
|
|
|
bfs::directory_iterator it;
|
|
|
|
std::string filter;
|
|
|
|
PrivData(path const& p, std::string const& filter) : it(p, ec), filter(filter) { }
|
|
|
|
|
|
|
|
bool bad() const {
|
|
|
|
return
|
|
|
|
it == bfs::directory_iterator() ||
|
|
|
|
(!filter.empty() && fnmatch(filter.c_str(), it->path().filename().c_str(), 0));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DirectoryIterator::DirectoryIterator() { }
|
|
|
|
DirectoryIterator::DirectoryIterator(path const& p, std::string const& filter)
|
|
|
|
: privdata(new PrivData(p, filter))
|
|
|
|
{
|
|
|
|
if (privdata->it == bfs::directory_iterator())
|
|
|
|
privdata.reset();
|
|
|
|
else if (privdata->bad())
|
|
|
|
++*this;
|
|
|
|
else
|
|
|
|
value = privdata->it->path().filename().string();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DirectoryIterator::operator==(DirectoryIterator const& rhs) const {
|
|
|
|
return privdata.get() == rhs.privdata.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
DirectoryIterator& DirectoryIterator::operator++() {
|
|
|
|
if (!privdata) return *this;
|
|
|
|
|
|
|
|
++privdata->it;
|
|
|
|
|
|
|
|
while (privdata->bad()) {
|
|
|
|
if (privdata->it == bfs::directory_iterator()) {
|
|
|
|
privdata.reset();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
++privdata->it;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = privdata->it->path().filename().string();
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
DirectoryIterator::~DirectoryIterator() { }
|
|
|
|
|
|
|
|
} }
|