diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-12-27 13:07:35 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-12-27 13:07:35 +0100 |
commit | 16d5aac0190787fa3213435a7f4b1d5f2e1c2c4e (patch) | |
tree | f0c55cc537ece26f76ec4e1653bc0731ca0522da /oop2eindopdr | |
parent | abad89cbb44744d02964eb3d3a56f7227dc7e5cf (diff) |
restructure export mode
Diffstat (limited to 'oop2eindopdr')
-rw-r--r-- | oop2eindopdr/ZipExport.cpp | 28 | ||||
-rw-r--r-- | oop2eindopdr/ZipExport.h | 11 | ||||
-rw-r--r-- | oop2eindopdr/main.cpp | 37 |
3 files changed, 44 insertions, 32 deletions
diff --git a/oop2eindopdr/ZipExport.cpp b/oop2eindopdr/ZipExport.cpp index 3e91256..ae4f6bb 100644 --- a/oop2eindopdr/ZipExport.cpp +++ b/oop2eindopdr/ZipExport.cpp @@ -1,6 +1,7 @@ #include <csv2/reader.hpp> #include <fstream> #include <iostream> +#include <vector> #include <zip.h> #include "ZipExport.h" @@ -10,36 +11,21 @@ ZipExport::ZipExport() { } ZipExport::~ZipExport() { } -ZipExport::ZipExport(Pokedex* pokedex, std::string input_csv, std::string output_zip) { - set_pokedex(pokedex); - import_csv(input_csv); - export_zip(output_zip); -} - void ZipExport::set_pokedex(Pokedex* pokedex) { this->pokedex = pokedex; } -void ZipExport::export_zip(std::string filename) { +void ZipExport::export_zip(std::string filename, std::vector<PokemonCard*> cards) { this->zip_path = filename; std::vector<zip_source*> zip_srcs; zip_t* zip = zip_open(this->zip_path.c_str(), ZIP_CREATE | ZIP_EXCL, nullptr); std::string csv_content = "id,value\n"; - double total_value = 0.f; - - for (std::string card_id : this->id_list) { - PokemonCard* card = this->pokedex->get_card_by_id(card_id); - if (card == nullptr) { - csv_content.append(card->id + "," + "???" + "\n"); - continue; - } - card->fetch_market_value(); + for (PokemonCard* card : cards) { char* value_str; asprintf(&value_str, "%.2f", card->value); - total_value += card->value; csv_content.append(card->id + "," + std::string(value_str) + "\n"); free(value_str); @@ -52,13 +38,9 @@ void ZipExport::export_zip(std::string filename) { zip_file_add(zip, "cards.csv", csv_file, 0); zip_close(zip); - - char* total_value_str; - asprintf(&total_value_str, "%.2f", total_value); - std::cout << "sum of market value of cards: " << std::string(total_value_str) << std::endl; } -void ZipExport::import_csv(std::string filename) { +std::vector<std::string> ZipExport::import_csv(std::string filename) { this->csv_path = filename; csv2::Reader<csv2::delimiter<','>, @@ -78,5 +60,7 @@ void ZipExport::import_csv(std::string filename) { std::cout << "parsing csv failed!" << std::endl; exit(EXIT_FAILURE); } + + return this->id_list; } diff --git a/oop2eindopdr/ZipExport.h b/oop2eindopdr/ZipExport.h index 0800b41..f2dd28a 100644 --- a/oop2eindopdr/ZipExport.h +++ b/oop2eindopdr/ZipExport.h @@ -4,6 +4,7 @@ #include <vector> class Pokedex; +class PokemonCard; /** @brief handle csv parsing and zip export */ class ZipExport { @@ -13,20 +14,18 @@ private: std::string csv_path; /** @brief input csv file path */ std::string zip_path; /** @brief output zip file path */ - std::vector<std::string> id_list; /** @brief list of id's in csv file */ public: - /** @brief create ZipExport */ ZipExport(); - /** @brief create ZipExport class and immediately start export */ - ZipExport(Pokedex* pokedex, std::string input_csv, std::string output_zip); virtual ~ZipExport(); /** @brief set pokedex reference */ virtual void set_pokedex(Pokedex* pokedex); /** @brief set csv_path and parse csv file */ - virtual void import_csv(std::string filename); + virtual std::vector<std::string> import_csv(std::string filename); /** @brief generate output export zip file */ - virtual void export_zip(std::string filename); + virtual void export_zip(std::string filename, std::vector<PokemonCard*> cards); + + std::vector<std::string> id_list; /** @brief list of id's in csv file */ }; diff --git a/oop2eindopdr/main.cpp b/oop2eindopdr/main.cpp index 9a8c352..278d62f 100644 --- a/oop2eindopdr/main.cpp +++ b/oop2eindopdr/main.cpp @@ -38,15 +38,44 @@ int interactive_mode() { } int export_mode(int argc, char** argv) { + cout << "entering export mode..." << endl; std::string csv_file = argv[1]; std::string zip_file = argc == 3 ? argv[2] : ""; - while (zip_file.size() == 0) { // make sure target file is given + + ZipExport zip_export; + zip_export.set_pokedex(g_pokedex); + std::vector<std::string> card_ids = zip_export.import_csv(csv_file); + std::vector<PokemonCard*> cards; + for (std::string card_id : card_ids) { + PokemonCard* card = g_pokedex->get_card_by_id(card_id); + if (card == nullptr) { + cout << "could not find card with id " << card_id << endl; + continue; + } + card->fetch_market_value(); + cards.push_back(card); + } + + double total_value = 0.f; + for (PokemonCard* card : cards) { + cout << card->short_identifier() << " currently sells for " << card->value << endl; + total_value += card->value; + } + cout << "sum value of cards: " << total_value << endl; + + if (zip_file.size() == 0) { + cout << "export as zip? [Y/n]: "; + std::string export_yn; + if (!std::getline(cin, export_yn)) return EXIT_SUCCESS; // exit if EOF (ctrl-d) + if (export_yn.size() > 0 && (export_yn[0] | (1 << 5)) != 'y') + return EXIT_SUCCESS; // exit if input is not 'Y', 'y', or <enter> + cout << "zip file location?: "; - if (!std::getline(cin, zip_file)) break; + if (!std::getline(cin, zip_file)) return EXIT_FAILURE; } - cout << "exporting cards in " << csv_file << " to " << zip_file << endl; - ZipExport zip_export(g_pokedex, csv_file, zip_file); + cout << "exporting as zip..." << endl; + zip_export.export_zip(zip_file, cards); return EXIT_SUCCESS; } |