string_util.c
Go to the documentation of this file.
1 /*!**********************************************************************
2 *
3 * @copyright Copyright (C) 2016 Siemens Aktiengesellschaft.\n
4 * All rights reserved.
5 *
6 *************************************************************************
7 *
8 * @file string_util.c
9 * @date Aug 12, 2016
10 * @brief String utility module implementation file.
11 *
12 ************************************************************************/
13 
14 #include "string_util.h"
15 #include "log_util.h"
16 #include "definitions.h"
17 #include <string.h>
18 #include <stdio.h>
19 #include <stdarg.h>
20 #include <stdlib.h>
21 
22 #define LOWERCASE(n) ((n >= 'A' && n <= 'Z') ? (n + ('a' - 'A')) : n)
23 
24 mcl_size_t string_util_strlen(const char *buffer)
25 {
26  DEBUG_ENTRY("const char *buffer = <%s>", buffer)
27 
28  mcl_size_t return_value = strlen(buffer);
29 
30  DEBUG_LEAVE("retVal = <%u>", return_value);
31  return return_value;
32 }
33 
34 mcl_size_t string_util_strnlen(const char *buffer, mcl_size_t maximum_length)
35 {
36  DEBUG_ENTRY("const char *buffer = <%s>, mcl_size_t maximum_length = <%u>", buffer, maximum_length)
37 
38  mcl_size_t count = 0;
39  while ((count < maximum_length) && (MCL_NULL_CHAR != buffer[count]))
40  {
41  count++;
42  }
43 
44  DEBUG_LEAVE("retVal = <%u>", count);
45  return count;
46 }
47 
48 void string_util_strncpy(char *destination, const char *source, mcl_size_t count)
49 {
50  DEBUG_ENTRY("char *destination = <%s>, const char *source = <%s>, mcl_size_t count = <%u>", destination, source, count)
51 
52 #if defined(WIN32) || defined(WIN64)
53  strncpy_s(destination, count + 1, source, count);
54 #else
55  strncpy(destination, source, count);
56 #endif
57 
58  DEBUG_LEAVE("retVal = void");
59 }
60 
61 void string_util_strncat(char *destination, const char *source, mcl_size_t count)
62 {
63  DEBUG_ENTRY("char *destination = <%s>, const char *source = <%s>, mcl_size_t count = <%u>", destination, source, count)
64 
65 #if defined(WIN32) || defined(WIN64)
66  strncat_s(destination, strlen(destination) + count + 1, source, count);
67 #else
68  strncat(destination, source, count);
69 #endif
70 
71  DEBUG_LEAVE("retVal = void");
72 }
73 
74 E_MCL_ERROR_CODE string_util_strncmp(const char *string_1, const char *string_2, mcl_size_t count)
75 {
76  DEBUG_ENTRY("const char *string_1 = <%s>, const char *string_2 = <%s>, mcl_size_t count = <%u>", string_1, string_2, count)
77 
78  E_MCL_ERROR_CODE code;
79  int result = strncmp(string_1, string_2, count);
80 
81  // this check is necessary since result can be < 0
82  if (0 == result)
83  {
84  code = MCL_OK;
85  }
86  else
87  {
88  code = MCL_FAIL;
89  }
90 
91  DEBUG_LEAVE("retVal = <%d>", code);
92  return code;
93 }
94 
95 E_MCL_ERROR_CODE string_util_snprintf(char *string, mcl_size_t length, const char *format, ...)
96 {
97  DEBUG_ENTRY("char *string = <%p>, mcl_size_t length = <%u>, const char *format = <%s>", string, length, format)
98 
99  // We will use vsnprintf to be able to pass the va_args:
100 
101  va_list args;
102  mcl_int32_t count;
103 
104  va_start(args, format);
105 
106  count = vsnprintf(string, length, format, args);
107  va_end(args);
108 
109  if ((count > 0) && (((mcl_size_t)count) <= length))
110  {
111  DEBUG_LEAVE("retVal = <%d>", MCL_OK);
112  return MCL_OK;
113  }
114  else
115  {
116  // There was a problem about writing the string :
117  MCL_ERROR_RETURN(MCL_FAIL, "Couldn't write the string!");
118  }
119 }
120 
121 mcl_bool_t string_util_memcmp(const void *block_1, const void *block_2, mcl_size_t count)
122 {
123  DEBUG_ENTRY("const void *block_1 = <%p>, const void *block_2 = <%p>, mcl_size_t count = <%u>", block_1, block_2, count)
124 
125  mcl_bool_t result = (0 == memcmp(block_1, block_2, count)) ? MCL_TRUE : MCL_FALSE;
126 
127  DEBUG_LEAVE("retVal = <%s>", result ? "MCL_TRUE" : "MCL_FALSE");
128  return result;
129 }
130 
131 void string_util_memcpy(void *destination, const void *source, mcl_size_t count)
132 {
133  DEBUG_ENTRY("void *destination = <%p>, const void *source = <%p>, mcl_size_t count = <%u>", destination, source, count)
134 
135  memcpy(destination, source, count);
136 
137  DEBUG_LEAVE("retVal = void");
138 }
139 
140 char *string_util_strdup(const char *string)
141 {
142  DEBUG_ENTRY("const char *string = <%s>", string)
143 
144  char *result;
145 
146 #if defined(WIN32) || defined(WIN64)
147  result = _strdup(string);
148 #else
149  result = strdup(string);
150 #endif
151 
152  DEBUG_LEAVE("retVal = <%p>", result);
153  return result;
154 }
155 
156 mcl_bool_t string_util_find(const char *source, const char *target, mcl_size_t *start_index)
157 {
158  DEBUG_ENTRY("char *src = <%p>, char *target = <%p>, mcl_size_t *start_index = <%p>", source, target, start_index)
159 
160  mcl_size_t index;
161  mcl_size_t source_length;
162  mcl_size_t target_length;
163  mcl_bool_t is_found = MCL_FALSE;
164  source_length = string_util_strlen(source);
165  target_length = string_util_strlen(target);
166 
167  if (!(0 == target_length) && !(target_length > source_length))
168  {
169  mcl_size_t state = 0;
170 
171  for (index = 0; index < source_length; index++)
172  {
173  state = (source[index] == target[state]) ? state + 1 : 0;
174 
175  if (state == target_length)
176  {
177  *start_index = index - (target_length - 1);
178  is_found = MCL_TRUE;
179 
180  break;
181  }
182  }
183  }
184 
185  DEBUG_LEAVE("retVal = <%s>", is_found ? "MCL_TRUE" : "MCL_FALSE");
186  return is_found;
187 }
188 
189 mcl_bool_t string_util_find_case_insensitive(const char *source, const char *target, mcl_size_t *start_index)
190 {
191  DEBUG_ENTRY("char *src = <%p>, char *target = <%p>, mcl_size_t *start_index = <%p>", source, target, start_index)
192 
193  mcl_size_t index;
194  mcl_size_t source_length;
195  mcl_size_t target_length;
196  mcl_bool_t is_found = MCL_FALSE;
197  source_length = string_util_strlen(source);
198  target_length = string_util_strlen(target);
199 
200  if ((0 != target_length) && (target_length <= source_length))
201  {
202  mcl_size_t state = 0;
203 
204  for (index = 0; index < source_length; index++)
205  {
206  state = (source[index] == target[state] || LOWERCASE(source[index]) == LOWERCASE(target[state])) ? state + 1 : 0;
207 
208  if (state == target_length)
209  {
210  *start_index = index - (target_length - 1);
211  is_found = MCL_TRUE;
212 
213  break;
214  }
215  }
216  }
217 
218  DEBUG_LEAVE("retVal = <%s>", is_found ? "MCL_TRUE" : "MCL_FALSE");
219  return is_found;
220 }
221 
223 {
224  DEBUG_ENTRY("char character = <%c>", character)
225 
226  mcl_bool_t result = (character == ' ') || (character == '\t') || (character == '\n') || (character == '\v') || (character == '\f') || (character == '\r');
227 
228  DEBUG_LEAVE("retVal = <%s>", result ? "MCL_TRUE" : "MCL_FALSE");
229  return result;
230 }
231 
232 long string_util_strtol(const char *source, int base, char **end_pointer)
233 {
234  DEBUG_ENTRY("char* source = <%p>, int base = <%p>, char *end_pointer = <%p>", source, end_pointer, base)
235 
236  long result = strtol(source, end_pointer, base);
237 
238  DEBUG_LEAVE("retVal = <%u>", result);
239  return result;
240 }
void string_util_strncat(char *destination, const char *source, mcl_size_t count)
Standard library strncat wrapper.
Definition: string_util.c:61
Internal failure in MCL.
Definition: mcl_common.h:141
mcl_size_t string_util_strnlen(const char *buffer, mcl_size_t maximum_length)
Standard library strnlen wrapper.
Definition: string_util.c:34
char * string_util_strdup(const char *string)
Standard library strdup wrapper.
Definition: string_util.c:140
long string_util_strtol(const char *source, int base, char **end_pointer)
Returns the first occurrence of an integral value in source string.
Definition: string_util.c:232
#define DEBUG_LEAVE(...)
Definition: log_util.h:81
#define DEBUG_ENTRY(...)
Definition: log_util.h:80
#define MCL_TRUE
Definition: mcl_common.h:54
mcl_bool_t string_util_find(const char *source, const char *target, mcl_size_t *start_index)
Finds the first occurence of target in source and puts it&#39;s first index to start_index.
Definition: string_util.c:156
void string_util_strncpy(char *destination, const char *source, mcl_size_t count)
Standard library strncpy wrapper. Also sets the terminating null char at the end if source is not lon...
Definition: string_util.c:48
#define MCL_FALSE
MCL bool type.
Definition: mcl_common.h:53
Log utility module header file.
mcl_bool_t string_util_find_case_insensitive(const char *source, const char *target, mcl_size_t *start_index)
Finds the first occurence of target in source and puts it&#39;s first index to start_index.
Definition: string_util.c:189
String utility module implementation file.
E_MCL_ERROR_CODE
MCL Error code definitions. Every function returning an error code uses this enum values...
Definition: mcl_common.h:137
E_MCL_ERROR_CODE string_util_snprintf(char *string, mcl_size_t length, const char *format,...)
Standard library snprintf wrapper.
Definition: string_util.c:95
void string_util_memcpy(void *destination, const void *source, mcl_size_t count)
Standard library memcpy wrapper.
Definition: string_util.c:131
Definitions module header file.
E_MCL_ERROR_CODE string_util_strncmp(const char *string_1, const char *string_2, mcl_size_t count)
Standard library strncmp wrapper.
Definition: string_util.c:74
size_t mcl_size_t
Definition: mcl_common.h:38
#define LOWERCASE(n)
Definition: string_util.c:22
mcl_uint8_t mcl_bool_t
Definition: mcl_common.h:47
Success.
Definition: mcl_common.h:140
mcl_bool_t string_util_memcmp(const void *block_1, const void *block_2, mcl_size_t count)
Standard library memcmp wrapper.
Definition: string_util.c:121
mcl_bool_t string_util_is_space(char character)
Checks if a character is whitespace.
Definition: string_util.c:222
int32_t mcl_int32_t
Definition: mcl_common.h:41
mcl_size_t string_util_strlen(const char *buffer)
Standard library strlen wrapper.
Definition: string_util.c:24
#define MCL_NULL_CHAR
Definition: definitions.h:26
#define MCL_ERROR_RETURN(return_value,...)
Definition: definitions.h:69