From cfddf414644b16cffa32c045944a8fb2f99084ac Mon Sep 17 00:00:00 2001 From: Phitherek Date: Tue, 19 Mar 2013 10:11:49 +0100 Subject: [PATCH] added more functionality to ConfigFile --- ConfigFile.cpp | 11 ++++++++ ConfigFile.h | 3 +++ tests/ConfigFileTest.cpp | 56 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/ConfigFile.cpp b/ConfigFile.cpp index 77c4ccf..91b4ed8 100644 --- a/ConfigFile.cpp +++ b/ConfigFile.cpp @@ -3,6 +3,13 @@ #include using namespace mmm; +ConfigFile::ConfigFile() { +_path = ""; +_localpath = ""; +_modlist = ""; +_repoinfo = ""; +} + ConfigFile::ConfigFile(std::string path) { std::ifstream conf(path.c_str()); if(!conf) { @@ -95,6 +102,10 @@ std::string& ConfigFile::getRepoInfo() { return _repoinfo; } +void ConfigFile::setPath(std::string path) { + _path = path; +} + void ConfigFile::write() { std::ofstream conf(_path.c_str()); if(!conf) { diff --git a/ConfigFile.h b/ConfigFile.h index 3ae4fe1..35c2fc2 100644 --- a/ConfigFile.h +++ b/ConfigFile.h @@ -20,6 +20,7 @@ private: std::string _modlist; std::string _repoinfo; public: + ConfigFile(); ///< A default constructor of an empty object ConfigFile(std::string path); ///< \brief A constructor from file path. ///< Tries to open the config file and parse it. It throws FileException or ParseException. /// \param path Path to the config file. @@ -29,6 +30,8 @@ public: ///< \return A reference to local modlist file path. std::string& getRepoInfo(); ///< \brief A function returning a reference to local repoinfo file path. ///< \return A reference to local repoinfo file path + void setPath(std::string path); ///< \brief A function setting the path of config file. + ///< \param path A path to config file. void write(); ///< A function that writes the changes to config file. }; } diff --git a/tests/ConfigFileTest.cpp b/tests/ConfigFileTest.cpp index 446b17a..dae09ed 100644 --- a/tests/ConfigFileTest.cpp +++ b/tests/ConfigFileTest.cpp @@ -28,10 +28,58 @@ conf.write(); cout << "All OK! Thank you for testing!" << endl; return EXIT_SUCCESS; } catch(mmm::FileException& exc) { -cout << "File exception occured: " << exc.what() << endl; -return 1; +cerr << "File exception occured: " << exc.what() << endl; +cout << "Failed to open file, creating new..." << endl; +mmm::ConfigFile conf; +conf.setPath(path); +string localpath, modlist, repoinfo; +cout << "Enter new localpath: " << endl; +cin >> localpath; +cout << "modlist: " << endl; +cin >> modlist; +cout << "repoinfo: " << endl; +cin >> repoinfo; +conf.getLocalPath() = localpath; +conf.getModList() = modlist; +conf.getRepoInfo() = repoinfo; +cout << "Generating the config file..." << endl; +try { +conf.write(); +cout << "All OK! Thank you for testing!" << endl; +return EXIT_SUCCESS; +} catch(mmm::FileException& exc) { + cerr << "File exception occured: " << exc.what() << endl; + return 1; } catch(mmm::ParseException& exc) { -cout << "Parse exception occured: " << exc.what() << endl; -return 2; + cerr << "Parse exception occured: " << exc.what() << endl; + return 2; +} +} catch(mmm::ParseException& exc) { +cerr << "Parse exception occured: " << exc.what() << endl; +cout << "Failed to parse file, creating new..." << endl; +mmm::ConfigFile conf; +conf.setPath(path); +string localpath, modlist, repoinfo; +cout << "Enter new localpath: " << endl; +cin >> localpath; +cout << "modlist: " << endl; +cin >> modlist; +cout << "repoinfo: " << endl; +cin >> repoinfo; +conf.getLocalPath() = localpath; +conf.getModList() = modlist; +conf.getRepoInfo() = repoinfo; +cout << "Generating the config file..." << endl; +try { +conf.write(); +cout << "All OK! Thank you for testing!" << endl; +return EXIT_SUCCESS; +} catch(mmm::FileException& exc) { + cerr << "File exception occured: " << exc.what() << endl; + return 1; +} catch(mmm::ParseException& exc) { + cerr << "Parse exception occured: " << exc.what() << endl; + return 2; +} } }