summaryrefslogtreecommitdiff
path: root/pn_cmd.c
blob: ae0dc4ad12494b0f7549ec8916455824afb72912 (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
#include "pn_cmd.h"

#include <glib.h>

#include <stdlib.h> /* for strtol */
#include <string.h> /* for strchr */
#include <stdbool.h>

struct pn_cmd *
pn_cmd_new_from_string(const char *str, size_t len)
{
	struct pn_cmd *cmd;
	char *param_start;

	cmd = g_malloc0(sizeof(*cmd));

	cmd->base = g_strndup(str, len);
	param_start = strchr(cmd->base, ' ');

	if (param_start) {
		*param_start++ = '\0';
		if (param_start[0])
			cmd->paramv = g_strsplit(param_start, " ", 0);
	}

	if (cmd->paramv && cmd->paramv[0]) {
		const char *param;
		int c;

		for (c = 0; cmd->paramv[c]; c++)
			;
		cmd->paramc = c;

		param = cmd->paramv[0];

		cmd->tr_id = strtol(param, NULL, 10);;
	}
	else
		cmd->tr_id = 0;

	return cmd;
}

void
pn_cmd_free(struct pn_cmd *cmd)
{
	g_free(cmd->base);
	g_strfreev(cmd->paramv);

	g_free(cmd);
}