Skip to content

Commit

Permalink
Replace boost::filesystem by std::filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldk committed May 4, 2020
1 parent 7f32f9c commit 18165f0
Show file tree
Hide file tree
Showing 17 changed files with 103 additions and 109 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ enable_testing()

set (ALPINOCORPUS_VERSION "2.6.0")

set (CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_STANDARD 17)

if (NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
set (CMAKE_BUILD_TYPE Release)
Expand Down Expand Up @@ -41,7 +41,7 @@ endif()

include(GNUInstallDirs REQUIRED)

find_package(Boost 1.50 COMPONENTS system filesystem REQUIRED)
find_package(Boost 1.50 COMPONENTS system REQUIRED)

if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
Expand Down
13 changes: 6 additions & 7 deletions src/CompactCorpusReaderPrivate.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <filesystem>
#include <fstream>
#include <memory>
#include <mutex>
Expand All @@ -7,8 +8,6 @@
#include <typeinfo>
#include <vector>

#include <boost/filesystem.hpp>

#include <AlpinoCorpus/Error.hh>

#include "DzIstream.hh"
Expand All @@ -20,7 +19,7 @@ namespace {
char const * const INDEX_EXT = ".index";
}

namespace bf = boost::filesystem;
namespace fs = std::filesystem;

namespace alpinocorpus {

Expand Down Expand Up @@ -51,12 +50,12 @@ void CompactCorpusReaderPrivate::construct(std::string const &canonical,
std::string const &indexPath)
{
// XXX race condition up ahead
bf::path dataP(dataPath);
if (!bf::is_regular_file(dataP))
fs::path dataP(dataPath);
if (!fs::is_regular_file(dataP))
throw OpenError(dataPath, "not a regular file");

bf::path indexP(indexPath);
if (!bf::is_regular_file(indexP))
fs::path indexP(indexPath);
if (!fs::is_regular_file(indexP))
throw OpenError(indexPath, "not a regular file");

open(dataPath, indexPath);
Expand Down
9 changes: 4 additions & 5 deletions src/DbCorpusWriter.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <sstream>
#include <string>

#include <boost/filesystem.hpp>

#include <dbxml/DbXml.hpp>

#include <AlpinoCorpus/DbCorpusWriter.hh>
#include <AlpinoCorpus/Entry.hh>
#include <AlpinoCorpus/Error.hh>
#include <AlpinoCorpus/util/NonCopyable.hh>

namespace bf = boost::filesystem;
namespace fs = std::filesystem;
namespace db = DbXml;

namespace alpinocorpus {
Expand Down Expand Up @@ -76,7 +75,7 @@ namespace alpinocorpus {
db::XmlContainer
::NodeContainer);
} else {
if (bf::exists(path))
if (fs::exists(path))
d_container = d_mgr.openContainer(path, config);
else
d_container = d_mgr.createContainer(path, config,
Expand Down Expand Up @@ -149,7 +148,7 @@ namespace alpinocorpus {
db::XmlUpdateContext &ctx)
{
try {
std::string canonical(bf::path(name).generic_string());
std::string canonical(fs::path(name).generic_string());
d_container.putDocument(canonical, content, ctx,
db::DBXML_WELL_FORMED_ONLY);
} catch (db::XmlException const &e) {
Expand Down
48 changes: 25 additions & 23 deletions src/DirectoryCorpusReaderPrivate.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <limits>
Expand All @@ -8,7 +9,6 @@

#include <boost/algorithm/string/predicate.hpp>
#include <boost/assert.hpp>
#include <boost/filesystem.hpp>

#include <AlpinoCorpus/Error.hh>
#include <AlpinoCorpus/Entry.hh>
Expand All @@ -18,17 +18,17 @@
#include "util/NameCompare.hh"
#include "util/textfile.hh"

namespace bf = boost::filesystem;
namespace fs = std::filesystem;

namespace {
class DirIter : public alpinocorpus::IterImpl
{
boost::filesystem::recursive_directory_iterator iter;
boost::filesystem::path d_directory;
fs::recursive_directory_iterator iter;
fs::path d_directory;

public:
DirIter(boost::filesystem::path const &path,
boost::filesystem::recursive_directory_iterator i);
DirIter(fs::path const &path,
fs::recursive_directory_iterator i);
alpinocorpus::IterImpl *copy() const;
bool hasNext();
alpinocorpus::Entry next(alpinocorpus::CorpusReader const &rdr);
Expand All @@ -37,7 +37,7 @@ namespace {
};

DirIter::DirIter(
bf::path const &path, bf::recursive_directory_iterator i) :
fs::path const &path, fs::recursive_directory_iterator i) :
d_directory(path), iter(i)
{
}
Expand All @@ -51,7 +51,7 @@ namespace {
bool DirIter::isValid()
{
// End is a correct iterator state.
if (iter == bf::recursive_directory_iterator())
if (iter == fs::recursive_directory_iterator())
return true;

return iter->path().extension() == ".xml";
Expand All @@ -64,7 +64,7 @@ namespace {
++iter;
}

return iter != bf::recursive_directory_iterator();
return iter != fs::recursive_directory_iterator();
}

alpinocorpus::Entry DirIter::next(alpinocorpus::CorpusReader const &rdr)
Expand All @@ -78,7 +78,7 @@ namespace {
if (entryPathStr[0] == '/')
entryPathStr.erase(0, 1);

bf::path entryPath(entryPathStr);
fs::path entryPath(entryPathStr);

// Move the iterator.
++iter;
Expand All @@ -94,8 +94,8 @@ namespace {
std::vector<std::string>::const_iterator d_iter;

public:
SortedDirIter(boost::filesystem::path const &path,
boost::filesystem::recursive_directory_iterator i);
SortedDirIter(fs::path const &path,
fs::recursive_directory_iterator i);
alpinocorpus::IterImpl *copy() const;
bool hasNext();
alpinocorpus::Entry next(alpinocorpus::CorpusReader const &rdr);
Expand All @@ -106,9 +106,9 @@ namespace {
};

SortedDirIter::SortedDirIter(
bf::path const &path, bf::recursive_directory_iterator i)
fs::path const &path, fs::recursive_directory_iterator i)
{
for (; i != bf::recursive_directory_iterator(); i++)
for (; i != fs::recursive_directory_iterator(); i++)
{
std::string entryPathStr = i->path().string();
entryPathStr.erase(0, path.string().size());
Expand Down Expand Up @@ -161,7 +161,7 @@ namespace {
{
// We assume the iterator is valid, since hasNext() should be called

bf::path entryPath(*d_iter);
fs::path entryPath(*d_iter);

// Move the iterator.
++d_iter;
Expand All @@ -180,12 +180,12 @@ DirectoryCorpusReaderPrivate::DirectoryCorpusReaderPrivate(
d_nEntries(std::numeric_limits<size_t>::max())
{
if (directory[directory.size() - 1] == '/')
d_directory = bf::path(directory).parent_path();
d_directory = fs::path(directory).parent_path();
else
d_directory = bf::path(directory);
d_directory = fs::path(directory);

if (!bf::exists(d_directory) ||
!bf::is_directory(d_directory))
if (!fs::exists(d_directory) ||
!fs::is_directory(d_directory))
throw OpenError(directory, "non-existent or not a directory");
}

Expand All @@ -197,10 +197,12 @@ CorpusReader::EntryIterator DirectoryCorpusReaderPrivate::getEntries(SortOrder s
switch (sortOrder) {
case NaturalOrder:
return EntryIterator(new DirIter(d_directory,
bf::recursive_directory_iterator(d_directory, bf::symlink_option::recurse)));
fs::recursive_directory_iterator(d_directory,
fs::directory_options::follow_directory_symlink)));
case NumericalOrder:
return EntryIterator(new SortedDirIter(d_directory,
bf::recursive_directory_iterator(d_directory, bf::symlink_option::recurse)));
fs::recursive_directory_iterator(d_directory,
fs::directory_options::follow_directory_symlink)));
default:
throw NotImplemented("Unexpected sort order.");
}
Expand Down Expand Up @@ -232,12 +234,12 @@ size_t DirectoryCorpusReaderPrivate::getSize() const

std::string DirectoryCorpusReaderPrivate::readEntry(std::string const &entry) const
{
bf::path p(d_directory);
fs::path p(d_directory);
p /= entry;
return util::readFile(p.string());
}

bf::path DirectoryCorpusReaderPrivate::cachePath() const
fs::path DirectoryCorpusReaderPrivate::cachePath() const
{
return d_directory.parent_path() / d_directory.filename().replace_extension(".dir_index");
}
Expand Down
7 changes: 3 additions & 4 deletions src/DirectoryCorpusReaderPrivate.hh
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#ifndef ALPINO_DIRECTORYCORPUSREADER_PRIVATE_HH
#define ALPINO_DIRECTORYCORPUSREADER_PRIVATE_HH

#include <filesystem>
#include <string>
#include <vector>

#include <boost/filesystem.hpp>

#include <AlpinoCorpus/CorpusReader.hh>
#include <AlpinoCorpus/Entry.hh>
#include <AlpinoCorpus/IterImpl.hh>
Expand All @@ -32,9 +31,9 @@ public:
virtual size_t getSize() const;

private:
boost::filesystem::path cachePath() const;
std::filesystem::path cachePath() const;

boost::filesystem::path d_directory;
std::filesystem::path d_directory;
mutable size_t d_nEntries;
bool d_entriesRead;
};
Expand Down
35 changes: 20 additions & 15 deletions src/DzOstreamBuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <iostream>
#include <limits>
#include <stdexcept>
Expand All @@ -11,14 +13,12 @@

#include <zlib.h>

#include <boost/filesystem.hpp>

#include "DzOstreamBuf.hh"
#include "gzip.hh"
#include "util/bufutil.hh"


namespace bf = boost::filesystem;
namespace fs = std::filesystem;

namespace {

Expand All @@ -44,13 +44,18 @@ DzOstreamBuf::DzOstreamBuf(char const *filename) : d_size(0), d_crc32(crc32(0L,
return;
}

// XXX - There is a race condition here, but Boost does not seem to
// provide a variant that returns a file descriptor. We used mkstemp
// previously, but it is not portable.
std::string tmpFilename =
bf::unique_path(std::string(filename) + "-%%%%-%%%%-%%%%-%%%%").string();
d_tmpFilename = tmpFilename;
d_zDataStream = fopen(tmpFilename.c_str(), "w");
// XXX - Seems like C++17 does not provide an alternative to mkstemp
// yet. But since we haven't recently built any Windows versions and
// Windows has WSL anyway, this should be fine.
std::string tmpFilename =
std::string(filename) + "-XXXX-XXXX-XXXX-XXXX";
int fd;
if ((fd = mkstemp(tmpFilename.data())) == -1)
throw std::runtime_error(std::string("DzOstreamBuf::DzOstreamBuf: Could not create temporary file") +
d_tmpFilename);

d_tmpFilename = tmpFilename;
d_zDataStream = fdopen(fd, "w");

if (d_zDataStream == NULL)
throw std::runtime_error(std::string("DzOstreamBuf::DzOstreamBuf: Could not open ") +
Expand Down Expand Up @@ -114,7 +119,7 @@ DzOstreamBuf::~DzOstreamBuf()

fclose(d_dzStream);

bf::remove(d_tmpFilename);
fs::remove(d_tmpFilename);
}

void DzOstreamBuf::flushBuffer()
Expand Down Expand Up @@ -202,14 +207,14 @@ void DzOstreamBuf::writeHeader()
auto secsSinceEpoch = std::chrono::duration_cast<std::chrono::seconds>(
clock.time_since_epoch()).count();

if (secsSinceEpoch > std::numeric_limits<boost::int32_t>::max())
if (secsSinceEpoch > std::numeric_limits<int32_t>::max())
secsSinceEpoch = 0;

header[GZ_HEADER_ID1] = gzipId1;
header[GZ_HEADER_ID2] = gzipId2;
header[GZ_HEADER_CM] = GZ_CM_DEFLATE;
header[GZ_HEADER_FLG] = GZ_FLG_EXTRA;
util::writeToBuf<boost::uint32_t>(&header[0] + GZ_HEADER_MTIME, secsSinceEpoch);
util::writeToBuf<uint32_t>(&header[0] + GZ_HEADER_MTIME, secsSinceEpoch);
header[GZ_HEADER_XFL] = GZ_XFL_MAX;
header[GZ_HEADER_OS] = GZ_OS_UNIX;

Expand All @@ -220,8 +225,8 @@ void DzOstreamBuf::writeTrailer()
{
std::vector<unsigned char> trailer(GZ_TRAILER_SIZE);

util::writeToBuf<boost::uint32_t>(&trailer[0] + GZ_TRAILER_CRC32, d_crc32);
util::writeToBuf<boost::uint32_t>(&trailer[0] + GZ_TRAILER_ISIZE, d_size);
util::writeToBuf<uint32_t>(&trailer[0] + GZ_TRAILER_CRC32, d_crc32);
util::writeToBuf<uint32_t>(&trailer[0] + GZ_TRAILER_ISIZE, d_size);

fwrite(&trailer[0], 1, GZ_TRAILER_SIZE, d_dzStream);
}
Expand Down
9 changes: 4 additions & 5 deletions src/MultiCorpusReaderPrivate.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include <filesystem>
#include <list>
#include <memory>
#include <mutex>
#include <stdexcept>
#include <string>
#include <utility>

#include <boost/filesystem.hpp>

#include <AlpinoCorpus/CorpusReader.hh>
#include <AlpinoCorpus/CorpusReaderFactory.hh>
#include <AlpinoCorpus/Error.hh>
Expand All @@ -21,7 +20,7 @@

#include "MultiCorpusReaderPrivate.hh"

namespace bf = boost::filesystem;
namespace fs = std::filesystem;

namespace alpinocorpus {

Expand Down Expand Up @@ -80,8 +79,8 @@ size_t MultiCorpusReaderPrivate::getSize() const
void MultiCorpusReaderPrivate::push_back(std::string const &name,
std::string const &filename, bool recursive)
{
bf::path corpusPath(filename);
if (!bf::exists(corpusPath))
fs::path corpusPath(filename);
if (!fs::exists(corpusPath))
throw OpenError(filename);

d_corpora.push_back(std::make_pair(filename, recursive));
Expand Down
Loading

0 comments on commit 18165f0

Please sign in to comment.