~ubuntu-branches/ubuntu/breezy/malaga/breezy

« back to all changes in this revision

Viewing changes to symbols.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Bushnell, BSG
  • Date: 2005-01-10 11:52:04 UTC
  • mfrom: (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050110115204-hpgncw5pb0m1t8i6
Tags: 6.13-5
debian/control (malaga-doc Recommends): Suggest gv as a
postscript-viewer instead of ghostview.  (Closes: #289701).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 1995 Bjoern Beutel. */
 
2
 
 
3
/* Description. =============================================================*/
 
4
 
 
5
/* This module administrates the name and atoms for each symbol. */
 
6
 
 
7
/* Includes. ================================================================*/
 
8
 
 
9
#include <stdio.h>
 
10
#include <stdlib.h>
 
11
#include <time.h>
 
12
#include <setjmp.h>
 
13
#include "basic.h"
 
14
#include "pools.h"
 
15
#include "values.h"
 
16
#include "files.h"
 
17
#include "malaga_files.h"
 
18
#include "symbols.h"
 
19
 
 
20
/* Variables. ===============================================================*/
 
21
 
 
22
static struct /* This is the symbol table. */
 
23
 
24
  int_t symbol_count; /* Number of symbols in this table. */
 
25
 
 
26
  symbol_entry_t *symbols; /* The names and atoms of all symbols. */
 
27
  symbol_t *symbols_name; /* All symbols sorted by their names. */
 
28
  symbol_t *symbols_atoms; /* All symbols sorted by their atom lists. */
 
29
 
 
30
  int_t values_size;
 
31
  symbol_t *values; /* Contains the lists of atomic symbols. */
 
32
 
 
33
  int_t strings_size;     
 
34
  char_t *strings; /* Contains the symbol names. */
 
35
} symbol_table;
 
36
 
 
37
/* Functions. ===============================================================*/
 
38
 
 
39
string_t 
 
40
get_symbol_name( symbol_t symbol )
 
41
/* Return the name of SYMBOL. */
 
42
{
 
43
  return symbol_table.strings + symbol_table.symbols[ symbol ].name;
 
44
}
 
45
 
 
46
/*---------------------------------------------------------------------------*/
 
47
 
 
48
value_t 
 
49
get_atoms( symbol_t symbol )
 
50
/* Return the atom list of SYMBOL. */
 
51
{
 
52
  return symbol_table.values + symbol_table.symbols[ symbol ].atoms;
 
53
}
 
54
 
 
55
/*---------------------------------------------------------------------------*/
 
56
 
 
57
symbol_t 
 
58
find_symbol( string_t name )
 
59
/* Find a symbol NAME in the symbol table and return its code.
 
60
 * If there is no symbol NAME, report an error. */
 
61
{
 
62
  int_t lower, upper, middle, result;
 
63
  symbol_t symbol;
 
64
 
 
65
  /* We do a binary search in SYMBOLS_NAME. */
 
66
  lower = 0;
 
67
  upper = symbol_table.symbol_count - 1;
 
68
  while (lower <= upper) 
 
69
  { 
 
70
    middle = (lower + upper) / 2;
 
71
    symbol = symbol_table.symbols_name[ middle ];
 
72
    result = strcmp_no_case( name, get_symbol_name( symbol ) );
 
73
    if (result < 0) 
 
74
      upper = middle - 1;
 
75
    else if (result > 0) 
 
76
      lower = middle + 1;
 
77
    else 
 
78
      return symbol;
 
79
  }
 
80
  complain( "Unknown symbol \"%s\".", name );
 
81
}
 
82
 
 
83
/*---------------------------------------------------------------------------*/
 
84
 
 
85
symbol_t 
 
86
find_multi_symbol( value_t atoms )
 
87
/* Find a symbol by its atoms in the symbol table and return its code.
 
88
 * If there is no multi-symbol for ATOMS, report an error. */
 
89
{
 
90
  int_t lower, upper, middle, result;
 
91
  symbol_t symbol;
 
92
 
 
93
  /* We do a binary search in SYMBOLS_ATOMS. */
 
94
  lower = 0;
 
95
  upper = symbol_table.symbol_count - 1;
 
96
  while (lower <= upper) 
 
97
  { 
 
98
    middle = (lower + upper) / 2;
 
99
    symbol = symbol_table.symbols_atoms[ middle ];
 
100
    result = compare_atom_lists( atoms, get_atoms( symbol ) );
 
101
    if (result < 0) 
 
102
      upper = middle - 1;
 
103
    else if (result > 0) 
 
104
      lower = middle + 1;
 
105
    else 
 
106
      return symbol;
 
107
  }
 
108
  complain( "No multi symbol for this atom list." );
 
109
}
 
110
 
 
111
/*---------------------------------------------------------------------------*/
 
112
 
 
113
int_t 
 
114
symbol_count( void )
 
115
/* Return the number of symbols defined. */
 
116
{
 
117
  return symbol_table.symbol_count;
 
118
}
 
119
 
 
120
/*---------------------------------------------------------------------------*/
 
121
 
 
122
static int 
 
123
compare_symbols_name( const void *symbol1, const void *symbol2 )
 
124
/* Return -1 if name( SYMBOL1 ) < name( SYMBOL2 )
 
125
 *         0 if name( SYMBOL1 ) == name( SYMBOL2 )
 
126
 *         1 if name( SYMBOL1 ) > name( SYMBOL2 ). */
 
127
{
 
128
  return strcmp_no_case( get_symbol_name( *(symbol_t *) symbol1 ), 
 
129
                         get_symbol_name( *(symbol_t *) symbol2 ) );
 
130
}
 
131
 
 
132
/*---------------------------------------------------------------------------*/
 
133
 
 
134
static int 
 
135
compare_symbols_atoms( const void *symbol1, const void *symbol2 )
 
136
/* Return -1 if atoms( SYMBOL1 ) < atoms( SYMBOL2 )
 
137
 *         0 if atoms( SYMBOL1 ) == atoms( SYMBOL2 )
 
138
 *         1 if atoms( SYMBOL1 ) > atoms( SYMBOL2 ). */
 
139
{
 
140
  return compare_atom_lists( get_atoms( *(symbol_t *) symbol1 ), 
 
141
                             get_atoms( *(symbol_t *) symbol2 ) );
 
142
}
 
143
 
 
144
/*---------------------------------------------------------------------------*/
 
145
 
 
146
void 
 
147
init_symbols( string_t file_name )
 
148
/* Initialise this module. Read SYMBOL_TABLE from file FILE_NAME. */
 
149
{
 
150
  FILE *stream;
 
151
  symbol_header_t header;
 
152
  int_t i;
 
153
  
 
154
  stream = open_stream( file_name, "rb" );
 
155
  read_vector( &header, sizeof( header ), 1, stream, file_name );
 
156
  check_header( &header.common_header, file_name, 
 
157
                SYMBOL_FILE, MIN_SYMBOL_CODE_VERSION, SYMBOL_CODE_VERSION );
 
158
  
 
159
  symbol_table.symbol_count = header.symbol_count;
 
160
  symbol_table.symbols = read_new_vector( sizeof( symbol_entry_t ), 
 
161
                                          header.symbol_count, 
 
162
                                          stream, file_name );
 
163
  symbol_table.values_size = header.values_size;
 
164
  symbol_table.values = read_new_vector( sizeof( cell_t ), header.values_size, 
 
165
                                         stream, file_name );
 
166
  symbol_table.strings_size = header.strings_size;
 
167
  symbol_table.strings = read_new_vector( sizeof( char_t ), 
 
168
                                          header.strings_size,
 
169
                                          stream, file_name );
 
170
  close_stream( &stream, file_name );
 
171
  
 
172
  /* Build a list of all symbols sorted by their names
 
173
   * and a list of all symbols sorted by their atom lists. */
 
174
  symbol_table.symbols_name = new_vector( sizeof( symbol_t ),
 
175
                                          header.symbol_count );
 
176
  symbol_table.symbols_atoms = new_vector( sizeof( symbol_t ),
 
177
                                           header.symbol_count );
 
178
  for (i = 0; i < header.symbol_count; i++) 
 
179
  { 
 
180
    symbol_table.symbols_name[i] = i;
 
181
    symbol_table.symbols_atoms[i] = i;
 
182
  }
 
183
  qsort( symbol_table.symbols_name, header.symbol_count, 
 
184
         sizeof( symbol_t ), compare_symbols_name );
 
185
  qsort( symbol_table.symbols_atoms, header.symbol_count, 
 
186
         sizeof( symbol_t ), compare_symbols_atoms );
 
187
  
 
188
  values_get_symbol_name = get_symbol_name;
 
189
  values_get_atoms = get_atoms;
 
190
}
 
191
 
 
192
/*---------------------------------------------------------------------------*/
 
193
 
 
194
void 
 
195
terminate_symbols( void )
 
196
/* Terminate this module. */
 
197
{
 
198
  free_mem( &symbol_table.symbols );
 
199
  free_mem( &symbol_table.symbols_name );
 
200
  free_mem( &symbol_table.symbols_atoms );
 
201
  free_mem( &symbol_table.values );
 
202
  free_mem( &symbol_table.strings );
 
203
}
 
204
 
 
205
/* End of file. =============================================================*/