MAP_RESULT Map_Add()

in src/map.c [340:388]


MAP_RESULT Map_Add(MAP_HANDLE handle, const char* key, const char* value)
{
    MAP_RESULT result;
    /*Codes_SRS_MAP_02_006: [If parameter handle is NULL then Map_Add shall return MAP_INVALID_ARG.] */
    /*Codes_SRS_MAP_02_007: [If parameter key is NULL then Map_Add shall return MAP_INVALID_ARG.]*/
    /*Codes_SRS_MAP_02_008: [If parameter value is NULL then Map_Add shall return MAP_INVALID_ARG.] */
    if (
        (handle == NULL) ||
        (key == NULL) ||
        (value == NULL)
        )
    {
        result = MAP_INVALIDARG;
        LOG_MAP_ERROR;
    }
    else
    {
        MAP_HANDLE_DATA* handleData = handle;
        /*Codes_SRS_MAP_02_009: [If the key already exists, then Map_Add shall return MAP_KEYEXISTS.] */
        if (findKey(handleData, key) != NULL)
        {
            result = MAP_KEYEXISTS;
        }
        else
        {
            /* Codes_SRS_MAP_07_009: [If the mapFilterCallback function is not NULL, then the return value will be check and if it is not zero then Map_Add shall return MAP_FILTER_REJECT.] */
            if ( (handleData->mapFilterCallback != NULL) && (handleData->mapFilterCallback(key, value) != 0) )
            {
                result = MAP_FILTER_REJECT;
            }
            else
            {
                /*Codes_SRS_MAP_02_010: [Otherwise, Map_Add shall add the pair <key,value> to the map.] */
                if (insertNewKeyValue(handleData, key, value) != 0)
                {
                    /*Codes_SRS_MAP_02_011: [If adding the pair <key,value> fails then Map_Add shall return MAP_ERROR.] */
                    result = MAP_ERROR;
                    LOG_MAP_ERROR;
                }
                else
                {
                    /*Codes_SRS_MAP_02_012: [Otherwise, Map_Add shall return MAP_OK.] */
                    result = MAP_OK;
                }
            }
        }
    }
    return result;
}