summaryrefslogtreecommitdiffhomepage
path: root/mixlib/mix_symbol_table.c
blob: debf04be8e7c094a6a6b2b89089c4c236af59474 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* -*-c-*- -------------- mix_symbol_table.c :
 * Implementation of the functions declared in mix_symbol_table.h
 * ------------------------------------------------------------------
 * Copyright (C) 2000, 2001 Free Software Foundation, Inc.
 *  
 * 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 <ctype.h> /* isdigit */
#include <string.h>

#include "mix_symbol_table.h"

/* Create an empty table */
mix_symbol_table_t *
mix_symbol_table_new (void)
{
  return g_hash_table_new (g_str_hash, g_str_equal);
}

/* Create a table and populate it with the contents of a table stored
   in -file- using mix_symbol_table_print (table, MIX_SYM_LINE, file)
*/
mix_symbol_table_t *
mix_symbol_table_new_from_file (FILE *file)
{
  mix_symbol_table_t *result = mix_symbol_table_new ();
  if ( result != NULL )
    {
      gchar sym[MIX_SYM_MAX_LEN + 1];
      glong val;
      
      while ( getc (file) == ',' )
	{
	  if ( fscanf (file, "%s =%ld", sym, &val) != EOF )
	      mix_symbol_table_add (result, sym, mix_word_new (val));
	}
    }
  return result;
}

/* Delete a table */
static void
delete_hash_keys_ (gpointer key, gpointer value, gpointer data)
{
  g_free (key);
}

void
mix_symbol_table_delete (mix_symbol_table_t *table)
{
  g_hash_table_foreach (table, delete_hash_keys_, NULL);
  g_hash_table_destroy (table);
}

/* add/remove symbols from other table */
static void
add_symbol_ (gpointer symbol, gpointer value, gpointer target)
{
  mix_symbol_table_t *t = (mix_symbol_table_t *)target;
  gchar *s = (gchar *)symbol;
  mix_word_t v = (mix_word_t)GPOINTER_TO_UINT (value);
  mix_symbol_table_insert (t, s, v);
}

static void
remove_symbol_ (gpointer symbol, gpointer value, gpointer target)
{
  mix_symbol_table_t *t = (mix_symbol_table_t *)target;
  gchar *s = (gchar *)symbol;
  mix_symbol_table_remove (t, s);
}

gboolean
mix_symbol_table_merge_table (mix_symbol_table_t *table,
			      const mix_symbol_table_t *from)
{
  g_return_val_if_fail (table != NULL, FALSE);
  if (from != NULL)
    {
      mix_symbol_table_foreach ((gpointer)from, add_symbol_, table);
    }
  return TRUE;
}

gboolean
mix_symbol_table_substract_table (mix_symbol_table_t *table,
				  const mix_symbol_table_t *other)
{
  g_return_val_if_fail (table != NULL, FALSE);
  if (other != NULL)
    {
      mix_symbol_table_foreach ((gpointer)other, remove_symbol_, table);
    }
  return TRUE;
}


/* Add/remove symbols one by one */
gint
mix_symbol_table_add (mix_symbol_table_t *table, 
		     const gchar *sym, mix_word_t value)
{
  gpointer key, val;
  
  if ( table == NULL || sym == NULL ) return MIX_SYM_FAIL;
  if ( strlen (sym) > MIX_SYM_MAX_LEN ) return  MIX_SYM_LONG;

  if ( !g_hash_table_lookup_extended (table, sym, &key, &val) )
    {
      key = g_strdup (sym);
      g_hash_table_insert (table, key, (gpointer)value);
      return MIX_SYM_OK;
    }
  else
    return MIX_SYM_DUP;
}

/* Add or modify symbol if it exists */
gint
mix_symbol_table_insert (mix_symbol_table_t *table, 
			const gchar *sym, mix_word_t new_value)
{
  gpointer key, val;
  
  if ( table == NULL || sym == NULL ) return MIX_SYM_FAIL;
  if ( strlen (sym) > MIX_SYM_MAX_LEN ) return MIX_SYM_LONG;

  if ( !g_hash_table_lookup_extended (table, sym, &key, &val) )
      key = g_strdup (sym);
  g_hash_table_insert (table, key, (gpointer)new_value);
  return MIX_SYM_OK;
}


/* Symbols lookup */
gboolean
mix_symbol_table_is_defined (const mix_symbol_table_t *table, const gchar *sym)
{
  gpointer key, val;
  return g_hash_table_lookup_extended((GHashTable *)table, sym, &key, &val);
}

/* Print table */
#define is_local_sym_(sym) \
  ((sym) && (strlen (sym)==2) && (sym[1] == 'B') && isdigit (sym[0]))

static gboolean skip_ = FALSE;

static void
print_sym_rows_ (gpointer symbol, gpointer value, gpointer file)
{
  char *s = (char *)symbol;
  if (skip_ && !is_local_sym_ (s)) {
    mix_word_t word = (mix_word_t)GPOINTER_TO_UINT (value);
    fprintf((FILE *)file, "%s:  %s%ld\n", s, 
	    mix_word_is_negative (word)? "-":"",
	    mix_word_magnitude (word));
  }
}

static void
print_sym_line_ (gpointer symbol, gpointer value, gpointer file)
{
  char *s = (char *)symbol;
  if (skip_ && !is_local_sym_ (s)) {
    mix_word_t word = (mix_word_t)GPOINTER_TO_UINT (value);
    fprintf((FILE *)file, ",%s =%s%ld", s, 
	    mix_word_is_negative (word)? "-":"",
	    mix_word_magnitude (word));
  }
}

void
mix_symbol_table_print (const mix_symbol_table_t *table, gint mode,
		       FILE *file, gboolean skiplocal)
{
  GHFunc func = (mode == MIX_SYM_LINE)? print_sym_line_ : print_sym_rows_;
  skip_ = skiplocal;
  if ( table != NULL )
    g_hash_table_foreach ((GHashTable *)table, func, (gpointer)file);
  if ( mode == MIX_SYM_LINE ) putc (';', file); /* to mark end-of-table */
}