#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include "mslp_sd.h"
#include "slp.h"
#include "mslp.h"
EXPORT int list_intersection(const char *pcL1, const char *pcL2)
{
int i1 = 0, i2 = 0;
char *pcS1, *pcS2;
char c;
if (!pcL1 || !pcL2)
{
LOG(SLP_LOG_ERR,"list_intersection: got NULL value as a parameter!");
return 0;
}
if (*pcL1 == '\0' && *pcL2 == '\0')
return 1;
for ( pcS1=get_next_string(",",pcL1,&i1,&c); pcS1; pcS1=get_next_string(",",pcL1,&i1,&c) )
{
i2 = 0;
for (pcS2=get_next_string(",",pcL2,&i2,&c); pcS2;pcS2=get_next_string(",",pcL2,&i2,&c))
{
int result = SDstrcasecmp(pcS1,pcS2);
SLPFree(pcS2);
if (result==0)
{
SLPFree(pcS1);
return 1;
}
}
SLPFree(pcS1);
}
return 0;
}
EXPORT void list_merge(const char *pcNewList, char **ppcList, int *piListLen, int iCheck)
{
int offset = 0;
char c, *pcScope;
int initial = 0;
if (!pcNewList)
return;
if (!ppcList)
return;
if (!*ppcList)
{
*ppcList = safe_malloc(strlen(pcNewList)+1,pcNewList,strlen(pcNewList));
*piListLen = strlen(*ppcList);
return;
}
if (*piListLen == 0)
initial = 1;
if (!iCheck)
{
int iSLen = strlen(pcNewList);
if ((iSLen + (int) strlen(*ppcList)+1) >= *piListLen)
{
char *pcOld = *ppcList;
*piListLen += iSLen + LISTINCR;
*ppcList = safe_malloc(*piListLen,*ppcList,strlen(*ppcList));
SLPFree(pcOld);
}
if (!initial)
slp_strcat(*ppcList,",");
slp_strcat(*ppcList,pcNewList);
return;
}
while((pcScope = get_next_string(",",pcNewList,&offset,&c)))
{
if (!list_intersection(pcScope,*ppcList))
{
int iSLen = strlen(pcScope);
if ((iSLen + (int) strlen(*ppcList)+1) >= *piListLen)
{
char *pcOld = *ppcList;
*piListLen += iSLen + LISTINCR;
*ppcList = safe_malloc(*piListLen,*ppcList,strlen(*ppcList));
SLPFree(pcOld);
}
if (initial != 1)
slp_strcat(*ppcList,",");
slp_strcat(*ppcList,pcScope);
}
SLPFree(pcScope);
}
}
EXPORT int list_subset(const char *pcSub, const char *pcSuper) {
int offset = 0;
char c, *pcScope;
if (!pcSub || !pcSuper ||
(pcSub[0] == '\0' && pcSuper[0] != '\0')) {
return 0;
}
while((pcScope = get_next_string(",",pcSub,&offset,&c))) {
if (!list_intersection(pcScope,pcSuper)) {
SLPFree(pcScope);
return 0;
}
SLPFree(pcScope);
}
return 1;
}
EXPORT char * list_pack(const char *pc) {
const char *pcSrc;
char *pcTemp, *pcDest;
if ( !pc || !*pc) return safe_malloc(1,0,0);
pcTemp = safe_malloc(strlen(pc)+1,0,0);
pcDest = pcTemp;
pcSrc = pc;
while(*pcSrc) {
char *pcStart = NULL;
while (*pcSrc && isspace(*pcSrc))
pcSrc++;
if (*pcSrc == ',' || *pcSrc == '(' || *pcSrc == ')' || *pcSrc == '=') {
*pcDest++ = *pcSrc++;
continue;
}
pcStart = NULL;
while (*pcSrc) {
if (*pcSrc == ',' || *pcSrc == '(' || *pcSrc == ')' || *pcSrc == '=') {
if (pcStart) {
*--pcDest = *pcSrc++;
pcDest++;
pcStart = NULL;
} else {
*pcDest++ = *pcSrc++;
}
break;
}
if (*pcSrc && !isspace(*pcSrc)) {
*pcDest++ = *pcSrc++;
pcStart = NULL;
} else if (pcStart == NULL) {
pcStart = pcDest;
*pcDest++ = ' ';
pcSrc++;
while (*pcSrc && isspace(*pcSrc)) pcSrc++;
} else {
pcSrc++;
}
}
if (!*pcSrc) {
if (pcStart) {
*--pcDest = '\0';
}
break;
}
}
return pcTemp;
}
EXPORT char * list_remove_element(const char *list, const char *element)
{
int elementLen, newListLen;
char tempElem[1024];
char* curPtr;
char* newList = NULL;
if ( !element || !list || strlen(list) < strlen(element) )
return NULL;
elementLen = strlen(element);
strcpy( tempElem, "," );
slp_strcat( tempElem, element );
slp_strcat( tempElem, "," );
curPtr = strstr( list, tempElem );
if ( curPtr )
{
newListLen = strlen(list)-(strlen(tempElem)-1); newList = (char*)malloc(newListLen+1);
memcpy( newList, list, curPtr-list );
memcpy( newList+(curPtr-list), curPtr+strlen(tempElem)-1, strlen(list)-((curPtr-list)+strlen(tempElem)-1) ); newList[newListLen] = '\0';
}
else if ( memcmp( list, &tempElem[1], strlen(tempElem)-1 ) == 0 )
{
newListLen = strlen(list)-(strlen(tempElem)-1); newList = (char*)malloc(newListLen+1); memcpy( newList, list+(strlen(tempElem)-1), newListLen );
newList[newListLen] = '\0';
}
else if ( memcmp( &list[strlen(list)-(strlen(tempElem)-1)], tempElem, strlen(tempElem)-1 ) == 0 )
{
newListLen = strlen(list)-(strlen(tempElem)-1); newList = (char*)malloc(newListLen+1); memcpy( newList, list, newListLen );
newList[newListLen] = '\0';
}
else if ( strcmp( list, element ) == 0 )
{
newList = (char*)malloc(1);
newList[0] = '\0';
}
return newList;
}