<commit_before>#include <stdlib.h>
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <Validator.h>
#include <Calculator.h>
#include <Representer.h>



int main (int argc, char * argv [])
{
    using Pslrk::Core::Validator;
    using Pslrk::Core::Calculator;
    using Pslrk::Core::Representer;


    std::vector <std::string> codes;

    auto actionHelp = []() {
        std::cout << "[Command]         [Parameters]    [Description]" << std::endl;
        std::cout << "help                              Print this message." << std::endl;
        std::cout << "push code         <string>        Put specified code into local storage." << std::endl;
        std::cout << "push family       <string>        Put family of specified code into local storage." << std::endl;
        std::cout << "show codes                        Print local storage." << std::endl;
        std::cout << "show msl                          Print MSL for each of codes from local storage." << std::endl;
        std::cout << "show family                       Print family for each of codes from local storage." << std::endl;
        std::cout << "show id                           Print ID for each of codes from local storage." << std::endl;
        std::cout << "to string                         Convert each of codes from local storage into string format." << std::endl;
        std::cout << "to hex                            Convert each of codes from local storage into hex format." << std::endl;
        std::cout << "erase code        <int>           Erase code with specified index from local storage." << std::endl;
        std::cout << "clear codes                       Clear local storage." << std::endl;
        std::cout << "quit                              Exit the program." << std::endl;
    };

    auto actionPushCode = [&](const std::string & code) {
        codes.push_back (code);
    };

    auto actionPushFamily = [&](const std::string & code) {
        for (const auto & variant : Representer::GenerateCodeFamily (code) ) {
            codes.push_back (variant);
        }
    };

    auto actionShowCodes = [&]() {
        for (const auto & code : codes) {
            std::cout << code << std::endl;
        }
    };

    auto actionShowMsl = [&]() {
        for (const auto & code : codes) {
            std::cout << Calculator::CalculateMsl (code) << std::endl;
        }
    };

    auto actionShowFamily = [&]() {
        for (const auto & code : codes) {
            for (const auto & variant : Representer::GenerateCodeFamily (code) ) {
                std::cout << code << " - " << variant << std::endl;
            }
        }
    };

    auto actionShowId = [&]() {
        for (const auto & code : codes) {
            std::cout << code << " - " << Representer::DetectCodeId (code) << std::endl;
        }
    };

    auto actionToString = [&]() {
        for (auto & code : codes) {
            code = Representer::HexViewToStringView (code);
        }
    };

    auto actionToHex = [&]() {
        for (auto & code : codes) {
            code = Representer::StringViewToHexView (code);
        }
    };

    auto actionEraseCode = [&](const int position) {
        if (position < codes.size () ) {
            codes.erase (codes.begin () + position);
        }
    };

    auto actionClearCodes = [&]() {
        codes.clear ();
    };

    while (true) {
        std::cout << "> ";
        {
            std::string command {""};
            std::getline (std::cin, command);

            if      (0 == command.find ("help") ) {
                actionHelp ();
            }
            else if (0 == command.find ("push code ") ) {
                actionPushCode (command.substr (std::string ("push code ").length () ) );
            }
            else if (0 == command.find ("push family ") ) {
                actionPushFamily (command.substr (std::string ("push family ").length () ) );
            }
            else if (0 == command.find ("show codes") ) {
                actionShowCodes ();
            }
            else if (0 == command.find ("show msl") ) {
                actionShowMsl ();
            }
            else if (0 == command.find ("show family") ) {
                actionShowFamily ();
            }
            else if (0 == command.find ("show id") ) {
                actionShowId ();
            }
            else if (0 == command.find ("to string") ) {
                actionToString ();
            }
            else if (0 == command.find ("to hex") ) {
                actionToHex ();
            }
            else if (0 == command.find ("quit") ) {
                return EXIT_SUCCESS;
            }
            else if (0 == command.find ("erase code ") ) {
                actionEraseCode (boost::lexical_cast <int> (command.substr (std::string ("erase code ").length () ) ) );
            }
            else if (0 == command.find ("clear codes") ) {
                actionClearCodes ();
            }
            else {
                std::cout << "Unrecognized command: " << command << std::endl;
            }
        }
    }


    return EXIT_SUCCESS;
}<commit_msg>Added CLI UI for basic XML operations.<commit_after>#include <stdlib.h>
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <Validator.h>
#include <Calculator.h>
#include <Representer.h>
#include <XmlManager.h>



// Some kind of RAII helper.
struct Finally
{
    Finally  (const std::function <void ()> action) : finalAction (action) {};
    ~Finally ()                                      {finalAction ();};
    const std::function <void ()>                     finalAction;
};



int main (int argc, char * argv [])
{
    using Pslrk::Core::Exception;
    using Pslrk::Core::Validator;
    using Pslrk::Core::Calculator;
    using Pslrk::Core::Representer;
    using Pslrk::Core::XmlManager;


    std::vector <std::string> codes;
    XmlManager * xmlManager {nullptr};
    Finally xmlManagerFinally ([&](){delete xmlManager;});


    auto actionHelp = []() {
        std::cout << "[Command]         [Parameters]    [Description]" << std::endl;
        std::cout << "help                              Print this message." << std::endl;
        std::cout << "push code         <string>        Put specified code into local storage." << std::endl;
        std::cout << "push family       <string>        Put family of specified code into local storage." << std::endl;
        std::cout << "show codes                        Print local storage." << std::endl;
        std::cout << "show msl                          Print MSL for each of codes from local storage." << std::endl;
        std::cout << "show family                       Print family for each of codes from local storage." << std::endl;
        std::cout << "show id                           Print ID for each of codes from local storage." << std::endl;
        std::cout << "to string                         Convert each of codes from local storage into string format." << std::endl;
        std::cout << "to hex                            Convert each of codes from local storage into hex format." << std::endl;
        std::cout << "erase code        <int>           Erase code with specified index from local storage." << std::endl;
        std::cout << "clear codes                       Clear local storage." << std::endl;
        std::cout << "open base         <string>        Open XML file (whith codes)." << std::endl;
        std::cout << "close base                        Close XML file." << std::endl;
        std::cout << "save base                         Save XML file." << std::endl;
        std::cout << "show base                         Print codes which contained in XML file." << std::endl;
        std::cout << "add base                          Add code to XML file." << std::endl;
        std::cout << "remove base       <string>        Remove code from XML file." << std::endl;
        std::cout << "search base       <string>        Search code in XML file." << std::endl;
        std::cout << "quit                              Exit the program." << std::endl;
    };

    auto actionPushCode = [&](const std::string & code) {
        codes.push_back (code);
    };

    auto actionPushFamily = [&](const std::string & code) {
        for (const auto & variant : Representer::GenerateCodeFamily (code) ) {
            codes.push_back (variant);
        }
    };

    auto actionShowCodes = [&]() {
        for (const auto & code : codes) {
            std::cout << code << std::endl;
        }
    };

    auto actionShowMsl = [&]() {
        for (const auto & code : codes) {
            std::cout << Calculator::CalculateMsl (code) << std::endl;
        }
    };

    auto actionShowFamily = [&]() {
        for (const auto & code : codes) {
            for (const auto & variant : Representer::GenerateCodeFamily (code) ) {
                std::cout << code << " - " << variant << std::endl;
            }
        }
    };

    auto actionShowId = [&]() {
        for (const auto & code : codes) {
            std::cout << code << " - " << Representer::DetectCodeId (code) << std::endl;
        }
    };

    auto actionToString = [&]() {
        for (auto & code : codes) {
            code = Representer::HexViewToStringView (code);
        }
    };

    auto actionToHex = [&]() {
        for (auto & code : codes) {
            code = Representer::StringViewToHexView (code);
        }
    };

    auto actionEraseCode = [&](const int position) {
        if (position < codes.size () ) {
            codes.erase (codes.begin () + position);
        }
    };

    auto actionClearCodes = [&]() {
        codes.clear ();
    };

    auto actionOpenBase = [&](const std::string & fileName) {
        try {
            xmlManager = new XmlManager (fileName);
        }
        catch (Exception & e) {
            std::cout << e.what () << std::endl;
        }
    };

    auto actionCloseBase = [&]() {
        delete xmlManager;
        xmlManager = nullptr;
    };

    auto actionSaveBase = [&]() {
        if (xmlManager) {
            try {
                xmlManager->Save ();
            }
            catch (Exception & e) {
                std::cout << e.what () << std::endl;
            }
        }
        else {
            std::cout << "Attempting to save file which was not opened." << std::endl;
        }
    };

    auto actionShowBase = [&]() {
        if (xmlManager) {
            const pugi::xml_node codes = xmlManager->Select ("/").node ();
            for (const auto & code : codes.child ("codes").children ("code") ) {
                std::cout << " Code: id       = " << code.attribute ("id").value     () << std::endl <<
                             "       length   = " << code.attribute ("length").value () << std::endl <<
                             "       max PSL  = " << code.attribute ("maxpsl").value () << std::endl <<
                             "       sequence = " << code.child_value ("sequence")      << std::endl;
                std::cout << std::endl;
            }
        }
        else {
            std::cout << "Attempting to print file which was not opened." << std::endl;
        }
    };

    auto actionAddBase = [&]() {
        if (xmlManager) {
            std::string input {""};
            std::cout << "Input code: > ";
            std::getline (std::cin, input);
            std::string stringView;
            if      (Validator::ValidateStringView (input) == Pslrk::Core::viewIsValid) {
                stringView = input;
            }
            else if (Validator::ValidateHexView    (input) == Pslrk::Core::viewIsValid) {
                stringView = Representer::HexViewToStringView (input);
            }
            else {
                std::cout << "Invalid code, stop adding!" << std::endl;
                return;
            }

            const std::string codeId {Representer::DetectCodeId (stringView)};
            const int codeLength = stringView.length ();
            const int maxPsl {Calculator::CalculateMsl (stringView)};

            std::cout << "Sequence: " << stringView << std::endl;
            std::cout << "ID:       " << codeId     << std::endl;
            std::cout << "Length:   " << codeLength << std::endl;
            std::cout << "Max PSL:  " << maxPsl     << std::endl;

            std::cout << "Input reference article: > ";
            std::getline (std::cin, input);
            const std::string referenceArticle  {input};
            std::cout << "Input reference author: > ";
            std::getline (std::cin, input);
            const std::string referenceAuthor {input};
            std::cout << "Input reference link: > ";
            std::getline (std::cin, input);
            const std::string referenceLink   {input};

            xmlManager->Insert (codeId, codeLength, maxPsl, stringView, { {referenceAuthor, referenceArticle, referenceLink} });
        }
        else {
            std::cout << "Attempting to process file which was not opened." << std::endl;
        }
    };

    auto actionRemoveBase = [&](const std::string & id) {
        if (xmlManager) {
            try {
                xmlManager->Remove (id);
            }
            catch (Exception & e) {
                std::cout << e.what () << std::endl;
            }
        }
        else {
            std::cout << "Attempting to process file which was not opened." << std::endl;
        }
    };

    auto actionSearchBase = [&](const std::string & xPathQuery) {
        if (xmlManager) {
            try {
                pugi::xml_document result;
                result.append_child () = xmlManager->Select (xPathQuery).node ();
                result.print (std::cout);
            }
            catch (Exception & e) {
                std::cout << e.what () << std::endl;
            }
        }
        else {
            std::cout << "Attempting to process file which was not opened." << std::endl;
        }
    };


    while (true) {
        std::cout << "> ";
        {
            std::string command {""};
            std::getline (std::cin, command);

            if      (0 == command.find ("help") ) {
                actionHelp ();
            }
            else if (0 == command.find ("push code ") ) {
                actionPushCode (command.substr (std::string ("push code ").length () ) );
            }
            else if (0 == command.find ("push family ") ) {
                actionPushFamily (command.substr (std::string ("push family ").length () ) );
            }
            else if (0 == command.find ("show codes") ) {
                actionShowCodes ();
            }
            else if (0 == command.find ("show msl") ) {
                actionShowMsl ();
            }
            else if (0 == command.find ("show family") ) {
                actionShowFamily ();
            }
            else if (0 == command.find ("show id") ) {
                actionShowId ();
            }
            else if (0 == command.find ("to string") ) {
                actionToString ();
            }
            else if (0 == command.find ("to hex") ) {
                actionToHex ();
            }
            else if (0 == command.find ("quit") ) {
                return EXIT_SUCCESS;
            }
            else if (0 == command.find ("erase code ") ) {
                actionEraseCode (boost::lexical_cast <int> (command.substr (std::string ("erase code ").length () ) ) );
            }
            else if (0 == command.find ("clear codes") ) {
                actionClearCodes ();
            }
            else if (0 == command.find ("open base") ) {
                actionOpenBase (command.substr (std::string ("open base ").length () ) );
            }
            else if (0 == command.find ("close base") ) {
                actionCloseBase ();
            }
            else if (0 == command.find ("save base") ) {
                actionSaveBase ();
            }
            else if (0 == command.find ("show base") ) {
                actionShowBase ();
            }
            else if (0 == command.find ("add base") ) {
                actionAddBase ();
            }
            else if (0 == command.find ("remove base") ) {
                actionRemoveBase (command.substr (std::string ("remove base ").length () ) );
            }
            else if (0 == command.find ("search base") ) {
                actionSearchBase (command.substr (std::string ("search base ").length () ) );
            }
            else {
                std::cout << "Unrecognized command: " << command << std::endl;
            }
        }
    }


    return EXIT_SUCCESS;
}
{'fraction_non_alphanumeric': 0.14350018005041412, 'fraction_numerical': 0.0018605209458648422, 'mean_word_length': 1.5447464874770922, 'pattern_counts': {'":': 0, '<': 327, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 18}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1065387', 'n_tokens_mistral': 4506, 'n_tokens_neox': 4352, 'n_words': 1200}