<commit_before>/*
 * See GPL.txt for copyright information.
 *
 * Author: <NAME>
 *
 */
#include <vector>
#include <sstream>
#include <forward_list>
#include <memory>
#include <time.h>
#include "console.h"
#include "cldevice.h"
#include "data.h"
#include "analytic.h"
#include "plugins/plugins.h"



bool Console::_lock {false};



/// @brief Prepares console interface.
///
/// Initializes internal variables, locks console interface, and sets terminal
/// header.
///
/// @pre Console must not be locked.
Console::Console(int argc, char* argv[], Terminal& terminal, DataMap& datamap):
   _alive {false},
   _tm {terminal},
   _dataMap {datamap},
   _device {nullptr}
{
   assert<InvalidUse>(!_lock,__FILE__,__LINE__);
   _lock = true;
   _tm.header("KINC:> ");
}



/// Deletes OpenCL device if one is set and unsets lock to console.
Console::~Console()
{
   _lock = false;
   if (_device)
   {
      delete _device;
   }
}



/// Prints welcome message and goes directly to terminal loop.
void Console::run()
{
   _tm << "Welcome to KINC!\nVersion 0.0001 :)\n\n";
   terminal_loop();
}



/// @brief Main program terminal loop.
///
/// This is the main function loop of the program. This loop will continue to
/// grab one input from the Terminal interface until the quit command has been
/// processed. Each user line is processed by calling parse. The _alive state
/// of the program is set to false in a subfunction if quit is given.
void Console::terminal_loop()
{
   bool alive = true;
   while (alive)
   {
      string line;
      _tm >> line;
      _tm << "\n";
      if (!line.empty())
      {
         GetOpts ops(line);
         try
         {
            process(ops);
         }
         catch (CommandError e)
         {
            _tm << e.cmd << ": " << e.msg << Terminal::endl;
         }
         catch (CommandQuit)
         {
            alive = false;
         }
         /*catch (DataException e)
         {
            std::ostringstream buffer;
            buffer << "Data Exception Caught!" << std::endl;
            buffer << "Level: ";
            switch (e.level())
            {
            case DataException::Level::general:
               buffer << "General" << std::endl;
               break;
            case DataException::Level::caution:
               buffer << "Caution" << std::endl;
               break;
            case DataException::Level::warning:
               buffer << "Warning" << std::endl;
               break;
            case DataException::Level::severe:
               buffer << "Severe" << std::endl;
               break;
            case DataException::Level::fatal:
               buffer << "Fatal" << std::endl;
               break;
            }
            buffer << "Location: " << e.file() << ":" << e.line() << std::endl;
            buffer << "What: " << e.what();
            throw CommandError("data",buffer.str());
         }
         catch (AnalyticException e)
         {
            ;
         }
         catch (Exception e)
         {
            ;
         }
         catch (std::exception stde)
         {
            ;
         }
         catch (...)
         {
            ;
         }*/
      }
      _tm << Terminal::flush;
   }
}



/// Processes decoded user command and routes to specific command.
///
/// @param comm Enumerated command to be processed.
/// @param list List of user arguments for command.
void Console::process(GetOpts& ops)
{
   enum {Analytic=0,GPU,Open,Load,Select,Dump,Query,Close,List,Clear,History,
         Quit};
   switch (ops.com_get({"gpu","open","load","select","dump","query","close",
                        "list","clear","history","quit"}))
   {
   case GPU:
      ops.com_pop();
      gpu_process(ops);
      break;
   case Open:
      ops.com_pop();
      data_open(ops);
      break;
   case Close:
      ops.com_pop();
      data_close(ops);
      break;<commit_msg>Added to conolse class.<commit_after>/*
 * See GPL.txt for copyright information.
 *
 * Author: <NAME>urns
 *
 */
#include <vector>
#include <sstream>
#include <forward_list>
#include <memory>
#include <time.h>
#include "console.h"
#include "cldevice.h"
#include "data.h"
#include "analytic.h"
#include "plugins/plugins.h"



bool Console::_lock {false};



/// @brief Prepares console interface.
///
/// Initializes internal variables, locks console interface, and sets terminal
/// header.
///
/// @pre Console must not be locked.
Console::Console(int argc, char* argv[], Terminal& terminal, DataMap& datamap):
   _alive {false},
   _tm {terminal},
   _dataMap {datamap},
   _device {nullptr}
{
   assert<InvalidUse>(!_lock,__FILE__,__LINE__);
   _lock = true;
   _tm.header("KINC:> ");
}



/// Deletes OpenCL device if one is set and unsets lock to console.
Console::~Console()
{
   _lock = false;
   if (_device)
   {
      delete _device;
   }
}



/// Prints welcome message and goes directly to terminal loop.
void Console::run()
{
   _tm << "Welcome to KINC.\nAlpha Version 0.1.\n\n";
   terminal_loop();
}



/// @brief Main program terminal loop.
///
/// This is the main function loop of the program. This loop will continue to
/// grab one input from the Terminal interface until the quit command has been
/// processed. Each user line is processed by calling parse. The _alive state
/// of the program is set to false in a subfunction if quit is given.
void Console::terminal_loop()
{
   bool alive = true;
   while (alive)
   {
      string line;
      _tm >> line;
      _tm << "\n";
      if (!line.empty())
      {
         GetOpts ops(line);
         try
         {
            process(ops);
         }
         catch (CommandError e)
         {
            _tm << e.cmd << ": " << e.msg << Terminal::endl;
         }
         catch (CommandQuit)
         {
            alive = false;
         }
         catch (DataException e)
         {
            _tm << "Data Exception Caught!" << Terminal::endl;
            _tm << "Level: ";
            switch (e.level())
            {
            case DataException::Level::general:
               _tm << "General" << Terminal::endl;
               break;
            case DataException::Level::caution:
               _tm << "Caution" << Terminal::endl;
               break;
            case DataException::Level::warning:
               _tm << "Warning" << Terminal::endl;
               break;
            case DataException::Level::severe:
               _tm << "Severe" << Terminal::endl;
               break;
            case DataException::Level::fatal:
               _tm << "Fatal" << Terminal::endl;
               break;
            }
            _tm << "Location: " << e.file() << ":" << e.line()
                << Terminal::endl;
            _tm << "What: " << e.what() << Terminal::endl;
         }
         catch (AnalyticException e)
         {
            _tm << "Analytic Exception Caught!" << Terminal::endl;
            _tm << "Location: " << e.file() << ":" << e.line()
                << Terminal::endl;
            _tm << "What: " << e.what() << Terminal::endl;
         }
         catch (Exception e)
         {
            _tm << "GENERAL Exception Caught!" << Terminal::endl;
            _tm << "Location: " << e.file() << ":" << e.line()
                << Terminal::endl;
            _tm << "What: " << e.what() << Terminal::endl;
            _tm << "It is HIGHLY recommended you immediately close this"
                   " program." << Terminal::endl;
         }
         catch (std::exception stde)
         {
            _tm << "Standard Library Exception Caught!" << Terminal::endl;
            _tm << "What: " << stde.what() << Terminal::endl;
            _tm << "It is HIGHLY recommended you immediately close this"
                   " program." << Terminal::endl;
         }
         catch (...)
         {
            _tm << "UNKNOWN EXCEPTION CAUGHT!" << Terminal::endl;
            _tm << "It is HIGHLY recommended you immediately close this"
                   " program." << Terminal::endl;
         }
      }
      _tm << Terminal::flush;
   }
}



/// Processes decoded user command and routes to specific command.
///
/// @param comm Enumerated command to be processed.
/// @param list List of user arguments for command.
void Console::process(GetOpts& ops)
{
   enum {Analytic=0,GPU,Open,Load,Select,Dump,Query,Close,List,Clear,History,
         Quit};
   switch (ops.com_get({"gpu","open","load","select","dump","query","close",
                        "list","clear","history","quit"}))
   {
   case GPU:
      ops.com_pop();
      gpu_process(ops);
      break;
   case Open:
      ops.com_pop();
      data_open(ops);
      break;
   case Close:
      ops.com_pop();
      data_close(ops);
      break;
{'fraction_non_alphanumeric': 0.143398392652124, 'fraction_numerical': 0.0010332950631458094, 'mean_word_length': 1.8711272247857613, 'pattern_counts': {'":': 6, '<': 193, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6134321', 'n_tokens_mistral': 2586, 'n_tokens_neox': 2495, 'n_words': 695}