<commit_before>/*
 * libngf - Non-graphical feedback library
 *
 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

#include "list_p.h"
#include "proplist.h"

#define VALUE_TYPE_STRING "string"
#define VALUE_TYPE_INTEGER "integer"
#define VALUE_TYPE_UNSIGNED "unsigned"
#define VALUE_TYPE_BOOLEAN "boolean"

#define MAX_KEY_LENGTH 32
#define MAX_VALUE_LENGTH 512

typedef struct _PropEntry PropEntry;

struct _PropEntry
{
    LIST_INIT (PropEntry)

    char *key;
    void *value;
    NgfProplistType type;
};

struct _NgfProplist
{
    PropEntry *entries;
    size_t num_entries;
};

NgfProplist*
ngf_proplist_new ()
{
    NgfProplist *proplist = NULL;

    proplist = (NgfProplist*) malloc (sizeof (NgfProplist));
    if (proplist == NULL)
        return NULL;

    memset (proplist, 0, sizeof (NgfProplist));
    return proplist;
}

static void
_free_item (PropEntry *entry, void *userdata)
{
    (void) userdata;

    if (!entry)
        return;

    if (entry->key)
        free (entry->key);
    if (entry->value)
        free (entry->value);
    free (entry);
}

void ngf_proplist_free (NgfProplist *proplist)
{
    if (proplist == NULL)
        return;

    LIST_FOREACH (proplist->entries, _free_item, NULL);
    free (proplist);
}

void
ngf_proplist_sets (NgfProplist *proplist,
                   const char *key,
                   const char *value)
{
    PropEntry *item = NULL;

    if (proplist == NULL || key == NULL || value == NULL)
        return;

    item = (PropEntry*) malloc (sizeof (PropEntry));
    if (item == NULL)
        return;

    item->key   = strndup (key, (size_t) MAX_KEY_LENGTH);
    item->value = strndup (value, (size_t) MAX_VALUE_LENGTH);
    item->type  = NGF_PROPLIST_VALUE_TYPE_STRING;
    item->next  = NULL;

    LIST_APPEND (proplist->entries, item);
}

const char*
ngf_proplist_gets (NgfProplist *proplist,
                   const char *key)
{
    PropEntry *iter = NULL;

    if (proplist == NULL || key == NULL)
        return NULL;

    for (iter = proplist->entries; iter; iter = iter->next) {
        if (strncmp (iter->key, key, (size_t) MAX_KEY_LENGTH) == 0)
            return (const char*) iter->value;
    }

    return NULL;
}

static char*
_string_from_integer (int value)
{
    char   *result      = NULL;
    size_t  result_size = 0;

    char value_buf[MAX_VALUE_LENGTH + 1];
    memset (&value_buf, 0, sizeof (char) * (MAX_VALUE_LENGTH + 1));

    snprintf (value_buf, MAX_VALUE_LENGTH, "%d", value);
    result_size = strlen (value_buf);

    if ((result = (char*) malloc (sizeof (char) * (result_size + 1))) == NULL)
        return NULL;

    strncpy (result, value_buf, result_size);
    result[result_size] = '\0';
    return result;
}

void
ngf_proplist_set_as_integer (NgfProplist *proplist,
                             const char *key,
                             int32_t value)
{
    PropEntry *item = NULL;
    int32_t *data;

    if (proplist == NULL || key == NULL)
        return;

    item = (PropEntry*) malloc (sizeof (PropEntry));
    if (item == NULL)
        return;

    item->key   = strndup (key, (size_t) MAX_KEY_LENGTH);
    data = malloc (sizeof(int32_t));
    *data = value;
    item->value = data;
    item->type  = NGF_PROPLIST_VALUE_TYPE_INTEGER;
    item->next  = NULL;

    LIST_APPEND (proplist->entries, item);
}

static char*
_string_from_unsigned (uint32_t value)
{
    char   *result      = NULL;
    size_t  result_size = 0;

    char value_buf[MAX_VALUE_LENGTH + 1];
    memset (&value_buf, 0, sizeof (char) * (MAX_VALUE_LENGTH + 1));

    snprintf (value_buf, MAX_VALUE_LENGTH, "%u", value);
    result_size = strlen (value_buf);

    if ((result = (char*) malloc (sizeof (char) * (result_size + 1))) == NULL)
        return NULL;

    strncpy (result, value_buf, result_size);
    result[result_size] = '\0';
    return result;
}

void
ngf_proplist_set_as_unsigned (NgfProplist *proplist,
                              const char *key,
                              uint32_t value)
{
    PropEntry *item = NULL;
    uint32_t *data;

    if (proplist == NULL || key == NULL)
        return;

    item = (PropEntry*) malloc (sizeof (PropEntry));
    if (item == NULL)
        return;<commit_msg>Remove unused functions from proplist.<commit_after>/*
 * libngf - Non-graphical feedback library
 *
 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

#include "list_p.h"
#include "proplist.h"

#define VALUE_TYPE_STRING "string"
#define VALUE_TYPE_INTEGER "integer"
#define VALUE_TYPE_UNSIGNED "unsigned"
#define VALUE_TYPE_BOOLEAN "boolean"

#define MAX_KEY_LENGTH 32
#define MAX_VALUE_LENGTH 512

typedef struct _PropEntry PropEntry;

struct _PropEntry
{
    LIST_INIT (PropEntry)

    char *key;
    void *value;
    NgfProplistType type;
};

struct _NgfProplist
{
    PropEntry *entries;
    size_t num_entries;
};

NgfProplist*
ngf_proplist_new ()
{
    NgfProplist *proplist = NULL;

    proplist = (NgfProplist*) malloc (sizeof (NgfProplist));
    if (proplist == NULL)
        return NULL;

    memset (proplist, 0, sizeof (NgfProplist));
    return proplist;
}

static void
_free_item (PropEntry *entry, void *userdata)
{
    (void) userdata;

    if (!entry)
        return;

    if (entry->key)
        free (entry->key);
    if (entry->value)
        free (entry->value);
    free (entry);
}

void ngf_proplist_free (NgfProplist *proplist)
{
    if (proplist == NULL)
        return;

    LIST_FOREACH (proplist->entries, _free_item, NULL);
    free (proplist);
}

void
ngf_proplist_sets (NgfProplist *proplist,
                   const char *key,
                   const char *value)
{
    PropEntry *item = NULL;

    if (proplist == NULL || key == NULL || value == NULL)
        return;

    item = (PropEntry*) malloc (sizeof (PropEntry));
    if (item == NULL)
        return;

    item->key   = strndup (key, (size_t) MAX_KEY_LENGTH);
    item->value = strndup (value, (size_t) MAX_VALUE_LENGTH);
    item->type  = NGF_PROPLIST_VALUE_TYPE_STRING;
    item->next  = NULL;

    LIST_APPEND (proplist->entries, item);
}

const char*
ngf_proplist_gets (NgfProplist *proplist,
                   const char *key)
{
    PropEntry *iter = NULL;

    if (proplist == NULL || key == NULL)
        return NULL;

    for (iter = proplist->entries; iter; iter = iter->next) {
        if (strncmp (iter->key, key, (size_t) MAX_KEY_LENGTH) == 0)
            return (const char*) iter->value;
    }

    return NULL;
}

void
ngf_proplist_set_as_integer (NgfProplist *proplist,
                             const char *key,
                             int32_t value)
{
    PropEntry *item = NULL;
    int32_t *data;

    if (proplist == NULL || key == NULL)
        return;

    item = (PropEntry*) malloc (sizeof (PropEntry));
    if (item == NULL)
        return;

    item->key   = strndup (key, (size_t) MAX_KEY_LENGTH);
    data = malloc (sizeof(int32_t));
    *data = value;
    item->value = data;
    item->type  = NGF_PROPLIST_VALUE_TYPE_INTEGER;
    item->next  = NULL;

    LIST_APPEND (proplist->entries, item);
}

void
ngf_proplist_set_as_unsigned (NgfProplist *proplist,
                              const char *key,
                              uint32_t value)
{
    PropEntry *item = NULL;
    uint32_t *data;

    if (proplist == NULL || key == NULL)
        return;

    item = (PropEntry*) malloc (sizeof (PropEntry));
    if (item == NULL)
        return;
{'fraction_non_alphanumeric': 0.10848692197329213, 'fraction_numerical': 0.00904977375565611, 'mean_word_length': 2.7948073701842544, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 49, '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': '3552993', 'n_tokens_mistral': 3141, 'n_tokens_neox': 2893, 'n_words': 895}