Skip to content

File NPFunction.cxx

File List > core > NPFunction.cxx

Go to the documentation of this file

#include "NPFunction.h"
#include "NPTerminalColor.h"
#include <cmath>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
namespace nptool {
  static std::map<int, std::string> itoa_map;

  class itoa_proxy {
   public:
    itoa_proxy() {
      char buffer[12] = "10000000000";
      for (int i = 0; i < 1000; i++) {
        snprintf(buffer, 12, "%d", i);
        itoa_map[i] = buffer;
      }
    }
  };
  static itoa_proxy itoa_p;
} // namespace nptool

std::string nptool::itoa(const int& i) {
  auto it = nptool::itoa_map.find(i);
  if (it != nptool::itoa_map.end())
    return it->second;
  else {
    // char* buffer = new char((int)(log10(abs(i)) + 1));
    // sprintf(buffer, "%d", i);
    std::stringstream buffer;
    buffer << i;
    nptool::itoa_map[i] = buffer.str();
    // delete buffer;
  }
  return nptool::itoa_map[i];
}

void nptool::message(std::string color, std::string plugin, std::string tclass, std::string mess, bool prog) {
  if (color == "red") {
    std::cout << cli_red;
  }
  else if (color == "green") {

    std::cout << cli_green;
  }
  else if (color == "yellow") {

    std::cout << cli_yellow;
  }

  else if (color == "blue") {

    std::cout << cli_blue;
  }

  else if (color == "purple") {

    std::cout << cli_purple;
  }

  else if (color == "cian") {

    std::cout << cli_cian;
  }

  std::cout << "//////// ";
  if (plugin.length()) {
    std::cout << "Plugin: " << plugin << "\t | ";
  }
  if (tclass.length()) {
    if (!prog)
      std::cout << "Class : " << tclass << "\t | ";
    else
      std::cout << "Program : " << tclass << "\t | ";
  }
  std::cout << mess << cli_restore_color << std::endl;
}