diff --git a/.travis.yml b/.travis.yml index de41cdf..1ba552d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,9 +9,7 @@ addons: key_url: 'https://apt.llvm.org/llvm-snapshot.gpg.key' packages: - libarchive-dev - - libboost-filesystem-dev - libboost-locale-dev - - libboost-system-dev - libtdb-dev - g++-8 - clang-7 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index addf652..d3566e2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -25,8 +25,6 @@ ENDIF(STATIC_LINKS) FIND_PACKAGE(Boost 1.44 COMPONENTS - filesystem - system locale REQUIRED) @@ -39,6 +37,8 @@ FIND_PACKAGE(LibArchive 3 REQUIRED) INCLUDE_DIRECTORIES(${LibArchive_INCLUDE_DIRS}) LIST(APPEND EXTRA_LIBRARIES ${LibArchive_LIBRARIES}) +LIST(APPEND EXTRA_LIBRARIES stdc++fs) + ###### PROJECT CONFIGURATION ###### ### Names ### @@ -57,11 +57,6 @@ SET (VERSION_SHORT "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}${VERSION_ GET_GIT_HEAD_REVISION(GIT_REFSPEC GIT_HASH GIT_HASH_SHORT) -IF ("${CMAKE_BUILD_TYPE}" MATCHES "^Debug$") - SET (DEBUG 1) - MESSAGE (STATUS "Compiling with debug symbols") -ENDIF() - ### Application Configuration ### SET (DATABASE_PATH ${CMAKE_INSTALL_PREFIX}/var/lib/${BINARY_NAME}) @@ -73,11 +68,6 @@ CONFIGURE_FILE ( "${PROJECT_BINARY_DIR}/config.cpp" ) -CONFIGURE_FILE ( - "${PROJECT_SOURCE_DIR}/config.h.in" - "${PROJECT_BINARY_DIR}/config.h" -) - CONFIGURE_FILE ( "${PROJECT_SOURCE_DIR}/cnf-sync.in" "${PROJECT_BINARY_DIR}/cnf-sync" @@ -100,7 +90,8 @@ INCLUDE_DIRECTORIES ("${PROJECT_BINARY_DIR}" "${PROJECT_SOURCE_DIR}") ###### EXECUTABLES and LIBRARIES###### -SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Werror -std=c++14") +SET(CMAKE_CXX_STANDARD 17) +SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Werror") OPTION(ENABLE_CLANG_TIDY OFF) IF(ENABLE_CLANG_TIDY) diff --git a/src/config.h.in b/src/config.h similarity index 98% rename from src/config.h.in rename to src/config.h index 9370229..4bcfa43 100644 --- a/src/config.h.in +++ b/src/config.h @@ -21,8 +21,6 @@ #include -#cmakedefine DEBUG - namespace cnf { extern const std::string PROGRAM_NAME; diff --git a/src/custom_exceptions.h b/src/custom_exceptions.h index a6acfff..62ecb2b 100644 --- a/src/custom_exceptions.h +++ b/src/custom_exceptions.h @@ -16,8 +16,8 @@ along with command-not-found. If not, see . */ -#ifndef EXCEPTIONS_H_ -#define EXCEPTIONS_H_ +#ifndef CUSTOM_EXCEPTIONS_H_ +#define CUSTOM_EXCEPTIONS_H_ #include #include @@ -50,4 +50,4 @@ class DatabaseException : public ErrorCodeException { }; } // namespace cnf -#endif /* EXCEPTIONS_H_ */ +#endif /* CUSTOM_EXCEPTIONS_H_ */ diff --git a/src/db.cpp b/src/db.cpp index e2fe18f..2a154ef 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -17,6 +17,7 @@ */ #include +#include #include #include #include @@ -33,49 +34,46 @@ #include "db_tdb.h" #include "similar.h" -namespace bf = boost::filesystem; -using namespace std; +namespace fs = std::filesystem; using boost::format; using boost::locale::translate; namespace cnf { -const shared_ptr getDatabase(const string& id, - const bool readonly, - const string& base_path) { - return shared_ptr(new TdbDatabase(id, readonly, base_path)); +std::unique_ptr getDatabase(const std::string& id, + const bool readonly, + const std::string& base_path) { + return std::make_unique(id, readonly, base_path); } -void getCatalogs(const string& database_path, vector& result) { +void getCatalogs(const std::string& database_path, + std::vector& result) { TdbDatabase::getCatalogs(database_path, result); } -void lookup(const string& search_string, - const string& database_path, +void lookup(const std::string& search_string, + const std::string& database_path, ResultMap& result, - vector* const inexact_matches) { - vector catalogs; + std::vector* const inexact_matches) { + std::vector catalogs; getCatalogs(database_path, catalogs); if (!catalogs.empty()) { - vector terms; + std::vector terms; if (inexact_matches) { terms = similar_words(search_string); } for (const auto& catalog : catalogs) { - vector packs; + std::vector packs; try { + auto d = getDatabase(catalog, true, database_path); if (inexact_matches == nullptr) { - getDatabase(catalog, true, database_path) - ->getPackages(search_string, packs); + d->getPackages(search_string, packs); } else { - const shared_ptr& d = - getDatabase(catalog, true, database_path); - for (const auto& term : terms) { - vector tempPack; + std::vector tempPack; d->getPackages(term, tempPack); if (!tempPack.empty()) { packs.insert(packs.end(), tempPack.begin(), @@ -86,7 +84,7 @@ void lookup(const string& search_string, } } catch (const DatabaseException& e) { - cerr << e.what() << endl; + std::cerr << e.what() << '\n'; } if (!packs.empty()) { @@ -95,38 +93,39 @@ void lookup(const string& search_string, } } } else { - cout << format(translate("WARNING: No database for lookup!")) << endl; + std::cout << format(translate("WARNING: No database for lookup!")) + << '\n'; } } -void populate_mirror(const bf::path& mirror_path, - const string& database_path, +void populate_mirror(const fs::path& mirror_path, + const std::string& database_path, const bool truncate, const uint8_t verbosity) { - using dirIter = bf::directory_iterator; + using dirIter = fs::directory_iterator; - static const string architectures[] = {"i686", "x86_64"}; + static const std::string architectures[] = {"i686", "x86_64"}; for (const auto& architecture : architectures) { - vector catalogs; + std::vector catalogs; for (dirIter iter = dirIter(mirror_path); iter != dirIter(); ++iter) { - const string& catalog = - bf::path(*iter).stem().string() + "-" + architecture; + const std::string& catalog = + fs::path(*iter).stem().string() + "-" + architecture; bool truncated = !truncate; bool list_catalog = false; - bf::path dir = bf::path(*iter) / "os" / architecture; - if (bf::is_directory(dir)) { + fs::path dir = fs::path(*iter) / "os" / architecture; + if (fs::is_directory(dir)) { populate(dir, database_path, catalog, !truncated, verbosity); truncated = true; list_catalog = true; } - dir = bf::path(*iter) / "os" / "any"; - if (bf::is_directory(dir)) { + dir = fs::path(*iter) / "os" / "any"; + if (fs::is_directory(dir)) { populate(dir, database_path, catalog, !truncated, verbosity); list_catalog = true; } @@ -135,30 +134,31 @@ void populate_mirror(const bf::path& mirror_path, } } - const bf::path& catalogs_file_name = - bf::path(database_path) / ("catalogs-" + architecture + "-tdb"); + const fs::path& catalogs_file_name = + fs::path(database_path) / ("catalogs-" + architecture + "-tdb"); - ofstream catalogs_file; + std::ofstream catalogs_file; - catalogs_file.open(catalogs_file_name.c_str(), ios::trunc | ios::out); + catalogs_file.open(catalogs_file_name.c_str(), + std::ios::trunc | std::ios::out); for (const auto& catalog : catalogs) { - catalogs_file << catalog << ".tdb" << endl; + catalogs_file << catalog << ".tdb" << '\n'; } catalogs_file.close(); } } -void populate(const bf::path& path, - const string& database_path, - const string& catalog, +void populate(const fs::path& path, + const std::string& database_path, + const std::string& catalog, const bool truncate, const uint8_t verbosity) { - shared_ptr d; + std::unique_ptr d; try { d = getDatabase(catalog, false, database_path); } catch (const DatabaseException& e) { - cerr << e.what() << endl; + std::cerr << e.what() << '\n'; return; } @@ -166,7 +166,7 @@ void populate(const bf::path& path, d->truncate(); } - using dirIter = bf::directory_iterator; + using dirIter = fs::directory_iterator; uint32_t count = 0; @@ -178,23 +178,24 @@ void populate(const bf::path& path, for (dirIter iter = dirIter(path); iter != dirIter(); ++iter) { if (verbosity > 0) { - cout << format(translate("[ %d / %d ] %s...")) % ++current % count % - *iter; - cout.flush(); + std::cout << format(translate("[ %d / %d ] %s...")) % ++current % + count % *iter; + std::cout.flush(); } try { Package p(*iter, true); d->storePackage(p); if (verbosity > 0) { - cout << translate("done") << endl; + std::cout << translate("done") << '\n'; } } catch (const InvalidArgumentException& e) { if (verbosity > 0) { - cout << format(translate("skipping (%s)")) % e.what() << endl; + std::cout << format(translate("skipping (%s)")) % e.what() + << '\n'; } continue; } catch (const DatabaseException& e) { - cerr << e.what() << endl; + std::cerr << e.what() << '\n'; return; } } diff --git a/src/db.h b/src/db.h index 537c762..3930600 100644 --- a/src/db.h +++ b/src/db.h @@ -20,6 +20,7 @@ #define DB_H_ #include +#include #include #include #include @@ -61,9 +62,9 @@ class Database { using ResultMap = std::map>; -const std::shared_ptr getDatabase(const std::string& id, - bool readonly, - const std::string& base_path); +std::unique_ptr getDatabase(const std::string& id, + bool readonly, + const std::string& base_path); void getCatalogs(const std::string& database_path, std::vector& result); @@ -73,12 +74,12 @@ void lookup(const std::string& search_string, ResultMap& result, std::vector* inexact_matches = nullptr); -void populate_mirror(const boost::filesystem::path& path, +void populate_mirror(const std::filesystem::path& path, const std::string& database_path, bool truncate, uint8_t verbosity); -void populate(const boost::filesystem::path& path, +void populate(const std::filesystem::path& path, const std::string& database_path, const std::string& catalog, bool truncate, diff --git a/src/db_tdb.cpp b/src/db_tdb.cpp index 250972c..41b785d 100644 --- a/src/db_tdb.cpp +++ b/src/db_tdb.cpp @@ -17,6 +17,7 @@ */ #include +#include #include #include #include @@ -29,38 +30,38 @@ #include "db.h" #include "db_tdb.h" -namespace bf = boost::filesystem; -using namespace std; +namespace fs = std::filesystem; using boost::format; using boost::locale::translate; namespace cnf { -TdbDatabase::TdbDatabase(const string& id, +TdbDatabase::TdbDatabase(const std::string& id, const bool readonly, - const string& base_path) + const std::string& base_path) : Database(id, readonly, base_path) , m_databaseName(m_basePath + "/" + m_id + ".tdb") { - if (!bf::is_directory(base_path)) { - cout << format(translate( - "Directory '%s' does not exist. Trying to create it ...")) % - base_path - << endl; + if (!fs::is_directory(base_path)) { + std::cout + << format(translate( + "Directory '%s' does not exist. Trying to create it ...")) % + base_path + << '\n'; try { - bf::create_directories(base_path); - } catch (const bf::filesystem_error& e) { - cerr << format(translate( - "Could not create database directory: %s: ")) % - e.code().message(); + fs::create_directories(base_path); + } catch (const fs::filesystem_error& e) { + std::cerr << format(translate( + "Could not create database directory: %s: ")) % + e.code().message(); if (!e.path1().empty()) { - cerr << e.path1(); + std::cerr << e.path1(); } if (!e.path2().empty()) { - cerr << ", " << e.path2(); + std::cerr << ", " << e.path2(); } - cerr << endl; + std::cerr << '\n'; - string message; + std::string message; message += translate("Error opening tdb database: "); message += translate("Missing database directory!"); throw DatabaseException(CONNECT_ERROR, message); @@ -75,7 +76,7 @@ TdbDatabase::TdbDatabase(const string& id, } if (m_tdbFile == nullptr) { - string message; + std::string message; message += translate("Error opening tdb database: "); message += m_databaseName; throw DatabaseException(CONNECT_ERROR, message); @@ -136,7 +137,7 @@ void TdbDatabase::storePackage(const Package& p) { tdb_store(m_tdbFile, kv.key(), kv.value(), TDB_INSERT); } - string filesString; + std::string filesString; bool first = true; for (const auto& elem : p.files()) { @@ -147,16 +148,17 @@ void TdbDatabase::storePackage(const Package& p) { if (ret != 0) { fkv.setValue(tdb_fetch(m_tdbFile, fkv.key())); - vector others; - istringstream iss(fkv.value_str()); - copy(istream_iterator(iss), istream_iterator(), - back_inserter>(others)); + std::vector others; + std::istringstream iss(fkv.value_str()); + copy(std::istream_iterator(iss), + std::istream_iterator(), + std::back_inserter>(others)); others.push_back(p.name()); auto uIter = unique(others.begin(), others.end()); others.resize(uIter - others.begin()); - string newValue; + std::string newValue; bool isthefirst = true; for (auto& other : others) { if (isthefirst) { @@ -188,16 +190,17 @@ void TdbDatabase::storePackage(const Package& p) { } } -void TdbDatabase::getPackages(const string& search, - vector& result) const { +void TdbDatabase::getPackages(const std::string& search, + std::vector& result) const { TdbKeyValue name_kv; name_kv.setKey(search); name_kv.setValue(tdb_fetch(m_tdbFile, name_kv.key())); - vector package_names; - istringstream iss(name_kv.value_str()); - copy(istream_iterator(iss), istream_iterator(), - back_inserter>(package_names)); + std::vector package_names; + std::istringstream iss(name_kv.value_str()); + copy(std::istream_iterator(iss), + std::istream_iterator(), + std::back_inserter>(package_names)); for (auto& package_name : package_names) { TdbKeyValue version_kv; @@ -220,10 +223,11 @@ void TdbDatabase::getPackages(const string& search, files_kv.setKey(package_name + "-files"); files_kv.setValue(tdb_fetch(m_tdbFile, files_kv.key())); - istringstream iss(files_kv.value_str()); - vector files; - copy(istream_iterator(iss), istream_iterator(), - back_inserter>(files)); + std::istringstream iss(files_kv.value_str()); + std::vector files; + copy(std::istream_iterator(iss), + std::istream_iterator(), + std::back_inserter>(files)); Package p(package_name, version_kv.value_str(), release_kv.value_str(), arch_kv.value_str(), compression_kv.value_str(), files); @@ -241,30 +245,30 @@ void TdbDatabase::truncate() { S_IRWXU | S_IRGRP | S_IROTH); if (m_tdbFile == nullptr) { - string message; + std::string message; message += translate("Error opening tdb database: "); message += m_databaseName; throw DatabaseException(CONNECT_ERROR, message); } } -void TdbDatabase::getCatalogs(const string& database_path, - vector& result) { - const bf::path p(database_path); +void TdbDatabase::getCatalogs(const std::string& database_path, + std::vector& result) { + const fs::path p(database_path); - if (bf::is_directory(p)) { - using dirIter = bf::directory_iterator; + if (fs::is_directory(p)) { + using dirIter = fs::directory_iterator; for (dirIter iter = dirIter(p); iter != dirIter(); ++iter) { - bf::path cand(*iter); - if (cand.extension() == ".tdb" && bf::is_regular_file(cand)) { + fs::path cand(*iter); + if (cand.extension() == ".tdb" && fs::is_regular_file(cand)) { result.push_back(cand.stem().string()); } } } } -TdbKeyValue::TdbKeyValue(const string& key, const string& value) +TdbKeyValue::TdbKeyValue(const std::string& key, const std::string& value) : m_key(TDB_DATA()), m_value(TDB_DATA()) { setKey(key); setValue(value); @@ -272,7 +276,7 @@ TdbKeyValue::TdbKeyValue(const string& key, const string& value) TdbKeyValue::TdbKeyValue() : m_key(TDB_DATA()), m_value(TDB_DATA()) {} -void TdbKeyValue::setKey(const string& key) { +void TdbKeyValue::setKey(const std::string& key) { if (m_key.dptr != nullptr) { free(m_key.dptr); } @@ -288,7 +292,7 @@ void TdbKeyValue::setKey(const TDB_DATA& key) { m_key = key; } -void TdbKeyValue::setValue(const string& value) { +void TdbKeyValue::setValue(const std::string& value) { if (m_value.dptr != nullptr) { free(m_value.dptr); } @@ -311,7 +315,7 @@ TdbKeyValue::~TdbKeyValue() { m_value.dptr = nullptr; } -unsigned char* getWritableUCString(const string& aString) { +unsigned char* getWritableUCString(const std::string& aString) { auto* result = (unsigned char*)malloc((aString.size() + 1) * sizeof(unsigned char)); copy(aString.begin(), aString.end(), result); diff --git a/src/db_tdb.h b/src/db_tdb.h index 9b75de4..209f9f3 100644 --- a/src/db_tdb.h +++ b/src/db_tdb.h @@ -16,8 +16,8 @@ along with command-not-found. If not, see . */ -#ifndef TDB_H_ -#define TDB_H_ +#ifndef DB_TDB_H_ +#define DB_TDB_H_ #include #include @@ -83,4 +83,4 @@ inline unsigned char* getWritableUCString(const std::string& aString); } // namespace cnf -#endif /* TDB_H_ */ +#endif /* DB_TDB_H_ */ diff --git a/src/lookup.cpp b/src/lookup.cpp index 8fd6316..d4a4b6c 100644 --- a/src/lookup.cpp +++ b/src/lookup.cpp @@ -32,16 +32,14 @@ #include "db.h" #include "package.h" -using namespace cnf; -using namespace std; using boost::format; using boost::locale::translate; static struct args_t { - string database_path; + std::string database_path; bool colors; int verbosity; - string search_string; + std::string search_string; } args; static const char* OPT_STRING = "d:cvh?"; @@ -54,50 +52,51 @@ static const struct option LONG_OPTS[] = { {nullptr, no_argument, nullptr, 0}}; void usage() { - cout << format(translate(" *** %s %s *** " - " \n")) % - PROGRAM_NAME % VERSION_LONG - << translate( - "Usage: " - " \n") - << translate( - " cnf-lookup [ -d ] " - " \n") - << translate( - " " - " \n") - << translate( - "Options: " - " \n") - << translate( - " --help -? -h Show this help and exit " - " \n") - << translate( - " --verbose -v Display verbose output " - " \n") - << translate( - " " - " \n") - << format(translate(" --database-path -d Customize the " - "database lookup path\n" - " default is %s " - " \n")) % - DATABASE_PATH - << translate( - " --colors -c Pretty colored output " - " \n") - << endl; + std::cout + << format(translate(" *** %s %s *** " + " \n")) % + cnf::PROGRAM_NAME % cnf::VERSION_LONG + << translate( + "Usage: " + " \n") + << translate( + " cnf-lookup [ -d ] " + " \n") + << translate( + " " + " \n") + << translate( + "Options: " + " \n") + << translate( + " --help -? -h Show this help and exit " + " \n") + << translate( + " --verbose -v Display verbose output " + " \n") + << translate( + " " + " \n") + << format(translate(" --database-path -d Customize the " + "database lookup path\n" + " default is %s " + " \n")) % + cnf::DATABASE_PATH + << translate( + " --colors -c Pretty colored output " + " \n") + << '\n'; exit(1); } int main(int argc, char** argv) { boost::locale::generator gen; - gen.add_messages_path(LC_MESSAGE_PATH); - gen.add_messages_domain(PROGRAM_NAME); - locale::global(gen("")); - cout.imbue(locale()); + gen.add_messages_path(cnf::LC_MESSAGE_PATH); + gen.add_messages_domain(cnf::PROGRAM_NAME); + std::locale::global(gen("")); + std::cout.imbue(std::locale()); - args.database_path = DATABASE_PATH; + args.database_path = cnf::DATABASE_PATH; args.colors = false; args.verbosity = 0; args.search_string = ""; // actually done implicit @@ -132,11 +131,11 @@ int main(int argc, char** argv) { args.search_string = argv[optind]; - ResultMap result; + cnf::ResultMap result; lookup(args.search_string, args.database_path, result); - stringstream out; + std::stringstream out; for (auto& elem : result) { for (auto& piter : elem.second) { @@ -147,57 +146,55 @@ int main(int argc, char** argv) { } out << format(translate(" (%s-%s) from %s")) % piter.version() % piter.release() % elem.first - << endl; + << '\n'; if (args.colors) { out << piter.hl_str(args.search_string, "\t", "\033[0;31m") - << endl; + << '\n'; } else { - out << piter.hl_str(args.search_string, "\t", "") << endl; + out << piter.hl_str(args.search_string, "\t", "") << '\n'; } } } if (!result.empty()) { - cout + std::cout << format(translate( "The command '%s' is provided by the following packages:")) % args.search_string - << endl; - cout << out.str(); + << '\n'; + std::cout << out.str(); return 0; - } - std::shared_ptr> matches(new vector()); - ResultMap inexactResult; - lookup(args.search_string, args.database_path, inexactResult, - matches.get()); - - for (auto& elem : inexactResult) { - for (auto& piter : elem.second) { - if (args.colors) { - out << "\033[1m" << piter.name() << "\033[0m"; - } else { - out << piter.name(); - } - out << format(translate(" (%s-%s) from %s")) % piter.version() % - piter.release() % elem.first - << endl; - if (args.colors) { - out << piter.hl_str(matches.get(), "\t", "\033[0;31m") - << endl; - } else { - out << piter.hl_str(matches.get(), "\t", "") << endl; - } + auto matches = std::vector(); + cnf::ResultMap inexactResult; + lookup(args.search_string, args.database_path, inexactResult, &matches); + + for (auto& elem : inexactResult) { + for (auto& piter : elem.second) { + if (args.colors) { + out << "\033[1m" << piter.name() << "\033[0m"; + } else { + out << piter.name(); + } + out << format(translate(" (%s-%s) from %s")) % piter.version() % + piter.release() % elem.first + << '\n'; + if (args.colors) { + out << piter.hl_str(&matches, "\t", "\033[0;31m") << '\n'; + } else { + out << piter.hl_str(&matches, "\t", "") << '\n'; } } - if (!inexactResult.empty()) { - cout << format(translate("A similar command to '%s' is provided by " - "the following packages:")) % - args.search_string - << endl; - cout << out.str(); - return 0; - } + } + if (!inexactResult.empty()) { + std::cout << format( + translate("A similar command to '%s' is provided by " + "the following packages:")) % + args.search_string + << '\n'; + std::cout << out.str(); + return 0; + } return 1; } diff --git a/src/package.cpp b/src/package.cpp index 352f6c3..737e04b 100644 --- a/src/package.cpp +++ b/src/package.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -31,29 +32,27 @@ #include "package.h" -namespace bf = boost::filesystem; -using namespace std; using boost::format; using boost::locale::translate; namespace cnf { -Package::Package(const bf::path& path, const bool lazy) - : m_filesDetermined(false), m_path(new bf::path(path)) { +Package::Package(const std::filesystem::path& path, const bool lazy) + : m_filesDetermined(false), m_path(path) { // checks - if (!bf::is_regular_file(path)) { - string message; + if (!std::filesystem::is_regular_file(path)) { + std::string message; message += translate("not a file: "); message += path.string(); throw InvalidArgumentException(MISSING_FILE, message); } - static const regex valid_name( + static const std::regex valid_name( "(.+)-(.+)-(.+)-(any|i686|x86_64).pkg.tar.(xz|gz)"); - cmatch what; + std::cmatch what; - const string& filename = path.filename().string(); + const std::string& filename = path.filename().string(); try { if (regex_match(filename.c_str(), what, valid_name)) { @@ -63,7 +62,7 @@ Package::Package(const bf::path& path, const bool lazy) m_architecture = what[4]; m_compression = what[5]; } else { - string message; + std::string message; message += translate("this is not a valid package file: "); message += path.string(); throw InvalidArgumentException(INVALID_FILE, message); @@ -77,12 +76,12 @@ Package::Package(const bf::path& path, const bool lazy) } } -const vector& Package::files() const { +const std::vector& Package::files() const { if (!m_filesDetermined) { try { updateFiles(); } catch (const InvalidArgumentException& e) { - cerr << e.what() << endl; + std::cerr << e.what() << '\n'; } } return m_files; @@ -91,12 +90,12 @@ const vector& Package::files() const { void Package::updateFiles() const { // read package file list - assert(m_path); + assert(m_path.has_value()); struct archive* arc = nullptr; struct archive_entry* entry = nullptr; int rc = 0; - vector candidates; + std::vector candidates; arc = archive_read_new(); archive_read_support_filter_all(arc); @@ -123,9 +122,9 @@ void Package::updateFiles() const { throw InvalidArgumentException(INVALID_FILE, message.str()); } - const regex significant("((usr/)?(s)?bin/([0-9A-Za-z.-]+))"); + const std::regex significant("((usr/)?(s)?bin/([0-9A-Za-z.-]+))"); - cmatch what; + std::cmatch what; try { for (const auto& candidate : candidates) { if (regex_match(candidate.c_str(), what, significant)) { @@ -139,19 +138,19 @@ void Package::updateFiles() const { m_filesDetermined = true; } -const string Package::hl_str(const string& hl, - const string& files_indent, - const string& color) const { - vector hls; +const std::string Package::hl_str(const std::string& hl, + const std::string& files_indent, + const std::string& color) const { + std::vector hls; hls.push_back(hl); return hl_str(&hls, files_indent, color); } -const string Package::hl_str(const vector* hl, - const string& files_indent, - const string& color) const { - stringstream out; +const std::string Package::hl_str(const std::vector* hl, + const std::string& files_indent, + const std::string& color) const { + std::stringstream out; out << files_indent << "[ "; int linelength = 0; @@ -168,7 +167,7 @@ const string Package::hl_str(const vector* hl, if (linelength + file.size() > 80) { linelength = 0; - out << endl << files_indent << " "; + out << '\n' << files_indent << " "; } linelength += file.size() + 1; @@ -187,8 +186,8 @@ const string Package::hl_str(const vector* hl, return out.str(); } -ostream& operator<<(ostream& out, const Package& p) { - out << p.name() << " (" << p.version() << "-" << p.release() << ")" << endl; +std::ostream& operator<<(std::ostream& out, const Package& p) { + out << p.name() << " (" << p.version() << "-" << p.release() << ")" << '\n'; return out; } diff --git a/src/package.h b/src/package.h index 31af073..d46bfdb 100644 --- a/src/package.h +++ b/src/package.h @@ -16,22 +16,22 @@ along with command-not-found. If not, see . */ -#ifndef PARSEPKG_H_ -#define PARSEPKG_H_ +#ifndef PACKAGE_H_ +#define PACKAGE_H_ +#include +#include #include #include #include -#include - #include "custom_exceptions.h" namespace cnf { class Package { public: - explicit Package(const boost::filesystem::path& path, bool lazy = false); + explicit Package(const std::filesystem::path& path, bool lazy = false); explicit Package(std::string name, std::string version, std::string release, @@ -44,10 +44,7 @@ class Package { , m_architecture(std::move(architecture)) , m_compression(std::move(compression)) , m_files(std::move(files)) - , m_filesDetermined(true) - , m_path(nullptr) {} - - ~Package() { delete m_path; } + , m_filesDetermined(true) {} const std::vector& files() const; @@ -73,7 +70,7 @@ class Package { std::string m_compression; mutable std::vector m_files; mutable bool m_filesDetermined; - boost::filesystem::path* m_path; + std::optional m_path; }; enum PackageError { MISSING_FILE, INVALID_FILE, UNKNOWN_ERROR }; @@ -85,4 +82,4 @@ bool operator==(const Package& lhs, const Package& rhs); } // namespace cnf -#endif /* PARSEPKG_H */ +#endif /* PACKAGE_H */ diff --git a/src/populate.cpp b/src/populate.cpp index 8f29de2..0003f31 100644 --- a/src/populate.cpp +++ b/src/populate.cpp @@ -17,27 +17,24 @@ */ #include +#include #include #include #include -#include #include #include #include "config.h" #include "db.h" -namespace bf = boost::filesystem; -using namespace cnf; -using namespace std; using boost::format; using boost::locale::translate; static struct args_t { - string catalog; - string package_path; - string database_path; + std::string catalog; + std::string package_path; + std::string database_path; int verbosity; bool mirror; bool truncate; @@ -56,59 +53,60 @@ static const struct option LONG_OPTS[] = { {nullptr, no_argument, nullptr, 0}}; void usage() { - cout << format(translate(" *** %s %s *** " - " \n")) % - PROGRAM_NAME % VERSION_LONG - << translate( - "Usage: " - " \n") - << translate( - " cnf-populate -p ( -c | -m ) [ -d ] " - " \n") - << translate( - " " - " \n") - << translate( - "Options: " - " \n") - << translate( - " --help -? -h Show this help and exit " - " \n") - << translate( - " --verbose -v Display verbose output " - " \n") - << translate( - " " - " \n") - << translate( - " --package-path -p Set the path containing the " - "packages \n") - << translate( - " --catalog -c Set the catalog name to index " - "(e.g. core)\n") - << translate( - " --mirror -m Scan mirror structure and detect " - "catalogs\n") - << translate( - " --truncate -t Truncate the catalog before " - "indexing \n") - << format(translate(" --database-path -d Customize the " - "database lookup path \n" - " default is %s " - " \n")) % - DATABASE_PATH - << endl; + std::cout + << format(translate(" *** %s %s *** " + " \n")) % + cnf::PROGRAM_NAME % cnf::VERSION_LONG + << translate( + "Usage: " + " \n") + << translate( + " cnf-populate -p ( -c | -m ) [ -d ] " + " \n") + << translate( + " " + " \n") + << translate( + "Options: " + " \n") + << translate( + " --help -? -h Show this help and exit " + " \n") + << translate( + " --verbose -v Display verbose output " + " \n") + << translate( + " " + " \n") + << translate( + " --package-path -p Set the path containing the " + "packages \n") + << translate( + " --catalog -c Set the catalog name to index " + "(e.g. core)\n") + << translate( + " --mirror -m Scan mirror structure and detect " + "catalogs\n") + << translate( + " --truncate -t Truncate the catalog before " + "indexing \n") + << format(translate(" --database-path -d Customize the " + "database lookup path \n" + " default is %s " + " \n")) % + cnf::DATABASE_PATH + << '\n'; exit(1); } int main(int argc, char** argv) { boost::locale::generator gen; - gen.add_messages_path(LC_MESSAGE_PATH); - gen.add_messages_domain(PROGRAM_NAME); - locale::global(gen("")); - cout.imbue(locale()); + gen.add_messages_path(cnf::LC_MESSAGE_PATH); + gen.add_messages_domain(cnf::PROGRAM_NAME); + std::locale::global(gen("")); + std::cout.imbue(std::locale()); - args.database_path = DATABASE_PATH; + args.database_path = cnf::DATABASE_PATH; args.catalog = ""; args.mirror = false; args.package_path = ""; @@ -159,21 +157,20 @@ int main(int argc, char** argv) { usage(); } - if (!bf::is_directory(args.package_path)) { - cerr << format(translate("Not a valid package path: %s")) % - args.package_path - << endl - << endl; + if (!std::filesystem::is_directory(args.package_path)) { + std::cerr << format(translate("Not a valid package path: %s")) % + args.package_path + << "\n\n"; usage(); return 1; } if (args.mirror) { - populate_mirror(args.package_path, args.database_path, args.truncate, - args.verbosity); + cnf::populate_mirror(args.package_path, args.database_path, + args.truncate, args.verbosity); } else { - populate(args.package_path, args.database_path, args.catalog, - args.truncate, args.verbosity); + cnf::populate(args.package_path, args.database_path, args.catalog, + args.truncate, args.verbosity); } return 0; }