- refactored to modular structure
This commit is contained in:
75
src/ext/xstring.c
Normal file
75
src/ext/xstring.c
Normal file
@@ -0,0 +1,75 @@
|
||||
/*!
|
||||
* @file xstring.c
|
||||
* @brief
|
||||
* @details
|
||||
* Project: \n
|
||||
* Subsystem: \n
|
||||
* Module: \n
|
||||
* Code: GNU-C\n
|
||||
*
|
||||
* @date 27.02.2017
|
||||
* @author SESA354004
|
||||
*/
|
||||
#include "xstring.h"
|
||||
#include "ctype.h"
|
||||
|
||||
char* gnu_basename(char *path)
|
||||
{
|
||||
char *base = strrchr(path, '/');
|
||||
return base ? base + 1 : path;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Trim leading and trailing whitespaces
|
||||
* @param [in] *str Input string
|
||||
* @retval char* pointer to the trimmed substring
|
||||
*
|
||||
* @details
|
||||
* This function returns a pointer to a substring of the original string.
|
||||
* If the given string was allocated dynamically, the caller must not overwrite
|
||||
* that pointer with the returned value, since the original pointer must be
|
||||
* deallocated using the same allocator with which it was allocated. The return
|
||||
* value must NOT be deallocated using free() etc.
|
||||
*/
|
||||
char* strntrimStatic(char *aStr, size_t aMaxLength)
|
||||
{
|
||||
char *end;
|
||||
|
||||
// Trim leading space
|
||||
while (isspace((unsigned char)*aStr) && 0 < aMaxLength)
|
||||
{
|
||||
if (--aMaxLength > 0)
|
||||
{
|
||||
++aStr;
|
||||
}
|
||||
}
|
||||
|
||||
// All spaces at termination or end of max length
|
||||
if (*aStr == '\0' || 0 == aMaxLength)
|
||||
{
|
||||
return aStr;
|
||||
}
|
||||
|
||||
// Trim trailing space
|
||||
if (strlen(aStr) >= aMaxLength)
|
||||
{
|
||||
end = aStr + aMaxLength - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
end = aStr + strlen(aStr) - 1;
|
||||
}
|
||||
|
||||
while (end > aStr && isspace((unsigned char)*end))
|
||||
{
|
||||
end--;
|
||||
}
|
||||
|
||||
// Write new null terminator (if length is enough)
|
||||
if (end < (aStr + aMaxLength - 1))
|
||||
{
|
||||
*(end + 1) = '\0';
|
||||
}
|
||||
|
||||
return aStr;
|
||||
}
|
Reference in New Issue
Block a user