string: making shortening function global

pull/4062/head
Victor Julien 6 years ago
parent 26c5249574
commit 8a59ad6096

@ -19,6 +19,7 @@
#include "conf.h"
#include "util-device.h"
#include "util-ioctl.h"
#include "util-misc.h"
#include "device-storage.h"
@ -189,33 +190,6 @@ const char *LiveGetDeviceName(int number)
return NULL;
}
static void ShortenString(const char *input,
char *output, size_t output_size, char c)
{
const size_t str_len = strlen(input);
size_t half = (output_size - 1) / 2;
/* If the output size is an even number */
if (half * 2 == (output_size - 1)) {
half = half - 1;
}
size_t spaces = (output_size - 1) - (half * 2);
/* Add the first half to the new string */
snprintf(output, half+1, "%s", input);
/* Add the amount of spaces wanted */
size_t length = half;
for (size_t i = half; i < half + spaces; i++) {
char s[2] = "";
snprintf(s, sizeof(s), "%c", c);
length = strlcat(output, s, output_size);
}
snprintf(output + length, half + 1, "%s", input + (str_len - half));
}
/** \internal
* \brief Shorten a device name that is to long
*

@ -217,6 +217,33 @@ int ParseSizeStringU64(const char *size, uint64_t *res)
return 0;
}
void ShortenString(const char *input,
char *output, size_t output_size, char c)
{
const size_t str_len = strlen(input);
size_t half = (output_size - 1) / 2;
/* If the output size is an even number */
if (half * 2 == (output_size - 1)) {
half = half - 1;
}
size_t spaces = (output_size - 1) - (half * 2);
/* Add the first half to the new string */
snprintf(output, half+1, "%s", input);
/* Add the amount of spaces wanted */
size_t length = half;
for (size_t i = half; i < half + spaces; i++) {
char s[2] = "";
snprintf(s, sizeof(s), "%c", c);
length = strlcat(output, s, output_size);
}
snprintf(output + length, half + 1, "%s", input + (str_len - half));
}
/*********************************Unittests********************************/
#ifdef UNITTESTS

@ -52,4 +52,7 @@ void UtilMiscRegisterTests(void);
void ParseSizeInit(void);
void ParseSizeDeinit(void);
void ShortenString(const char *input,
char *output, size_t output_size, char c);
#endif /* __UTIL_MISC_H__ */

Loading…
Cancel
Save