~micahg/ubuntu/natty/pidgin/2.7.9-2

« back to all changes in this revision

Viewing changes to libpurple/protocols/oscar/util.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-11-05 19:44:21 UTC
  • mfrom: (62.1.1 maverick-security) (2.3.12 sid)
  • Revision ID: james.westby@ubuntu.com-20101105194421-8r8o4pzw2m5j4hiy
Tags: 1:2.7.5-1ubuntu1
Resync on Debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
107
107
        return g_strdup_printf("%s/%s", name, version);;
108
108
}
109
109
 
110
 
/*
111
 
 * Tokenizing functions.  Used to portably replace strtok/sep.
112
 
 *   -- DMP.
113
 
 *
114
 
 */
115
 
/* TODO: Get rid of this and use glib functions */
116
 
int
117
 
aimutil_tokslen(char *toSearch, int theindex, char dl)
118
 
{
119
 
        int curCount = 1;
120
 
        char *next;
121
 
        char *last;
122
 
        int toReturn;
123
 
 
124
 
        last = toSearch;
125
 
        next = strchr(toSearch, dl);
126
 
 
127
 
        while(curCount < theindex && next != NULL) {
128
 
                curCount++;
129
 
                last = next + 1;
130
 
                next = strchr(last, dl);
131
 
        }
132
 
 
133
 
        if ((curCount < theindex) || (next == NULL))
134
 
                toReturn = strlen(toSearch) - (curCount - 1);
135
 
        else
136
 
                toReturn = next - toSearch - (curCount - 1);
137
 
 
138
 
        return toReturn;
139
 
}
140
 
 
141
 
int
142
 
aimutil_itemcnt(char *toSearch, char dl)
143
 
{
144
 
        int curCount;
145
 
        char *next;
146
 
 
147
 
        curCount = 1;
148
 
 
149
 
        next = strchr(toSearch, dl);
150
 
 
151
 
        while(next != NULL) {
152
 
                curCount++;
153
 
                next = strchr(next + 1, dl);
154
 
        }
155
 
 
156
 
        return curCount;
157
 
}
158
 
 
159
 
char *
160
 
aimutil_itemindex(char *toSearch, int theindex, char dl)
161
 
{
162
 
        int curCount;
163
 
        char *next;
164
 
        char *last;
165
 
        char *toReturn;
166
 
 
167
 
        curCount = 0;
168
 
 
169
 
        last = toSearch;
170
 
        next = strchr(toSearch, dl);
171
 
 
172
 
        while (curCount < theindex && next != NULL) {
173
 
                curCount++;
174
 
                last = next + 1;
175
 
                next = strchr(last, dl);
176
 
        }
177
 
        next = strchr(last, dl);
178
 
 
179
 
        if (curCount < theindex) {
180
 
                toReturn = g_malloc(sizeof(char));
181
 
                *toReturn = '\0';
182
 
        } else {
183
 
                if (next == NULL) {
184
 
                        toReturn = g_malloc((strlen(last) + 1) * sizeof(char));
185
 
                        strcpy(toReturn, last);
186
 
                } else {
187
 
                        toReturn = g_malloc((next - last + 1) * sizeof(char));
188
 
                        memcpy(toReturn, last, (next - last));
189
 
                        toReturn[next - last] = '\0';
190
 
                }
191
 
        }
192
 
        return toReturn;
193
 
}
194
 
 
195
110
/**
196
111
 * Calculate the checksum of a given icon.
197
112
 */
323
238
 
324
239
        return 0;
325
240
}
 
241
 
 
242
/**
 
243
 * Looks for %n, %d, or %t in a string, and replaces them with the
 
244
 * specified name, date, and time, respectively.
 
245
 *
 
246
 * @param str  The string that may contain the special variables.
 
247
 * @param name The sender name.
 
248
 *
 
249
 * @return A newly allocated string where the special variables are
 
250
 *         expanded.  This should be g_free'd by the caller.
 
251
 */
 
252
gchar *
 
253
oscar_util_format_string(const char *str, const char *name)
 
254
{
 
255
        char *c;
 
256
        GString *cpy;
 
257
        time_t t;
 
258
        struct tm *tme;
 
259
 
 
260
        g_return_val_if_fail(str  != NULL, NULL);
 
261
        g_return_val_if_fail(name != NULL, NULL);
 
262
 
 
263
        /* Create an empty GString that is hopefully big enough for most messages */
 
264
        cpy = g_string_sized_new(1024);
 
265
 
 
266
        t = time(NULL);
 
267
        tme = localtime(&t);
 
268
 
 
269
        c = (char *)str;
 
270
        while (*c) {
 
271
                switch (*c) {
 
272
                case '%':
 
273
                        if (*(c + 1)) {
 
274
                                switch (*(c + 1)) {
 
275
                                case 'n':
 
276
                                        /* append name */
 
277
                                        g_string_append(cpy, name);
 
278
                                        c++;
 
279
                                        break;
 
280
                                case 'd':
 
281
                                        /* append date */
 
282
                                        g_string_append(cpy, purple_date_format_short(tme));
 
283
                                        c++;
 
284
                                        break;
 
285
                                case 't':
 
286
                                        /* append time */
 
287
                                        g_string_append(cpy, purple_time_format(tme));
 
288
                                        c++;
 
289
                                        break;
 
290
                                default:
 
291
                                        g_string_append_c(cpy, *c);
 
292
                                }
 
293
                        } else {
 
294
                                g_string_append_c(cpy, *c);
 
295
                        }
 
296
                        break;
 
297
                default:
 
298
                        g_string_append_c(cpy, *c);
 
299
                }
 
300
                c++;
 
301
        }
 
302
 
 
303
        return g_string_free(cpy, FALSE);
 
304
}
 
305
 
 
306
gchar *
 
307
oscar_format_buddies(GSList *buddies, const gchar *no_buddies_message)
 
308
{
 
309
        GSList *cur;
 
310
        GString *result;
 
311
        if (!buddies) {
 
312
                return g_strdup_printf("<i>%s</i>", no_buddies_message);
 
313
        }
 
314
        result = g_string_new("");
 
315
        for (cur = buddies; cur != NULL; cur = cur->next) {
 
316
                PurpleBuddy *buddy = cur->data;
 
317
                const gchar *bname = purple_buddy_get_name(buddy);
 
318
                const gchar *alias = purple_buddy_get_alias_only(buddy);
 
319
                g_string_append(result, bname);
 
320
                if (alias) {
 
321
                        g_string_append_printf(result, " (%s)", alias);
 
322
                }
 
323
                g_string_append(result, "<br>");
 
324
        }
 
325
        return g_string_free(result, FALSE);
 
326
}