File: strexp.c

package info (click to toggle)
yiff 2.06-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 3,464 kB
  • ctags: 3,028
  • sloc: ansic: 47,463; makefile: 218; sh: 77
file content (99 lines) | stat: -rw-r--r-- 1,714 bytes parent folder | download | duplicates (6)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <ctype.h>
#include <string.h>

#include "../include/strexp.h"


char **strexp(char *str, int *n)
{
	char **ret;
	char *head, *tail;
	int num;
        int len;


	/* return NULL if given an empty string */
	if(!str)
		return(NULL);

	num = 0;
	head = tail = str;
	ret = NULL;

	/* iterate while we're inside the string */
	while(*head)
	{
		/* skip to next non-blank character */
		while(isblank(*head))
			head++;

		/* find next blank character */
		tail = head;
		while(*tail && !isblank(*tail))
			tail++;

		/* determine length of substring */
		len = (tail - head);

		/* add string to list */
		num++;
		ret = (char **)realloc(ret, num*sizeof(char *));
		ret[num - 1] = (char *)malloc(len + 1);

		/* copy string over and null-terminate */
		strncpy (ret[num - 1], head, len);
		ret[num-1][len] = '\0';

		/* get ready for next substring */
		head = tail;
	}

	/* we're done... */
	*n = num;
	return ret;
}

char **strchrexp(char *str, char c, int *n)
{
	char **ret;
	char *head, *tail;
	int num;
        int len;


	/* return NULL if given an empty string */
	if(!str)
		return(NULL);

	num = 0;
	head = tail = str;
	ret = NULL;

	/* iterate while we're inside the string */
	while (*head && *tail)
	{
		/* find next blank character */
		tail = head;
		while (*tail && (*tail != c))
			tail++;

		/* determine length of substring */
		len = (tail - head);

		/* add string to list */
		num++;
		ret = (char **)realloc(ret, num*sizeof(char *));
		ret[num - 1] = (char *)malloc(len + 1);

		/* copy string over and null-terminate */
		strncpy (ret[num - 1], head, len);
		ret[num-1][len] = '\0';

		/* get ready for next field */
		head = tail + 1;
	}

	/* we're done... */
	*n = num;
	return ret;
}