Azure IoT common library
Fork of azure_c_shared_utility by
strings.c@34:651c23af382c, 2017-08-24 (annotated)
- Committer:
- wiggly
- Date:
- Thu Aug 24 14:14:15 2017 +0100
- Revision:
- 34:651c23af382c
- Parent:
- 30:ce3813c5a692
Pass in network stack to platform initialisation
Remove NTP setup from azure platform code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Azure.IoT Build | 0:fa2de1b79154 | 1 | // Copyright (c) Microsoft. All rights reserved. |
Azure.IoT Build | 0:fa2de1b79154 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
Azure.IoT Build | 0:fa2de1b79154 | 3 | |
Azure.IoT Build | 0:fa2de1b79154 | 4 | // |
Azure.IoT Build | 0:fa2de1b79154 | 5 | // PUT NO INCLUDES BEFORE HERE |
Azure.IoT Build | 0:fa2de1b79154 | 6 | // |
Azure.IoT Build | 0:fa2de1b79154 | 7 | #include <stdlib.h> |
Azure.IoT Build | 0:fa2de1b79154 | 8 | #include "azure_c_shared_utility/gballoc.h" |
Azure.IoT Build | 0:fa2de1b79154 | 9 | #include <stddef.h> |
Azure.IoT Build | 0:fa2de1b79154 | 10 | #include <string.h> |
AzureIoTClient | 8:3db46d1e5471 | 11 | #include <stdarg.h> |
AzureIoTClient | 8:3db46d1e5471 | 12 | #include <stdio.h> |
AzureIoTClient | 8:3db46d1e5471 | 13 | |
Azure.IoT Build | 0:fa2de1b79154 | 14 | // |
Azure.IoT Build | 0:fa2de1b79154 | 15 | // PUT NO CLIENT LIBRARY INCLUDES BEFORE HERE |
Azure.IoT Build | 0:fa2de1b79154 | 16 | // |
Azure.IoT Build | 0:fa2de1b79154 | 17 | |
Azure.IoT Build | 0:fa2de1b79154 | 18 | #include "azure_c_shared_utility/strings.h" |
AzureIoTClient | 21:b92006c5b9ff | 19 | #include "azure_c_shared_utility/optimize_size.h" |
Azure.IoT Build | 6:c55b013dfc2a | 20 | #include "azure_c_shared_utility/xlogging.h" |
Azure.IoT Build | 0:fa2de1b79154 | 21 | |
Azure.IoT Build | 0:fa2de1b79154 | 22 | static const char hexToASCII[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; |
Azure.IoT Build | 0:fa2de1b79154 | 23 | |
Azure.IoT Build | 0:fa2de1b79154 | 24 | typedef struct STRING_TAG |
Azure.IoT Build | 0:fa2de1b79154 | 25 | { |
Azure.IoT Build | 0:fa2de1b79154 | 26 | char* s; |
AzureIoTClient | 30:ce3813c5a692 | 27 | } STRING; |
Azure.IoT Build | 0:fa2de1b79154 | 28 | |
Azure.IoT Build | 0:fa2de1b79154 | 29 | /*this function will allocate a new string with just '\0' in it*/ |
Azure.IoT Build | 0:fa2de1b79154 | 30 | /*return NULL if it fails*/ |
Azure.IoT Build | 0:fa2de1b79154 | 31 | /* Codes_SRS_STRING_07_001: [STRING_new shall allocate a new STRING_HANDLE pointing to an empty string.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 32 | STRING_HANDLE STRING_new(void) |
Azure.IoT Build | 0:fa2de1b79154 | 33 | { |
Azure.IoT Build | 0:fa2de1b79154 | 34 | STRING* result; |
Azure.IoT Build | 0:fa2de1b79154 | 35 | if ((result = (STRING*)malloc(sizeof(STRING))) != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 36 | { |
Azure.IoT Build | 0:fa2de1b79154 | 37 | if ((result->s = (char*)malloc(1)) != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 38 | { |
Azure.IoT Build | 0:fa2de1b79154 | 39 | result->s[0] = '\0'; |
Azure.IoT Build | 0:fa2de1b79154 | 40 | } |
Azure.IoT Build | 0:fa2de1b79154 | 41 | else |
Azure.IoT Build | 0:fa2de1b79154 | 42 | { |
Azure.IoT Build | 0:fa2de1b79154 | 43 | /* Codes_SRS_STRING_07_002: [STRING_new shall return an NULL STRING_HANDLE on any error that is encountered.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 44 | free(result); |
Azure.IoT Build | 0:fa2de1b79154 | 45 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 46 | } |
Azure.IoT Build | 0:fa2de1b79154 | 47 | } |
Azure.IoT Build | 0:fa2de1b79154 | 48 | return (STRING_HANDLE)result; |
Azure.IoT Build | 0:fa2de1b79154 | 49 | } |
Azure.IoT Build | 0:fa2de1b79154 | 50 | |
Azure.IoT Build | 0:fa2de1b79154 | 51 | /*Codes_SRS_STRING_02_001: [STRING_clone shall produce a new string having the same content as the handle string.*/ |
Azure.IoT Build | 0:fa2de1b79154 | 52 | STRING_HANDLE STRING_clone(STRING_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 53 | { |
Azure.IoT Build | 0:fa2de1b79154 | 54 | STRING* result; |
Azure.IoT Build | 0:fa2de1b79154 | 55 | /*Codes_SRS_STRING_02_002: [If parameter handle is NULL then STRING_clone shall return NULL.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 56 | if (handle == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 57 | { |
Azure.IoT Build | 0:fa2de1b79154 | 58 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 59 | } |
Azure.IoT Build | 0:fa2de1b79154 | 60 | else |
Azure.IoT Build | 0:fa2de1b79154 | 61 | { |
Azure.IoT Build | 0:fa2de1b79154 | 62 | /*Codes_SRS_STRING_02_003: [If STRING_clone fails for any reason, it shall return NULL.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 63 | if ((result = (STRING*)malloc(sizeof(STRING))) != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 64 | { |
Azure.IoT Build | 0:fa2de1b79154 | 65 | STRING* source = (STRING*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 66 | /*Codes_SRS_STRING_02_003: [If STRING_clone fails for any reason, it shall return NULL.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 67 | size_t sourceLen = strlen(source->s); |
Azure.IoT Build | 0:fa2de1b79154 | 68 | if ((result->s = (char*)malloc(sourceLen + 1)) == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 69 | { |
Azure.IoT Build | 0:fa2de1b79154 | 70 | free(result); |
Azure.IoT Build | 0:fa2de1b79154 | 71 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 72 | } |
Azure.IoT Build | 0:fa2de1b79154 | 73 | else |
Azure.IoT Build | 0:fa2de1b79154 | 74 | { |
AzureIoTClient | 19:2e0811512ceb | 75 | (void)memcpy(result->s, source->s, sourceLen + 1); |
Azure.IoT Build | 0:fa2de1b79154 | 76 | } |
Azure.IoT Build | 0:fa2de1b79154 | 77 | } |
Azure.IoT Build | 0:fa2de1b79154 | 78 | else |
Azure.IoT Build | 0:fa2de1b79154 | 79 | { |
Azure.IoT Build | 0:fa2de1b79154 | 80 | /*not much to do, result is NULL from malloc*/ |
Azure.IoT Build | 0:fa2de1b79154 | 81 | } |
Azure.IoT Build | 0:fa2de1b79154 | 82 | } |
Azure.IoT Build | 0:fa2de1b79154 | 83 | return (STRING_HANDLE)result; |
Azure.IoT Build | 0:fa2de1b79154 | 84 | } |
Azure.IoT Build | 0:fa2de1b79154 | 85 | |
Azure.IoT Build | 0:fa2de1b79154 | 86 | /* Codes_SRS_STRING_07_003: [STRING_construct shall allocate a new string with the value of the specified const char*.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 87 | STRING_HANDLE STRING_construct(const char* psz) |
Azure.IoT Build | 0:fa2de1b79154 | 88 | { |
Azure.IoT Build | 0:fa2de1b79154 | 89 | STRING_HANDLE result; |
Azure.IoT Build | 0:fa2de1b79154 | 90 | if (psz == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 91 | { |
Azure.IoT Build | 0:fa2de1b79154 | 92 | /* Codes_SRS_STRING_07_005: [If the supplied const char* is NULL STRING_construct shall return a NULL value.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 93 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 94 | } |
Azure.IoT Build | 0:fa2de1b79154 | 95 | else |
Azure.IoT Build | 0:fa2de1b79154 | 96 | { |
Azure.IoT Build | 0:fa2de1b79154 | 97 | STRING* str; |
Azure.IoT Build | 0:fa2de1b79154 | 98 | if ((str = (STRING*)malloc(sizeof(STRING))) != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 99 | { |
Azure.IoT Build | 0:fa2de1b79154 | 100 | size_t nLen = strlen(psz) + 1; |
Azure.IoT Build | 0:fa2de1b79154 | 101 | if ((str->s = (char*)malloc(nLen)) != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 102 | { |
AzureIoTClient | 19:2e0811512ceb | 103 | (void)memcpy(str->s, psz, nLen); |
Azure.IoT Build | 0:fa2de1b79154 | 104 | result = (STRING_HANDLE)str; |
Azure.IoT Build | 0:fa2de1b79154 | 105 | } |
Azure.IoT Build | 0:fa2de1b79154 | 106 | /* Codes_SRS_STRING_07_032: [STRING_construct encounters any error it shall return a NULL value.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 107 | else |
Azure.IoT Build | 0:fa2de1b79154 | 108 | { |
Azure.IoT Build | 0:fa2de1b79154 | 109 | free(str); |
Azure.IoT Build | 0:fa2de1b79154 | 110 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 111 | } |
Azure.IoT Build | 0:fa2de1b79154 | 112 | } |
Azure.IoT Build | 0:fa2de1b79154 | 113 | else |
Azure.IoT Build | 0:fa2de1b79154 | 114 | { |
Azure.IoT Build | 0:fa2de1b79154 | 115 | /* Codes_SRS_STRING_07_032: [STRING_construct encounters any error it shall return a NULL value.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 116 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 117 | } |
Azure.IoT Build | 0:fa2de1b79154 | 118 | } |
Azure.IoT Build | 0:fa2de1b79154 | 119 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 120 | } |
Azure.IoT Build | 0:fa2de1b79154 | 121 | |
AzureIoTClient | 25:8507bf644fdf | 122 | #if defined(__GNUC__) |
AzureIoTClient | 25:8507bf644fdf | 123 | __attribute__ ((format (printf, 1, 2))) |
AzureIoTClient | 25:8507bf644fdf | 124 | #endif |
AzureIoTClient | 8:3db46d1e5471 | 125 | STRING_HANDLE STRING_construct_sprintf(const char* format, ...) |
AzureIoTClient | 8:3db46d1e5471 | 126 | { |
AzureIoTClient | 8:3db46d1e5471 | 127 | STRING* result; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 128 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 129 | #ifdef ARDUINO_ARCH_ESP8266 |
Azure.IoT.Build | 16:18e7ebd42bb2 | 130 | size_t maxBufSize = 512; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 131 | char buf[512]; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 132 | #else |
Azure.IoT.Build | 16:18e7ebd42bb2 | 133 | size_t maxBufSize = 0; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 134 | char* buf = NULL; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 135 | #endif |
Azure.IoT.Build | 16:18e7ebd42bb2 | 136 | |
AzureIoTClient | 8:3db46d1e5471 | 137 | if (format != NULL) |
AzureIoTClient | 8:3db46d1e5471 | 138 | { |
AzureIoTClient | 8:3db46d1e5471 | 139 | va_list arg_list; |
AzureIoTClient | 30:ce3813c5a692 | 140 | int length; |
AzureIoTClient | 8:3db46d1e5471 | 141 | va_start(arg_list, format); |
AzureIoTClient | 8:3db46d1e5471 | 142 | |
AzureIoTClient | 8:3db46d1e5471 | 143 | /* Codes_SRS_STRING_07_041: [STRING_construct_sprintf shall determine the size of the resulting string and allocate the necessary memory.] */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 144 | length = vsnprintf(buf, maxBufSize, format, arg_list); |
AzureIoTClient | 8:3db46d1e5471 | 145 | va_end(arg_list); |
AzureIoTClient | 8:3db46d1e5471 | 146 | if (length > 0) |
AzureIoTClient | 8:3db46d1e5471 | 147 | { |
AzureIoTClient | 8:3db46d1e5471 | 148 | result = (STRING*)malloc(sizeof(STRING)); |
AzureIoTClient | 8:3db46d1e5471 | 149 | if (result != NULL) |
AzureIoTClient | 8:3db46d1e5471 | 150 | { |
AzureIoTClient | 8:3db46d1e5471 | 151 | result->s = (char*)malloc(length+1); |
AzureIoTClient | 8:3db46d1e5471 | 152 | if (result->s != NULL) |
AzureIoTClient | 8:3db46d1e5471 | 153 | { |
AzureIoTClient | 8:3db46d1e5471 | 154 | va_start(arg_list, format); |
AzureIoTClient | 8:3db46d1e5471 | 155 | if (vsnprintf(result->s, length+1, format, arg_list) < 0) |
AzureIoTClient | 8:3db46d1e5471 | 156 | { |
AzureIoTClient | 8:3db46d1e5471 | 157 | /* Codes_SRS_STRING_07_040: [If any error is encountered STRING_construct_sprintf shall return NULL.] */ |
AzureIoTClient | 8:3db46d1e5471 | 158 | free(result->s); |
AzureIoTClient | 8:3db46d1e5471 | 159 | free(result); |
AzureIoTClient | 8:3db46d1e5471 | 160 | result = NULL; |
AzureIoTClient | 8:3db46d1e5471 | 161 | LogError("Failure: vsnprintf formatting failed."); |
AzureIoTClient | 8:3db46d1e5471 | 162 | } |
AzureIoTClient | 8:3db46d1e5471 | 163 | va_end(arg_list); |
AzureIoTClient | 8:3db46d1e5471 | 164 | } |
AzureIoTClient | 8:3db46d1e5471 | 165 | else |
AzureIoTClient | 8:3db46d1e5471 | 166 | { |
AzureIoTClient | 8:3db46d1e5471 | 167 | /* Codes_SRS_STRING_07_040: [If any error is encountered STRING_construct_sprintf shall return NULL.] */ |
AzureIoTClient | 8:3db46d1e5471 | 168 | free(result); |
AzureIoTClient | 8:3db46d1e5471 | 169 | result = NULL; |
AzureIoTClient | 8:3db46d1e5471 | 170 | LogError("Failure: allocation failed."); |
AzureIoTClient | 8:3db46d1e5471 | 171 | } |
AzureIoTClient | 8:3db46d1e5471 | 172 | } |
AzureIoTClient | 8:3db46d1e5471 | 173 | else |
AzureIoTClient | 8:3db46d1e5471 | 174 | { |
AzureIoTClient | 8:3db46d1e5471 | 175 | LogError("Failure: allocation failed."); |
AzureIoTClient | 8:3db46d1e5471 | 176 | } |
AzureIoTClient | 8:3db46d1e5471 | 177 | } |
AzureIoTClient | 8:3db46d1e5471 | 178 | else if (length == 0) |
AzureIoTClient | 8:3db46d1e5471 | 179 | { |
AzureIoTClient | 8:3db46d1e5471 | 180 | result = (STRING*)STRING_new(); |
AzureIoTClient | 8:3db46d1e5471 | 181 | } |
AzureIoTClient | 8:3db46d1e5471 | 182 | else |
AzureIoTClient | 8:3db46d1e5471 | 183 | { |
AzureIoTClient | 8:3db46d1e5471 | 184 | /* Codes_SRS_STRING_07_039: [If the parameter format is NULL then STRING_construct_sprintf shall return NULL.] */ |
AzureIoTClient | 8:3db46d1e5471 | 185 | result = NULL; |
AzureIoTClient | 8:3db46d1e5471 | 186 | LogError("Failure: vsnprintf return 0 length"); |
AzureIoTClient | 8:3db46d1e5471 | 187 | } |
AzureIoTClient | 8:3db46d1e5471 | 188 | } |
AzureIoTClient | 8:3db46d1e5471 | 189 | else |
AzureIoTClient | 8:3db46d1e5471 | 190 | { |
AzureIoTClient | 8:3db46d1e5471 | 191 | LogError("Failure: invalid argument."); |
AzureIoTClient | 8:3db46d1e5471 | 192 | result = NULL; |
AzureIoTClient | 8:3db46d1e5471 | 193 | } |
AzureIoTClient | 8:3db46d1e5471 | 194 | /* Codes_SRS_STRING_07_045: [STRING_construct_sprintf shall allocate a new string with the value of the specified printf formated const char. ] */ |
AzureIoTClient | 8:3db46d1e5471 | 195 | return (STRING_HANDLE)result; |
AzureIoTClient | 8:3db46d1e5471 | 196 | } |
AzureIoTClient | 8:3db46d1e5471 | 197 | |
Azure.IoT Build | 0:fa2de1b79154 | 198 | /*this function will return a new STRING with the memory for the actual string passed in as a parameter.*/ |
Azure.IoT Build | 0:fa2de1b79154 | 199 | /*return NULL if it fails.*/ |
Azure.IoT Build | 0:fa2de1b79154 | 200 | /* The supplied memory must have been allocated with malloc! */ |
Azure.IoT Build | 0:fa2de1b79154 | 201 | /* Codes_SRS_STRING_07_006: [STRING_new_with_memory shall return a STRING_HANDLE by using the supplied char* memory.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 202 | STRING_HANDLE STRING_new_with_memory(const char* memory) |
Azure.IoT Build | 0:fa2de1b79154 | 203 | { |
Azure.IoT Build | 0:fa2de1b79154 | 204 | STRING* result; |
Azure.IoT Build | 0:fa2de1b79154 | 205 | if (memory == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 206 | { |
Azure.IoT Build | 0:fa2de1b79154 | 207 | /* Codes_SRS_STRING_07_007: [STRING_new_with_memory shall return a NULL STRING_HANDLE if the supplied char* is NULL.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 208 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 209 | } |
Azure.IoT Build | 0:fa2de1b79154 | 210 | else |
Azure.IoT Build | 0:fa2de1b79154 | 211 | { |
Azure.IoT Build | 0:fa2de1b79154 | 212 | if ((result = (STRING*)malloc(sizeof(STRING))) != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 213 | { |
Azure.IoT Build | 0:fa2de1b79154 | 214 | result->s = (char*)memory; |
Azure.IoT Build | 0:fa2de1b79154 | 215 | } |
Azure.IoT Build | 0:fa2de1b79154 | 216 | } |
Azure.IoT Build | 0:fa2de1b79154 | 217 | return (STRING_HANDLE)result; |
Azure.IoT Build | 0:fa2de1b79154 | 218 | } |
Azure.IoT Build | 0:fa2de1b79154 | 219 | |
Azure.IoT Build | 0:fa2de1b79154 | 220 | /* Codes_SRS_STRING_07_008: [STRING_new_quoted shall return a valid STRING_HANDLE Copying the supplied const char* value surrounded by quotes.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 221 | STRING_HANDLE STRING_new_quoted(const char* source) |
Azure.IoT Build | 0:fa2de1b79154 | 222 | { |
Azure.IoT Build | 0:fa2de1b79154 | 223 | STRING* result; |
Azure.IoT Build | 0:fa2de1b79154 | 224 | if (source == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 225 | { |
Azure.IoT Build | 0:fa2de1b79154 | 226 | /* Codes_SRS_STRING_07_009: [STRING_new_quoted shall return a NULL STRING_HANDLE if the supplied const char* is NULL.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 227 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 228 | } |
Azure.IoT Build | 0:fa2de1b79154 | 229 | else if ((result = (STRING*)malloc(sizeof(STRING))) != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 230 | { |
Azure.IoT Build | 0:fa2de1b79154 | 231 | size_t sourceLength = strlen(source); |
Azure.IoT Build | 0:fa2de1b79154 | 232 | if ((result->s = (char*)malloc(sourceLength + 3)) != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 233 | { |
Azure.IoT Build | 0:fa2de1b79154 | 234 | result->s[0] = '"'; |
AzureIoTClient | 19:2e0811512ceb | 235 | (void)memcpy(result->s + 1, source, sourceLength); |
Azure.IoT Build | 0:fa2de1b79154 | 236 | result->s[sourceLength + 1] = '"'; |
Azure.IoT Build | 0:fa2de1b79154 | 237 | result->s[sourceLength + 2] = '\0'; |
Azure.IoT Build | 0:fa2de1b79154 | 238 | } |
Azure.IoT Build | 0:fa2de1b79154 | 239 | else |
Azure.IoT Build | 0:fa2de1b79154 | 240 | { |
Azure.IoT Build | 0:fa2de1b79154 | 241 | /* Codes_SRS_STRING_07_031: [STRING_new_quoted shall return a NULL STRING_HANDLE if any error is encountered.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 242 | free(result); |
Azure.IoT Build | 0:fa2de1b79154 | 243 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 244 | } |
Azure.IoT Build | 0:fa2de1b79154 | 245 | } |
Azure.IoT Build | 0:fa2de1b79154 | 246 | return (STRING_HANDLE)result; |
Azure.IoT Build | 0:fa2de1b79154 | 247 | } |
Azure.IoT Build | 0:fa2de1b79154 | 248 | |
Azure.IoT Build | 0:fa2de1b79154 | 249 | /*this function takes a regular const char* and turns in into "this is a\"JSON\" strings\u0008" (starting and ending quote included)*/ |
Azure.IoT Build | 0:fa2de1b79154 | 250 | /*the newly created handle needs to be disposed of with STRING_delete*/ |
Azure.IoT Build | 0:fa2de1b79154 | 251 | /*returns NULL if there are errors*/ |
Azure.IoT Build | 0:fa2de1b79154 | 252 | STRING_HANDLE STRING_new_JSON(const char* source) |
Azure.IoT Build | 0:fa2de1b79154 | 253 | { |
Azure.IoT Build | 0:fa2de1b79154 | 254 | STRING* result; |
Azure.IoT Build | 0:fa2de1b79154 | 255 | if (source == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 256 | { |
Azure.IoT Build | 0:fa2de1b79154 | 257 | /*Codes_SRS_STRING_02_011: [If source is NULL then STRING_new_JSON shall return NULL.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 258 | result = NULL; |
AzureIoTClient | 1:9190c0f4d23a | 259 | LogError("invalid arg (NULL)"); |
Azure.IoT Build | 0:fa2de1b79154 | 260 | } |
Azure.IoT Build | 0:fa2de1b79154 | 261 | else |
Azure.IoT Build | 0:fa2de1b79154 | 262 | { |
Azure.IoT Build | 0:fa2de1b79154 | 263 | size_t i; |
Azure.IoT Build | 0:fa2de1b79154 | 264 | size_t nControlCharacters = 0; /*counts how many characters are to be expanded from 1 character to \uxxxx (6 characters)*/ |
Azure.IoT Build | 0:fa2de1b79154 | 265 | size_t nEscapeCharacters = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 266 | size_t vlen = strlen(source); |
Azure.IoT Build | 0:fa2de1b79154 | 267 | |
Azure.IoT Build | 0:fa2de1b79154 | 268 | for (i = 0; i < vlen; i++) |
Azure.IoT Build | 0:fa2de1b79154 | 269 | { |
Azure.IoT Build | 0:fa2de1b79154 | 270 | /*Codes_SRS_STRING_02_014: [If any character has the value outside [1...127] then STRING_new_JSON shall fail and return NULL.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 271 | if ((unsigned char)source[i] >= 128) /*this be a UNICODE character begin*/ |
Azure.IoT Build | 0:fa2de1b79154 | 272 | { |
Azure.IoT Build | 0:fa2de1b79154 | 273 | break; |
Azure.IoT Build | 0:fa2de1b79154 | 274 | } |
Azure.IoT Build | 0:fa2de1b79154 | 275 | else |
Azure.IoT Build | 0:fa2de1b79154 | 276 | { |
Azure.IoT Build | 0:fa2de1b79154 | 277 | if (source[i] <= 0x1F) |
Azure.IoT Build | 0:fa2de1b79154 | 278 | { |
Azure.IoT Build | 0:fa2de1b79154 | 279 | nControlCharacters++; |
Azure.IoT Build | 0:fa2de1b79154 | 280 | } |
Azure.IoT Build | 0:fa2de1b79154 | 281 | else if ( |
Azure.IoT Build | 0:fa2de1b79154 | 282 | (source[i] == '"') || |
Azure.IoT Build | 0:fa2de1b79154 | 283 | (source[i] == '\\') || |
Azure.IoT Build | 0:fa2de1b79154 | 284 | (source[i] == '/') |
Azure.IoT Build | 0:fa2de1b79154 | 285 | ) |
Azure.IoT Build | 0:fa2de1b79154 | 286 | { |
Azure.IoT Build | 0:fa2de1b79154 | 287 | nEscapeCharacters++; |
Azure.IoT Build | 0:fa2de1b79154 | 288 | } |
Azure.IoT Build | 0:fa2de1b79154 | 289 | } |
Azure.IoT Build | 0:fa2de1b79154 | 290 | } |
Azure.IoT Build | 0:fa2de1b79154 | 291 | |
Azure.IoT Build | 0:fa2de1b79154 | 292 | if (i < vlen) |
Azure.IoT Build | 0:fa2de1b79154 | 293 | { |
Azure.IoT Build | 0:fa2de1b79154 | 294 | result = NULL; |
AzureIoTClient | 1:9190c0f4d23a | 295 | LogError("invalid character in input string"); |
Azure.IoT Build | 0:fa2de1b79154 | 296 | } |
Azure.IoT Build | 0:fa2de1b79154 | 297 | else |
Azure.IoT Build | 0:fa2de1b79154 | 298 | { |
Azure.IoT Build | 0:fa2de1b79154 | 299 | if ((result = (STRING*)malloc(sizeof(STRING))) == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 300 | { |
Azure.IoT Build | 0:fa2de1b79154 | 301 | /*Codes_SRS_STRING_02_021: [If the complete JSON representation cannot be produced, then STRING_new_JSON shall fail and return NULL.] */ |
AzureIoTClient | 1:9190c0f4d23a | 302 | LogError("malloc failure"); |
Azure.IoT Build | 0:fa2de1b79154 | 303 | } |
Azure.IoT Build | 0:fa2de1b79154 | 304 | else if ((result->s = (char*)malloc(vlen + 5 * nControlCharacters + nEscapeCharacters + 3)) == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 305 | { |
Azure.IoT Build | 0:fa2de1b79154 | 306 | /*Codes_SRS_STRING_02_021: [If the complete JSON representation cannot be produced, then STRING_new_JSON shall fail and return NULL.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 307 | free(result); |
Azure.IoT Build | 0:fa2de1b79154 | 308 | result = NULL; |
AzureIoTClient | 1:9190c0f4d23a | 309 | LogError("malloc failed"); |
Azure.IoT Build | 0:fa2de1b79154 | 310 | } |
Azure.IoT Build | 0:fa2de1b79154 | 311 | else |
Azure.IoT Build | 0:fa2de1b79154 | 312 | { |
Azure.IoT Build | 0:fa2de1b79154 | 313 | size_t pos = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 314 | /*Codes_SRS_STRING_02_012: [The string shall begin with the quote character.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 315 | result->s[pos++] = '"'; |
Azure.IoT Build | 0:fa2de1b79154 | 316 | for (i = 0; i < vlen; i++) |
Azure.IoT Build | 0:fa2de1b79154 | 317 | { |
Azure.IoT Build | 0:fa2de1b79154 | 318 | if (source[i] <= 0x1F) |
Azure.IoT Build | 0:fa2de1b79154 | 319 | { |
Azure.IoT Build | 0:fa2de1b79154 | 320 | /*Codes_SRS_STRING_02_019: [If the character code is less than 0x20 then it shall be represented as \u00xx, where xx is the hex representation of the character code.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 321 | result->s[pos++] = '\\'; |
Azure.IoT Build | 0:fa2de1b79154 | 322 | result->s[pos++] = 'u'; |
Azure.IoT Build | 0:fa2de1b79154 | 323 | result->s[pos++] = '0'; |
Azure.IoT Build | 0:fa2de1b79154 | 324 | result->s[pos++] = '0'; |
Azure.IoT Build | 0:fa2de1b79154 | 325 | result->s[pos++] = hexToASCII[(source[i] & 0xF0) >> 4]; /*high nibble*/ |
Azure.IoT Build | 0:fa2de1b79154 | 326 | result->s[pos++] = hexToASCII[source[i] & 0x0F]; /*low nibble*/ |
Azure.IoT Build | 0:fa2de1b79154 | 327 | } |
Azure.IoT Build | 0:fa2de1b79154 | 328 | else if (source[i] == '"') |
Azure.IoT Build | 0:fa2de1b79154 | 329 | { |
Azure.IoT Build | 0:fa2de1b79154 | 330 | /*Codes_SRS_STRING_02_016: [If the character is " (quote) then it shall be repsented as \".] */ |
Azure.IoT Build | 0:fa2de1b79154 | 331 | result->s[pos++] = '\\'; |
Azure.IoT Build | 0:fa2de1b79154 | 332 | result->s[pos++] = '"'; |
Azure.IoT Build | 0:fa2de1b79154 | 333 | } |
Azure.IoT Build | 0:fa2de1b79154 | 334 | else if (source[i] == '\\') |
Azure.IoT Build | 0:fa2de1b79154 | 335 | { |
Azure.IoT Build | 0:fa2de1b79154 | 336 | /*Codes_SRS_STRING_02_017: [If the character is \ (backslash) then it shall represented as \\.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 337 | result->s[pos++] = '\\'; |
Azure.IoT Build | 0:fa2de1b79154 | 338 | result->s[pos++] = '\\'; |
Azure.IoT Build | 0:fa2de1b79154 | 339 | } |
Azure.IoT Build | 0:fa2de1b79154 | 340 | else if (source[i] == '/') |
Azure.IoT Build | 0:fa2de1b79154 | 341 | { |
Azure.IoT Build | 0:fa2de1b79154 | 342 | /*Codes_SRS_STRING_02_018: [If the character is / (slash) then it shall be represented as \/.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 343 | result->s[pos++] = '\\'; |
Azure.IoT Build | 0:fa2de1b79154 | 344 | result->s[pos++] = '/'; |
Azure.IoT Build | 0:fa2de1b79154 | 345 | } |
Azure.IoT Build | 0:fa2de1b79154 | 346 | else |
Azure.IoT Build | 0:fa2de1b79154 | 347 | { |
Azure.IoT Build | 0:fa2de1b79154 | 348 | /*Codes_SRS_STRING_02_013: [The string shall copy the characters of source "as they are" (until the '\0' character) with the following exceptions:] */ |
Azure.IoT Build | 0:fa2de1b79154 | 349 | result->s[pos++] = source[i]; |
Azure.IoT Build | 0:fa2de1b79154 | 350 | } |
Azure.IoT Build | 0:fa2de1b79154 | 351 | } |
Azure.IoT Build | 0:fa2de1b79154 | 352 | /*Codes_SRS_STRING_02_020: [The string shall end with " (quote).] */ |
Azure.IoT Build | 0:fa2de1b79154 | 353 | result->s[pos++] = '"'; |
Azure.IoT Build | 0:fa2de1b79154 | 354 | /*zero terminating it*/ |
Azure.IoT Build | 0:fa2de1b79154 | 355 | result->s[pos] = '\0'; |
Azure.IoT Build | 0:fa2de1b79154 | 356 | } |
Azure.IoT Build | 0:fa2de1b79154 | 357 | } |
Azure.IoT Build | 0:fa2de1b79154 | 358 | |
Azure.IoT Build | 0:fa2de1b79154 | 359 | } |
Azure.IoT Build | 0:fa2de1b79154 | 360 | return (STRING_HANDLE)result; |
Azure.IoT Build | 0:fa2de1b79154 | 361 | } |
Azure.IoT Build | 0:fa2de1b79154 | 362 | |
Azure.IoT Build | 0:fa2de1b79154 | 363 | /*this function will concatenate to the string s1 the string s2, resulting in s1+s2*/ |
Azure.IoT Build | 0:fa2de1b79154 | 364 | /*returns 0 if success*/ |
Azure.IoT Build | 0:fa2de1b79154 | 365 | /*any other error code is failure*/ |
Azure.IoT Build | 0:fa2de1b79154 | 366 | /* Codes_SRS_STRING_07_012: [STRING_concat shall concatenate the given STRING_HANDLE and the const char* value and place the value in the handle.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 367 | int STRING_concat(STRING_HANDLE handle, const char* s2) |
Azure.IoT Build | 0:fa2de1b79154 | 368 | { |
Azure.IoT Build | 0:fa2de1b79154 | 369 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 370 | if ((handle == NULL) || (s2 == NULL)) |
Azure.IoT Build | 0:fa2de1b79154 | 371 | { |
Azure.IoT Build | 0:fa2de1b79154 | 372 | /* Codes_SRS_STRING_07_013: [STRING_concat shall return a nonzero number if an error is encountered.] */ |
AzureIoTClient | 21:b92006c5b9ff | 373 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 374 | } |
Azure.IoT Build | 0:fa2de1b79154 | 375 | else |
Azure.IoT Build | 0:fa2de1b79154 | 376 | { |
Azure.IoT Build | 0:fa2de1b79154 | 377 | STRING* s1 = (STRING*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 378 | size_t s1Length = strlen(s1->s); |
Azure.IoT Build | 0:fa2de1b79154 | 379 | size_t s2Length = strlen(s2); |
Azure.IoT Build | 0:fa2de1b79154 | 380 | char* temp = (char*)realloc(s1->s, s1Length + s2Length + 1); |
Azure.IoT Build | 0:fa2de1b79154 | 381 | if (temp == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 382 | { |
Azure.IoT Build | 0:fa2de1b79154 | 383 | /* Codes_SRS_STRING_07_013: [STRING_concat shall return a nonzero number if an error is encountered.] */ |
AzureIoTClient | 21:b92006c5b9ff | 384 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 385 | } |
Azure.IoT Build | 0:fa2de1b79154 | 386 | else |
Azure.IoT Build | 0:fa2de1b79154 | 387 | { |
Azure.IoT Build | 0:fa2de1b79154 | 388 | s1->s = temp; |
AzureIoTClient | 19:2e0811512ceb | 389 | (void)memcpy(s1->s + s1Length, s2, s2Length + 1); |
Azure.IoT Build | 0:fa2de1b79154 | 390 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 391 | } |
Azure.IoT Build | 0:fa2de1b79154 | 392 | } |
Azure.IoT Build | 0:fa2de1b79154 | 393 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 394 | } |
Azure.IoT Build | 0:fa2de1b79154 | 395 | |
Azure.IoT Build | 0:fa2de1b79154 | 396 | /*this function will concatenate to the string s1 the string s2, resulting in s1+s2*/ |
Azure.IoT Build | 0:fa2de1b79154 | 397 | /*returns 0 if success*/ |
Azure.IoT Build | 0:fa2de1b79154 | 398 | /*any other error code is failure*/ |
Azure.IoT Build | 0:fa2de1b79154 | 399 | /* Codes_SRS_STRING_07_034: [String_Concat_with_STRING shall concatenate a given STRING_HANDLE variable with a source STRING_HANDLE.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 400 | int STRING_concat_with_STRING(STRING_HANDLE s1, STRING_HANDLE s2) |
Azure.IoT Build | 0:fa2de1b79154 | 401 | { |
Azure.IoT Build | 0:fa2de1b79154 | 402 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 403 | if ((s1 == NULL) || (s2 == NULL)) |
Azure.IoT Build | 0:fa2de1b79154 | 404 | { |
Azure.IoT Build | 0:fa2de1b79154 | 405 | /* Codes_SRS_STRING_07_035: [String_Concat_with_STRING shall return a nonzero number if an error is encountered.] */ |
AzureIoTClient | 21:b92006c5b9ff | 406 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 407 | } |
Azure.IoT Build | 0:fa2de1b79154 | 408 | else |
Azure.IoT Build | 0:fa2de1b79154 | 409 | { |
Azure.IoT Build | 0:fa2de1b79154 | 410 | STRING* dest = (STRING*)s1; |
Azure.IoT Build | 0:fa2de1b79154 | 411 | STRING* src = (STRING*)s2; |
Azure.IoT Build | 0:fa2de1b79154 | 412 | |
Azure.IoT Build | 0:fa2de1b79154 | 413 | size_t s1Length = strlen(dest->s); |
Azure.IoT Build | 0:fa2de1b79154 | 414 | size_t s2Length = strlen(src->s); |
Azure.IoT Build | 0:fa2de1b79154 | 415 | char* temp = (char*)realloc(dest->s, s1Length + s2Length + 1); |
Azure.IoT Build | 0:fa2de1b79154 | 416 | if (temp == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 417 | { |
Azure.IoT Build | 0:fa2de1b79154 | 418 | /* Codes_SRS_STRING_07_035: [String_Concat_with_STRING shall return a nonzero number if an error is encountered.] */ |
AzureIoTClient | 21:b92006c5b9ff | 419 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 420 | } |
Azure.IoT Build | 0:fa2de1b79154 | 421 | else |
Azure.IoT Build | 0:fa2de1b79154 | 422 | { |
Azure.IoT Build | 0:fa2de1b79154 | 423 | dest->s = temp; |
Azure.IoT Build | 0:fa2de1b79154 | 424 | /* Codes_SRS_STRING_07_034: [String_Concat_with_STRING shall concatenate a given STRING_HANDLE variable with a source STRING_HANDLE.] */ |
AzureIoTClient | 19:2e0811512ceb | 425 | (void)memcpy(dest->s + s1Length, src->s, s2Length + 1); |
Azure.IoT Build | 0:fa2de1b79154 | 426 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 427 | } |
Azure.IoT Build | 0:fa2de1b79154 | 428 | } |
Azure.IoT Build | 0:fa2de1b79154 | 429 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 430 | } |
Azure.IoT Build | 0:fa2de1b79154 | 431 | |
Azure.IoT Build | 0:fa2de1b79154 | 432 | /*this function will copy the string from s2 to s1*/ |
Azure.IoT Build | 0:fa2de1b79154 | 433 | /*returns 0 if success*/ |
Azure.IoT Build | 0:fa2de1b79154 | 434 | /*any other error code is failure*/ |
Azure.IoT Build | 0:fa2de1b79154 | 435 | /* Codes_SRS_STRING_07_016: [STRING_copy shall copy the const char* into the supplied STRING_HANDLE.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 436 | int STRING_copy(STRING_HANDLE handle, const char* s2) |
Azure.IoT Build | 0:fa2de1b79154 | 437 | { |
Azure.IoT Build | 0:fa2de1b79154 | 438 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 439 | if ((handle == NULL) || (s2 == NULL)) |
Azure.IoT Build | 0:fa2de1b79154 | 440 | { |
Azure.IoT Build | 0:fa2de1b79154 | 441 | /* Codes_SRS_STRING_07_017: [STRING_copy shall return a nonzero value if any of the supplied parameters are NULL.] */ |
AzureIoTClient | 21:b92006c5b9ff | 442 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 443 | } |
Azure.IoT Build | 0:fa2de1b79154 | 444 | else |
Azure.IoT Build | 0:fa2de1b79154 | 445 | { |
Azure.IoT Build | 0:fa2de1b79154 | 446 | STRING* s1 = (STRING*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 447 | /* Codes_SRS_STRING_07_026: [If the underlying char* refered to by s1 handle is equal to char* s2 than STRING_copy shall be a noop and return 0.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 448 | if (s1->s != s2) |
Azure.IoT Build | 0:fa2de1b79154 | 449 | { |
Azure.IoT Build | 0:fa2de1b79154 | 450 | size_t s2Length = strlen(s2); |
Azure.IoT Build | 0:fa2de1b79154 | 451 | char* temp = (char*)realloc(s1->s, s2Length + 1); |
Azure.IoT Build | 0:fa2de1b79154 | 452 | if (temp == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 453 | { |
Azure.IoT Build | 0:fa2de1b79154 | 454 | /* Codes_SRS_STRING_07_027: [STRING_copy shall return a nonzero value if any error is encountered.] */ |
AzureIoTClient | 21:b92006c5b9ff | 455 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 456 | } |
Azure.IoT Build | 0:fa2de1b79154 | 457 | else |
Azure.IoT Build | 0:fa2de1b79154 | 458 | { |
Azure.IoT Build | 0:fa2de1b79154 | 459 | s1->s = temp; |
Azure.IoT Build | 0:fa2de1b79154 | 460 | memmove(s1->s, s2, s2Length + 1); |
Azure.IoT Build | 0:fa2de1b79154 | 461 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 462 | } |
Azure.IoT Build | 0:fa2de1b79154 | 463 | } |
Azure.IoT Build | 0:fa2de1b79154 | 464 | else |
Azure.IoT Build | 0:fa2de1b79154 | 465 | { |
Azure.IoT Build | 0:fa2de1b79154 | 466 | /* Codes_SRS_STRING_07_033: [If overlapping pointer address is given to STRING_copy the behavior is undefined.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 467 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 468 | } |
Azure.IoT Build | 0:fa2de1b79154 | 469 | } |
Azure.IoT Build | 0:fa2de1b79154 | 470 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 471 | } |
Azure.IoT Build | 0:fa2de1b79154 | 472 | |
Azure.IoT Build | 0:fa2de1b79154 | 473 | /*this function will copy n chars from s2 to the string s1, resulting in n chars only from s2 being stored in s1.*/ |
Azure.IoT Build | 0:fa2de1b79154 | 474 | /*returns 0 if success*/ |
Azure.IoT Build | 0:fa2de1b79154 | 475 | /*any other error code is failure*/ |
Azure.IoT Build | 0:fa2de1b79154 | 476 | /* Codes_SRS_STRING_07_018: [STRING_copy_n shall copy the number of characters in const char* or the size_t whichever is lesser.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 477 | int STRING_copy_n(STRING_HANDLE handle, const char* s2, size_t n) |
Azure.IoT Build | 0:fa2de1b79154 | 478 | { |
Azure.IoT Build | 0:fa2de1b79154 | 479 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 480 | if ((handle == NULL) || (s2 == NULL)) |
Azure.IoT Build | 0:fa2de1b79154 | 481 | { |
Azure.IoT Build | 0:fa2de1b79154 | 482 | /* Codes_SRS_STRING_07_019: [STRING_copy_n shall return a nonzero value if STRING_HANDLE or const char* is NULL.] */ |
AzureIoTClient | 21:b92006c5b9ff | 483 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 484 | } |
Azure.IoT Build | 0:fa2de1b79154 | 485 | else |
Azure.IoT Build | 0:fa2de1b79154 | 486 | { |
Azure.IoT Build | 0:fa2de1b79154 | 487 | STRING* s1 = (STRING*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 488 | size_t s2Length = strlen(s2); |
Azure.IoT Build | 0:fa2de1b79154 | 489 | char* temp; |
Azure.IoT Build | 0:fa2de1b79154 | 490 | if (s2Length > n) |
Azure.IoT Build | 0:fa2de1b79154 | 491 | { |
Azure.IoT Build | 0:fa2de1b79154 | 492 | s2Length = n; |
Azure.IoT Build | 0:fa2de1b79154 | 493 | } |
Azure.IoT Build | 0:fa2de1b79154 | 494 | |
Azure.IoT Build | 0:fa2de1b79154 | 495 | temp = (char*)realloc(s1->s, s2Length + 1); |
Azure.IoT Build | 0:fa2de1b79154 | 496 | if (temp == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 497 | { |
Azure.IoT Build | 0:fa2de1b79154 | 498 | /* Codes_SRS_STRING_07_028: [STRING_copy_n shall return a nonzero value if any error is encountered.] */ |
AzureIoTClient | 21:b92006c5b9ff | 499 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 500 | } |
Azure.IoT Build | 0:fa2de1b79154 | 501 | else |
Azure.IoT Build | 0:fa2de1b79154 | 502 | { |
Azure.IoT Build | 0:fa2de1b79154 | 503 | s1->s = temp; |
AzureIoTClient | 19:2e0811512ceb | 504 | (void)memcpy(s1->s, s2, s2Length); |
Azure.IoT Build | 0:fa2de1b79154 | 505 | s1->s[s2Length] = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 506 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 507 | } |
Azure.IoT Build | 0:fa2de1b79154 | 508 | |
Azure.IoT Build | 0:fa2de1b79154 | 509 | } |
Azure.IoT Build | 0:fa2de1b79154 | 510 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 511 | } |
Azure.IoT Build | 0:fa2de1b79154 | 512 | |
AzureIoTClient | 25:8507bf644fdf | 513 | #if defined(__GNUC__) |
AzureIoTClient | 25:8507bf644fdf | 514 | __attribute__ ((format (printf, 2, 3))) |
AzureIoTClient | 25:8507bf644fdf | 515 | #endif |
AzureIoTClient | 8:3db46d1e5471 | 516 | int STRING_sprintf(STRING_HANDLE handle, const char* format, ...) |
AzureIoTClient | 8:3db46d1e5471 | 517 | { |
AzureIoTClient | 8:3db46d1e5471 | 518 | int result; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 519 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 520 | #ifdef ARDUINO_ARCH_ESP8266 |
Azure.IoT.Build | 16:18e7ebd42bb2 | 521 | size_t maxBufSize = 512; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 522 | char buf[512]; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 523 | #else |
Azure.IoT.Build | 16:18e7ebd42bb2 | 524 | size_t maxBufSize = 0; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 525 | char* buf = NULL; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 526 | #endif |
Azure.IoT.Build | 16:18e7ebd42bb2 | 527 | |
AzureIoTClient | 8:3db46d1e5471 | 528 | if (handle == NULL || format == NULL) |
AzureIoTClient | 8:3db46d1e5471 | 529 | { |
AzureIoTClient | 8:3db46d1e5471 | 530 | /* Codes_SRS_STRING_07_042: [if the parameters s1 or format are NULL then STRING_sprintf shall return non zero value.] */ |
AzureIoTClient | 8:3db46d1e5471 | 531 | LogError("Invalid arg (NULL)"); |
AzureIoTClient | 21:b92006c5b9ff | 532 | result = __FAILURE__; |
AzureIoTClient | 8:3db46d1e5471 | 533 | } |
AzureIoTClient | 8:3db46d1e5471 | 534 | else |
AzureIoTClient | 8:3db46d1e5471 | 535 | { |
AzureIoTClient | 8:3db46d1e5471 | 536 | va_list arg_list; |
AzureIoTClient | 30:ce3813c5a692 | 537 | int s2Length; |
AzureIoTClient | 8:3db46d1e5471 | 538 | va_start(arg_list, format); |
AzureIoTClient | 8:3db46d1e5471 | 539 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 540 | s2Length = vsnprintf(buf, maxBufSize, format, arg_list); |
AzureIoTClient | 8:3db46d1e5471 | 541 | va_end(arg_list); |
AzureIoTClient | 8:3db46d1e5471 | 542 | if (s2Length < 0) |
AzureIoTClient | 8:3db46d1e5471 | 543 | { |
AzureIoTClient | 8:3db46d1e5471 | 544 | /* Codes_SRS_STRING_07_043: [If any error is encountered STRING_sprintf shall return a non zero value.] */ |
AzureIoTClient | 8:3db46d1e5471 | 545 | LogError("Failure vsnprintf return < 0"); |
AzureIoTClient | 21:b92006c5b9ff | 546 | result = __FAILURE__; |
AzureIoTClient | 8:3db46d1e5471 | 547 | } |
AzureIoTClient | 8:3db46d1e5471 | 548 | else if (s2Length == 0) |
AzureIoTClient | 8:3db46d1e5471 | 549 | { |
AzureIoTClient | 8:3db46d1e5471 | 550 | // Don't need to reallocate and nothing should be added |
AzureIoTClient | 8:3db46d1e5471 | 551 | result = 0; |
AzureIoTClient | 8:3db46d1e5471 | 552 | } |
AzureIoTClient | 8:3db46d1e5471 | 553 | else |
AzureIoTClient | 8:3db46d1e5471 | 554 | { |
AzureIoTClient | 8:3db46d1e5471 | 555 | STRING* s1 = (STRING*)handle; |
AzureIoTClient | 30:ce3813c5a692 | 556 | char* temp; |
AzureIoTClient | 8:3db46d1e5471 | 557 | size_t s1Length = strlen(s1->s); |
AzureIoTClient | 9:079c39803432 | 558 | temp = (char*)realloc(s1->s, s1Length + s2Length + 1); |
AzureIoTClient | 8:3db46d1e5471 | 559 | if (temp != NULL) |
AzureIoTClient | 8:3db46d1e5471 | 560 | { |
AzureIoTClient | 8:3db46d1e5471 | 561 | s1->s = temp; |
AzureIoTClient | 8:3db46d1e5471 | 562 | va_start(arg_list, format); |
AzureIoTClient | 8:3db46d1e5471 | 563 | if (vsnprintf(s1->s + s1Length, s1Length + s2Length + 1, format, arg_list) < 0) |
AzureIoTClient | 8:3db46d1e5471 | 564 | { |
AzureIoTClient | 8:3db46d1e5471 | 565 | /* Codes_SRS_STRING_07_043: [If any error is encountered STRING_sprintf shall return a non zero value.] */ |
AzureIoTClient | 8:3db46d1e5471 | 566 | LogError("Failure vsnprintf formatting error"); |
AzureIoTClient | 8:3db46d1e5471 | 567 | s1->s[s1Length] = '\0'; |
AzureIoTClient | 21:b92006c5b9ff | 568 | result = __FAILURE__; |
AzureIoTClient | 8:3db46d1e5471 | 569 | } |
AzureIoTClient | 8:3db46d1e5471 | 570 | else |
AzureIoTClient | 8:3db46d1e5471 | 571 | { |
AzureIoTClient | 8:3db46d1e5471 | 572 | /* Codes_SRS_STRING_07_044: [On success STRING_sprintf shall return 0.]*/ |
AzureIoTClient | 8:3db46d1e5471 | 573 | result = 0; |
AzureIoTClient | 8:3db46d1e5471 | 574 | } |
AzureIoTClient | 8:3db46d1e5471 | 575 | va_end(arg_list); |
AzureIoTClient | 8:3db46d1e5471 | 576 | } |
AzureIoTClient | 8:3db46d1e5471 | 577 | else |
AzureIoTClient | 8:3db46d1e5471 | 578 | { |
AzureIoTClient | 8:3db46d1e5471 | 579 | /* Codes_SRS_STRING_07_043: [If any error is encountered STRING_sprintf shall return a non zero value.] */ |
AzureIoTClient | 8:3db46d1e5471 | 580 | LogError("Failure unable to reallocate memory"); |
AzureIoTClient | 21:b92006c5b9ff | 581 | result = __FAILURE__; |
AzureIoTClient | 8:3db46d1e5471 | 582 | } |
AzureIoTClient | 8:3db46d1e5471 | 583 | } |
AzureIoTClient | 8:3db46d1e5471 | 584 | } |
AzureIoTClient | 8:3db46d1e5471 | 585 | return result; |
AzureIoTClient | 8:3db46d1e5471 | 586 | } |
AzureIoTClient | 8:3db46d1e5471 | 587 | |
Azure.IoT Build | 0:fa2de1b79154 | 588 | /*this function will quote the string passed as argument string =>"string"*/ |
Azure.IoT Build | 0:fa2de1b79154 | 589 | /*returns 0 if success*/ /*doesn't change the string otherwise*/ |
Azure.IoT Build | 0:fa2de1b79154 | 590 | /*any other error code is failure*/ |
Azure.IoT Build | 0:fa2de1b79154 | 591 | /* Codes_SRS_STRING_07_014: [STRING_quote shall "quote" the supplied STRING_HANDLE and return 0 on success.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 592 | int STRING_quote(STRING_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 593 | { |
Azure.IoT Build | 0:fa2de1b79154 | 594 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 595 | if (handle == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 596 | { |
Azure.IoT Build | 0:fa2de1b79154 | 597 | /* Codes_SRS_STRING_07_015: [STRING_quote shall return a nonzero value if any of the supplied parameters are NULL.] */ |
AzureIoTClient | 21:b92006c5b9ff | 598 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 599 | } |
Azure.IoT Build | 0:fa2de1b79154 | 600 | else |
Azure.IoT Build | 0:fa2de1b79154 | 601 | { |
Azure.IoT Build | 0:fa2de1b79154 | 602 | STRING* s1 = (STRING*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 603 | size_t s1Length = strlen(s1->s); |
Azure.IoT Build | 0:fa2de1b79154 | 604 | char* temp = (char*)realloc(s1->s, s1Length + 2 + 1);/*2 because 2 quotes, 1 because '\0'*/ |
Azure.IoT Build | 0:fa2de1b79154 | 605 | if (temp == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 606 | { |
Azure.IoT Build | 0:fa2de1b79154 | 607 | /* Codes_SRS_STRING_07_029: [STRING_quote shall return a nonzero value if any error is encountered.] */ |
AzureIoTClient | 21:b92006c5b9ff | 608 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 609 | } |
Azure.IoT Build | 0:fa2de1b79154 | 610 | else |
Azure.IoT Build | 0:fa2de1b79154 | 611 | { |
Azure.IoT Build | 0:fa2de1b79154 | 612 | s1->s = temp; |
Azure.IoT Build | 0:fa2de1b79154 | 613 | memmove(s1->s + 1, s1->s, s1Length); |
Azure.IoT Build | 0:fa2de1b79154 | 614 | s1->s[0] = '"'; |
Azure.IoT Build | 0:fa2de1b79154 | 615 | s1->s[s1Length + 1] = '"'; |
Azure.IoT Build | 0:fa2de1b79154 | 616 | s1->s[s1Length + 2] = '\0'; |
Azure.IoT Build | 0:fa2de1b79154 | 617 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 618 | } |
Azure.IoT Build | 0:fa2de1b79154 | 619 | } |
Azure.IoT Build | 0:fa2de1b79154 | 620 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 621 | } |
Azure.IoT Build | 0:fa2de1b79154 | 622 | /*this function will revert a string to an empty state*/ |
Azure.IoT Build | 0:fa2de1b79154 | 623 | /*Returns 0 if the revert was succesful*/ |
Azure.IoT Build | 0:fa2de1b79154 | 624 | /* Codes_SRS_STRING_07_022: [STRING_empty shall revert the STRING_HANDLE to an empty state.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 625 | int STRING_empty(STRING_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 626 | { |
Azure.IoT Build | 0:fa2de1b79154 | 627 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 628 | if (handle == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 629 | { |
Azure.IoT Build | 0:fa2de1b79154 | 630 | /* Codes_SRS_STRING_07_023: [STRING_empty shall return a nonzero value if the STRING_HANDLE is NULL.] */ |
AzureIoTClient | 21:b92006c5b9ff | 631 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 632 | } |
Azure.IoT Build | 0:fa2de1b79154 | 633 | else |
Azure.IoT Build | 0:fa2de1b79154 | 634 | { |
Azure.IoT Build | 0:fa2de1b79154 | 635 | STRING* s1 = (STRING*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 636 | char* temp = (char*)realloc(s1->s, 1); |
Azure.IoT Build | 0:fa2de1b79154 | 637 | if (temp == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 638 | { |
Azure.IoT Build | 0:fa2de1b79154 | 639 | /* Codes_SRS_STRING_07_030: [STRING_empty shall return a nonzero value if the STRING_HANDLE is NULL.] */ |
AzureIoTClient | 21:b92006c5b9ff | 640 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 641 | } |
Azure.IoT Build | 0:fa2de1b79154 | 642 | else |
Azure.IoT Build | 0:fa2de1b79154 | 643 | { |
Azure.IoT Build | 0:fa2de1b79154 | 644 | s1->s = temp; |
Azure.IoT Build | 0:fa2de1b79154 | 645 | s1->s[0] = '\0'; |
Azure.IoT Build | 0:fa2de1b79154 | 646 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 647 | } |
Azure.IoT Build | 0:fa2de1b79154 | 648 | } |
Azure.IoT Build | 0:fa2de1b79154 | 649 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 650 | } |
Azure.IoT Build | 0:fa2de1b79154 | 651 | |
Azure.IoT Build | 0:fa2de1b79154 | 652 | /*this function will deallocate a string constructed by str_new*/ |
Azure.IoT Build | 0:fa2de1b79154 | 653 | /* Codes_SRS_STRING_07_010: [STRING_delete will free the memory allocated by the STRING_HANDLE.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 654 | void STRING_delete(STRING_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 655 | { |
Azure.IoT Build | 0:fa2de1b79154 | 656 | /* Codes_SRS_STRING_07_011: [STRING_delete will not attempt to free anything with a NULL STRING_HANDLE.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 657 | if (handle != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 658 | { |
Azure.IoT Build | 0:fa2de1b79154 | 659 | STRING* value = (STRING*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 660 | free(value->s); |
Azure.IoT Build | 0:fa2de1b79154 | 661 | value->s = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 662 | free(value); |
Azure.IoT Build | 0:fa2de1b79154 | 663 | } |
Azure.IoT Build | 0:fa2de1b79154 | 664 | } |
Azure.IoT Build | 0:fa2de1b79154 | 665 | |
Azure.IoT Build | 0:fa2de1b79154 | 666 | /* Codes_SRS_STRING_07_020: [STRING_c_str shall return the const char* associated with the given STRING_HANDLE.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 667 | const char* STRING_c_str(STRING_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 668 | { |
Azure.IoT Build | 0:fa2de1b79154 | 669 | const char* result; |
Azure.IoT Build | 0:fa2de1b79154 | 670 | if (handle != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 671 | { |
Azure.IoT Build | 0:fa2de1b79154 | 672 | result = ((STRING*)handle)->s; |
Azure.IoT Build | 0:fa2de1b79154 | 673 | } |
Azure.IoT Build | 0:fa2de1b79154 | 674 | else |
Azure.IoT Build | 0:fa2de1b79154 | 675 | { |
Azure.IoT Build | 0:fa2de1b79154 | 676 | /* Codes_SRS_STRING_07_021: [STRING_c_str shall return NULL if the STRING_HANDLE is NULL.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 677 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 678 | } |
Azure.IoT Build | 0:fa2de1b79154 | 679 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 680 | } |
Azure.IoT Build | 0:fa2de1b79154 | 681 | |
Azure.IoT Build | 0:fa2de1b79154 | 682 | /* Codes_SRS_STRING_07_024: [STRING_length shall return the length of the underlying char* for the given handle] */ |
Azure.IoT Build | 0:fa2de1b79154 | 683 | size_t STRING_length(STRING_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 684 | { |
Azure.IoT Build | 0:fa2de1b79154 | 685 | size_t result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 686 | /* Codes_SRS_STRING_07_025: [STRING_length shall return zero if the given handle is NULL.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 687 | if (handle != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 688 | { |
Azure.IoT Build | 0:fa2de1b79154 | 689 | STRING* value = (STRING*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 690 | result = strlen(value->s); |
Azure.IoT Build | 0:fa2de1b79154 | 691 | } |
Azure.IoT Build | 0:fa2de1b79154 | 692 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 693 | } |
Azure.IoT Build | 0:fa2de1b79154 | 694 | |
Azure.IoT Build | 0:fa2de1b79154 | 695 | /*Codes_SRS_STRING_02_007: [STRING_construct_n shall construct a STRING_HANDLE from first "n" characters of the string pointed to by psz parameter.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 696 | STRING_HANDLE STRING_construct_n(const char* psz, size_t n) |
Azure.IoT Build | 0:fa2de1b79154 | 697 | { |
Azure.IoT Build | 0:fa2de1b79154 | 698 | STRING_HANDLE result; |
Azure.IoT Build | 0:fa2de1b79154 | 699 | /*Codes_SRS_STRING_02_008: [If psz is NULL then STRING_construct_n shall return NULL.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 700 | if (psz == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 701 | { |
Azure.IoT Build | 0:fa2de1b79154 | 702 | result = NULL; |
AzureIoTClient | 1:9190c0f4d23a | 703 | LogError("invalid arg (NULL)"); |
Azure.IoT Build | 0:fa2de1b79154 | 704 | } |
Azure.IoT Build | 0:fa2de1b79154 | 705 | else |
Azure.IoT Build | 0:fa2de1b79154 | 706 | { |
Azure.IoT Build | 0:fa2de1b79154 | 707 | size_t len = strlen(psz); |
Azure.IoT Build | 0:fa2de1b79154 | 708 | /*Codes_SRS_STRING_02_009: [If n is bigger than the size of the string psz, then STRING_construct_n shall return NULL.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 709 | if (n > len) |
Azure.IoT Build | 0:fa2de1b79154 | 710 | { |
Azure.IoT Build | 0:fa2de1b79154 | 711 | result = NULL; |
AzureIoTClient | 1:9190c0f4d23a | 712 | LogError("invalig arg (n is bigger than the size of the string)"); |
Azure.IoT Build | 0:fa2de1b79154 | 713 | } |
Azure.IoT Build | 0:fa2de1b79154 | 714 | else |
Azure.IoT Build | 0:fa2de1b79154 | 715 | { |
Azure.IoT Build | 0:fa2de1b79154 | 716 | STRING* str; |
Azure.IoT Build | 0:fa2de1b79154 | 717 | if ((str = (STRING*)malloc(sizeof(STRING))) != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 718 | { |
Azure.IoT Build | 0:fa2de1b79154 | 719 | if ((str->s = (char*)malloc(len + 1)) != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 720 | { |
AzureIoTClient | 19:2e0811512ceb | 721 | (void)memcpy(str->s, psz, n); |
Azure.IoT Build | 0:fa2de1b79154 | 722 | str->s[n] = '\0'; |
Azure.IoT Build | 0:fa2de1b79154 | 723 | result = (STRING_HANDLE)str; |
Azure.IoT Build | 0:fa2de1b79154 | 724 | } |
Azure.IoT Build | 0:fa2de1b79154 | 725 | /* Codes_SRS_STRING_02_010: [In all other error cases, STRING_construct_n shall return NULL.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 726 | else |
Azure.IoT Build | 0:fa2de1b79154 | 727 | { |
Azure.IoT Build | 0:fa2de1b79154 | 728 | free(str); |
Azure.IoT Build | 0:fa2de1b79154 | 729 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 730 | } |
Azure.IoT Build | 0:fa2de1b79154 | 731 | } |
Azure.IoT Build | 0:fa2de1b79154 | 732 | else |
Azure.IoT Build | 0:fa2de1b79154 | 733 | { |
Azure.IoT Build | 0:fa2de1b79154 | 734 | /* Codes_SRS_STRING_02_010: [In all other error cases, STRING_construct_n shall return NULL.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 735 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 736 | } |
Azure.IoT Build | 0:fa2de1b79154 | 737 | } |
Azure.IoT Build | 0:fa2de1b79154 | 738 | } |
Azure.IoT Build | 0:fa2de1b79154 | 739 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 740 | } |
Azure.IoT Build | 0:fa2de1b79154 | 741 | |
Azure.IoT Build | 0:fa2de1b79154 | 742 | /* Codes_SRS_STRING_07_034: [STRING_compare returns an integer greater than, equal to, or less than zero, accordingly as the string pointed to by s1 is greater than, equal to, or less than the string s2.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 743 | int STRING_compare(STRING_HANDLE s1, STRING_HANDLE s2) |
Azure.IoT Build | 0:fa2de1b79154 | 744 | { |
Azure.IoT Build | 0:fa2de1b79154 | 745 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 746 | if (s1 == NULL && s2 == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 747 | { |
Azure.IoT Build | 0:fa2de1b79154 | 748 | /* Codes_SRS_STRING_07_035: [If h1 and h2 are both NULL then STRING_compare shall return 0.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 749 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 750 | } |
Azure.IoT Build | 0:fa2de1b79154 | 751 | else if (s1 == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 752 | { |
Azure.IoT Build | 0:fa2de1b79154 | 753 | /* Codes_SRS_STRING_07_036: [If h1 is NULL and h2 is nonNULL then STRING_compare shall return 1.]*/ |
Azure.IoT Build | 0:fa2de1b79154 | 754 | result = 1; |
Azure.IoT Build | 0:fa2de1b79154 | 755 | } |
Azure.IoT Build | 0:fa2de1b79154 | 756 | else if (s2 == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 757 | { |
Azure.IoT Build | 0:fa2de1b79154 | 758 | /* Codes_SRS_STRING_07_037: [If h2 is NULL and h1 is nonNULL then STRING_compare shall return -1.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 759 | result = -1; |
Azure.IoT Build | 0:fa2de1b79154 | 760 | } |
Azure.IoT Build | 0:fa2de1b79154 | 761 | else |
Azure.IoT Build | 0:fa2de1b79154 | 762 | { |
Azure.IoT Build | 0:fa2de1b79154 | 763 | /* Codes_SRS_STRING_07_038: [STRING_compare shall compare the char s variable using the strcmp function.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 764 | STRING* value1 = (STRING*)s1; |
Azure.IoT Build | 0:fa2de1b79154 | 765 | STRING* value2 = (STRING*)s2; |
Azure.IoT Build | 0:fa2de1b79154 | 766 | result = strcmp(value1->s, value2->s); |
Azure.IoT Build | 0:fa2de1b79154 | 767 | } |
Azure.IoT Build | 0:fa2de1b79154 | 768 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 769 | } |
AzureIoTClient | 4:2bd2074fe6c4 | 770 | |
AzureIoTClient | 4:2bd2074fe6c4 | 771 | STRING_HANDLE STRING_from_byte_array(const unsigned char* source, size_t size) |
AzureIoTClient | 4:2bd2074fe6c4 | 772 | { |
AzureIoTClient | 4:2bd2074fe6c4 | 773 | STRING* result; |
AzureIoTClient | 4:2bd2074fe6c4 | 774 | /*Codes_SRS_STRING_02_022: [ If source is NULL and size > 0 then STRING_from_BUFFER shall fail and return NULL. ]*/ |
AzureIoTClient | 4:2bd2074fe6c4 | 775 | if ((source == NULL) && (size > 0)) |
AzureIoTClient | 4:2bd2074fe6c4 | 776 | { |
AzureIoTClient | 4:2bd2074fe6c4 | 777 | LogError("invalid parameter (NULL)"); |
AzureIoTClient | 4:2bd2074fe6c4 | 778 | result = NULL; |
AzureIoTClient | 4:2bd2074fe6c4 | 779 | } |
AzureIoTClient | 4:2bd2074fe6c4 | 780 | else |
AzureIoTClient | 4:2bd2074fe6c4 | 781 | { |
AzureIoTClient | 4:2bd2074fe6c4 | 782 | /*Codes_SRS_STRING_02_023: [ Otherwise, STRING_from_BUFFER shall build a string that has the same content (byte-by-byte) as source and return a non-NULL handle. ]*/ |
AzureIoTClient | 4:2bd2074fe6c4 | 783 | result = (STRING*)malloc(sizeof(STRING)); |
AzureIoTClient | 4:2bd2074fe6c4 | 784 | if (result == NULL) |
AzureIoTClient | 4:2bd2074fe6c4 | 785 | { |
AzureIoTClient | 4:2bd2074fe6c4 | 786 | /*Codes_SRS_STRING_02_024: [ If building the string fails, then STRING_from_BUFFER shall fail and return NULL. ]*/ |
AzureIoTClient | 4:2bd2074fe6c4 | 787 | LogError("oom - unable to malloc"); |
AzureIoTClient | 4:2bd2074fe6c4 | 788 | /*return as is*/ |
AzureIoTClient | 4:2bd2074fe6c4 | 789 | } |
AzureIoTClient | 4:2bd2074fe6c4 | 790 | else |
AzureIoTClient | 4:2bd2074fe6c4 | 791 | { |
AzureIoTClient | 4:2bd2074fe6c4 | 792 | /*Codes_SRS_STRING_02_023: [ Otherwise, STRING_from_BUFFER shall build a string that has the same content (byte-by-byte) as source and return a non-NULL handle. ]*/ |
AzureIoTClient | 4:2bd2074fe6c4 | 793 | result->s = (char*)malloc(size + 1); |
AzureIoTClient | 4:2bd2074fe6c4 | 794 | if (result->s == NULL) |
AzureIoTClient | 4:2bd2074fe6c4 | 795 | { |
AzureIoTClient | 4:2bd2074fe6c4 | 796 | /*Codes_SRS_STRING_02_024: [ If building the string fails, then STRING_from_BUFFER shall fail and return NULL. ]*/ |
AzureIoTClient | 4:2bd2074fe6c4 | 797 | LogError("oom - unable to malloc"); |
AzureIoTClient | 4:2bd2074fe6c4 | 798 | free(result); |
AzureIoTClient | 4:2bd2074fe6c4 | 799 | result = NULL; |
AzureIoTClient | 4:2bd2074fe6c4 | 800 | } |
AzureIoTClient | 4:2bd2074fe6c4 | 801 | else |
AzureIoTClient | 4:2bd2074fe6c4 | 802 | { |
AzureIoTClient | 19:2e0811512ceb | 803 | (void)memcpy(result->s, source, size); |
AzureIoTClient | 4:2bd2074fe6c4 | 804 | result->s[size] = '\0'; /*all is fine*/ |
AzureIoTClient | 4:2bd2074fe6c4 | 805 | } |
AzureIoTClient | 4:2bd2074fe6c4 | 806 | } |
AzureIoTClient | 4:2bd2074fe6c4 | 807 | } |
AzureIoTClient | 4:2bd2074fe6c4 | 808 | return (STRING_HANDLE)result; |
AzureIoTClient | 4:2bd2074fe6c4 | 809 | } |
AzureIoTClient | 30:ce3813c5a692 | 810 | |
AzureIoTClient | 30:ce3813c5a692 | 811 | int STRING_replace(STRING_HANDLE handle, char target, char replace) |
AzureIoTClient | 30:ce3813c5a692 | 812 | { |
AzureIoTClient | 30:ce3813c5a692 | 813 | int result; |
AzureIoTClient | 30:ce3813c5a692 | 814 | if (handle == NULL) |
AzureIoTClient | 30:ce3813c5a692 | 815 | { |
AzureIoTClient | 30:ce3813c5a692 | 816 | /* Codes_SRS_STRING_07_046: [ If handle is NULL STRING_replace shall return a non-zero value. ] */ |
AzureIoTClient | 30:ce3813c5a692 | 817 | result = __FAILURE__; |
AzureIoTClient | 30:ce3813c5a692 | 818 | } |
AzureIoTClient | 30:ce3813c5a692 | 819 | else if (target == replace) |
AzureIoTClient | 30:ce3813c5a692 | 820 | { |
AzureIoTClient | 30:ce3813c5a692 | 821 | /* Codes_SRS_STRING_07_048: [ If target and replace are equal STRING_replace, shall do nothing shall return zero. ] */ |
AzureIoTClient | 30:ce3813c5a692 | 822 | result = 0; |
AzureIoTClient | 30:ce3813c5a692 | 823 | } |
AzureIoTClient | 30:ce3813c5a692 | 824 | else |
AzureIoTClient | 30:ce3813c5a692 | 825 | { |
AzureIoTClient | 30:ce3813c5a692 | 826 | size_t length; |
AzureIoTClient | 30:ce3813c5a692 | 827 | size_t index; |
AzureIoTClient | 30:ce3813c5a692 | 828 | /* Codes_SRS_STRING_07_047: [ STRING_replace shall replace all instances of target with replace. ] */ |
AzureIoTClient | 30:ce3813c5a692 | 829 | STRING* str_value = (STRING*)handle; |
AzureIoTClient | 30:ce3813c5a692 | 830 | length = strlen(str_value->s); |
AzureIoTClient | 30:ce3813c5a692 | 831 | for (index = 0; index < length; index++) |
AzureIoTClient | 30:ce3813c5a692 | 832 | { |
AzureIoTClient | 30:ce3813c5a692 | 833 | if (str_value->s[index] == target) |
AzureIoTClient | 30:ce3813c5a692 | 834 | { |
AzureIoTClient | 30:ce3813c5a692 | 835 | str_value->s[index] = replace; |
AzureIoTClient | 30:ce3813c5a692 | 836 | } |
AzureIoTClient | 30:ce3813c5a692 | 837 | } |
AzureIoTClient | 30:ce3813c5a692 | 838 | /* Codes_SRS_STRING_07_049: [ On success STRING_replace shall return zero. ] */ |
AzureIoTClient | 30:ce3813c5a692 | 839 | result = 0; |
AzureIoTClient | 30:ce3813c5a692 | 840 | } |
AzureIoTClient | 30:ce3813c5a692 | 841 | return result; |
AzureIoTClient | 30:ce3813c5a692 | 842 | } |