| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
#include <stddef.h> |
| 5 |
#include <string.h> |
| 6 |
#include "umock_c/umockalloc.h" |
| 7 |
#include "umock_c/umockstring.h" |
| 8 |
#include "umock_c/umock_log.h" |
| 9 |
|
| 10 |
char* umockstring_clone(const char* source) |
| 11 |
{ |
| 12 |
char* result; |
| 13 |
|
| 14 |
if (source == NULL) |
| 15 |
{ |
| 16 |
/* Codes_UMOCK_STRING_01_005: [ If umockstring_clone is called with a NULL source, it shall return NULL. ]*/ |
| 17 |
UMOCK_LOG("umockstring_clone called with NULL source"); |
| 18 |
result = NULL; |
| 19 |
} |
| 20 |
else |
| 21 |
{ |
| 22 |
size_t string_length = strlen(source); |
| 23 |
|
| 24 |
/* Codes_UMOCK_STRING_01_001: [ umockstring_clone shall allocate memory for the cloned string (including the NULL terminator). ]*/ |
| 25 |
/* Codes_UMOCK_STRING_01_003: [ On success umockstring_clone shall return a pointer to the newly allocated memory containing the copy of the string. ]*/ |
| 26 |
result = (char*)umockalloc_malloc(string_length + 1); |
| 27 |
if (result == NULL) |
| 28 |
{ |
| 29 |
/* Codes_UMOCK_STRING_01_004: [ If allocating the memory fails, umockstring_clone shall return NULL. ]*/ |
| 30 |
UMOCK_LOG("Error allocating memory for string clone"); |
| 31 |
} |
| 32 |
else |
| 33 |
{ |
| 34 |
/* Codes_UMOCK_STRING_01_002: [ umockstring_clone shall copy the string to the newly allocated memory (including the NULL terminator). ]*/ |
| 35 |
(void)memcpy(result, source, string_length + 1); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
return result; |
| 40 |
} |