lib: Wrapper for retrieving info from a database directory

Currently provides list of segbits files saved in the database.

Signed-off-by: Rick Altherr <kc8apf@kc8apf.net>
Signed-off-by: Tim 'mithro' Ansell <mithro@mithis.com>
This commit is contained in:
Rick Altherr 2017-11-20 16:38:34 -08:00 committed by Tim 'mithro' Ansell
parent 3e203b956b
commit 51b586365b
3 changed files with 63 additions and 0 deletions

View File

@ -1,4 +1,5 @@
add_library(libprjxray
database.cc
memory_mapped_file.cc
segbits_file_reader.cc)
target_include_directories(libprjxray PUBLIC "include")

37
lib/database.cc Normal file
View File

@ -0,0 +1,37 @@
#include <prjxray/database.h>
#include <glob.h>
#include <memory>
#include <absl/strings/str_cat.h>
namespace prjxray {
static constexpr const char kSegbitsGlobPattern[] = "segbits_*.db";
std::vector<std::unique_ptr<prjxray::SegbitsFileReader>> Database::segbits() const {
std::vector<std::unique_ptr<prjxray::SegbitsFileReader>> segbits;
glob_t segbits_glob_results;
int ret = glob(absl::StrCat(db_path_, "/", kSegbitsGlobPattern).c_str(),
GLOB_NOSORT | GLOB_TILDE,
NULL, &segbits_glob_results);
if (ret < 0) {
return {};
}
for(size_t idx = 0; idx < segbits_glob_results.gl_pathc; idx++) {
auto this_segbit = SegbitsFileReader::InitWithFile(
segbits_glob_results.gl_pathv[idx]);
if (this_segbit) {
segbits.emplace_back(std::move(this_segbit));
}
}
globfree(&segbits_glob_results);
return segbits;
}
} // namespace prjxray

View File

@ -0,0 +1,25 @@
#ifndef PRJXRAY_LIB_DATABASE_H
#define PRJXRAY_LIB_DATABASE_H
#include <memory>
#include <string>
#include <vector>
#include <prjxray/segbits_file_reader.h>
namespace prjxray {
class Database {
public:
Database(const std::string &path)
: db_path_(path) {}
std::vector<std::unique_ptr<SegbitsFileReader>> segbits() const;
private:
std::string db_path_;
};
} // namespace prjxray
#endif // PRJXRAY_LIB_DATABASE_H