view libpurple/stringref.c @ 16307:9326d4cf5497

If anyone sees the "Unable to add buddy 1" message after this commit, please let me know. More changes in an effort to get rid of the "Unable to Add, Could not add the buddy 1 for an unknown reason. The most common reason for this is that you have the maximum number of allowed buddies in your buddy list" message. My previous checkin fixed a problem that resulted in the same error, but the cause was completely different. The important change in this commit is the one in aim_ssi_itemlist_add(). Apparently there's this funky thing where items in the master group can't have a buddy ID equal to any group ID. Who knew? There are a few other minor changes in this commit. I added a "break" when looping through the items making sure we don't pick a buddy ID that's already in use. And added some checks to make sure we never try to update our data if we haven't received the list from the server yet. Oh, and the 2 bytes that specify the length of the checksum for the icon are two separate values. The first byte is either a 0 or a 1 and I don't know what it means. The second byte is the length of the checksum.
author Mark Doliner <mark@kingant.net>
date Mon, 23 Apr 2007 01:05:27 +0000
parents 32c366eeeb99
children 44b4e8bd759b
line wrap: on
line source

/**
 * @file stringref.c Reference-counted immutable strings
 * @ingroup core
 *
 * purple
 *
 * Purple is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "internal.h"

#include <string.h>
#include <stdarg.h>

#include "debug.h"
#include "stringref.h"

/**
 * The internal representation of a stringref.
 *
 * @note For this structure to be useful, the string contained within
 * it must be immutable -- for this reason, do _not_ access it
 * directly!
 */
struct _PurpleStringref {
	guint32 ref;	/**< The reference count of this string.
					 *   Note that reference counts are only
					 *   31 bits, and the high-order bit
					 *   indicates whether this string is up
					 *   for GC at the next idle handler...
					 *   But you aren't going to touch this
					 *   anyway, right? */
	char value[1];	/**< The string contained in this ref.
					 *   Notice that it is simply "hanging
					 *   off the end" of the ref ... this
					 *   is to save an allocation. */
};

#define REFCOUNT(x) ((x) & 0x7fffffff)

static GList *gclist = NULL;

static void stringref_free(PurpleStringref *stringref);
static gboolean gs_idle_cb(gpointer data);

PurpleStringref *purple_stringref_new(const char *value)
{
	PurpleStringref *newref;

	if (value == NULL)
		return NULL;

	newref = g_malloc(sizeof(PurpleStringref) + strlen(value));
	strcpy(newref->value, value);
	newref->ref = 1;

	return newref;
}

PurpleStringref *purple_stringref_new_noref(const char *value)
{
	PurpleStringref *newref;

	if (value == NULL)
		return NULL;

	newref = g_malloc(sizeof(PurpleStringref) + strlen(value));
	strcpy(newref->value, value);
	newref->ref = 0x80000000;

	if (gclist == NULL)
		g_idle_add(gs_idle_cb, NULL);
	gclist = g_list_prepend(gclist, newref);

	return newref;
}

PurpleStringref *purple_stringref_printf(const char *format, ...)
{
	PurpleStringref *newref;
	va_list ap;

	if (format == NULL)
		return NULL;

	va_start(ap, format);
	newref = g_malloc(sizeof(PurpleStringref) + g_printf_string_upper_bound(format, ap));
	vsprintf(newref->value, format, ap);
	va_end(ap);
	newref->ref = 1;

	return newref;
}

PurpleStringref *purple_stringref_ref(PurpleStringref *stringref)
{
	if (stringref == NULL)
		return NULL;
	stringref->ref++;
	return stringref;
}

void purple_stringref_unref(PurpleStringref *stringref)
{
	if (stringref == NULL)
		return;
	if (REFCOUNT(--(stringref->ref)) == 0) {
		if (stringref->ref & 0x80000000)
			gclist = g_list_remove(gclist, stringref);
		stringref_free(stringref);
	}
}

const char *purple_stringref_value(const PurpleStringref *stringref)
{
	return (stringref == NULL ? NULL : stringref->value);
}

int purple_stringref_cmp(const PurpleStringref *s1, const PurpleStringref *s2)
{
	return (s1 == s2 ? 0 : strcmp(purple_stringref_value(s1), purple_stringref_value(s2)));
}

size_t purple_stringref_len(const PurpleStringref *stringref)
{
	return strlen(purple_stringref_value(stringref));
}

static void stringref_free(PurpleStringref *stringref)
{
#ifdef DEBUG
	if (REFCOUNT(stringref->ref) != 0) {
		purple_debug(PURPLE_DEBUG_ERROR, "stringref", "Free of nonzero (%d) ref stringref!\n", REFCOUNT(stringref->ref));
		return;
	}
#endif /* DEBUG */
	g_free(stringref);
}

static gboolean gs_idle_cb(gpointer data)
{
	PurpleStringref *ref;
	GList *del;

	while (gclist != NULL) {
		ref = gclist->data;
		if (REFCOUNT(ref->ref) == 0) {
			stringref_free(ref);
		}
		del = gclist;
		gclist = gclist->next;
		g_list_free_1(del);
	}

	return FALSE;
}