int singlylinkedlist_foreach()

in src/singlylinkedlist.c [336:373]


int singlylinkedlist_foreach(SINGLYLINKEDLIST_HANDLE list, LIST_ACTION_FUNCTION action_function, const void* action_context)
{
    int result;

    /* Codes_SRS_LIST_09_008: [ If the list or the action_function argument is NULL, singlylinkedlist_foreach shall return non-zero value. ] */
    if ((list == NULL) ||
        (action_function == NULL))
    {
        LogError("Invalid argument (list=%p, action_function=%p)", list, action_function);
        result = MU_FAILURE;
    }
    else
    {
        LIST_INSTANCE* list_instance = list;
        LIST_ITEM_INSTANCE* list_item = list_instance->head;

        while (list_item != NULL)
        {
            bool continue_processing = false;

            /* Codes_SRS_LIST_09_009: [ singlylinkedlist_foreach shall iterate through all items in a list and invoke action_function for each one of them. ] */
            action_function(list_item->item, action_context, &continue_processing);

            /* Codes_SRS_LIST_09_010: [ If the condition function returns continue_processing as false, singlylinkedlist_foreach shall stop iterating through the list and return. ] */
            if (continue_processing == false)
            {
                break;
            }

            list_item = list_item->next;
        }

        /* Codes_SRS_LIST_09_011: [ If no errors occur, singlylinkedlist_foreach shall return zero. ] */
        result = 0;
    }

    return result;
}