symbol.c 2.39 KB
Newer Older
1 2 3 4 5 6
/*
 * Symbol management for the Gnumeric spreadsheet
 *
 * Author:
 *   Miguel de Icaza (miguel@kernel.org)
 */
7 8 9 10 11 12
#include <gnumeric-config.h>
#include "gnumeric.h"
#include "symbol.h"

#include "gutils.h"

13
#include <string.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
14
#include <ctype.h>
15
#include <stdio.h>
16

Miguel de Icaza's avatar
Miguel de Icaza committed
17 18 19 20 21
/**
 * symbol_lookup:
 * @st: The symbol table where lookup takes place
 * @str: string to be looked up in the symbol table
 */
22
Symbol *
23
symbol_lookup (SymbolTable *st, const char *str)
24 25 26 27
{
	Symbol *sym;

	g_return_val_if_fail (str != NULL, NULL);
Miguel de Icaza's avatar
Miguel de Icaza committed
28
	g_return_val_if_fail (st != NULL, NULL);
29

Miguel de Icaza's avatar
Miguel de Icaza committed
30
	sym = (Symbol *) g_hash_table_lookup (st->hash, str);
31 32 33
	return sym;
}

Miguel de Icaza's avatar
Miguel de Icaza committed
34 35
/**
 * symbol_install:
36
 *
Miguel de Icaza's avatar
Miguel de Icaza committed
37
 * @st: The symbol table
38 39 40 41
 * @str: the string name
 * @SymbolType: in which hash table we perform the lookup
 * @data: information attached to the symbol
 */
42
Symbol *
43
symbol_install (SymbolTable *st, const char *str, SymbolType type, void *data)
44 45 46 47
{
	Symbol *sym;

	g_return_val_if_fail (str != NULL, NULL);
Miguel de Icaza's avatar
Miguel de Icaza committed
48
	g_return_val_if_fail (st != NULL, NULL);
49

50
	sym = (Symbol *) g_hash_table_lookup (st->hash, str);
51
	if (sym) printf ("(leak) Symbol [%s] redefined.\n", str);
52

53 54 55 56 57
	sym = g_new (Symbol, 1);
	sym->ref_count = 1;
	sym->type = type;
	sym->data = data;
	sym->str  = g_strdup (str);
Miguel de Icaza's avatar
Miguel de Icaza committed
58
	sym->st   = st;
59

Miguel de Icaza's avatar
Miguel de Icaza committed
60
	g_hash_table_insert (st->hash, sym->str, sym);
61

62 63 64
	return sym;
}

Miguel de Icaza's avatar
Miguel de Icaza committed
65
/**
66
 * symbol_ref:
Miguel de Icaza's avatar
Miguel de Icaza committed
67
 * @sym: The symbol to reference
68 69 70
 *
 * Increments the reference count for the symbol
 */
71 72 73 74 75 76
void
symbol_ref (Symbol *sym)
{
	g_return_if_fail (sym != NULL);

	sym->ref_count++;
77 78
}

79 80 81 82 83
/*
 * symbol_unref:
 * @Sym:  The symbol to remove the reference from
 *
 * Unreferences a symbol.  If the count reaches zero, the symbol
Miguel de Icaza's avatar
Miguel de Icaza committed
84
 * Is deallocated
85
 */
86 87 88 89
void
symbol_unref (Symbol *sym)
{
	g_return_if_fail (sym != NULL);
90
	g_return_if_fail (sym->ref_count > 0);
91

92
	if (--(sym->ref_count) == 0){
Miguel de Icaza's avatar
Miguel de Icaza committed
93
		g_hash_table_remove (sym->st->hash, sym->str);
94 95 96 97 98
		g_free (sym->str);
		g_free (sym);
	}
}

Miguel de Icaza's avatar
Miguel de Icaza committed
99 100 101 102 103
SymbolTable *
symbol_table_new (void)
{
	SymbolTable *st = g_new (SymbolTable, 1);

Morten Welinder's avatar
Morten Welinder committed
104
	st->hash = g_hash_table_new (gnumeric_strcase_hash, gnumeric_strcase_equal);
Miguel de Icaza's avatar
Miguel de Icaza committed
105 106 107 108 109 110 111 112 113

	return st;
}

/**
 * symbol_table_destroy:
 * @st: The symbol table to destroy
 *
 * This only releases the resources associated with a SymbolTable.
Morten Welinder's avatar
Morten Welinder committed
114
 * Note that the symbols on the SymbolTable are not unrefed, it is
Miguel de Icaza's avatar
Miguel de Icaza committed
115 116 117 118 119 120 121 122 123 124
 * up to the caller to unref them.
 */
void
symbol_table_destroy (SymbolTable *st)
{
	g_return_if_fail (st != NULL);

	g_hash_table_destroy (st->hash);
	g_free (st);
}