Пример #1
0
NRTAPI(nrt_List *) nrt_List_clone(nrt_List * source, NRT_DATA_ITEM_CLONE cloner,
                                  nrt_Error * error)
{
    nrt_List *l = NULL;
    if (source)
    {
        nrt_ListIterator iter = nrt_List_begin(source);
        nrt_ListIterator end = nrt_List_end(source);
        l = nrt_List_construct(error);
        if (!l)
            return NULL;

        while (nrt_ListIterator_notEqualTo(&iter, &end))
        {

            /* Foreach item in each list... */
            NRT_DATA *data = nrt_ListIterator_get(&iter);

            /* Use the function pointer to clone the object...  */
            NRT_DATA *newData = (NRT_DATA *) cloner(data, error);
            if (!newData)
                return NULL;

            /* ... and then insert it with the key into the new table */
            if (!nrt_List_pushBack(l, newData, error))
            {
                /* destruct the created list. NOTE - there is no way for us to
                 * destroy the NRT_DATA* chunks that have already been cloned */
                nrt_List_destruct(&l);
                return NULL;
            }

            nrt_ListIterator_increment(&iter);
        }
    }
    else
    {
        nrt_Error_initf(error, NRT_CTXT, NRT_ERR_INVALID_OBJECT,
                        "Trying to clone NULL pointer");
    }

    return l;
}
Пример #2
0
NRTAPI(nrt_List *) nrt_Utils_splitString(char *str, unsigned int max,
        nrt_Error * error)
{
    unsigned int count = 0;
    nrt_List *parts;
    char *op, *cur, *end;
    size_t strLen;

    parts = nrt_List_construct(error);
    if (!parts)
        return NULL;

    strLen = strlen(str);
    end = str + strLen;

    op = str;

    if (max == 1)
    {
        char *val = NRT_MALLOC(strLen + 1);
        if (!val)
        {
            nrt_Error_init(error, NRT_STRERROR(NRT_ERRNO), NRT_CTXT,
                           NRT_ERR_MEMORY);
            return NULL;
        }
        memset(val, 0, strLen + 1);
        memcpy(val, str, strLen);
        nrt_List_pushBack(parts, val, error);
    }
    else
    {
        /* strtok is not thread safe */
        while (op < end)
        {
            char *val = NULL;
            int sz;
            /* skip past white space */
            while (isspace(*op) && op < end)
                ++op;
            cur = op;

            while (!isspace(*op) && op < end)
                ++op;

            if (cur == op)
                break;

            sz = op - cur;
            val = NRT_MALLOC(sz + 1);
            if (!val)
            {
                nrt_Error_init(error, NRT_STRERROR(NRT_ERRNO), NRT_CTXT,
                               NRT_ERR_MEMORY);
                return NULL;
            }
            memset(val, 0, sz + 1);
            memcpy(val, cur, sz);
            nrt_List_pushBack(parts, val, error);
            count++;

            /* check the count limit */
            if (max != 0 && count == (max - 1) && op < end)
            {
                /* push on the rest of the string - skip spaces first */
                while (isspace(*op) && op < end)
                    ++op;

                if (op < end)
                {
                    sz = end - op;
                    val = NRT_MALLOC(sz + 1);
                    if (!val)
                    {
                        nrt_Error_init(error, NRT_STRERROR(NRT_ERRNO), NRT_CTXT,
                                       NRT_ERR_MEMORY);
                        return NULL;
                    }
                    memset(val, 0, sz + 1);
                    memcpy(val, op, sz);
                    nrt_List_pushBack(parts, val, error);
                }
                break;
            }
        }
    }

    return parts;
}