Mercurial > pidgin
annotate src/protocols/msn/msnobject.c @ 7008:ecce6b23ea25
[gaim-migrate @ 7565]
plug a big memleak, and simplify some code in the process
committer: Tailor Script <tailor@pidgin.im>
| author | Nathan Walp <nwalp@pidgin.im> |
|---|---|
| date | Mon, 29 Sep 2003 04:30:35 +0000 |
| parents | 1aea5e6186bd |
| children | f0784ce8189a |
| rev | line source |
|---|---|
| 6701 | 1 /** |
| 2 * @file msnobject.c MSNObject API | |
| 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 "msnobject.h" | |
| 23 | |
| 24 #define GET_STRING_TAG(field, id) \ | |
| 25 if ((tag = strstr(str, id "=\"")) != NULL) \ | |
| 26 { \ | |
| 27 tag += strlen(id "=\""); \ | |
| 28 c = strchr(tag, '"'); \ | |
|
6790
1aea5e6186bd
[gaim-migrate @ 7329]
Christian Hammond <chipx86@chipx86.com>
parents:
6789
diff
changeset
|
29 obj->field = g_strndup(tag, c - tag); \ |
| 6701 | 30 } |
| 31 | |
| 32 #define GET_INT_TAG(field, id) \ | |
| 33 if ((tag = strstr(str, id "=\"")) != NULL) \ | |
| 34 { \ | |
| 35 char buf[16]; \ | |
| 36 tag += strlen(id "=\""); \ | |
| 37 c = strchr(tag, '"'); \ | |
|
6790
1aea5e6186bd
[gaim-migrate @ 7329]
Christian Hammond <chipx86@chipx86.com>
parents:
6789
diff
changeset
|
38 strncpy(buf, tag, c - tag); \ |
| 6701 | 39 obj->field = atoi(buf); \ |
| 40 } | |
| 41 | |
| 42 MsnObject * | |
| 43 msn_object_new(void) | |
| 44 { | |
| 45 MsnObject *obj; | |
| 46 | |
| 47 obj = g_new0(MsnObject, 1); | |
| 48 | |
| 49 msn_object_set_type(obj, MSN_OBJECT_UNKNOWN); | |
| 50 msn_object_set_friendly(obj, "AAA="); | |
| 51 | |
| 52 return obj; | |
| 53 } | |
| 54 | |
| 55 MsnObject * | |
| 56 msn_object_new_from_string(const char *str) | |
| 57 { | |
| 58 MsnObject *obj; | |
| 59 char *tag, *c; | |
| 60 | |
| 61 g_return_val_if_fail(str != NULL, NULL); | |
| 62 g_return_val_if_fail(!strncmp(str, "<msnobj ", 8), NULL); | |
| 63 | |
| 64 obj = msn_object_new(); | |
| 65 | |
| 66 GET_STRING_TAG(creator, "Creator"); | |
| 67 GET_INT_TAG(size, "Size"); | |
| 68 GET_INT_TAG(type, "Type"); | |
| 69 GET_STRING_TAG(location, "Location"); | |
| 70 GET_STRING_TAG(friendly, "Friendly"); | |
| 71 GET_STRING_TAG(sha1d, "SHA1D"); | |
| 72 GET_STRING_TAG(sha1c, "SHA1C"); | |
| 73 | |
| 74 return obj; | |
| 75 } | |
| 76 | |
|
6789
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
77 void |
|
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
78 msn_object_destroy(MsnObject *obj) |
|
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
79 { |
|
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
80 g_return_if_fail(obj != NULL); |
|
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
81 |
|
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
82 if (obj->creator != NULL) |
|
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
83 g_free(obj->creator); |
|
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
84 |
|
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
85 if (obj->location != NULL) |
|
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
86 g_free(obj->location); |
|
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
87 |
|
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
88 if (obj->friendly != NULL) |
|
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
89 g_free(obj->friendly); |
|
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
90 |
|
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
91 if (obj->sha1d != NULL) |
|
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
92 g_free(obj->sha1d); |
|
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
93 |
|
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
94 if (obj->sha1c != NULL) |
|
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
95 g_free(obj->sha1c); |
|
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
96 |
|
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
97 g_free(obj); |
|
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
98 } |
|
94b575afb77e
[gaim-migrate @ 7328]
Christian Hammond <chipx86@chipx86.com>
parents:
6701
diff
changeset
|
99 |
| 6701 | 100 char * |
| 101 msn_object_to_string(const MsnObject *obj) | |
| 102 { | |
| 103 char *str; | |
| 104 | |
| 105 g_return_val_if_fail(obj != NULL, NULL); | |
| 106 | |
| 107 str = g_strdup_printf("<msnobj Creator=\"%s\" Size=\"%d\" Type=\"%d\" " | |
| 108 "Location=\"%s\" Friendly=\"%s\" SHA1D=\"%s\" " | |
| 109 "SHA1C=\"%s\"/>", | |
| 110 msn_object_get_creator(obj), | |
| 111 msn_object_get_size(obj), | |
| 112 msn_object_get_type(obj), | |
| 113 msn_object_get_location(obj), | |
| 114 msn_object_get_friendly(obj), | |
| 115 msn_object_get_sha1d(obj), | |
| 116 msn_object_get_sha1c(obj)); | |
| 117 | |
| 118 return str; | |
| 119 } | |
| 120 | |
| 121 void | |
| 122 msn_object_set_creator(MsnObject *obj, const char *creator) | |
| 123 { | |
| 124 g_return_if_fail(obj != NULL); | |
| 125 | |
| 126 if (obj->creator != NULL) | |
| 127 g_free(obj->creator); | |
| 128 | |
| 129 obj->creator = (creator == NULL ? NULL : g_strdup(creator)); | |
| 130 } | |
| 131 | |
| 132 void | |
| 133 msn_object_set_size(MsnObject *obj, int size) | |
| 134 { | |
| 135 g_return_if_fail(obj != NULL); | |
| 136 | |
| 137 obj->size = size; | |
| 138 } | |
| 139 | |
| 140 void | |
| 141 msn_object_set_type(MsnObject *obj, MsnObjectType type) | |
| 142 { | |
| 143 g_return_if_fail(obj != NULL); | |
| 144 | |
| 145 obj->type = type; | |
| 146 } | |
| 147 | |
| 148 void | |
| 149 msn_object_set_location(MsnObject *obj, const char *location) | |
| 150 { | |
| 151 g_return_if_fail(obj != NULL); | |
| 152 | |
| 153 if (obj->location != NULL) | |
| 154 g_free(obj->location); | |
| 155 | |
| 156 obj->location = (location == NULL ? NULL : g_strdup(location)); | |
| 157 } | |
| 158 | |
| 159 void | |
| 160 msn_object_set_friendly(MsnObject *obj, const char *friendly) | |
| 161 { | |
| 162 g_return_if_fail(obj != NULL); | |
| 163 | |
| 164 if (obj->friendly != NULL) | |
| 165 g_free(obj->friendly); | |
| 166 | |
| 167 obj->friendly = (friendly == NULL ? NULL : g_strdup(friendly)); | |
| 168 } | |
| 169 | |
| 170 void | |
| 171 msn_object_set_sha1d(MsnObject *obj, const char *sha1d) | |
| 172 { | |
| 173 g_return_if_fail(obj != NULL); | |
| 174 | |
| 175 if (obj->sha1d != NULL) | |
| 176 g_free(obj->sha1d); | |
| 177 | |
| 178 obj->sha1d = (sha1d == NULL ? NULL : g_strdup(sha1d)); | |
| 179 } | |
| 180 | |
| 181 void | |
| 182 msn_object_set_sha1c(MsnObject *obj, const char *sha1c) | |
| 183 { | |
| 184 g_return_if_fail(obj != NULL); | |
| 185 | |
| 186 if (obj->sha1c != NULL) | |
| 187 g_free(obj->sha1c); | |
| 188 | |
| 189 obj->sha1c = (sha1c == NULL ? NULL : g_strdup(sha1c)); | |
| 190 } | |
| 191 | |
| 192 const char * | |
| 193 msn_object_get_creator(const MsnObject *obj) | |
| 194 { | |
| 195 g_return_val_if_fail(obj != NULL, NULL); | |
| 196 | |
| 197 return obj->creator; | |
| 198 } | |
| 199 | |
| 200 int | |
| 201 msn_object_get_size(const MsnObject *obj) | |
| 202 { | |
| 203 g_return_val_if_fail(obj != NULL, 0); | |
| 204 | |
| 205 return obj->size; | |
| 206 } | |
| 207 | |
| 208 MsnObjectType | |
| 209 msn_object_get_type(const MsnObject *obj) | |
| 210 { | |
| 211 g_return_val_if_fail(obj != NULL, MSN_OBJECT_UNKNOWN); | |
| 212 | |
| 213 return obj->type; | |
| 214 } | |
| 215 | |
| 216 const char * | |
| 217 msn_object_get_location(const MsnObject *obj) | |
| 218 { | |
| 219 g_return_val_if_fail(obj != NULL, NULL); | |
| 220 | |
| 221 return obj->location; | |
| 222 } | |
| 223 | |
| 224 const char * | |
| 225 msn_object_get_friendly(const MsnObject *obj) | |
| 226 { | |
| 227 g_return_val_if_fail(obj != NULL, NULL); | |
| 228 | |
| 229 return obj->friendly; | |
| 230 } | |
| 231 | |
| 232 const char * | |
| 233 msn_object_get_sha1d(const MsnObject *obj) | |
| 234 { | |
| 235 g_return_val_if_fail(obj != NULL, NULL); | |
| 236 | |
| 237 return obj->sha1d; | |
| 238 } | |
| 239 | |
| 240 const char * | |
| 241 msn_object_get_sha1c(const MsnObject *obj) | |
| 242 { | |
| 243 g_return_val_if_fail(obj != NULL, NULL); | |
| 244 | |
| 245 return obj->sha1c; | |
| 246 } |
