예제 #1
0
파일: string.c 프로젝트: childhood/libu
static int test_u_str (u_test_case_t *tc)
{
    u_string_t *s = NULL;

    u_test_err_if (u_string_create("0", 1, &s));

    u_test_err_if (strcmp(u_string_c(s), "0"));

    u_test_err_if (u_string_sprintf(s, "%s", "1"));
    u_test_err_if (strcmp(u_string_c(s), "1"));

    u_test_err_if (u_string_aprintf(s, "%s", "23"));
    u_test_err_if (strcmp(u_string_c(s), "123"));

    u_test_err_if (u_string_cat(s, "45"));
    u_test_err_if (strcmp(u_string_c(s), "12345"));

    u_test_err_if (u_string_ncat(s, "6777", 2));
    u_test_err_if (strcmp(u_string_c(s), "1234567"));

    u_test_err_if (u_string_sprintf(s, "%s", "reset"));
    u_test_err_if (strcmp(u_string_c(s), "reset"));

    u_string_free(s);

    return U_TEST_SUCCESS;
err:
    return U_TEST_FAILURE;
}
예제 #2
0
파일: var.c 프로젝트: DanielTillett/klone
/*
 * \ingroup vars
 * \brief   Free a variable
 *
 * \return \c 0, always
 */
int var_free(var_t *v)
{
    if(v)
    {
        if(v->sname)
            u_string_free(v->sname);

        if(v->svalue)
            u_string_free(v->svalue);

        if(v->opaque)
            U_FREE(v->opaque);

        U_FREE(v->data);
        U_FREE(v);
    }

    return 0;
}
예제 #3
0
/**
 *  \brief  Create a new string
 *
 *  Create a new ::u_string_t object and save its reference to \p *ps.
 *  If \p buf is not \c NULL (and <code>len > 0</code>), the string will be 
 *  initialized with the content of \p buf
 *
 *  \param  buf initial string value, or \c NULL
 *  \param  len length of \p buf
 *  \param  ps  result argument that will get the newly created ::u_string_t
 *              object on success
 *
 *  \retval  0  on success
 *  \retval ~0  on failure
 */
int u_string_create (const char *buf, size_t len, u_string_t **ps)
{
    u_string_t *s = NULL;

    dbg_return_if (ps == NULL, ~0);

    dbg_err_sif ((s = u_zalloc(sizeof(u_string_t))) == NULL);

    s->data = null;

    if (buf)
        dbg_err_if (u_string_append(s, buf, len));

    *ps = s;

    return 0;
err:
    if (s)
        u_string_free(s);
    return ~0;
}