You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
configreader/containers/stringarray.c

83 lines
1.9 KiB

#include "stringarray.h"
#include <proto/containerkit.h>
#include <proto/exec.h>
#include <string.h>
#define MIN(x,y) (x<y?x:y)
StringArray StringArrayNew(VOID)
{
#define SIZE_STRPTR 2
return NewArray(SIZE_STRPTR);
}
VOID StringArrayAppend(StringArray array, CONST_STRPTR value)
{
AppendToArray(CONST_STRPTR, array, value);
}
VOID StringArrayAppendAndRetain(StringArray array, CONST_STRPTR value)
{
STRPTR localCopy = AllocVec(strlen(value)+1, MEMF_CLEAR);
CopyMem(value, localCopy, strlen(value));
StringArrayAppend(array, localCopy);
}
VOID StringArrayFree(StringArray array, BOOL freeStrings)
{
if( array != NULL )
{
if( freeStrings )
{
StringArrayForEach(array, FreeVec(aString););
}
DeleteArray(array);
}
}
STRPTR StringArrayJoined(StringArray array, BYTE linkCharacter)
{
return StringArrayJoinedParts(array, linkCharacter, 0, SizeOfArray(array));
}
STRPTR StringArrayJoinedParts(StringArray array, BYTE linkCharacter, ULONG startIndex, ULONG maxParts)
{
ULONG requiredSize = 0;
ULONG index = 0;
ULONG cursor = 0;
STRPTR result = NULL;
ULONG lastIndex = MIN(startIndex+maxParts, SizeOfArray(array)-1);
if( lastIndex-startIndex < 0 )
{
return (STRPTR)AllocVec(1, MEMF_CLEAR); //return an empty string
}
for( index = startIndex; index < lastIndex; index++ )
{
requiredSize += strlen(StringArrayValues(array)[index]);
}
requiredSize += (maxParts == 0 ? 0 : maxParts - 1); // add the join characters
requiredSize += 1; // trailing NULL
result = AllocVec(requiredSize, MEMF_CLEAR);
for( index = startIndex; index < lastIndex; index++ )
{
CONST_STRPTR entry = StringArrayValues(array)[index];
CopyMem((STRPTR)entry, result+cursor, strlen(entry));
cursor += strlen(entry);
result[cursor] = linkCharacter;
cursor += 1;
}
result[requiredSize-1] = '\0';
return result;
}
CONST_STRPTR* StringArrayValues(StringArray array)
{
return ArrayValues(CONST_STRPTR, array);
}