#include "accountManager.h" AccountManager::AccountManager(std::string pathToFil, std::string pathToAccountNumberFile, PAGE page) : pathToAccountNumberFile(pathToAccountNumberFile + "_" + page.name_id), pageInUse(page) { std::string pathToAccountsFile = pathToFil + "_" + page.name_id; std::ifstream ifs(pathToAccountsFile); if(!ifs.is_open()) { if(writeDefault(pathToAccountsFile) != 0) exit(12); ifs.open(pathToAccountsFile); if(!ifs.is_open()) { std::cout << " => Error: Konnte Account File nicht öffnen" << std::endl; exit(13); } } std::string line; while (std::getline(ifs, line)) { if(line.length() > 0 && line[0] == '#') continue; Account account; size_t Delimeter = line.find("/"); if(Delimeter == std::string::npos) { account.Email = line; account.Password = line; } else { account.Email = std::string(line).erase(Delimeter, line.length() - Delimeter); account.Password = line.erase(0, Delimeter + 1); } if(account.Email == "" || account.Password == "") continue; else accounts.push_back(account); } ifs.close(); } Account AccountManager::getNextAccount() { if(accounts.size() == 0) { std::cout << " => Error: Keine Accounts vorhanden." << std::endl; exit(36); } size_t accountNumber = getLastAccountNumber(); ++accountNumber; if( accountNumber >= accounts.size() ) accountNumber=0; if(setLastAccountNumber(accountNumber) != 0) exit(45); return accounts.at(accountNumber); } int AccountManager::writeDefault(std::string path) { std::ofstream ofs(path); if(!ofs.is_open()) { perror((std::string(" => Error: Konnte Account Datei nicht öffnen: ") + path).c_str()); return -1; } std::cout << " => Erstelle Datei mit Accounts unter: " << path << "..." < WARNUNG: Keine standart Accounts für die Url '" << pageInUse.url << "' vorhanden." < Error: Konnte Account Number Datei nicht erstellen"); exit(34); } } std::string content( (std::istreambuf_iterator(fStream) ), (std::istreambuf_iterator() ) ); return static_cast( atoi(content.c_str()) ); } int AccountManager::setLastAccountNumber(size_t number) { std::ofstream ofs; ofs.open(pathToAccountNumberFile, std::ios::trunc); if(!ofs.is_open()) { std::cout << " => Error: Account Number Datei ist nicht geöffnet." << std::endl; return 110; } //fStream.clear(); ofs << number << std::endl; return 0; }