|
Packit Service |
b3514a |
/*
|
|
Packit Service |
b3514a |
* Copyright (c) 2020 Red Hat, Inc.
|
|
Packit Service |
b3514a |
*
|
|
Packit Service |
b3514a |
* This program is free software; you can redistribute it and/or
|
|
Packit Service |
b3514a |
* modify it under the terms of the GNU General Public License
|
|
Packit Service |
b3514a |
* as published by the Free Software Foundation; either version 2
|
|
Packit Service |
b3514a |
* of the License, or (at your option) any later version.
|
|
Packit Service |
b3514a |
*
|
|
Packit Service |
b3514a |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
b3514a |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
b3514a |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
b3514a |
* GNU General Public License for more details.
|
|
Packit Service |
b3514a |
*
|
|
Packit Service |
b3514a |
* You should have received a copy of the GNU General Public License
|
|
Packit Service |
b3514a |
* along with this program; if not, write to the Free Software
|
|
Packit Service |
b3514a |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
Packit Service |
b3514a |
* 02110-1301, USA.
|
|
Packit Service |
b3514a |
*
|
|
Packit Service |
b3514a |
* $Id: //eng/vdo-releases/aluminum/src/c++/vdo/kernel/vdoStringUtils.c#1 $
|
|
Packit Service |
b3514a |
*/
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
#include "vdoStringUtils.h"
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
#include "errors.h"
|
|
Packit Service |
b3514a |
#include "logger.h"
|
|
Packit Service |
b3514a |
#include "memoryAlloc.h"
|
|
Packit Service |
b3514a |
#include "stringUtils.h"
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
#include "statusCodes.h"
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
/**********************************************************************/
|
|
Packit Service |
b3514a |
char *vAppendToBuffer(char *buffer,
|
|
Packit Service |
b3514a |
char *bufEnd,
|
|
Packit Service |
b3514a |
const char *fmt,
|
|
Packit Service |
b3514a |
va_list args)
|
|
Packit Service |
b3514a |
{
|
|
Packit Service |
b3514a |
size_t n = vsnprintf(buffer, bufEnd - buffer, fmt, args);
|
|
Packit Service |
b3514a |
if (n >= (size_t) (bufEnd - buffer)) {
|
|
Packit Service |
b3514a |
buffer = bufEnd;
|
|
Packit Service |
b3514a |
} else {
|
|
Packit Service |
b3514a |
buffer += n;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
return buffer;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
/**********************************************************************/
|
|
Packit Service |
b3514a |
char *appendToBuffer(char *buffer, char *bufEnd, const char *fmt, ...)
|
|
Packit Service |
b3514a |
{
|
|
Packit Service |
b3514a |
va_list ap;
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
va_start(ap, fmt);
|
|
Packit Service |
b3514a |
char *pos = vAppendToBuffer(buffer, bufEnd, fmt, ap);
|
|
Packit Service |
b3514a |
va_end(ap);
|
|
Packit Service |
b3514a |
return pos;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
/**********************************************************************/
|
|
Packit Service |
b3514a |
void freeStringArray(char **stringArray)
|
|
Packit Service |
b3514a |
{
|
|
Packit Service |
b3514a |
for (unsigned int offset = 0; stringArray[offset] != NULL; offset++) {
|
|
Packit Service |
b3514a |
FREE(stringArray[offset]);
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
FREE(stringArray);
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
/**********************************************************************/
|
|
Packit Service |
b3514a |
int splitString(const char *string, char separator, char ***substringArrayPtr)
|
|
Packit Service |
b3514a |
{
|
|
Packit Service |
b3514a |
unsigned int substringCount = 1;
|
|
Packit Service |
b3514a |
for (const char *s = string; *s != 0; s++) {
|
|
Packit Service |
b3514a |
if (*s == separator) {
|
|
Packit Service |
b3514a |
substringCount++;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
char **substrings;
|
|
Packit Service |
b3514a |
int result = ALLOCATE(substringCount + 1, char *, "string-splitting array",
|
|
Packit Service |
b3514a |
&substrings);
|
|
Packit Service |
b3514a |
if (result != UDS_SUCCESS) {
|
|
Packit Service |
b3514a |
return result;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
unsigned int currentSubstring = 0;
|
|
Packit Service |
b3514a |
for (const char *s = string; *s != 0; s++) {
|
|
Packit Service |
b3514a |
if (*s == separator) {
|
|
Packit Service |
b3514a |
ptrdiff_t length = s - string;
|
|
Packit Service |
b3514a |
result = ALLOCATE(length + 1, char, "split string",
|
|
Packit Service |
b3514a |
&substrings[currentSubstring]);
|
|
Packit Service |
b3514a |
if (result != UDS_SUCCESS) {
|
|
Packit Service |
b3514a |
freeStringArray(substrings);
|
|
Packit Service |
b3514a |
return result;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
// Trailing NUL is already in place after allocation; deal with
|
|
Packit Service |
b3514a |
// the zero or more non-NUL bytes in the string.
|
|
Packit Service |
b3514a |
if (length > 0) {
|
|
Packit Service |
b3514a |
memcpy(substrings[currentSubstring], string, length);
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
string = s + 1;
|
|
Packit Service |
b3514a |
currentSubstring++;
|
|
Packit Service |
b3514a |
BUG_ON(currentSubstring >= substringCount);
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
// Process final string, with no trailing separator.
|
|
Packit Service |
b3514a |
BUG_ON(currentSubstring != (substringCount - 1));
|
|
Packit Service |
b3514a |
ptrdiff_t length = strlen(string);
|
|
Packit Service |
b3514a |
result = ALLOCATE(length + 1, char, "split string",
|
|
Packit Service |
b3514a |
&substrings[currentSubstring]);
|
|
Packit Service |
b3514a |
if (result != UDS_SUCCESS) {
|
|
Packit Service |
b3514a |
freeStringArray(substrings);
|
|
Packit Service |
b3514a |
return result;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
memcpy(substrings[currentSubstring], string, length);
|
|
Packit Service |
b3514a |
currentSubstring++;
|
|
Packit Service |
b3514a |
// substrings[currentSubstring] is NULL already
|
|
Packit Service |
b3514a |
*substringArrayPtr = substrings;
|
|
Packit Service |
b3514a |
return UDS_SUCCESS;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
/**********************************************************************/
|
|
Packit Service |
b3514a |
int joinStrings(char **substringArray,
|
|
Packit Service |
b3514a |
size_t arrayLength,
|
|
Packit Service |
b3514a |
char separator,
|
|
Packit Service |
b3514a |
char **stringPtr)
|
|
Packit Service |
b3514a |
{
|
|
Packit Service |
b3514a |
size_t stringLength = 0;
|
|
Packit Service |
b3514a |
for (size_t i = 0; (i < arrayLength) && (substringArray[i] != NULL); i++) {
|
|
Packit Service |
b3514a |
stringLength += strlen(substringArray[i]) + 1;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
char *output;
|
|
Packit Service |
b3514a |
int result = ALLOCATE(stringLength, char, __func__, &output);
|
|
Packit Service |
b3514a |
if (result != VDO_SUCCESS) {
|
|
Packit Service |
b3514a |
return result;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
char *currentPosition = &output[0];
|
|
Packit Service |
b3514a |
for (size_t i = 0; (i < arrayLength) && (substringArray[i] != NULL); i++) {
|
|
Packit Service |
b3514a |
currentPosition = appendToBuffer(currentPosition, output + stringLength,
|
|
Packit Service |
b3514a |
"%s", substringArray[i]);
|
|
Packit Service |
b3514a |
*currentPosition = separator;
|
|
Packit Service |
b3514a |
currentPosition++;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
// We output one too many separators; replace the last with a zero byte.
|
|
Packit Service |
b3514a |
if (currentPosition != output) {
|
|
Packit Service |
b3514a |
*(currentPosition - 1) = '\0';
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
*stringPtr = output;
|
|
Packit Service |
b3514a |
return UDS_SUCCESS;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
/**********************************************************************/
|
|
Packit Service |
b3514a |
int stringToUInt(const char *input, unsigned int *valuePtr)
|
|
Packit Service |
b3514a |
{
|
|
Packit Service |
b3514a |
unsigned long longValue;
|
|
Packit Service |
b3514a |
int result = kstrtoul(input, 10, &longValue);
|
|
Packit Service |
b3514a |
if (result != 0) {
|
|
Packit Service |
b3514a |
return result;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
if (longValue > UINT_MAX) {
|
|
Packit Service |
b3514a |
return -ERANGE;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
*valuePtr = longValue;
|
|
Packit Service |
b3514a |
return UDS_SUCCESS;
|
|
Packit Service |
b3514a |
}
|