File: split.c

package info (click to toggle)
shell-fm 0.7%2Bgit20100414-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 444 kB
  • ctags: 305
  • sloc: ansic: 4,422; makefile: 135; python: 80; haskell: 76; sh: 67; perl: 19
file content (45 lines) | stat: -rw-r--r-- 859 bytes parent folder | download | duplicates (2)
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
/*
	Copyright (C) 2006 by Jonas Kramer
	Published under the terms of the GNU General Public License (GPL).
*/


#include <string.h>
#include <stdlib.h>
#include <assert.h>

#include "split.h"

char ** split(char * string, const char * del, unsigned * pnsplt) {
	char ** splt = NULL;
	unsigned nsplt = 0;
	register char * ptr = string;

	if(!string)
		return NULL;
	
	while(* ptr) {
		while(* ptr && strchr(del, * ptr))
			++ptr;
		if(* ptr) {
			register unsigned length = 0;

			splt = realloc(splt, sizeof(char *) * (nsplt + 2));
			assert(splt != NULL);

			splt[nsplt] = calloc(strlen(ptr) + 1, sizeof(char));
			assert(splt[nsplt] != NULL);

			while(* ptr && !strchr(del, * ptr))
				splt[nsplt][length++] = * (ptr++);

			splt[nsplt] = realloc(splt[nsplt], length + 1);
			splt[++nsplt] = NULL;
		}
	}

	if(pnsplt)
		* pnsplt = nsplt;

	return splt;
}