<commit_before>/*$Id$
 *
 * This source file is a part of the Fresco Project.
 * Copyright (C) 2000 <NAME> <<EMAIL>> 
 * http://www.fresco.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
 * MA 02139, USA.
 */
#include <Berlin/RCManager.hh>
#include <Berlin/Logger.hh>
#include <Prague/Sys/User.hh>
#include <fstream>
#include <stdexcept>
#include <map>

#ifdef RC_SYSCONFDIR
const std::string sysconfdir = RC_SYSCONFDIR;
#else
const std::string sysconfdir = "";
#endif

// ----------------------------------------------------------------------
// Internals
// ----------------------------------------------------------------------
static std::map<std::string, Prague::Path> my_paths;

namespace RCManager_internals
{
  void read_file(const std::string &file)
  {
      std::ifstream ifs(file.c_str());
      if (!ifs) { throw std::runtime_error("RCManager: file not found.");}
      while (ifs)
      {
          std::string name;
          ifs >> name;
          ifs.ignore(1024, '=');
          std::string path;
          ifs >> path;
          my_paths.insert(std::make_pair(name, path));
      }
  }

  void add_to_path(const std::string &name, const std::string &value)
  {
      Prague::Path &path = my_paths[name];
      path.append(value);
  }
}

// ----------------------------------------------------------------------
// Public functions
// ----------------------------------------------------------------------
Prague::Path Berlin::RCManager::get_path(const std::string &name)
{
    // NOTE: default (empty) path is constructed if not present in
    //       my_paths yet
    return my_paths[name];
}

// Find our resource file:
void Berlin::RCManager::setup(Prague::GetOpt const &getopt)
{
    std::string value;
    if (getopt.get("resource", &value))
    {
        // Load resourcefile given on the commandline:
        try
        {
            value = Prague::Path::expand_user(value);
            RCManager_internals::read_file(value);
            Logger::log(Logger::loader) << "Resourcefile \""
                                        << value << "\" read."
                                        << std::endl;
        }
        catch (const std::runtime_error &e)
        {
            std::cerr << "ERROR: Resourcefile \"" << value
                      << "\" given on the commandline failed to load: "
                      << e.what() << std::endl;
            exit (1);
        }
    }
    else
    {
        // Search for berlinrc in default places:
        value = sysconfdir + "/berlinrc";
    
        bool is_configured = 0;
        try
        {
            RCManager_internals::read_file(value);
            is_configured = 1;
            Logger::log(Logger::loader) << "Resourcefile \""
                                        << value << "\" read."
                                        << std::endl;
        }
        catch (const std::runtime_error &e)
        {
            std::cerr << "Warning: Default resourcefile \"" << value
                      << "\" failed to load: "
                      << e.what() << std::endl;
        }
    
        // FIXME: merge additional configuration:
        if(getenv("BERLINRC")) value = std::string(getenv("BERLINRC"));
        else value ="";

        if (value.empty())
            value = std::string(Prague::User().home()) + "/.berlin";

        try
        {
            if (!is_configured) 
            {
                RCManager_internals::read_file(value);
                Logger::log(Logger::loader) << "Resourcefile \""
                                            << value << "\" read."
                                            << std::endl;
            }
        }
        catch (const std::runtime_error &e)
        {
            std::cerr << "ERROR: System resourcefile is missing and "
                      << "user's resourcefile \"" << value
                      << "\" failed to load too: "
                      << e.what() << std::endl;
            exit(1);
        }
    }
}<commit_msg>Patch from <NAME> (cow):  * use anonymous namespace instead of RCManager_internals  * fix typo  * remove duplicate public declaration<commit_after>/*$Id$
 *
 * This source file is a part of the Fresco Project.
 * Copyright (C) 2000 <NAME> <<EMAIL>> 
 * http://www.fresco.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
 * MA 02139, USA.
 */
#include <Berlin/RCManager.hh>
#include <Berlin/Logger.hh>
#include <Prague/Sys/User.hh>
#include <fstream>
#include <stdexcept>
#include <map>

#ifdef RC_SYSCONFDIR
const std::string sysconfdir = RC_SYSCONFDIR;
#else
const std::string sysconfdir = "";
#endif

// ----------------------------------------------------------------------
// Internals
// ----------------------------------------------------------------------
static std::map<std::string, Prague::Path> my_paths;

namespace
{
  void read_file(const std::string &file)
  {
      std::ifstream ifs(file.c_str());
      if (!ifs) { throw std::runtime_error("RCManager: file not found.");}
      while (ifs)
      {
          std::string name;
          ifs >> name;
          ifs.ignore(1024, '=');
          std::string path;
          ifs >> path;
          my_paths.insert(std::make_pair(name, path));
      }
  }

  void add_to_path(const std::string &name, const std::string &value)
  {
      Prague::Path &path = my_paths[name];
      path.append(value);
  }
}

// ----------------------------------------------------------------------
// Public functions
// ----------------------------------------------------------------------
Prague::Path Berlin::RCManager::get_path(const std::string &name)
{
    // NOTE: default (empty) path is constructed if not present in
    //       my_paths yet
    return my_paths[name];
}

// Find our resource file:
void Berlin::RCManager::setup(Prague::GetOpt const &getopt)
{
    std::string value;
    if (getopt.get("resource", &value))
    {
        // Load resourcefile given on the commandline:
        try
        {
            value = Prague::Path::expand_user(value);
            read_file(value);
            Logger::log(Logger::loader) << "Resourcefile \""
                                        << value << "\" read."
                                        << std::endl;
        }
        catch (const std::runtime_error &e)
        {
            std::cerr << "ERROR: Resourcefile \"" << value
                      << "\" given on the commandline failed to load: "
                      << e.what() << std::endl;
            exit (1);
        }
    }
    else
    {
        // Search for berlinrc in default places:
        value = sysconfdir + "/berlinrc";
    
        bool is_configured = 0;
        try
        {
            read_file(value);
            is_configured = 1;
            Logger::log(Logger::loader) << "Resourcefile \""
                                        << value << "\" read."
                                        << std::endl;
        }
        catch (const std::runtime_error &e)
        {
            std::cerr << "Warning: Default resourcefile \"" << value
                      << "\" failed to load: "
                      << e.what() << std::endl;
        }
    
        // FIXME: merge additional configuration:
        if(getenv("BERLINRC")) value = std::string(getenv("BERLINRC"));
        else value ="";

        if (value.empty())
            value = std::string(Prague::User().home()) + "/.berlin";

        try
        {
            if (!is_configured) 
            {
                read_file(value);
                Logger::log(Logger::loader) << "Resourcefile \""
                                            << value << "\" read."
                                            << std::endl;
            }
        }
        catch (const std::runtime_error &e)
        {
            std::cerr << "ERROR: System resourcefile is missing and "
                      << "user's resourcefile \"" << value
                      << "\" failed to load too: "
                      << e.what() << std::endl;
            exit(1);
        }
    }
}
{'fraction_non_alphanumeric': 0.17797243884200406, 'fraction_numerical': 0.004486700138874052, 'mean_word_length': 1.5200538358008076, 'pattern_counts': {'":': 0, '<': 136, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5966632', 'n_tokens_mistral': 2466, 'n_tokens_neox': 2399, 'n_words': 704}