-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #363.
- Loading branch information
Showing
5 changed files
with
111 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,21 @@ | ||
find_package(fmt CONFIG REQUIRED) | ||
find_package(jsoncons CONFIG REQUIRED) | ||
find_package(libzippp CONFIG REQUIRED) | ||
|
||
add_library(HealthGPS.Datastore STATIC "") | ||
target_compile_features(HealthGPS.Datastore PUBLIC cxx_std_${CMAKE_CXX_STANDARD}) | ||
|
||
target_sources(HealthGPS.Datastore PRIVATE "api.h" "datamanager.cpp" "datamanager.h" "schema.cpp" | ||
"schema.h") | ||
target_sources( | ||
HealthGPS.Datastore | ||
PRIVATE "api.h" | ||
"datamanager.cpp" | ||
"datamanager.h" | ||
"schema.cpp" | ||
"schema.h" | ||
"zip_file.cpp" | ||
"zip_file.h") | ||
|
||
target_link_libraries(HealthGPS.Datastore PRIVATE HealthGPS.Core fmt::fmt jsoncons) | ||
target_link_libraries(HealthGPS.Datastore PRIVATE HealthGPS.Core fmt::fmt jsoncons | ||
libzippp::libzippp) | ||
|
||
set(ROOT_NAMESPACE hgps::data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include "zip_file.h" | ||
|
||
#include <fmt/format.h> | ||
#include <libzippp.h> | ||
|
||
#include <fstream> | ||
#include <random> | ||
|
||
namespace hgps::data { | ||
std::filesystem::path create_temporary_directory() { | ||
auto tmp_dir = std::filesystem::temp_directory_path(); | ||
std::random_device dev; | ||
std::mt19937 prng(dev()); | ||
std::uniform_int_distribution<unsigned> rand; | ||
std::filesystem::path path; | ||
|
||
while (true) { | ||
std::stringstream ss; | ||
ss << std::hex << rand(prng); | ||
path = tmp_dir / ss.str(); | ||
// true if the directory was created. | ||
if (std::filesystem::create_directory(path)) { | ||
return path; | ||
} | ||
} | ||
} | ||
|
||
void extract_zip_file(const std::filesystem::path &file_path, | ||
const std::filesystem::path &output_directory) { | ||
|
||
using namespace libzippp; | ||
|
||
ZipArchive zf(file_path); | ||
zf.open(); | ||
|
||
std::filesystem::path out_path; | ||
for (const auto &entry : zf.getEntries()) { | ||
out_path = output_directory / entry.getName(); | ||
if (entry.isDirectory()) { | ||
if (!std::filesystem::create_directories(out_path)) { | ||
throw std::runtime_error{ | ||
fmt::format("Failed to create directory: {}", out_path.string())}; | ||
} | ||
} else { | ||
std::ofstream ofs{out_path}; | ||
if (!ofs) { | ||
throw std::runtime_error{ | ||
fmt::format("Failed to create file: {}", out_path.string())}; | ||
} | ||
|
||
ofs << entry.readAsText(); | ||
} | ||
} | ||
} | ||
} // namespace hgps::data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
|
||
#include <filesystem> | ||
|
||
namespace hgps::data { | ||
//! Create a temporary directory with a unique path | ||
std::filesystem::path create_temporary_directory(); | ||
|
||
/// @brief Extract a zip file to the specified location | ||
/// @param file_path The path to the zip file | ||
/// @param output_directory The path to the output folder | ||
void extract_zip_file(const std::filesystem::path &file_path, | ||
const std::filesystem::path &output_directory); | ||
} // namespace hgps::data |