Azure IoT common library
Fork of azure_c_shared_utility by
Diff: strings.c
- Revision:
- 8:3db46d1e5471
- Parent:
- 6:c55b013dfc2a
- Child:
- 9:079c39803432
--- a/strings.c Fri Jul 29 16:01:07 2016 -0700 +++ b/strings.c Fri Aug 12 10:04:46 2016 -0700 @@ -12,6 +12,9 @@ #include <stddef.h> #include <string.h> +#include <stdarg.h> +#include <stdio.h> + // // PUT NO CLIENT LIBRARY INCLUDES BEFORE HERE // @@ -119,6 +122,69 @@ return result; } +STRING_HANDLE STRING_construct_sprintf(const char* format, ...) +{ + STRING* result; + if (format != NULL) + { + va_list arg_list; + va_start(arg_list, format); + + /* Codes_SRS_STRING_07_041: [STRING_construct_sprintf shall determine the size of the resulting string and allocate the necessary memory.] */ + int length = vsnprintf(NULL, 0, format, arg_list); + va_end(arg_list); + if (length > 0) + { + result = (STRING*)malloc(sizeof(STRING)); + if (result != NULL) + { + result->s = (char*)malloc(length+1); + if (result->s != NULL) + { + va_start(arg_list, format); + if (vsnprintf(result->s, length+1, format, arg_list) < 0) + { + /* Codes_SRS_STRING_07_040: [If any error is encountered STRING_construct_sprintf shall return NULL.] */ + free(result->s); + free(result); + result = NULL; + LogError("Failure: vsnprintf formatting failed."); + } + va_end(arg_list); + } + else + { + /* Codes_SRS_STRING_07_040: [If any error is encountered STRING_construct_sprintf shall return NULL.] */ + free(result); + result = NULL; + LogError("Failure: allocation failed."); + } + } + else + { + LogError("Failure: allocation failed."); + } + } + else if (length == 0) + { + result = (STRING*)STRING_new(); + } + else + { + /* Codes_SRS_STRING_07_039: [If the parameter format is NULL then STRING_construct_sprintf shall return NULL.] */ + result = NULL; + LogError("Failure: vsnprintf return 0 length"); + } + } + else + { + LogError("Failure: invalid argument."); + result = NULL; + } + /* Codes_SRS_STRING_07_045: [STRING_construct_sprintf shall allocate a new string with the value of the specified printf formated const char. ] */ + return (STRING_HANDLE)result; +} + /*this function will return a new STRING with the memory for the actual string passed in as a parameter.*/ /*return NULL if it fails.*/ /* The supplied memory must have been allocated with malloc! */ @@ -434,6 +500,67 @@ return result; } +int STRING_sprintf(STRING_HANDLE handle, const char* format, ...) +{ + int result; + if (handle == NULL || format == NULL) + { + /* Codes_SRS_STRING_07_042: [if the parameters s1 or format are NULL then STRING_sprintf shall return non zero value.] */ + result = __LINE__; + LogError("Invalid arg (NULL)"); + } + else + { + va_list arg_list; + va_start(arg_list, format); + + int s2Length = vsnprintf(NULL, 0, format, arg_list); + va_end(arg_list); + if (s2Length < 0) + { + /* Codes_SRS_STRING_07_043: [If any error is encountered STRING_sprintf shall return a non zero value.] */ + result = __LINE__; + LogError("Failure vsnprintf return < 0"); + } + else if (s2Length == 0) + { + // Don't need to reallocate and nothing should be added + result = 0; + } + else + { + STRING* s1 = (STRING*)handle; + size_t s1Length = strlen(s1->s); + char* temp = (char*)realloc(s1->s, s1Length + s2Length + 1); + if (temp != NULL) + { + s1->s = temp; + va_start(arg_list, format); + if (vsnprintf(s1->s + s1Length, s1Length + s2Length + 1, format, arg_list) < 0) + { + /* Codes_SRS_STRING_07_043: [If any error is encountered STRING_sprintf shall return a non zero value.] */ + LogError("Failure vsnprintf formatting error"); + s1->s[s1Length] = '\0'; + result = __LINE__; + } + else + { + /* Codes_SRS_STRING_07_044: [On success STRING_sprintf shall return 0.]*/ + result = 0; + } + va_end(arg_list); + } + else + { + /* Codes_SRS_STRING_07_043: [If any error is encountered STRING_sprintf shall return a non zero value.] */ + LogError("Failure unable to reallocate memory"); + result = __LINE__; + } + } + } + return result; +} + /*this function will quote the string passed as argument string =>"string"*/ /*returns 0 if success*/ /*doesn't change the string otherwise*/ /*any other error code is failure*/