Implemented basic XML support
This commit is contained in:
parent
2d06b3b8fe
commit
370177599c
|
@ -6,8 +6,12 @@ set(CMAKE_CXX_STANDARD 20)
|
|||
add_executable(${CMAKE_PROJECT_NAME}
|
||||
src/main.cpp
|
||||
src/AtmAdmin.cpp
|
||||
src/XmlHandling.cpp
|
||||
libs/XmlHandling.h)
|
||||
src/XMLStorage.cpp
|
||||
)
|
||||
|
||||
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/src/data
|
||||
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}
|
||||
)
|
||||
|
||||
find_package(pugixml CONFIG REQUIRED)
|
||||
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE libs)
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
namespace AtmAdmin {
|
||||
// startup() is inline because of how small it is.
|
||||
char startup();
|
||||
void handleInvalidArguments(int argc);
|
||||
bool handleUserInformation(char choice, std::string& information);
|
||||
std::string changeAccountPin();
|
||||
std::string changeAccountName();
|
||||
|
|
39
libs/XMLStorage.h
Normal file
39
libs/XMLStorage.h
Normal file
|
@ -0,0 +1,39 @@
|
|||
//
|
||||
// Created by skygrow on 5/14/25.
|
||||
//
|
||||
|
||||
#ifndef XMLSTORAGE_H
|
||||
#define XMLSTORAGE_H
|
||||
|
||||
#include "pugixml.hpp"
|
||||
|
||||
namespace AtmAdmin {
|
||||
class XMLStorage {
|
||||
public:
|
||||
explicit XMLStorage (const std::string& search) : m_search(search) {
|
||||
initXMLDoc();
|
||||
|
||||
pugi::xml_node accountData {(getXMLUserData(search))};
|
||||
|
||||
this->name ={accountData.child_value("name") };
|
||||
this->pin ={std::stoi(accountData.child_value("pin")) };
|
||||
}
|
||||
|
||||
void initXMLDoc ();
|
||||
pugi::xml_node getXMLUserData (const std::string& toSearch);
|
||||
|
||||
std::string name{};
|
||||
int pin{};
|
||||
|
||||
// ~XMLStorage() {
|
||||
// updateXMLData(m_search);
|
||||
// }
|
||||
|
||||
private:
|
||||
// void updateXMLData (const std::string& toSearch); // Is currently unused.
|
||||
pugi::xml_document m_doc;
|
||||
const std::string m_search;
|
||||
};
|
||||
}
|
||||
|
||||
#endif //XMLSTORAGE_H
|
|
@ -1,8 +0,0 @@
|
|||
//
|
||||
// Created by theskywinds on 11-06-25.
|
||||
//
|
||||
|
||||
#ifndef XMLHANDLING_H
|
||||
#define XMLHANDLING_H
|
||||
|
||||
#endif //XMLHANDLING_H
|
|
@ -14,6 +14,21 @@ namespace AtmAdmin {
|
|||
MONEY,
|
||||
};
|
||||
|
||||
void handleInvalidArguments(int argc) {
|
||||
if (argc != 2) {
|
||||
std::cerr << "Invalid number of arguments: " << argc - 1 << '\n';
|
||||
|
||||
if (argc < 2) {
|
||||
std::cerr << "The ability to make new accounts is not implemented." << '\n';
|
||||
std::cerr << "Please put in a pre-existing name." << std::endl;
|
||||
}
|
||||
else
|
||||
std::cerr << "Too many arguments." << std::endl;
|
||||
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
// Allows the user to double-check their inputted information.
|
||||
// It also saves and applies the information if the user accepts it.
|
||||
void confirmUserInformation (std::string& information, const std::string& tempInformation, const Choice choice) {
|
||||
|
|
34
src/XMLStorage.cpp
Normal file
34
src/XMLStorage.cpp
Normal file
|
@ -0,0 +1,34 @@
|
|||
#include <iomanip>
|
||||
#include <iostream>
|
||||
|
||||
#include "../libs/XMLStorage.h"
|
||||
#include "pugixml.hpp"
|
||||
|
||||
namespace AtmAdmin {
|
||||
void XMLStorage::initXMLDoc () {
|
||||
pugi::xml_parse_result res = m_doc.load_file("./data/users.xml");
|
||||
|
||||
if (!res) {
|
||||
std::cerr << "Error occurred: " << res.description();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
// getXMLData retrieves the data required by XMLStorage.
|
||||
pugi::xml_node XMLStorage::getXMLUserData (const std::string& toSearch) {
|
||||
pugi::xml_node data = m_doc.first_child().child("accounts").find_child_by_attribute("account", "owner", toSearch.c_str());
|
||||
|
||||
if (data.empty()) {
|
||||
std::cerr << "Account not found, exiting.";
|
||||
std::cout << data.name();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
// void XMLStorage::updateXMLData (const std::string& toSearch) {
|
||||
// // TODO: FILL WITH CODE THAT UPDATES DATABASE
|
||||
// }
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
//
|
||||
// Created by theskywinds on 11-06-25.
|
||||
//
|
||||
|
||||
#include "../libs/XmlHandling.h"
|
||||
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
#include <iostream>
|
||||
#include <cctype>
|
||||
#include "AtmAdmin.h"
|
||||
#include "XMLStorage.h"
|
||||
|
||||
char startup() {
|
||||
char choice{};
|
||||
|
@ -12,7 +13,10 @@ char startup() {
|
|||
return choice;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main(int argc, [[maybe_unused]] char** argv) {
|
||||
AtmAdmin::handleInvalidArguments(argc);
|
||||
|
||||
AtmAdmin::XMLStorage mainStorage{argv[1]};
|
||||
bool continueLoop{true};
|
||||
|
||||
while (continueLoop) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user