-
Notifications
You must be signed in to change notification settings - Fork 29
/
explode.c
executable file
·48 lines (42 loc) · 1.07 KB
/
explode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// explode.m
// AnsiLove/C
//
// Copyright (C) 2011-2014 Stefan Vogt, Brian Cassidy, Frederic Cambus.
// All rights reserved.
// https://github.com/ByteProject/AnsiLove-C/
//
// Use of this source code is governed by a MIT-style license.
// See the file LICENSE for details.
//
#if defined(__APPLE__) && defined(__MACH__)
#import "explode.h"
#else
#include "explode.h"
#endif
int32_t explode(char ***arr_ptr, char delimiter, char *str)
{
char *src = str, *end, *dst;
char **arr;
int32_t size = 1, i;
while ((end = strchr(src, delimiter)) != NULL)
{
++size;
src = end + 1;
}
arr = malloc(size * sizeof(char *) + (strlen(str) + 1) * sizeof(char));
src = str;
dst = (char *) arr + size * sizeof(char *);
for (i = 0; i < size; ++i)
{
if ((end = strchr(src, delimiter)) == NULL)
end = src + strlen(src);
arr[i] = dst;
strncpy(dst, src, end - src);
dst[end - src] = '\0';
dst += end - src + 1;
src = end + 1;
}
*arr_ptr = arr;
return size;
}