<commit_before>/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Monkey HTTP Daemon
 *  ------------------
 *  Copyright (C) 2001-2010, <NAME>. <<EMAIL>>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <dirent.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

#include "monkey.h"
#include "config.h"
#include "str.h"
#include "utils.h"
#include "mimetype.h"
#include "info.h"
#include "logfile.h"
#include "memory.h"
#include "plugin.h"

struct mk_config *mk_config_create(char *path)
{
    FILE *f;
    int len;
    char buf[255];
    char *key = 0, *val = 0, *last = 0;
    struct mk_config *cnf = 0, *new, *p;

    if ((f = fopen(path, "r")) == NULL) {
        fprintf(stderr, "\nConfig Error: I can't open %s file\n\n", path);
        exit(1);
    }

    /* looking for configuration directives */
    while (fgets(buf, 255, f)) {
        len = strlen(buf);
        if (buf[len - 1] == '\n') {
            buf[--len] = 0;
            if (len && buf[len - 1] == '\r')
                buf[--len] = 0;
        }

        if (!buf[0] || buf[0] == '#')
            continue;

        key = strtok_r(buf, "\"\t ", &last);
        val = strtok_r(NULL, "\n", &last);

        if (!key || !val) {
            continue;
        }

        /* Allow new entry found */
        new = mk_mem_malloc(sizeof(struct mk_config));
        new->key = mk_string_dup(key);
        new->val = mk_string_dup(val);
        new->next = NULL;

        /* Link to main list */
        if (!cnf) {
            cnf = new;
        }
        else {
            p = cnf;
            while (p->next) {
                p = p->next;
            }
            p->next = new;
        }
    }
<commit_msg>Fix config reader, do not read empty spaces<commit_after>/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Monkey HTTP Daemon
 *  ------------------
 *  Copyright (C) 2001-2010, <NAME>. <<EMAIL>>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <dirent.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

#include "monkey.h"
#include "config.h"
#include "str.h"
#include "utils.h"
#include "mimetype.h"
#include "info.h"
#include "logfile.h"
#include "memory.h"
#include "plugin.h"

struct mk_config *mk_config_create(char *path)
{
    FILE *f;
    int len;
    char buf[255];
    char *key = 0, *val = 0, *last = 0;
    struct mk_config *cnf = 0, *new, *p;

    if ((f = fopen(path, "r")) == NULL) {
        fprintf(stderr, "\nConfig Error: I can't open %s file\n\n", path);
        exit(1);
    }

    /* looking for configuration directives */
    while (fgets(buf, 255, f)) {
        len = strlen(buf);
        if (buf[len - 1] == '\n') {
            buf[--len] = 0;
            if (len && buf[len - 1] == '\r')
                buf[--len] = 0;
        }

        if (!buf[0] || buf[0] == '#')
            continue;

        key = strtok_r(buf, "\"\t ", &last);
        val = strtok_r(NULL, "\"\t ", &last);

        if (!key || !val) {
            continue;
        }

        /* Allow new entry found */
        new = mk_mem_malloc(sizeof(struct mk_config));
        new->key = mk_string_dup(key);

        new->val = mk_string_dup(val);
        new->next = NULL;

        /* Link to main list */
        if (!cnf) {
            cnf = new;
        }
        else {
            p = cnf;
            while (p->next) {
                p = p->next;
            }
            p->next = new;
        }
    }

{'fraction_non_alphanumeric': 0.15071956437183975, 'fraction_numerical': 0.015169194865810968, 'mean_word_length': 2.1150817686250756, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 41, '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': '5644234', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1681, 'n_words': 559}