<commit_before>/*ckwg +5
 * Copyright 2011-2012 by Kitware, Inc. All Rights Reserved. Please refer to
 * KITWARE_LICENSE.TXT for licensing information, or contact General Counsel,
 * Kitware, Inc., 28 Corporate Drive, Clifton Park, NY 12065.
 */

#include "providers.h"

#include "pipe_bakery_exception.h"

#include <vistk/utilities/path.h>

#include <vistk/pipeline/config.h>
#include <vistk/pipeline/utils.h>

#include <boost/filesystem/operations.hpp>
#include <boost/system/error_code.hpp>
#include <boost/thread/thread.hpp>
#include <boost/lexical_cast.hpp>

/**
 * \file providers.cxx
 *
 * \brief Implementation of configuration providers.
 */

namespace vistk
{

provider
::provider()
{
}

provider
::~provider()
{
}

config_provider
::config_provider(config_t const conf)
  : m_config(conf)
{
}

config_provider
::~config_provider()
{
}

config::value_t
config_provider
::operator () (config::value_t const& index) const
{
  return m_config->get_value<config::value_t>(index);
}

system_provider
::system_provider()
{
}

system_provider
::~system_provider()
{
}

config::value_t
system_provider
::operator () (config::value_t const& index) const
{
  config::value_t value;

  if (index == "processors")
  {
    value = boost::lexical_cast<config::value_t>(boost::thread::hardware_concurrency());
  }
  else if (index == "homedir")
  {
    envvar_value_t const home = get_envvar(
#if defined(_WIN32) || defined(_WIN64)
      "UserProfile"
#else
      "HOME"
#endif
    );

    if (home)
    {
      value = config::value_t(*home);
    }
  }
  else if (index == "curdir")
  {
    boost::system::error_code ec;
    path_t const curdir = boost::filesystem::current_path(ec);

    /// \todo Check ec.

    path_t::string_type const& raw_path = curdir.native();

    value = config::value_t(raw_path.begin(), raw_path.end());
  }
  else
  {
    throw unrecognized_system_index_exception(index);
  }

  return value;
}

environment_provider
::environment_provider()
{
}

environment_provider
::~environment_provider()
{
}

config::value_t
environment_provider<commit_msg>Add 'pid' value to the SYS provider<commit_after>/*ckwg +5
 * Copyright 2011-2012 by Kitware, Inc. All Rights Reserved. Please refer to
 * KITWARE_LICENSE.TXT for licensing information, or contact General Counsel,
 * Kitware, Inc., 28 Corporate Drive, Clifton Park, NY 12065.
 */

#include "providers.h"

#include "pipe_bakery_exception.h"

#include <vistk/utilities/path.h>

#include <vistk/pipeline/config.h>
#include <vistk/pipeline/utils.h>

#include <boost/filesystem/operations.hpp>
#include <boost/system/error_code.hpp>
#include <boost/thread/thread.hpp>
#include <boost/lexical_cast.hpp>

#if defined(_WIN32) || defined(_WIN64)
#include <process.h>
#else
#include <sys/types.h>
#include <unistd.h>
#endif

/**
 * \file providers.cxx
 *
 * \brief Implementation of configuration providers.
 */

namespace vistk
{

provider
::provider()
{
}

provider
::~provider()
{
}

config_provider
::config_provider(config_t const conf)
  : m_config(conf)
{
}

config_provider
::~config_provider()
{
}

config::value_t
config_provider
::operator () (config::value_t const& index) const
{
  return m_config->get_value<config::value_t>(index);
}

system_provider
::system_provider()
{
}

system_provider
::~system_provider()
{
}

config::value_t
system_provider
::operator () (config::value_t const& index) const
{
  config::value_t value;

  if (index == "processors")
  {
    value = boost::lexical_cast<config::value_t>(boost::thread::hardware_concurrency());
  }
  else if (index == "homedir")
  {
    envvar_value_t const home = get_envvar(
#if defined(_WIN32) || defined(_WIN64)
      "UserProfile"
#else
      "HOME"
#endif
    );

    if (home)
    {
      value = config::value_t(*home);
    }
  }
  else if (index == "curdir")
  {
    boost::system::error_code ec;
    path_t const curdir = boost::filesystem::current_path(ec);

    /// \todo Check ec.

    path_t::string_type const& raw_path = curdir.native();

    value = config::value_t(raw_path.begin(), raw_path.end());
  }
  else if (index == "pid")
  {
#if defined(_WIN32) || defined(_WIN64)
    int const pid = _getpid();
#else
    pid_t const pid = getpid();
#endif

    value = boost::lexical_cast<config::value_t>(pid);
  }
  else
  {
    throw unrecognized_system_index_exception(index);
  }

  return value;
}

environment_provider
::environment_provider()
{
}

environment_provider
::~environment_provider()
{
}

config::value_t
environment_provider
{'fraction_non_alphanumeric': 0.14406590959697171, 'fraction_numerical': 0.01068804275217101, 'mean_word_length': 4.498164014687882, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, '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': '7304683', 'n_tokens_mistral': 1626, 'n_tokens_neox': 1572, 'n_words': 325}