<commit_before>/*****************************************************************************
 * renderer_discovery.c : Renderer Discovery functions
 *****************************************************************************
 * Copyright(C) 2016 VLC authors and VideoLAN
 *
 * This program 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 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <assert.h>

#include <vlc_common.h>
#include <vlc_atomic.h>
#include <vlc_events.h>
#include <vlc_renderer_discovery.h>
#include <vlc_probe.h>
#include <vlc_modules.h>
#include <libvlc.h>

struct vlc_renderer_item_t
{
    char *psz_name;
    char *psz_sout;
    char *psz_icon_uri;
    int i_flags;
    atomic_uint refs;
};

vlc_renderer_item_t *
vlc_renderer_item_new(const char *psz_name, const char *psz_uri,
                      const char *psz_extra_sout, const char *psz_icon_uri,
                      int i_flags)
{
    assert(psz_uri != NULL);
    vlc_renderer_item_t *p_item = NULL;
    vlc_url_t url;
    vlc_UrlParse(&url, psz_uri);

    if (url.psz_protocol == NULL || url.psz_host == NULL)
        goto error;

    p_item = calloc(1, sizeof(vlc_renderer_item_t));
    if (unlikely(p_item == NULL))
        goto error;

    if (psz_name != NULL)
        p_item->psz_name = strdup(psz_name);
    else if (asprintf(&p_item->psz_name, "%s (%s)", url.psz_protocol,
                      url.psz_host) == -1)
        p_item->psz_name = NULL;
    if (p_item->psz_name == NULL)
        goto error;

    if (asprintf(&p_item->psz_sout, "%s{ip=%s,port=%d%s%s}",
                 url.psz_protocol, url.psz_host, url.i_port,
                 psz_extra_sout != NULL ? "," : "",
                 psz_extra_sout != NULL ? psz_extra_sout : "") == -1)
        goto error;

    if ((p_item->psz_icon_uri = strdup(psz_icon_uri)) == NULL)
        goto error;

    p_item->i_flags = i_flags;
    atomic_init(&p_item->refs, 1);
    vlc_UrlClean(&url);
    return p_item;

error:
    vlc_UrlClean(&url);
    if (p_item != NULL)
    {
        free(p_item->psz_name);
        free(p_item->psz_sout);
        free(p_item->psz_icon_uri);
        free(p_item);
    }
    return NULL;
}

const char *
vlc_renderer_item_name(const vlc_renderer_item_t *p_item)
{
    assert(p_item != NULL);

    return p_item->psz_name;
}

const char *
vlc_renderer_item_sout(const vlc_renderer_item_t *p_item)
{
    assert(p_item != NULL);

    return p_item->psz_sout;
}

const char *
vlc_renderer_item_icon_uri(const vlc_renderer_item_t *p_item)
{
    assert(p_item != NULL);

    return p_item->psz_icon_uri;
}

int
vlc_renderer_item_flags(const vlc_renderer_item_t *p_item)
{
    assert(p_item != NULL);

    return p_item->i_flags;
}

vlc_renderer_item_t *
vlc_renderer_item_hold(vlc_renderer_item_t *p_item)
{
    assert(p_item != NULL);

    atomic_fetch_add(&p_item->refs, 1);
    return p_item;
}

void
vlc_renderer_item_release(vlc_renderer_item_t *p_item)
{
    assert(p_item != NULL);

    if (atomic_fetch_sub(&p_item->refs, 1) != 1)
        return;
    free(p_item->psz_name);
    free(p_item->psz_sout);
    free(p_item->psz_icon_uri);
    free(p_item);
}

struct vlc_rd_probe
{
    char *psz_name;
    char *psz_longname;
};

int
vlc_rd_probe_add(vlc_probe_t *probe, const char *psz_name,
                 const char *psz_longname)
{
    struct vlc_rd_probe names = { strdup(psz_name), strdup(psz_longname) };

    if (unlikely(names.psz_name == NULL || names.psz_longname == NULL
                 || vlc_probe_add(probe, &names, sizeof(names))))
    {
        free(names.psz_name);
        free(names.psz_longname);
        return VLC_ENOMEM;
    }
    return VLC_PROBE_CONTINUE;
}
<commit_msg>renderer: factor code<commit_after>/*****************************************************************************
 * renderer_discovery.c : Renderer Discovery functions
 *****************************************************************************
 * Copyright(C) 2016 VLC authors and VideoLAN
 *
 * This program 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 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <assert.h>

#include <vlc_common.h>
#include <vlc_atomic.h>
#include <vlc_events.h>
#include <vlc_renderer_discovery.h>
#include <vlc_probe.h>
#include <vlc_modules.h>
#include <libvlc.h>

struct vlc_renderer_item_t
{
    char *psz_name;
    char *psz_sout;
    char *psz_icon_uri;
    int i_flags;
    atomic_uint refs;
};

static void
item_free(vlc_renderer_item_t *p_item)
{
    free(p_item->psz_name);
    free(p_item->psz_sout);
    free(p_item->psz_icon_uri);
    free(p_item);
}

vlc_renderer_item_t *
vlc_renderer_item_new(const char *psz_name, const char *psz_uri,
                      const char *psz_extra_sout, const char *psz_icon_uri,
                      int i_flags)
{
    assert(psz_uri != NULL);
    vlc_renderer_item_t *p_item = NULL;
    vlc_url_t url;
    vlc_UrlParse(&url, psz_uri);

    if (url.psz_protocol == NULL || url.psz_host == NULL)
        goto error;

    p_item = calloc(1, sizeof(vlc_renderer_item_t));
    if (unlikely(p_item == NULL))
        goto error;

    if (psz_name != NULL)
        p_item->psz_name = strdup(psz_name);
    else if (asprintf(&p_item->psz_name, "%s (%s)", url.psz_protocol,
                      url.psz_host) == -1)
        p_item->psz_name = NULL;
    if (p_item->psz_name == NULL)
        goto error;

    if (asprintf(&p_item->psz_sout, "%s{ip=%s,port=%d%s%s}",
                 url.psz_protocol, url.psz_host, url.i_port,
                 psz_extra_sout != NULL ? "," : "",
                 psz_extra_sout != NULL ? psz_extra_sout : "") == -1)
        goto error;

    if ((p_item->psz_icon_uri = strdup(psz_icon_uri)) == NULL)
        goto error;

    p_item->i_flags = i_flags;
    atomic_init(&p_item->refs, 1);
    vlc_UrlClean(&url);
    return p_item;

error:
    vlc_UrlClean(&url);
    if (p_item != NULL)
        item_free(p_item);
    return NULL;
}

const char *
vlc_renderer_item_name(const vlc_renderer_item_t *p_item)
{
    assert(p_item != NULL);

    return p_item->psz_name;
}

const char *
vlc_renderer_item_sout(const vlc_renderer_item_t *p_item)
{
    assert(p_item != NULL);

    return p_item->psz_sout;
}

const char *
vlc_renderer_item_icon_uri(const vlc_renderer_item_t *p_item)
{
    assert(p_item != NULL);

    return p_item->psz_icon_uri;
}

int
vlc_renderer_item_flags(const vlc_renderer_item_t *p_item)
{
    assert(p_item != NULL);

    return p_item->i_flags;
}

vlc_renderer_item_t *
vlc_renderer_item_hold(vlc_renderer_item_t *p_item)
{
    assert(p_item != NULL);

    atomic_fetch_add(&p_item->refs, 1);
    return p_item;
}

void
vlc_renderer_item_release(vlc_renderer_item_t *p_item)
{
    assert(p_item != NULL);

    if (atomic_fetch_sub(&p_item->refs, 1) != 1)
        return;
    item_free(p_item);
}

struct vlc_rd_probe
{
    char *psz_name;
    char *psz_longname;
};

int
vlc_rd_probe_add(vlc_probe_t *probe, const char *psz_name,
                 const char *psz_longname)
{
    struct vlc_rd_probe names = { strdup(psz_name), strdup(psz_longname) };

    if (unlikely(names.psz_name == NULL || names.psz_longname == NULL
                 || vlc_probe_add(probe, &names, sizeof(names))))
    {
        free(names.psz_name);
        free(names.psz_longname);
        return VLC_ENOMEM;
    }
    return VLC_PROBE_CONTINUE;
}

{'fraction_non_alphanumeric': 0.15661016949152542, 'fraction_numerical': 0.005423728813559322, 'mean_word_length': 3.454453950679416, 'pattern_counts': {'":': 0, '<': 19, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1757699', 'n_tokens_mistral': 3111, 'n_tokens_neox': 3135, 'n_words': 702}