<commit_before><commit_msg>Include dependencies, remove excess debugging.<commit_after>//
// mysociety_config.cpp:
// Read the terrible mySociety PHP format config files, as similar code for
// other languages.
//
// Copyright (c) 2010 UK Citizens Online Democracy. All rights reserved.
// Email: <EMAIL>; WWW: http://www.mysociety.org/
//
// $Id: mysociety_error.h,v 1.4 2009-09-24 22:00:29 francis Exp $
//

#include "error.h"

#include <iostream>
#include <fstream>

void mysociety_read_conf(const std::string& mysociety_conf_file) {
    setenv("MYSOCIETY_CONFIG_FILE_PATH", mysociety_conf_file.c_str(), 1);

    std::string tmp_php_script = (boost::format("/tmp/c-php-mysociety-conf-%d") % getpid()).str();
    {
        std::ofstream out(tmp_php_script.c_str(), std::ios::out);
        out << 
            "#!/usr/bin/php\n"
            "<?php\n"
            "$b = get_defined_constants();\n"
            "require(getenv(\"MYSOCIETY_CONFIG_FILE_PATH\"));\n"
            "$a = array_diff_assoc(get_defined_constants(), $b);\n"
            "foreach ($a as $k => $v) {\n"
            "    print \"$k=$v\\n\";\n"
            "}\n"
            "?>\n";
    }
    chmod(tmp_php_script.c_str(), 0555);

    FILE * f = popen(tmp_php_script.c_str(), "r");
    if (f == 0) {
        throw Exception("Couldn't open temporary mySociety conf PHP script");
    }
    const int BUFSIZE = 2048;
    char buf[BUFSIZE];
    while(fgets(buf, BUFSIZE,  f)) {
        std::string line = std::string(buf);
        std::string::size_type found = line.find_first_of("=");
        if (found == std::string::npos) {
            throw Exception("config output had line without = in it");
        }
        std::string key = line.substr(0, found);
        std::string value = line.substr(found + 1, line.size() - found - 2);
        debug_log(boost::format("mySociety config loaded: %s %s") % key % value);

        setenv(key.c_str(), value.c_str(), 1);
    }

    pclose(f); 
}



{'fraction_non_alphanumeric': 0.17058222676200205, 'fraction_numerical': 0.017364657814096015, 'mean_word_length': 2.661682242990654, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '5597599', 'n_tokens_mistral': 717, 'n_tokens_neox': 677, 'n_words': 145}