STRING_HANDLE STRING_construct()

in src/strings.c [92:129]


STRING_HANDLE STRING_construct(const char* psz)
{
    STRING_HANDLE result;
    if (psz == NULL)
    {
        /* Codes_SRS_STRING_07_005: [If the supplied const char* is NULL STRING_construct shall return a NULL value.] */
        result = NULL;
    }
    else
    {
        STRING* str;
        if ((str = malloc(sizeof(STRING))) != NULL)
        {
            size_t nLen = strlen(psz);
            if ((str->s = malloc_flex(1, nLen, 1)) != NULL)
            {
                (void)memcpy(str->s, psz, nLen + 1);
                result = str;
            }
            /* Codes_SRS_STRING_07_032: [STRING_construct encounters any error it shall return a NULL value.] */
            else
            {
                LogError("Failure in malloc_flex(1, nLen=%zu, 1).",
                    nLen);
                free(str);
                result = NULL;
            }
        }
        else
        {
            /* Codes_SRS_STRING_07_032: [STRING_construct encounters any error it shall return a NULL value.] */
            LogError("Failure in malloc(sizeof(STRING)=%zu).",
                sizeof(STRING));
            result = NULL;
        }
    }
    return result;
}