8#ifndef _AUROSTD_DATA_CPP_
9#define _AUROSTD_DATA_CPP_
20#include <archive_entry.h>
28#ifndef CMAKE_SOURCE_DIR
29#define CMAKE_SOURCE_DIR ""
48 {
"README", {(
char *) &afdataReadMeData[0], afdataReadMeSize}},
49 {
"FINDSYM", {(
char *) &afdataFINDSYMData[0], afdataFINDSYMSize}},
50 {
"FROZSL", {(
char *) &afdataFROZSLData[0], afdataFROZSLSize}},
51 {
"PHVASP", {(
char *) &afdataPHVASPData[0], afdataPHVASPSize}},
52 {
"TEST", {(
char *) &afdataTestData[0], afdataTestSize}},
53 {
"IMAGES", {(
char *) &afdataImagesData[0], afdataImagesSize}},
54 {
"SCRIPTS", {(
char *) &afdataScriptsData[0], afdataScriptsSize}},
55 {
"PROTO", {(
char *) &afdataPrototypesData[0], afdataPrototypesSize}},
60 const size_t buff_size = 16384;
61 char buff[buff_size + 1];
64 size = archive_read_data(ar, &
buff, buff_size);
74 void write_collection_folder(
const std::pair<char *, unsigned int> raw_data,
const std::string &path,
const std::string &folder_out_raw) {
75 if (path.back() !=
'/') {
76 const std::string message =
"The requested path (" + path +
") is not a folder - use write_collection_file";
80 struct archive *a = archive_read_new();
81 struct archive_entry *entry;
82 static char buff[16384];
86 std::filesystem::path file_out;
87 std::filesystem::path raw_out;
89 archive_read_support_format_tar(a);
90 archive_read_support_filter_xz(a);
92 if (archive_read_open_memory(a, raw_data.first, raw_data.second)) {
93 const std::string message =
"Embedded archive could not be open while trying to read \"" + path +
"\". " + archive_error_string(a);
98 r = archive_read_next_header(a, &entry);
99 if (r == ARCHIVE_EOF) {
102 if (r != ARCHIVE_OK) {
103 const std::string message =
"Failed to open entries in archive while trying to read \"" + path +
"\". " + archive_error_string(a);
108 if (strncmp(archive_entry_pathname(entry), path.c_str(), path.size()) == 0) {
110 raw_out = std::filesystem::path(archive_entry_pathname(entry));
111 file_out = folder_out / std::filesystem::proximate(raw_out, path);
113 std::filesystem::create_directories(file_out.parent_path());
115 std::fstream fs(file_out, std::ios::out);
116 len = archive_read_data(a,
buff,
sizeof(
buff));
119 len = archive_read_data(a,
buff,
sizeof(
buff));
124 archive_read_data_skip(a);
130 archive_read_free(a);
133 void write_collection_file(
const std::pair<char *, unsigned int> raw_data,
const std::string &filename,
const std::string &outfile_raw) {
135 struct archive *a = archive_read_new();
136 struct archive_entry *entry;
137 static char buff[16384];
141 const std::filesystem::path path = std::filesystem::path(outfile);
142 archive_read_support_format_tar(a);
143 archive_read_support_filter_xz(a);
146 if (archive_read_open_memory(a, raw_data.first, raw_data.second)) {
147 const std::string message =
"Embedded archive could not be open while trying to read \"" + filename +
"\". " + archive_error_string(a);
151 r = archive_read_next_header(a, &entry);
152 if (r == ARCHIVE_EOF) {
155 if (r != ARCHIVE_OK) {
156 const std::string message =
"Failed to open entries in archive while trying to read \"" + filename +
"\". " + archive_error_string(a);
160 if (archive_entry_pathname(entry) == filename) {
162 if (outfile.empty()) {
163 outfile = path.parent_path().string() +
"/" + path.stem().string();
165 std::fstream fs(outfile, std::ios::out);
166 len = archive_read_data(a,
buff,
sizeof(
buff));
169 len = archive_read_data(a,
buff,
sizeof(
buff));
175 archive_read_data_skip(a);
181 archive_read_free(a);
184 std::string
get_collection_text(
const std::pair<char *, unsigned int> raw_data,
const std::string &filename) {
185 struct archive *a = archive_read_new();
186 struct archive_entry *entry;
188 archive_read_support_format_tar(a);
189 archive_read_support_filter_xz(a);
193 if (archive_read_open_memory(a, raw_data.first, raw_data.second)) {
194 const std::string message =
"Embedded archive could not be open while trying to read \"" + filename +
"\". " + archive_error_string(a);
198 r = archive_read_next_header(a, &entry);
199 if (r == ARCHIVE_EOF) {
202 if (r != ARCHIVE_OK) {
203 const std::string message =
"Failed to open entries in archive while trying to read \"" + filename +
"\". " + archive_error_string(a);
206 if (archive_entry_pathname(entry) == filename) {
211 archive_read_data_skip(a);
217 archive_read_free(a);
237 std::string
get_content(
const std::string &filename,
const std::string &collection) {
241 const string message =
"Collection \"" + collection +
" \" was not embedded";
251 void save_to_file(
const std::string &filename,
const std::string &collection,
const std::string &target_path) {
255 const string message =
"Collection \"" + collection +
" \" was not embedded";
265 void save_to_folder(
const std::string &path,
const std::string &collection,
const std::string &target_path) {
269 const string message =
"Collection \"" + collection +
" \" was not embedded";
#define INCBIN(...)
Include a binary file into the current translation unit.
std::string read_data(struct archive *ar)
void save_to_file(const std::string &filename, const std::string &collection, const std::string &target_path)
write a embedded file into the filesystem
static const std::map< std::string, std::pair< char *, unsigned int > > aflow_data_collections
std::string get_content(const std::string &filename, const std::string &collection)
get the content of an embedded file
string get_test_file(string file)
return data for unittests
void write_collection_folder(const std::pair< char *, unsigned int > raw_data, const std::string &path, const std::string &folder_out_raw)
void save_to_folder(const std::string &path, const std::string &collection, const std::string &target_path)
write a embedded file into the filesystem
std::string get_collection_text(const std::pair< char *, unsigned int > raw_data, const std::string &filename)
void write_collection_file(const std::pair< char *, unsigned int > raw_data, const std::string &filename, const std::string &outfile_raw)
aurostd::JSON::object get_unit_test()
return data for unittests
object loadString(const std::string &content)
create a JSON::object from raw string
string CleanFileName(const string &fileIN)
cleans file names from obvious things
storge container for a JSON object