7763
|
1 /**
|
|
2 * @file stringref.c Reference-counted strings
|
|
3 * @ingroup core
|
|
4 *
|
|
5 * gaim
|
|
6 *
|
|
7 * Copyright (C) 2003 Ethan Blanton <elb@elitists.net>
|
|
8 *
|
|
9 * This program is free software; you can redistribute it and/or modify
|
|
10 * it under the terms of the GNU General Public License as published by
|
|
11 * the Free Software Foundation; either version 2 of the License, or
|
|
12 * (at your option) any later version.
|
|
13 *
|
|
14 * This program is distributed in the hope that it will be useful,
|
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 * GNU General Public License for more details.
|
|
18 *
|
|
19 * You should have received a copy of the GNU General Public License
|
|
20 * along with this program; if not, write to the Free Software
|
|
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
22 *
|
|
23 */
|
|
24
|
|
25 #include "internal.h"
|
|
26
|
|
27 #include <string.h>
|
7767
|
28 #include <stdarg.h>
|
7763
|
29
|
|
30 #include "stringref.h"
|
|
31
|
|
32 GaimStringref *gaim_stringref_new(const char *value)
|
|
33 {
|
|
34 GaimStringref *newref;
|
|
35
|
|
36 newref = g_malloc(sizeof(GaimStringref) + strlen(value) + 1);
|
|
37 strcpy(newref->value, value);
|
|
38 newref->ref = 1;
|
|
39
|
|
40 return newref;
|
|
41 }
|
|
42
|
7767
|
43 GaimStringref *gaim_stringref_printf(const char *format, ...)
|
|
44 {
|
|
45 GaimStringref *newref;
|
|
46 va_list ap;
|
|
47
|
|
48 if (format == NULL)
|
|
49 return NULL;
|
|
50
|
|
51 va_start(ap, format);
|
|
52 newref = g_malloc(sizeof(GaimStringref) + g_printf_string_upper_bound(format, ap));
|
|
53 vsprintf(newref->value, format, ap);
|
|
54 va_end(ap);
|
|
55
|
|
56 return newref;
|
|
57 }
|
|
58
|
7763
|
59 GaimStringref *gaim_stringref_ref(GaimStringref *stringref)
|
|
60 {
|
|
61 if (stringref == NULL)
|
|
62 return NULL;
|
|
63 stringref->ref++;
|
|
64 return stringref;
|
|
65 }
|
|
66
|
|
67 void gaim_stringref_unref(GaimStringref *stringref)
|
|
68 {
|
|
69 g_return_if_fail(stringref != NULL);
|
|
70 if (--stringref->ref == 0)
|
|
71 g_free(stringref);
|
|
72 }
|
|
73
|
|
74 const char *gaim_stringref_value(GaimStringref *stringref)
|
|
75 {
|
|
76 return (stringref == NULL ? NULL : stringref->value);
|
|
77 }
|