<commit_before>// standard includes
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

// public disir interface
#include <disir/disir.h>
#include <disir/context.h>

// private disir includes
#include "log.h"
#include "mqueue.h"
#include "config.h"
#include "keyval.h"
#include "default.h"
#include "context_private.h"
#include "util_private.h"
#include "collection.h"


//! INTERNAL API
enum disir_status
dx_default_begin (dc_t *parent, dc_t **def)
{
    enum disir_status status;
    dc_t *context;

    status = CONTEXT_NULL_INVALID_TYPE_CHECK (parent);
    if (status != DISIR_STATUS_OK)
    {
        // already logged
        return status;
    }
    if (def == NULL)
    {
        log_debug ("invoked with NULL def context pointer.");
        return DISIR_STATUS_INVALID_ARGUMENT;
    }

    // A default can only be added to a DISIR_CONTEXT_KEYVAL
    status = CONTEXT_TYPE_CHECK (parent, DISIR_CONTEXT_KEYVAL);
    if (status != DISIR_STATUS_OK)
    {
        // Already logged
        return status;
    }
    if (dc_type (parent->cx_root_context) != DISIR_CONTEXT_SCHEMA)
    {
        dx_log_context (parent, "Cannot add default to a KEYVAL whose root is not a SCHEMA");
        return DISIR_STATUS_WRONG_CONTEXT;
    }

    if (dx_value_type_sanify (parent->cx_keyval->kv_type) == DISIR_VALUE_TYPE_UNKNOWN)
    {
        dx_log_context (parent,
                        "Cannot add a default entry to a keyval \
                        who has not yet defined a value type.");
        return DISIR_STATUS_INVALID_CONTEXT; // XXX: Revise error return code
    }

    context = dx_context_create (DISIR_CONTEXT_DEFAULT);
    if (context == NULL)
    {
        log_debug_context (parent, "failed to allocate new default context");
        return DISIR_STATUS_NO_MEMORY;
    }
    log_debug_context (parent, "created context: %p", context);

    context->cx_default = dx_default_create (context);
    if (context->cx_default == NULL)
    {
        dx_context_destroy (&context);
        dx_log_context (parent, "cannot allocate new default instance");
        return DISIR_STATUS_NO_MEMORY;
    }
    log_debug_context (parent, "allocated new default instance: %p", context->cx_default);

    // Inherrit keyval's value type to the newly allocated default's value type.
    context->cx_default->de_value.dv_type = parent->cx_keyval->kv_type;

    dx_context_attach (parent, context);
    *def = context;

    return DISIR_STATUS_OK;
}

//! INTERNAL API
enum disir_status
dx_default_finalize (dc_t **default_context)
{
    enum disir_status status;
    struct disir_default *def;
    struct disir_default **queue;
    int exists;
    char buffer[32];

    status = CONTEXT_DOUBLE_NULL_INVALID_TYPE_CHECK (default_context);
    if (status != DISIR_STATUS_OK)
    {
        // Already logged
        return status;
    }
    def = (*default_context)->cx_default;
    queue = &(*default_context)->cx_parent_context->cx_keyval->kv_default_queue;

    // TODO: Verify that the default respect the restrictions on the parent keyval

    exists = MQ_SIZE_COND (*queue,
            (dx_semantic_version_compare (&entry->de_introduced, &def->de_introduced) == 0));
    if (exists)
    {
        dx_log_context (*default_context,
                        "already contains a default entry with semantic version: %s",
                        dc_semantic_version_string (buffer, 32, &def->de_introduced));
        status = DISIR_STATUS_CONFLICTING_SEMVER;
    }
    else
    {
        MQ_ENQUEUE_CONDITIONAL (*queue, def,
            (dx_semantic_version_compare (&entry->de_introduced, &def->de_introduced) > 0));
        status = DISIR_STATUS_OK;
    }

    return status;
}

//! INTERNAL API
struct disir_default *
dx_default_create (dc_t *context)
{
    struct disir_default *def;

    def = calloc ( 1, sizeof (struct disir_default));
    if (def == NULL)
    {
        return NULL;
    }

    def->de_context = context;

    return def;
}
<commit_msg>fix: default - initialize to version 1.0.0<commit_after>// standard includes
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

// public disir interface
#include <disir/disir.h>
#include <disir/context.h>

// private disir includes
#include "log.h"
#include "mqueue.h"
#include "config.h"
#include "keyval.h"
#include "default.h"
#include "context_private.h"
#include "util_private.h"
#include "collection.h"


//! INTERNAL API
enum disir_status
dx_default_begin (dc_t *parent, dc_t **def)
{
    enum disir_status status;
    dc_t *context;

    status = CONTEXT_NULL_INVALID_TYPE_CHECK (parent);
    if (status != DISIR_STATUS_OK)
    {
        // already logged
        return status;
    }
    if (def == NULL)
    {
        log_debug ("invoked with NULL def context pointer.");
        return DISIR_STATUS_INVALID_ARGUMENT;
    }

    // A default can only be added to a DISIR_CONTEXT_KEYVAL
    status = CONTEXT_TYPE_CHECK (parent, DISIR_CONTEXT_KEYVAL);
    if (status != DISIR_STATUS_OK)
    {
        // Already logged
        return status;
    }
    if (dc_type (parent->cx_root_context) != DISIR_CONTEXT_SCHEMA)
    {
        dx_log_context (parent, "Cannot add default to a KEYVAL whose root is not a SCHEMA");
        return DISIR_STATUS_WRONG_CONTEXT;
    }

    if (dx_value_type_sanify (parent->cx_keyval->kv_type) == DISIR_VALUE_TYPE_UNKNOWN)
    {
        dx_log_context (parent,
                        "Cannot add a default entry to a keyval \
                        who has not yet defined a value type.");
        return DISIR_STATUS_INVALID_CONTEXT; // XXX: Revise error return code
    }

    context = dx_context_create (DISIR_CONTEXT_DEFAULT);
    if (context == NULL)
    {
        log_debug_context (parent, "failed to allocate new default context");
        return DISIR_STATUS_NO_MEMORY;
    }
    log_debug_context (parent, "created context: %p", context);

    context->cx_default = dx_default_create (context);
    if (context->cx_default == NULL)
    {
        dx_context_destroy (&context);
        dx_log_context (parent, "cannot allocate new default instance");
        return DISIR_STATUS_NO_MEMORY;
    }
    log_debug_context (parent, "allocated new default instance: %p", context->cx_default);

    // Inherrit keyval's value type to the newly allocated default's value type.
    context->cx_default->de_value.dv_type = parent->cx_keyval->kv_type;

    dx_context_attach (parent, context);
    *def = context;

    return DISIR_STATUS_OK;
}

//! INTERNAL API
enum disir_status
dx_default_finalize (dc_t **default_context)
{
    enum disir_status status;
    struct disir_default *def;
    struct disir_default **queue;
    int exists;
    char buffer[32];

    status = CONTEXT_DOUBLE_NULL_INVALID_TYPE_CHECK (default_context);
    if (status != DISIR_STATUS_OK)
    {
        // Already logged
        return status;
    }
    def = (*default_context)->cx_default;
    queue = &(*default_context)->cx_parent_context->cx_keyval->kv_default_queue;

    // TODO: Verify that the default respect the restrictions on the parent keyval

    exists = MQ_SIZE_COND (*queue,
            (dx_semantic_version_compare (&entry->de_introduced, &def->de_introduced) == 0));
    if (exists)
    {
        dx_log_context (*default_context,
                        "already contains a default entry with semantic version: %s",
                        dc_semantic_version_string (buffer, 32, &def->de_introduced));
        status = DISIR_STATUS_CONFLICTING_SEMVER;
    }
    else
    {
        MQ_ENQUEUE_CONDITIONAL (*queue, def,
            (dx_semantic_version_compare (&entry->de_introduced, &def->de_introduced) > 0));
        status = DISIR_STATUS_OK;
    }

    return status;
}

//! INTERNAL API
struct disir_default *
dx_default_create (dc_t *context)
{
    struct disir_default *def;

    def = calloc ( 1, sizeof (struct disir_default));
    if (def == NULL)
    {
        return NULL;
    }

    def->de_context = context;
    def->de_introduced.sv_major = 1;

    return def;
}

{'fraction_non_alphanumeric': 0.10005008765339343, 'fraction_numerical': 0.002253944402704733, 'mean_word_length': 2.8979990239141045, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2187139', 'n_tokens_mistral': 2600, 'n_tokens_neox': 2520, 'n_words': 645}