OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [sim/] [ppc/] [table.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/*  This file is part of the program psim.
2
 
3
    Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>
4
 
5
    This program is free software; you can redistribute it and/or modify
6
    it under the terms of the GNU General Public License as published by
7
    the Free Software Foundation; either version 2 of the License, or
8
    (at your option) any later version.
9
 
10
    This program is distributed in the hope that it will be useful,
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    GNU General Public License for more details.
14
 
15
    You should have received a copy of the GNU General Public License
16
    along with this program; if not, write to the Free Software
17
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
 
19
    */
20
 
21
 
22
#include <sys/types.h>
23
#include <sys/stat.h>
24
#include <stdio.h>
25
#include <fcntl.h>
26
#include <ctype.h>
27
 
28
#include "config.h"
29
#include "misc.h"
30
#include "lf.h"
31
#include "table.h"
32
 
33
#ifdef HAVE_UNISTD_H
34
#include <unistd.h>
35
#endif
36
 
37
#ifdef HAVE_STDLIB_H
38
#include <stdlib.h>
39
#endif
40
 
41
struct _table {
42
  size_t size;
43
  char *buffer;
44
  char *pos;
45
  int line_nr;
46
  int nr_fields;
47
  int nr_model_fields;
48
  char *file_name;
49
};
50
 
51
extern table *
52
table_open(const char *file_name,
53
           int nr_fields,
54
           int nr_model_fields)
55
{
56
  int fd;
57
  struct stat stat_buf;
58
  table *file;
59
  int nr;
60
 
61
  /* create a file descriptor */
62
  file = ZALLOC(table);
63
  ASSERT(file != NULL);
64
  file->nr_fields = nr_fields;
65
  file->nr_model_fields = nr_model_fields;
66
 
67
  /* save the file name */
68
  file->file_name = (char*)zalloc(strlen(file_name) + 1);
69
  ASSERT(file->file_name != NULL);
70
  strcpy(file->file_name, file_name);
71
 
72
  /* open the file */
73
  fd = open(file->file_name, O_RDONLY, 0);
74
  ASSERT(fd >= 0);
75
 
76
  /* determine the size */
77
  if (fstat(fd, &stat_buf) < 0) {
78
    perror("table_open.fstat");
79
    exit(1);
80
  }
81
  file->size = stat_buf.st_size;
82
 
83
  /* allocate this much memory */
84
  file->buffer = (char*)zalloc(file->size+1);
85
  if(file->buffer == NULL) {
86
    perror("table_open.calloc.file->size+1");
87
    exit(1);
88
  }
89
  file->pos = file->buffer;
90
 
91
  /* read it in */
92
#ifdef __CYGWIN32__
93
  if ((file->size) && ((nr = read(fd, file->buffer, file->size)) <= 0)) {
94
#else
95
  if ((nr = read(fd, file->buffer, file->size)) < file->size) {
96
#endif
97
    perror("table_open.read");
98
    exit(1);
99
  }
100
  file->size = nr;
101
  file->buffer[file->size] = '\0';
102
 
103
  /* done */
104
  close(fd);
105
  return file;
106
}
107
 
108
 
109
extern table_entry *
110
table_entry_read(table *file)
111
{
112
  int field;
113
  table_entry *entry;
114
 
115
  /* skip comments/blanks */
116
  while(1) {
117
    /* leading white space */
118
    while (*file->pos != '\0'
119
           && *file->pos != '\n'
120
           && isspace(*file->pos))
121
      file->pos++;
122
    /* comment */
123
    if (*file->pos == '#') {
124
      do {
125
        file->pos++;
126
      } while (*file->pos != '\0' && *file->pos != '\n');
127
    }
128
    /* end of line? */
129
    if (*file->pos == '\n') {
130
      file->pos++;
131
      file->line_nr++;
132
    }
133
    else
134
      break;
135
  }
136
  if (*file->pos == '\0')
137
    return NULL;
138
 
139
  /* create this new entry */
140
  entry = (table_entry*)zalloc(sizeof(table_entry)
141
                               + (file->nr_fields + 1) * sizeof(char*));
142
  ASSERT(entry != NULL);
143
  entry->file_name = file->file_name;
144
  entry->nr_fields = file->nr_fields;
145
 
146
  /* break the line into its colon delimitered fields */
147
  for (field = 0; field < file->nr_fields-1; field++) {
148
    entry->fields[field] = file->pos;
149
    while(*file->pos && *file->pos != ':' && *file->pos != '\n')
150
      file->pos++;
151
    if (*file->pos == ':') {
152
      *file->pos = '\0';
153
      file->pos++;
154
    }
155
  }
156
 
157
  /* any trailing stuff not the last field */
158
  ASSERT(field == file->nr_fields-1);
159
  entry->fields[field] = file->pos;
160
  while (*file->pos && *file->pos != '\n') {
161
    file->pos++;
162
  }
163
  if (*file->pos == '\n') {
164
    *file->pos = '\0';
165
    file->pos++;
166
  }
167
  file->line_nr++;
168
 
169
  /* if following lines begin with a star, add them to the model
170
     section.  */
171
  while ((file->nr_model_fields > 0) && (*file->pos == '*')) {
172
    table_model_entry *model = (table_model_entry*)zalloc(sizeof(table_model_entry)
173
                                                          + (file->nr_model_fields + 1) * sizeof(char*));
174
    if (entry->model_last)
175
      entry->model_last->next = model;
176
    else
177
      entry->model_first = model;
178
    entry->model_last = model;
179
 
180
    /* break the line into its colon delimitered fields */
181
    file->pos++;
182
    for (field = 0; field < file->nr_model_fields-1; field++) {
183
      model->fields[field] = file->pos;
184
      while(*file->pos && *file->pos != ':' && *file->pos != '\n')
185
        file->pos++;
186
      if (*file->pos == ':') {
187
        *file->pos = '\0';
188
        file->pos++;
189
      }
190
    }
191
 
192
    /* any trailing stuff not the last field */
193
    ASSERT(field == file->nr_model_fields-1);
194
    model->fields[field] = file->pos;
195
    while (*file->pos && *file->pos != '\n') {
196
      file->pos++;
197
    }
198
    if (*file->pos == '\n') {
199
      *file->pos = '\0';
200
      file->pos++;
201
    }
202
 
203
    file->line_nr++;
204
    model->line_nr = file->line_nr;
205
  }
206
 
207
  entry->line_nr = file->line_nr;
208
 
209
  /* if following lines are tab indented, put in the annex */
210
  if (*file->pos == '\t') {
211
    entry->annex = file->pos;
212
    do {
213
      do {
214
        file->pos++;
215
      } while (*file->pos != '\0' && *file->pos != '\n');
216
      if (*file->pos == '\n') {
217
        char *save_pos = ++file->pos;
218
        int extra_lines = 0;
219
        file->line_nr++;
220
        /* Allow tab indented to have blank lines */
221
        while (*save_pos == '\n') {
222
          save_pos++;
223
          extra_lines++;
224
        }
225
        if (*save_pos == '\t') {
226
          file->pos = save_pos;
227
          file->line_nr += extra_lines;
228
        }
229
      }
230
    } while (*file->pos != '\0' && *file->pos == '\t');
231
    if (file->pos[-1] == '\n')
232
      file->pos[-1] = '\0';
233
  }
234
  else
235
    entry->annex = NULL;
236
 
237
  /* return it */
238
  return entry;
239
 
240
}
241
 
242
 
243
extern void
244
dump_table_entry(table_entry *entry,
245
                 int indent)
246
{
247
  printf("(table_entry*)%p\n", entry);
248
 
249
  if (entry != NULL) {
250
    int field;
251
    char sep;
252
 
253
    sep = ' ';
254
    dumpf(indent, "(fields");
255
    for (field = 0; field < entry->nr_fields; field++) {
256
      printf("%c%s", sep, entry->fields[field]);
257
      sep = ':';
258
    }
259
    printf(")\n");
260
 
261
    dumpf(indent, "(line_nr %d)\n", entry->line_nr);
262
 
263
    dumpf(indent, "(file_name %s)\n", entry->file_name);
264
 
265
    dumpf(indent, "(annex\n%s\n", entry->annex);
266
    dumpf(indent, " )\n");
267
 
268
  }
269
}
270
 
271
 
272
extern void
273
table_entry_print_cpp_line_nr(lf *file,
274
                              table_entry *entry)
275
{
276
  lf_print__external_reference(file, entry->line_nr, entry->file_name);
277
}
278
 
279
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.