Blame src/e-util/e-poolv.c

Packit 15f964
/*
Packit 15f964
 * e-poolv.c
Packit 15f964
 *
Packit 15f964
 * This program is free software; you can redistribute it and/or modify it
Packit 15f964
 * under the terms of the GNU Lesser General Public License as published by
Packit 15f964
 * the Free Software Foundation.
Packit 15f964
 *
Packit 15f964
 * This program is distributed in the hope that it will be useful, but
Packit 15f964
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 15f964
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 15f964
 * for more details.
Packit 15f964
 *
Packit 15f964
 * You should have received a copy of the GNU Lesser General Public License
Packit 15f964
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit 15f964
 *
Packit 15f964
 */
Packit 15f964
Packit 15f964
#include "evolution-config.h"
Packit 15f964
Packit 15f964
#include "e-poolv.h"
Packit 15f964
Packit 15f964
#include <string.h>
Packit 15f964
#include <camel/camel.h>
Packit 15f964
Packit 15f964
struct _EPoolv {
Packit 15f964
	guchar length;
Packit 15f964
	const gchar *s[1];
Packit 15f964
};
Packit 15f964
Packit 15f964
/**
Packit 15f964
 * e_poolv_new:
Packit 15f964
 * @size: The number of elements in the poolv, maximum of 254 elements.
Packit 15f964
 *
Packit 15f964
 * Create a new #EPoolv: a string vector which shares a global string
Packit 15f964
 * pool.  An #EPoolv can be used to work with arrays of strings which
Packit 15f964
 * save memory by eliminating duplicated allocations of the same string.
Packit 15f964
 *
Packit 15f964
 * This is useful when you have a log of read-only strings that do not
Packit 15f964
 * go away and are duplicated a lot, such as email headers.
Packit 15f964
 *
Packit 15f964
 * Returns: a new #EPoolv
Packit 15f964
 **/
Packit 15f964
EPoolv *
Packit 15f964
e_poolv_new (guint size)
Packit 15f964
{
Packit 15f964
	EPoolv *poolv;
Packit 15f964
Packit 15f964
	g_return_val_if_fail (size < 255, NULL);
Packit 15f964
Packit 15f964
	poolv = g_malloc0 (sizeof (*poolv) + (size - 1) * sizeof (gchar *));
Packit 15f964
	poolv->length = size;
Packit 15f964
Packit 15f964
	return poolv;
Packit 15f964
}
Packit 15f964
Packit 15f964
/**
Packit 15f964
 * e_poolv_set:
Packit 15f964
 * @poolv: pooled string vector
Packit 15f964
 * @index: index in vector of string
Packit 15f964
 * @str: string to set
Packit 15f964
 * @freeit: whether the caller is releasing its reference to the
Packit 15f964
 * string
Packit 15f964
 *
Packit 15f964
 * Set a string vector reference.  If the caller will no longer be
Packit 15f964
 * referencing the string, freeit should be TRUE.  Otherwise, this
Packit 15f964
 * will duplicate the string if it is not found in the pool.
Packit 15f964
 *
Packit 15f964
 * Returns: @poolv
Packit 15f964
 **/
Packit 15f964
EPoolv *
Packit 15f964
e_poolv_set (EPoolv *poolv,
Packit 15f964
             gint index,
Packit 15f964
             gchar *str,
Packit 15f964
             gint freeit)
Packit 15f964
{
Packit 15f964
	const gchar *old_str;
Packit 15f964
Packit 15f964
	g_return_val_if_fail (poolv != NULL, NULL);
Packit 15f964
	g_return_val_if_fail (index >= 0 && index < poolv->length, NULL);
Packit 15f964
Packit 15f964
	if (!str) {
Packit 15f964
		camel_pstring_free (poolv->s[index]);
Packit 15f964
		poolv->s[index] = NULL;
Packit 15f964
		return poolv;
Packit 15f964
	}
Packit 15f964
Packit 15f964
	old_str = poolv->s[index];
Packit 15f964
	poolv->s[index] = (gchar *) camel_pstring_add (str, freeit);
Packit 15f964
Packit 15f964
	camel_pstring_free (old_str);
Packit 15f964
Packit 15f964
	return poolv;
Packit 15f964
}
Packit 15f964
Packit 15f964
/**
Packit 15f964
 * e_poolv_get:
Packit 15f964
 * @poolv: pooled string vector
Packit 15f964
 * @index: index in vector of string
Packit 15f964
 *
Packit 15f964
 * Retrieve a string by index.  This could possibly just be a macro.
Packit 15f964
 *
Packit 15f964
 * Since the pool is never freed, this string does not need to be
Packit 15f964
 * duplicated, but should not be modified.
Packit 15f964
 *
Packit 15f964
 * Returns: string at that index.
Packit 15f964
 **/
Packit 15f964
const gchar *
Packit 15f964
e_poolv_get (EPoolv *poolv,
Packit 15f964
             gint index)
Packit 15f964
{
Packit 15f964
	g_return_val_if_fail (poolv != NULL, NULL);
Packit 15f964
	g_return_val_if_fail (index >= 0 && index < poolv->length, NULL);
Packit 15f964
Packit 15f964
	return poolv->s[index] ? poolv->s[index] : "";
Packit 15f964
}
Packit 15f964
Packit 15f964
/**
Packit 15f964
 * e_poolv_destroy:
Packit 15f964
 * @poolv: pooled string vector to free
Packit 15f964
 *
Packit 15f964
 * Free a pooled string vector.  This doesn't free the strings from
Packit 15f964
 * the vector, however.
Packit 15f964
 **/
Packit 15f964
void
Packit 15f964
e_poolv_destroy (EPoolv *poolv)
Packit 15f964
{
Packit 15f964
	gint ii;
Packit 15f964
Packit 15f964
	g_return_if_fail (poolv != NULL);
Packit 15f964
Packit 15f964
	for (ii = 0; ii < poolv->length; ii++) {
Packit 15f964
		camel_pstring_free (poolv->s[ii]);
Packit 15f964
	}
Packit 15f964
Packit 15f964
	g_free (poolv);
Packit 15f964
}