mirror of
git://projects.qi-hardware.com/eda-tools.git
synced 2024-11-10 22:38:25 +02:00
59 lines
948 B
C
59 lines
948 B
C
/*
|
|
* 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;
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
}
|