summaryrefslogtreecommitdiff
path: root/param_array.c
blob: ad79f4164ac35276f35276d6185f485cfca1dced (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* ------------------------------------------------------------------
 * param_array.c
 * Utility to extract parameters from a string. Parameters are either
 * splitted by space characters, or wrapped by double quotations.
 * Please refer to main() for more usage tips.
 *
 * To unit-test this utility, try this:
 * $ make EXTRA_DEBUG_MSG= param_test
 * $ ./param_test
 * ----------------------------------------------------------------*/

#include <stdbool.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef EXTRA_DEBUG_MSG
#define PARAM_DEBUG_PRINT printf
#else
#define PARAM_DEBUG_PRINT(fmt, ...) \
		do {;} while (0)
#endif

#define SKIP(p) \
{\
	while (*p != '\0') {\
		if (isspace(*p)) {\
			p++;\
		} else {\
			if (*p == '\"') {\
				with_first_double_quotation = true;\
				p++;\
			}\
			break;\
		}\
	}\
}

static bool with_first_double_quotation = false;

static inline bool WANT(const char *p)
{
	if (with_first_double_quotation) {
		if (*p != '\0') {
			if (*p == '\"')
				with_first_double_quotation = false;
			return true;
		}
		return false;
	}

	return (*p && !isspace (*p));
}

/* Count the number of arguments. */
int count_param_num(const char * input)
{
	const char *p;
	int param_num = 0;

	p = input;

	/* lines which start with '#' are comments only */
	if (*p == '#')
		return 0;

	PARAM_DEBUG_PRINT("with_fdq=%d\n", with_first_double_quotation);
	while (*p) {
		SKIP(p);
		PARAM_DEBUG_PRINT("with_fdq=%d\n", with_first_double_quotation);
		if (WANT(p)) {
			param_num ++;
			PARAM_DEBUG_PRINT("At %d, p=%c, param_num=%d\n", (int)(p - input), *p, param_num);
			while (WANT (p))  /* move p till next delimiter */
				p++;
		}
	}
	return param_num;
}

/* copy split parameters into param_array
 * Input:
 *   - input: string to be used
 *   - param_num: number of parameters
 *   - param_array: a char* array at size of param_num
 * Output:
 *   - param_array: parameters to be copied to
 * Return:
 *   - 0: success
 *   - -1: failure
 */
static int copy_param_array(const char *input, int param_num, char **param_array)
{
	int i = 0;
	const char *p;

	p = input;
	while (*p) {
		SKIP(p);
		if (WANT(p)) {
			const char *end = p;
			char *copy;

			while (WANT(end))
				end++;

			PARAM_DEBUG_PRINT("i=%d, size=%d\n", i, (int)(end - p + 1));

			param_array[i] = malloc(end - p + 1);
			copy = param_array[i];

			if (param_array[i] == NULL)
				goto error_return;

			strncpy(copy, p, end - p + 1);
			PARAM_DEBUG_PRINT("*p=%c, *end=%c\n", *p, *end);
			*(copy + (end - p)) = '\0';
			/* exclude the last " */
			if (*(copy + (end - p) - 1) == '\"')
				*(copy + (end - p) - 1) = '\0';

			p = end;
			PARAM_DEBUG_PRINT("param_array[i]=%s\n", param_array[i]);
			i++;
		}
	}

	PARAM_DEBUG_PRINT("Line %d, %s()\n", __LINE__, __FUNCTION__);
	PARAM_DEBUG_PRINT("i=%d, param_num=%d\n", i, param_num);

	if (i != param_num)
		goto error_return;
	return 0;

error_return:
	PARAM_DEBUG_PRINT("Line %d, %s(), error_return\n", __LINE__, __FUNCTION__);

	/* clear all allocated memory */
	i = 0;
	while (i < param_num && param_array[i] != NULL) {
		free(param_array[i]);
		param_array[i] = NULL;
		i++;
	}
	return -1;
}

#undef SKIP

/* build param_num and param_array from an input string
 * Input:
 *   - input: string to be used
 * Output:
 *   - ptr_param_num: number of parameters
 *   - ptr_param_array: parameters in a char* array
 * Return:
 *   - 0: success
 *   - -1: failure
 */
int build_param_num_array(const char *input, int *ptr_param_num, char ***ptr_param_array)
{
	int param_num;
	char **param_array;

	param_num = count_param_num(input);
	if (param_num == 0)
	        return -1;

	param_array = malloc(sizeof(char *) * param_num);
	if (param_array == NULL)
		return -1;

	memset(param_array, '\0', sizeof(char *) * param_num);

	PARAM_DEBUG_PRINT("Line %d, %s()\n", __LINE__, __FUNCTION__);

	if (copy_param_array(input, param_num, param_array) == -1) {
		/* clear allocated resources on failure */
		free(param_array);
		return -1;
	}

	*ptr_param_num = param_num;
	*ptr_param_array = param_array;

	return 0;
}

/* free memory allocated for param_array
 * Input:
 *   - param_num: number of parameters
 *   - param_array: parameters in a char* array
 * Return:
 *   - 0: success
 *   - -1: failure
 */
int free_param_array(int param_num, char **param_array)
{
	int i;

	for (i = 0; i < param_num; i++)
		free(param_array[i]);

	free(param_array);

	return 0;
}

#ifdef PARAM_DEBUG_TEST_STUB
int main()
{
	char string_array[][100]={
		"this is an \"example to\" show how \"double\"quotations\" are processed in this tool",
		"this is an \"example to\" show how \t   spaces are processed: father mother i    love \t you!\n",
		"create_chan my2",
		"send_msg my2 \"hello world, stm!\""
		"",
		"",
		"# hi, this is a comment\n"
		};

	int ret;
	int param_num;
	char **param_array;
	int i, j;

	for (j = 0; j < sizeof(string_array)/sizeof(string_array[0]); j++) {

		printf("#####\n");
		printf("Original string: %s<\n", string_array[j]);
		printf("#####\n");

		ret = build_param_num_array(string_array[j], &param_num, &param_array);
		if (ret == -1) {
			printf("error returned from build_param_num_array.\n");
			continue;
		}

		PARAM_DEBUG_PRINT("Line %d, %s()\n", __LINE__, __FUNCTION__);

		for (i = 0; i < param_num; i++)
			printf("Param %d: %s<\n", i, param_array[i]);

		free_param_array(param_num, param_array);
	}

	return 0;
}
#endif