ARGC_ARGV_DATA_RESULT FABRIC_ENDPOINT_RESOURCE_DESCRIPTION_LIST_from_ARGC_ARGV()

in src/fc_erdl_argc_argv.c [82:164]


ARGC_ARGV_DATA_RESULT FABRIC_ENDPOINT_RESOURCE_DESCRIPTION_LIST_from_ARGC_ARGV(int argc, char** argv, FABRIC_ENDPOINT_RESOURCE_DESCRIPTION_LIST* fabric_endpoint_resource_description_list, int* argc_consumed)
{
    ARGC_ARGV_DATA_RESULT result;
    if (
        ((argv == NULL) &&(argc!=0)) ||
        (fabric_endpoint_resource_description_list == NULL) ||
        (argc_consumed == NULL)
        )
    {
        LogError("invalid argument int argc=%d, char** argv=%p, FABRIC_ENDPOINT_RESOURCE_DESCRIPTION_LIST* fabric_endpoint_resource_description_list=%p, int* argc_consumed=%p",
            argc, argv, fabric_endpoint_resource_description_list, argc_consumed);
        result = ARGC_ARGV_DATA_ERROR;
    }
    else
    {
        *argc_consumed = 0;
        fabric_endpoint_resource_description_list->Count = 0;
        fabric_endpoint_resource_description_list->Items = NULL;

        bool done = false;
        bool wasError = false;


        while(!wasError && !done)
        {
            FABRIC_ENDPOINT_RESOURCE_DESCRIPTION d;
            int c_argc;
            ARGC_ARGV_DATA_RESULT r = FABRIC_ENDPOINT_RESOURCE_DESCRIPTION_from_ARGC_ARGV(argc - *argc_consumed, argv + *argc_consumed, &d, &c_argc);
            switch (r)
            {
                case ARGC_ARGV_DATA_OK:
                {
                    fabric_endpoint_resource_description_list->Count++;
                    FABRIC_ENDPOINT_RESOURCE_DESCRIPTION* temp = realloc_2((void*)fabric_endpoint_resource_description_list->Items, fabric_endpoint_resource_description_list->Count, sizeof(FABRIC_ENDPOINT_RESOURCE_DESCRIPTION));
                    if (temp == NULL)
                    {
                        fabric_endpoint_resource_description_list->Count--;
                        LogError("Failure in realloc_2");
                        wasError = true;
                    }
                    else
                    {
                        fabric_endpoint_resource_description_list->Items = temp;
                        /*cast the const away*/
                        *(FABRIC_ENDPOINT_RESOURCE_DESCRIPTION*)(fabric_endpoint_resource_description_list->Items+fabric_endpoint_resource_description_list->Count - 1) = d;
                        /*advance to the next arguments*/
                        *argc_consumed += c_argc;
                    }
                    break;
                }
                case ARGC_ARGV_DATA_INVALID:
                {
                    /*not really an error, we are done scanning*/
                    done = true;
                    break;
                }
                case ARGC_ARGV_DATA_ERROR:
                default:
                {
                    LogError("failure in FABRIC_ENDPOINT_RESOURCE_DESCRIPTION_from_ARGC_ARGV, %" PRI_MU_ENUM "", MU_ENUM_VALUE(ARGC_ARGV_DATA_RESULT, r));
                    wasError = true;
                    break;
                }
            } /*switch*/
        } /*while*/

        if (wasError)
        {
            /*undo all changes*/
            for (ULONG i = 0; i < fabric_endpoint_resource_description_list->Count; i++)
            {
                FABRIC_ENDPOINT_RESOURCE_DESCRIPTION_free((FABRIC_ENDPOINT_RESOURCE_DESCRIPTION*)(fabric_endpoint_resource_description_list->Items + i));
            }
            free((void*)fabric_endpoint_resource_description_list->Items);
            result = ARGC_ARGV_DATA_ERROR;
        }
        else
        {
            result = ARGC_ARGV_DATA_OK; /*note: this can result in a 0 element for FABRIC_ENDPOINT_RESOURCE_DESCRIPTION_LIST, still totally valid*/
        }
    }
    return result;
}