#include #include #include #include #include #include "ZipExport.h" #include "Pokedex.h" #include "PokemonCard.h" ZipExport::ZipExport() { } ZipExport::~ZipExport() { } void ZipExport::set_pokedex(Pokedex* pokedex) { this->pokedex = pokedex; } void ZipExport::export_zip(std::string filename, std::vector cards) { this->zip_path = filename; std::vector zip_srcs; zip_t* zip = zip_open(this->zip_path.c_str(), ZIP_CREATE | ZIP_EXCL, nullptr); std::string csv_content = "id,value\n"; for (PokemonCard* card : cards) { char* value_str; asprintf(&value_str, "%.2f", card->value); csv_content.append(card->id + "," + std::string(value_str) + "\n"); free(value_str); zip_source* image = zip_source_file(zip, card->image_location_hires().c_str(), 0, 0); zip_file_add(zip, std::string(card->id + ".png").c_str(), image, 0); zip_srcs.push_back(image); } zip_source* csv_file = zip_source_buffer(zip, csv_content.data(), csv_content.size(), 0); zip_file_add(zip, "cards.csv", csv_file, 0); zip_close(zip); } std::vector ZipExport::import_csv(std::string filename) { this->csv_path = filename; csv2::Reader, csv2::quote_character<'"'>, csv2::first_row_is_header, csv2::trim_policy::trim_whitespace> csv; if (csv.mmap(this->csv_path)) { for (const auto row: csv) { for (const auto cell: row) { std::string value; cell.read_value(value); this->id_list.push_back(value); } } } else { std::cout << "parsing csv failed!" << std::endl; exit(EXIT_FAILURE); } return this->id_list; }