2014-09-15 20:22:02 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
|
* under the terms of the GNU General Public License version 2, as published by
|
|
|
|
|
* the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
|
* more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
|
* this program; if not, write to the Free Software Foundation, Inc., 51
|
|
|
|
|
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2014-09-20 09:40:59 +02:00
|
|
|
#include <stdint.h>
|
2014-09-15 20:00:05 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include "stringv.h"
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
size_t stringv_len(char **array)
|
2014-09-15 20:00:05 +02:00
|
|
|
{
|
2014-09-20 10:59:54 +02:00
|
|
|
size_t count = 0;
|
|
|
|
|
char **pos;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
if (array == NULL)
|
|
|
|
|
return 0;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
pos = array;
|
2014-09-20 10:59:54 +02:00
|
|
|
while (pos[0] != NULL) {
|
2014-09-15 20:00:05 +02:00
|
|
|
pos++;
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
return count;
|
2014-09-15 20:00:05 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
void stringv_free(char **array, int b_free)
|
2014-09-15 20:00:05 +02:00
|
|
|
{
|
2014-09-20 10:59:54 +02:00
|
|
|
char **pos;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
if (array == NULL)
|
|
|
|
|
return;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
pos = array;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
while ((*pos) != NULL) {
|
|
|
|
|
free(*pos);
|
|
|
|
|
++pos;
|
|
|
|
|
}
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
if (b_free == STRINGV_FREE_ALL)
|
|
|
|
|
free(array);
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
char **stringv_copy(char **array)
|
2014-09-15 20:00:05 +02:00
|
|
|
{
|
2014-09-20 10:59:54 +02:00
|
|
|
uint32_t array_len;
|
|
|
|
|
char **array_pos;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
char **dup_array;
|
|
|
|
|
char **dup_pos;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
if (array == NULL)
|
|
|
|
|
return (char **)NULL;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
array_pos = array;
|
|
|
|
|
|
|
|
|
|
array_len = stringv_len(array);
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
if (array_len > UINT32_MAX - 1)
|
|
|
|
|
array_len = UINT32_MAX - 1;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
dup_array = (char **)calloc(array_len, sizeof(char *));
|
2014-09-15 20:00:05 +02:00
|
|
|
dup_pos = dup_array;
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
while ((*array_pos) != NULL) {
|
|
|
|
|
*dup_pos = strdup((const char *)*array_pos);
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
array_pos++;
|
|
|
|
|
dup_pos++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dup_array;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
char **stringv_find(char *string, char *token, int limit)
|
2014-09-15 20:00:05 +02:00
|
|
|
{
|
2014-09-20 10:59:54 +02:00
|
|
|
char **results = NULL;
|
|
|
|
|
char *pos = NULL;
|
|
|
|
|
int count = 0;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
if (string == NULL)
|
|
|
|
|
return (char **)NULL;
|
|
|
|
|
if (token == NULL)
|
|
|
|
|
return (char **)NULL;
|
|
|
|
|
if (limit == 0)
|
|
|
|
|
return (char **)NULL;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
pos = string;
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
if (strlen(token) > strlen(string))
|
|
|
|
|
return (char **)NULL;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
while (count != limit) {
|
|
|
|
|
pos = strstr(pos, token);
|
|
|
|
|
if (pos == NULL)
|
|
|
|
|
break;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
count++;
|
2014-09-25 19:38:48 +02:00
|
|
|
results =
|
|
|
|
|
(char **)realloc((void *)results, sizeof(char *) * count + 1);
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
results[count - 1] = pos;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
if (count == 0)
|
|
|
|
|
return (char **)NULL;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
results[count] = (char *)NULL;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
char **stringv_split(char *string, char *token, int limit)
|
2014-09-15 20:00:05 +02:00
|
|
|
{
|
2014-09-20 10:59:54 +02:00
|
|
|
char **results = NULL;
|
|
|
|
|
char *curr = NULL;
|
|
|
|
|
char *next = NULL;
|
|
|
|
|
int count = 0;
|
|
|
|
|
unsigned int len;
|
|
|
|
|
size_t copy_len = 0;
|
|
|
|
|
|
|
|
|
|
if (string == NULL)
|
|
|
|
|
return (char **)NULL;
|
|
|
|
|
if (token == NULL)
|
|
|
|
|
return (char **)NULL;
|
|
|
|
|
if (limit == 0)
|
|
|
|
|
return (char **)NULL;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
len = strlen(string);
|
2014-09-20 10:59:54 +02:00
|
|
|
if (strlen(token) > len)
|
|
|
|
|
return (char **)NULL;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
curr = string;
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
do {
|
2014-09-15 20:00:05 +02:00
|
|
|
// alloc space for current item plus NULL vector terminator
|
2014-09-20 10:59:54 +02:00
|
|
|
results = (char **)realloc(results, sizeof(char *) * (count + 2));
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
// find the next occurrence
|
2014-09-20 10:59:54 +02:00
|
|
|
next = strstr(curr, token);
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
if (next != NULL)
|
2014-09-15 20:00:05 +02:00
|
|
|
copy_len = next - curr;
|
|
|
|
|
else
|
|
|
|
|
copy_len = strlen(curr);
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
results[count] = (char *)calloc(copy_len + 1, sizeof(char));
|
|
|
|
|
memcpy(results[count], curr, copy_len);
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
count++;
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
if (next == NULL)
|
|
|
|
|
break;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
curr = next;
|
|
|
|
|
curr++;
|
|
|
|
|
}
|
2014-09-20 10:59:54 +02:00
|
|
|
while (count < limit);
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
results[count] = NULL;
|
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
}
|