Esempio n. 1
0
static void add_command(client_t *c, int ntoken, char **tokens)
{
    char   command[1024];
    size_t osize, nsize;

    if (c->registered) {
        print(c, "You need to unregister first to modify commands.");
        return;
    }

    if (concat_tokens(command, sizeof(command), ntoken, tokens) == NULL) {
        print(c, "Command too long.");
        return;
    }

    osize = sizeof(*c->commands) *  c->ncommand;
    nsize = sizeof(*c->commands) * (c->ncommand + 1);

    if (!mrp_reallocz(c->commands, osize, nsize)) {
        print(c, "Failed to add new command.");
        return;
    }

    c->commands[c->ncommand] = mrp_strdup(command);

    if (c->commands[c->ncommand] != NULL) {
        c->ncommand++;
        print(c, "Command '%s' added to command set.", command);
    }
    else
        print(c, "Failed to register new command.");
}
Esempio n. 2
0
static uint32_t synth_load(const char *path, int cache, void *api_data)
{
    synth_t *synth = (synth_t *)api_data;
    sound_t *snd;

    if (mrp_reallocz(synth->sounds, synth->nsound, synth->nsound + 1) != NULL) {
        snd = synth->sounds + synth->nsound;

        snd->path  = mrp_strdup(path);
        snd->cache = cache ? 1 : 0;
        snd->id    = SYNTH_TYPE_SOUND | synth->nsound;

        if (snd->path != NULL) {
            synth->nsound++;

            return snd->id;
        }

        mrp_reallocz(synth->sounds, synth->nsound + 1, synth->nsound);
    }

    return SRS_VOICE_INVALID;
}
Esempio n. 3
0
static void *fragbuf_ensure(mrp_fragbuf_t *buf, size_t size)
{
    int more;

    if (buf->size - buf->used < (int)size) {
        more = size - (buf->size - buf->used);

        if (mrp_reallocz(buf->data, buf->size, buf->size + more) == NULL)
            return NULL;
        else
            buf->size += more;
    }

    return buf->data + buf->used;
}
Esempio n. 4
0
static tracklist_t *tracklist_parse(const char *path)
{
#define CHECK_STRTAG(_tag, _member)             \
    if (!strcasecmp(tag, _tag)) {               \
        mrp_free(t->_member);                   \
        t->_member = mrp_strdup(value);         \
        continue;                               \
    }

#define CHECK_INTTAG(_tag, _member)                             \
    if (!strcasecmp(tag, _tag)) {                               \
        t->_member = (int)strtoul(value, &e, 10);               \
        if (e && *e)                                            \
            goto invalid;                                       \
        continue;                                               \
    }

    tracklist_t *t;
    char         buf[16 * 1024], *tag, *sep, *value, *p, *e;
    int          fd, n;

    fd = open(path, O_RDONLY);

    if (fd < 0)
        goto failed;

    n = read(fd, buf, sizeof(buf) - 1);

    close(fd);

    if (n < 0)
        goto failed;

    if (n >= (int)sizeof(buf) - 1)
        goto nospace;

    buf[n] = '\0';

    t = mrp_allocz(sizeof(*t));

    if (t == NULL)
        goto nomem;

    p = buf;
    while (p && *p) {
        while (*p == ' ' || *p == '\t')
            p++;

        if (*p == '\n') {
            p++;
            continue;
        }

        tag = p;

        while (*p && *p != ':' && *p != '.' && *p != '\n')
            p++;

        if (*p == '\n')
            goto invalid;

        sep = p;
        p++;

        while (*p == ' ' || *p == '\t')
            p++;

        value = p;

        while (*p && *p != '\n')
            p++;

        if (p != '\0')
            *p++ = '\0';

        e = p - 1;
        while (e > value && (*e == ' ' || *e == '\t'))
            *e-- = '\0';

        if (*sep == ':') {
            *sep = '\0';

            CHECK_STRTAG("Album" , album);
            CHECK_STRTAG("Artist", artist);
            CHECK_STRTAG("Genre" , genre);
            CHECK_INTTAG("Year"  , year);

            mrp_log_warning("Ignoring unknown tag '%s'...", tag);
            continue;
        }

        if (*sep == '.') {
            *sep = '\0';

            n = strtoul(tag, &e, 10);

            if (e && *e)
                goto invalid;

            if (n < 1 || n > 99)         /* max. CDDA tracks */
                goto invalid;

            if (n > t->ntrack) {
                if (!mrp_reallocz(t->tracks, t->ntrack, n))
                    goto nomem;
                t->ntrack = n;
            }

            mrp_free(t->tracks[n - 1]);
            t->tracks[n - 1] = mrp_strdup(value);
        }
    }

    return t;

 nospace:
    errno = ENOBUFS;
 failed:
 nomem:
    return NULL;

 invalid:
    errno = EINVAL;
    tracklist_free(t);
    return NULL;
}