| |
1 /** |
| |
2 * @file command.c MSN command functions |
| |
3 * |
| |
4 * purple |
| |
5 * |
| |
6 * Purple is the legal property of its developers, whose names are too numerous |
| |
7 * to list here. Please refer to the COPYRIGHT file distributed with this |
| |
8 * source distribution. |
| |
9 * |
| |
10 * This program is free software; you can redistribute it and/or modify |
| |
11 * it under the terms of the GNU General Public License as published by |
| |
12 * the Free Software Foundation; either version 2 of the License, or |
| |
13 * (at your option) any later version. |
| |
14 * |
| |
15 * This program is distributed in the hope that it will be useful, |
| |
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| |
18 * GNU General Public License for more details. |
| |
19 * |
| |
20 * You should have received a copy of the GNU General Public License |
| |
21 * along with this program; if not, write to the Free Software |
| |
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
| |
23 */ |
| |
24 #include "msn.h" |
| |
25 #include "command.h" |
| |
26 |
| |
27 static gboolean |
| |
28 is_num(char *str) |
| |
29 { |
| |
30 char *c; |
| |
31 for (c = str; *c; c++) { |
| |
32 if (!(g_ascii_isdigit(*c))) |
| |
33 return FALSE; |
| |
34 } |
| |
35 |
| |
36 return TRUE; |
| |
37 } |
| |
38 |
| |
39 MsnCommand * |
| |
40 msn_command_from_string(const char *string) |
| |
41 { |
| |
42 MsnCommand *cmd; |
| |
43 char *tmp; |
| |
44 char *param_start; |
| |
45 |
| |
46 g_return_val_if_fail(string != NULL, NULL); |
| |
47 |
| |
48 tmp = g_strdup(string); |
| |
49 param_start = strchr(tmp, ' '); |
| |
50 |
| |
51 cmd = g_new0(MsnCommand, 1); |
| |
52 cmd->command = tmp; |
| |
53 |
| |
54 if (param_start) |
| |
55 { |
| |
56 *param_start++ = '\0'; |
| |
57 cmd->params = g_strsplit(param_start, " ", 0); |
| |
58 } |
| |
59 |
| |
60 if (cmd->params != NULL) |
| |
61 { |
| |
62 char *param; |
| |
63 int c; |
| |
64 |
| |
65 for (c = 0; cmd->params[c]; c++); |
| |
66 cmd->param_count = c; |
| |
67 |
| |
68 param = cmd->params[0]; |
| |
69 |
| |
70 cmd->trId = is_num(param) ? atoi(param) : 0; |
| |
71 } |
| |
72 else |
| |
73 cmd->trId = 0; |
| |
74 |
| |
75 msn_command_ref(cmd); |
| |
76 |
| |
77 return cmd; |
| |
78 } |
| |
79 |
| |
80 void |
| |
81 msn_command_destroy(MsnCommand *cmd) |
| |
82 { |
| |
83 g_return_if_fail(cmd != NULL); |
| |
84 |
| |
85 if (cmd->ref_count > 0) |
| |
86 { |
| |
87 msn_command_unref(cmd); |
| |
88 return; |
| |
89 } |
| |
90 |
| |
91 if (cmd->payload != NULL) |
| |
92 g_free(cmd->payload); |
| |
93 |
| |
94 g_free(cmd->command); |
| |
95 g_strfreev(cmd->params); |
| |
96 g_free(cmd); |
| |
97 } |
| |
98 |
| |
99 MsnCommand * |
| |
100 msn_command_ref(MsnCommand *cmd) |
| |
101 { |
| |
102 g_return_val_if_fail(cmd != NULL, NULL); |
| |
103 |
| |
104 cmd->ref_count++; |
| |
105 return cmd; |
| |
106 } |
| |
107 |
| |
108 MsnCommand * |
| |
109 msn_command_unref(MsnCommand *cmd) |
| |
110 { |
| |
111 g_return_val_if_fail(cmd != NULL, NULL); |
| |
112 g_return_val_if_fail(cmd->ref_count > 0, NULL); |
| |
113 |
| |
114 cmd->ref_count--; |
| |
115 |
| |
116 if (cmd->ref_count == 0) |
| |
117 { |
| |
118 msn_command_destroy(cmd); |
| |
119 return NULL; |
| |
120 } |
| |
121 |
| |
122 return cmd; |
| |
123 } |