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