<commit_before>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <zlib.h>

#include <lo/lo.h>

#include "mapper_internal.h"
#include "types_internal.h"
#include <mapper/mapper.h>

mapper_receiver mapper_receiver_new(mapper_device device, const char *host,
                                    int port, const char *name, int default_scope)
{
    char str[16];
    mapper_receiver r = (mapper_receiver) calloc(1, sizeof(struct _mapper_link));
    r->props.src_host = strdup(host);
    r->props.src_port = port;
    sprintf(str, "%d", port);
    r->remote_addr = lo_address_new(host, str);
    r->props.src_name = strdup(name);
    r->props.src_name_hash = crc32(0L, (const Bytef *)name, strlen(name));
    r->props.dest_name = strdup(mdev_name(device));
    if (default_scope) {
        r->props.num_scopes = 1;
        r->props.scope_names = (char **) malloc(sizeof(char *));
        r->props.scope_names[0] = strdup(name);
        r->props.scope_hashes = (uint32_t *) malloc(sizeof(uint32_t));
        r->props.scope_hashes[0] = crc32(0L, (const Bytef *)name, strlen(name));;
    }
    else {
        r->props.num_scopes = 0;
    }
    r->props.extra = table_new();
    r->device = device;
    r->signals = 0;
    r->n_connections = 0;

    if (!r->remote_addr) {
        mapper_receiver_free(r);
        return 0;
    }
    return r;
}

void mapper_receiver_free(mapper_receiver r)
{
    int i;

    if (r) {
        if (r->props.src_name)
            free(r->props.src_name);
        if (r->props.src_host)
            free(r->props.src_host);
        if (r->remote_addr)
            lo_address_free(r->remote_addr);
        if (r->props.dest_name)
            free(r->props.dest_name);
        while (r->signals && r->signals->connections) {
            // receiver_signal is freed with last child connection
            mapper_receiver_remove_connection(r, r->signals->connections);
        }
        for (i=0; i<r->props.num_scopes; i++) {
            free(r->props.scope_names[i]);
        }
        free(r->props.scope_names);
        free(r->props.scope_hashes);
        if (r->props.extra)
            table_free(r->props.extra, 1);
        free(r);
    }
}

/*! Set a router's properties based on message parameters. */
void mapper_receiver_set_from_message(mapper_receiver rc,
                                      mapper_message_t *msg)
{
    /* Extra properties. */
    mapper_msg_add_or_update_extra_params(rc->props.extra, msg);
}

static void message_add_coerced_signal_value(lo_message m,
                                             mapper_db_connection c,
                                             mapper_signal_instance si)
{
    int i, min_length = c->src_length < c->dest_length ?
                        c->src_length : c->dest_length;

    if (c->dest_type == 'f') {
        float *v = (float *) si->value;
        for (i = 0; i < min_length; i++) {
            if (!si->has_value && !(si->has_value_flags[i/8] & 1 << (i % 8)))
                lo_message_add_nil(m);
            else if (c->src_type == 'f')
                lo_message_add_float(m, v[i]);
            else if (c->src_type == 'i')
                lo_message_add_int32(m, (int)v[i]);
            else if (c->src_type == 'd')
                lo_message_add_double(m, (double)v[i]);
        }
    }
    else if (c->dest_type == 'i') {
        int *v = (int *) si->value;
        for (i = 0; i < min_length; i++) {
            if (!si->has_value && !(si->has_value_flags[i/8] & 1 << (i % 8)))
                lo_message_add_nil(m);
            else if (c->src_type == 'i')
                lo_message_add_int32(m, v[i]);
            else if (c->src_type == 'f')
                lo_message_add_float(m, (float)v[i]);
            else if (c->src_type == 'd')
                lo_message_add_double(m, (double)v[i]);
        }
    }
    else if (c->dest_type == 'd') {
        double *v = (double *) si->value;
        for (i = 0; i < min_length; i++) {
            if (!si->has_value && !(si->has_value_flags[i/8] & 1 << (i % 8)))
                lo_message_add_nil(m);
            else if (c->src_type == 'd')
                lo_message_add_double(m, (int)v[i]);
            else if (c->src_type == 'i')
                lo_message_add_int32(m, (int)v[i]);
            else if (c->src_type == 'f')
                lo_message_add_float(m, (float)v[i]);
        }
    }
    for (i = min_length; i < c->src_length; i++)
        lo_message_add_nil(m);
}

void mapper_receiver_send_update(mapper_receiver r,
                                 mapper_signal sig,
                                 int instance_index,
                                 mapper_timetag_t tt)
{
    // TODO: check vector has_values flags, include defined elements even if !has_value?

    int count=0;
    mapper_id_map map = sig->id_maps[instance_index].map;

    int in_scope = mapper_receiver_in_scope(r, map->origin);

    // find the signal connection
    mapper_receiver_signal rc = r->signals;
    while (rc) {
        if (rc->signal == sig)<commit_msg>Do not check link scope with NULL map.<commit_after>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <zlib.h>

#include <lo/lo.h>

#include "mapper_internal.h"
#include "types_internal.h"
#include <mapper/mapper.h>

mapper_receiver mapper_receiver_new(mapper_device device, const char *host,
                                    int port, const char *name, int default_scope)
{
    char str[16];
    mapper_receiver r = (mapper_receiver) calloc(1, sizeof(struct _mapper_link));
    r->props.src_host = strdup(host);
    r->props.src_port = port;
    sprintf(str, "%d", port);
    r->remote_addr = lo_address_new(host, str);
    r->props.src_name = strdup(name);
    r->props.src_name_hash = crc32(0L, (const Bytef *)name, strlen(name));
    r->props.dest_name = strdup(mdev_name(device));
    if (default_scope) {
        r->props.num_scopes = 1;
        r->props.scope_names = (char **) malloc(sizeof(char *));
        r->props.scope_names[0] = strdup(name);
        r->props.scope_hashes = (uint32_t *) malloc(sizeof(uint32_t));
        r->props.scope_hashes[0] = crc32(0L, (const Bytef *)name, strlen(name));;
    }
    else {
        r->props.num_scopes = 0;
    }
    r->props.extra = table_new();
    r->device = device;
    r->signals = 0;
    r->n_connections = 0;

    if (!r->remote_addr) {
        mapper_receiver_free(r);
        return 0;
    }
    return r;
}

void mapper_receiver_free(mapper_receiver r)
{
    int i;

    if (r) {
        if (r->props.src_name)
            free(r->props.src_name);
        if (r->props.src_host)
            free(r->props.src_host);
        if (r->remote_addr)
            lo_address_free(r->remote_addr);
        if (r->props.dest_name)
            free(r->props.dest_name);
        while (r->signals && r->signals->connections) {
            // receiver_signal is freed with last child connection
            mapper_receiver_remove_connection(r, r->signals->connections);
        }
        for (i=0; i<r->props.num_scopes; i++) {
            free(r->props.scope_names[i]);
        }
        free(r->props.scope_names);
        free(r->props.scope_hashes);
        if (r->props.extra)
            table_free(r->props.extra, 1);
        free(r);
    }
}

/*! Set a router's properties based on message parameters. */
void mapper_receiver_set_from_message(mapper_receiver rc,
                                      mapper_message_t *msg)
{
    /* Extra properties. */
    mapper_msg_add_or_update_extra_params(rc->props.extra, msg);
}

static void message_add_coerced_signal_value(lo_message m,
                                             mapper_db_connection c,
                                             mapper_signal_instance si)
{
    int i, min_length = c->src_length < c->dest_length ?
                        c->src_length : c->dest_length;

    if (c->dest_type == 'f') {
        float *v = (float *) si->value;
        for (i = 0; i < min_length; i++) {
            if (!si->has_value && !(si->has_value_flags[i/8] & 1 << (i % 8)))
                lo_message_add_nil(m);
            else if (c->src_type == 'f')
                lo_message_add_float(m, v[i]);
            else if (c->src_type == 'i')
                lo_message_add_int32(m, (int)v[i]);
            else if (c->src_type == 'd')
                lo_message_add_double(m, (double)v[i]);
        }
    }
    else if (c->dest_type == 'i') {
        int *v = (int *) si->value;
        for (i = 0; i < min_length; i++) {
            if (!si->has_value && !(si->has_value_flags[i/8] & 1 << (i % 8)))
                lo_message_add_nil(m);
            else if (c->src_type == 'i')
                lo_message_add_int32(m, v[i]);
            else if (c->src_type == 'f')
                lo_message_add_float(m, (float)v[i]);
            else if (c->src_type == 'd')
                lo_message_add_double(m, (double)v[i]);
        }
    }
    else if (c->dest_type == 'd') {
        double *v = (double *) si->value;
        for (i = 0; i < min_length; i++) {
            if (!si->has_value && !(si->has_value_flags[i/8] & 1 << (i % 8)))
                lo_message_add_nil(m);
            else if (c->src_type == 'd')
                lo_message_add_double(m, (int)v[i]);
            else if (c->src_type == 'i')
                lo_message_add_int32(m, (int)v[i]);
            else if (c->src_type == 'f')
                lo_message_add_float(m, (float)v[i]);
        }
    }
    for (i = min_length; i < c->src_length; i++)
        lo_message_add_nil(m);
}

void mapper_receiver_send_update(mapper_receiver r,
                                 mapper_signal sig,
                                 int instance_index,
                                 mapper_timetag_t tt)
{
    // TODO: check vector has_values flags, include defined elements even if !has_value?

    int count=0;
    mapper_id_map map = sig->id_maps[instance_index].map;

    int in_scope = map ? mapper_receiver_in_scope(r, map->origin) : 0;

    // find the signal connection
    mapper_receiver_signal rc = r->signals;
    while (rc) {
        if (rc->signal == sig)
{'fraction_non_alphanumeric': 0.14545994652935934, 'fraction_numerical': 0.008218635508466184, 'mean_word_length': 1.9627456732179525, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 149, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '863392', 'n_tokens_mistral': 3562, 'n_tokens_neox': 3463, 'n_words': 674}