2012-03-18 18:24:12 +02:00
|
|
|
/*
|
|
|
|
* util.h - Utility functions
|
|
|
|
*
|
|
|
|
* Copyright 2012 by Werner Almesberger
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
|
|
|
|
static GTree *tree = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
static gint comp(gconstpointer a, gconstpointer b)
|
|
|
|
{
|
|
|
|
return strcmp(a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *unique(const char *s)
|
|
|
|
{
|
|
|
|
char *u;
|
|
|
|
|
|
|
|
if (!tree)
|
|
|
|
tree = g_tree_new(comp);
|
|
|
|
u = g_tree_lookup(tree, s);
|
|
|
|
if (!u) {
|
|
|
|
u = strdup(s);
|
|
|
|
g_tree_insert(tree, u, u);
|
|
|
|
}
|
|
|
|
return u;
|
|
|
|
}
|
2012-05-23 22:44:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
const char *unique_n(const char *s, int n)
|
|
|
|
{
|
|
|
|
char *tmp, *u;
|
|
|
|
|
|
|
|
if (!tree)
|
|
|
|
tree = g_tree_new(comp);
|
|
|
|
tmp = stralloc_n(s, n);
|
|
|
|
u = g_tree_lookup(tree, tmp);
|
|
|
|
if (u) {
|
|
|
|
free(tmp);
|
|
|
|
return u;
|
|
|
|
} else {
|
|
|
|
g_tree_insert(tree, tmp, tmp);
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
}
|