in src/map.c [108:168]
MAP_HANDLE Map_Clone(MAP_HANDLE handle)
{
MAP_HANDLE_DATA* result;
if (handle == NULL)
{
/*Codes_SRS_MAP_02_038: [Map_Clone returns NULL if parameter handle is NULL.]*/
result = NULL;
LogError("invalid arg to Map_Clone (NULL)");
}
else
{
MAP_HANDLE_DATA* handleData = handle;
result = malloc(sizeof(MAP_HANDLE_DATA));
if (result == NULL)
{
/*Codes_SRS_MAP_02_047: [If during cloning, any operation fails, then Map_Clone shall return NULL.] */
/*do nothing, proceed to return it, this is an error case*/
LogError("unable to malloc");
}
else
{
if (handleData->count == 0)
{
result->count = 0;
result->keys = NULL;
result->values = NULL;
result->mapFilterCallback = NULL;
}
else
{
result->mapFilterCallback = handleData->mapFilterCallback;
result->count = handleData->count;
if( (result->keys = Map_CloneVector((const char* const*)handleData->keys, handleData->count))==NULL)
{
/*Codes_SRS_MAP_02_047: [If during cloning, any operation fails, then Map_Clone shall return NULL.] */
LogError("unable to clone keys");
free(result);
result = NULL;
}
else if ((result->values = Map_CloneVector((const char* const*)handleData->values, handleData->count)) == NULL)
{
size_t i;
/*Codes_SRS_MAP_02_047: [If during cloning, any operation fails, then Map_Clone shall return NULL.] */
LogError("unable to clone values");
for (i = 0; i < result->count; i++)
{
free(result->keys[i]);
}
free(result->keys);
free(result);
result = NULL;
}
else
{
/*all fine, return it*/
}
}
}
}
return (MAP_HANDLE)result;
}