<commit_before>/*
 * Copyright 2017 GoPro Inc.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include <inttypes.h>
#include <stdio.h>
#include <string.h>

#include "log.h"
#include "nodegl.h"
#include "nodes.h"
#include "params.h"

struct serial_ctx {
    struct ngl_node **nodes;
    int nb_nodes;
};

static int register_node(struct serial_ctx *sctx,
                         const struct ngl_node *node)
{
    struct ngl_node **new_nodes = realloc(sctx->nodes, (sctx->nb_nodes + 1) * sizeof(*new_nodes));
    if (!new_nodes)
        return -1;
    new_nodes[sctx->nb_nodes++] = (struct ngl_node *)node;
    sctx->nodes = new_nodes;
    return 0;
}

#define CASE_LITERAL(param_type, type, parse_func)      \
case param_type: {                                      \
    type v;                                             \
    len = parse_func(str, &v);                          \
    if (len < 0)                                        \
        return -1;                                      \
    int ret = ngli_params_vset(base_ptr, par, v);       \
    if (ret < 0)                                        \
        return ret;                                     \
    break;                                              \
}

static int parse_int(const char *s, int *valp)
{
    char *endptr = NULL;
    *valp = (int)strtol(s, &endptr, 10);
    return (int)(endptr - s);
}

static int parse_hexint(const char *s, int *valp)
{
    char *endptr = NULL;
    *valp = (int)strtol(s, &endptr, 16);
    return (int)(endptr - s);
}

static int64_t parse_i64(const char *s, int64_t *valp)
{
    char *endptr = NULL;
    *valp = strtoll(s, &endptr, 10);
    return (int)(endptr - s);
}

static int parse_bool(const char *s, int *valp)
{
    int ret = parse_int(s, valp);
    if (ret < 0)
        return ret;
    if (*valp != -1)
        *valp = !!*valp;
    return ret;
}

#define DECLARE_FLT_PARSE_FUNC(type, nbit, shift_exp)                       \
static int parse_##type(const char *s, type *valp)                          \
{                                                                           \
    int len, consumed = 0;                                                  \
    union { uint##nbit##_t i; type f; } u = {.i = 0};                       \
                                                                            \
    if (*s == '-') {                                                        \
        u.i = 1ULL << (nbit - 1);                                           \
        consumed++;                                                         \
    }                                                                       \
                                                                            \
    uint##nbit##_t exp, mant;                                               \
    int n = sscanf(s + consumed, "%" SCNx##nbit "z%" SCNx##nbit "%n",       \
                   &exp, &mant, &len);                                      \
    if (n != 2)                                                             \
        return -1;                                                          \
                                                                            \
    u.i |= exp<<shift_exp | mant;                                           \
                                                                            \
    *valp = u.f;                                                            \
    return len + consumed;                                                  \
}

DECLARE_FLT_PARSE_FUNC(float,  32, 23)
DECLARE_FLT_PARSE_FUNC(double, 64, 52)

#define DECLARE_PARSE_LIST_FUNC(type, parse_func)                           \
static int parse_func##s(const char *s, type **valsp, int *nb_valsp)        \
{                                                                           \
    type *vals = NULL;                                                      \
    int nb_vals = 0, consumed = 0, len;                                     \
                                                                            \
    for (;;) {                                                              \
        type v;                                                             \
        len = parse_func(s, &v);                                            \<commit_msg>deserialize: replace sscanf with strtoul*() for float parsing<commit_after>/*
 * Copyright 2017 GoPro Inc.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include <inttypes.h>
#include <stdio.h>
#include <string.h>

#include "log.h"
#include "nodegl.h"
#include "nodes.h"
#include "params.h"

struct serial_ctx {
    struct ngl_node **nodes;
    int nb_nodes;
};

static int register_node(struct serial_ctx *sctx,
                         const struct ngl_node *node)
{
    struct ngl_node **new_nodes = realloc(sctx->nodes, (sctx->nb_nodes + 1) * sizeof(*new_nodes));
    if (!new_nodes)
        return -1;
    new_nodes[sctx->nb_nodes++] = (struct ngl_node *)node;
    sctx->nodes = new_nodes;
    return 0;
}

#define CASE_LITERAL(param_type, type, parse_func)      \
case param_type: {                                      \
    type v;                                             \
    len = parse_func(str, &v);                          \
    if (len < 0)                                        \
        return -1;                                      \
    int ret = ngli_params_vset(base_ptr, par, v);       \
    if (ret < 0)                                        \
        return ret;                                     \
    break;                                              \
}

static int parse_int(const char *s, int *valp)
{
    char *endptr = NULL;
    *valp = (int)strtol(s, &endptr, 10);
    return (int)(endptr - s);
}

static int parse_hexint(const char *s, int *valp)
{
    char *endptr = NULL;
    *valp = (int)strtol(s, &endptr, 16);
    return (int)(endptr - s);
}

static int64_t parse_i64(const char *s, int64_t *valp)
{
    char *endptr = NULL;
    *valp = strtoll(s, &endptr, 10);
    return (int)(endptr - s);
}

static int parse_bool(const char *s, int *valp)
{
    int ret = parse_int(s, valp);
    if (ret < 0)
        return ret;
    if (*valp != -1)
        *valp = !!*valp;
    return ret;
}

#define DECLARE_FLT_PARSE_FUNC(type, nbit, shift_exp)                       \
static int parse_##type(const char *s, type *valp)                          \
{                                                                           \
    int consumed = 0;                                                       \
    union { uint##nbit##_t i; type f; } u = {.i = 0};                       \
                                                                            \
    if (*s == '-') {                                                        \
        u.i = 1ULL << (nbit - 1);                                           \
        consumed++;                                                         \
    }                                                                       \
                                                                            \
    char *endptr = NULL;                                                    \
    uint##nbit##_t exp = nbit == 64 ? strtoull(s + consumed, &endptr, 16)   \
                                    : strtoul(s + consumed, &endptr, 16);   \
    if (*endptr++ != 'z')                                                   \
        return -1;                                                          \
    uint##nbit##_t mant = nbit == 64 ? strtoull(endptr, &endptr, 16)        \
                                     : strtoul(endptr, &endptr, 16);        \
                                                                            \
    u.i |= exp<<shift_exp | mant;                                           \
                                                                            \
    *valp = u.f;                                                            \
    return (int)(endptr - s);                                               \
}

DECLARE_FLT_PARSE_FUNC(float,  32, 23)
DECLARE_FLT_PARSE_FUNC(double, 64, 52)

#define DECLARE_PARSE_LIST_FUNC(type, parse_func)                           \
static int parse_func##s(const char *s, type **valsp, int *nb_valsp)        \
{                                                                           \
    type *vals = NULL;                                                      \
    int nb_vals = 0, consumed = 0, len;                                     \
                                                                            \
    for (;;) {                                                              \
        type v;                                                             \
        len = parse_func(s, &v);                                            \
{'fraction_non_alphanumeric': 0.09090909090909091, 'fraction_numerical': 0.009574468085106383, 'mean_word_length': 0.8915310042070606, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 131, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '202602', 'n_tokens_mistral': 2563, 'n_tokens_neox': 2458, 'n_words': 731}