Exemple #1
0
NRTAPI(NRT_BOOL) nrt_List_insert(nrt_List * list, nrt_ListIterator iter,
                                 NRT_DATA * data, nrt_Error * error)
{
    /* If our iterator is not set, we will insert the data into the back */
    if (!iter.current)
    {
        /* Push the data onto the back */
        if (!nrt_List_pushBack(list, data, error))
        {
            return 0;
        }
        /* Update the iterator to point at us */
        iter.current = list->last;
    }
    /* If the iterator is at the first */
    else if (iter.current == list->first)
    {
        /* Push data onto the front */
        if (!nrt_List_pushFront(list, data, error))
        {
            return 0;
        }
        /* Update the iterator to point at us */
        iter.current = list->first;
    }
    else
    {

        /* Construct a new node and insert it before the current */
        nrt_ListNode *new_node = nrt_ListNode_construct(iter.current->prev,
                                                        iter.current,
                                                        data,
                                                        error);

        /* If an error occurred, the list node captured it */
        if (!new_node)
        {
            /* Error already populated, go home */
            return 0;
        }

        /* Point the iterator at our new node */
        iter.current->prev->next = new_node;
        new_node->next->prev = new_node;
        iter.current = new_node;
    }
    return 1;
}
Exemple #2
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;
}
Exemple #3
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;
}