Commit fee80da2 authored by Lucas Russo's avatar Lucas Russo

hal/hal_utils/hal_utils.*: add new concat function

Now, it is possbile to concatenate 2 or 3 strings
with a separator inbetween the first and the second
strings. This is a very restrict case, but it is
the one we need.

TODO: make this function more generic
parent 1756c485
......@@ -35,6 +35,9 @@
CHECK_HAL_ERR(err, HAL_UTILS, "[halutils]", \
halutils_err_str (err_type))
static char *_halutils_concat_strings_raw (const char *str1, const char* str2,
const char *str3, char sep);
uint32_t num_to_str_len (uint32_t key, uint32_t base)
{
uint32_t i = 0;
......@@ -86,15 +89,41 @@ char *halutils_stringify_hex_key (uint32_t key)
}
#define SEPARATOR_BYTES 1
char *halutils_concat_strings (const char *str1, const char* str2, char sep)
/* FIXME: poorly written */
static char *_halutils_concat_strings_raw (const char *str1, const char* str2,
const char *str3, char sep)
{
char *str = zmalloc (strlen(str1) + strlen(str2) +
SEPARATOR_BYTES /* separator length */+ 1 /* \0 */);
ASSERT_ALLOC(str, err_str_alloc);
sprintf (str, "%s%c%s", str1, sep, str2);
assert (str1);
assert (str2);
char *str = NULL;
if (str3 != NULL) {
str = zmalloc (strlen (str1) + strlen (str2) + strlen (str3) +
SEPARATOR_BYTES /* separator length */+ 1 /* \0 */);
ASSERT_ALLOC(str, err_str3_alloc);
sprintf (str, "%s%c%s%s", str1, sep, str2, str3);
}
else {
str = zmalloc (strlen(str1) + strlen(str2) +
SEPARATOR_BYTES /* separator length */+ 1 /* \0 */);
ASSERT_ALLOC(str, err_str2_alloc);
sprintf (str, "%s%c%s", str1, sep, str2);
}
return str;
err_str_alloc:
err_str3_alloc:
err_str2_alloc:
return NULL;
}
char *halutils_concat_strings (const char *str1, const char* str2, char sep)
{
return _halutils_concat_strings_raw (str1, str2, NULL, sep);
}
char *halutils_concat_strings3 (const char *str1, const char* str2,
const char* str3, char sep)
{
return _halutils_concat_strings_raw (str1, str2, str3, sep);
}
......@@ -35,4 +35,9 @@ char *halutils_stringify_hex_key (uint32_t key);
* OK, NULL in case of error */
char *halutils_concat_strings (const char *str1, const char* str2, char sep);
/* Concatenates 3 strings togheter with a separator between the first and second
* strings. returns the string if OK, NULL in case of error */
char *halutils_concat_strings3 (const char *str1, const char* str2,
const char* str3, char sep);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment