-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathss_spicestream.c
More file actions
346 lines (310 loc) · 8 KB
/
ss_spicestream.c
File metadata and controls
346 lines (310 loc) · 8 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* SpiceStream - simple, incremental reader for analog data files,
* such as those produced by spice-type simulators.
*
* Copyright (C) 1998,1999 Stephen G. Tell
*
* 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 library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include "ss_intern.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <float.h>
#include <stdarg.h>
#include <errno.h>
/* #include <config.h> */
#ifdef HAVE_GTK
#include <glib.h>
#else
/*#include "glib_fake.h"*/
#endif
#include "ss_spicestream.h"
extern SpiceStream *sf_rdhdr_hspice(char *name, FILE *fp);
extern SpiceStream *sf_rdhdr_hsascii(char *name, FILE *fp);
extern SpiceStream *sf_rdhdr_hsbin(char *name, FILE *fp);
extern SpiceStream *sf_rdhdr_cazm(char *name, FILE *fp);
extern SpiceStream *sf_rdhdr_s3raw(char *name, FILE *fp);
extern SpiceStream *sf_rdhdr_s2raw(char *name, FILE *fp);
extern SpiceStream *sf_rdhdr_ascii(char *name, FILE *fp);
static int ss_readrow_none(SpiceStream *, double *ivar, double *dvars);
SSMsgLevel spicestream_msg_level = WARN;
typedef SpiceStream* (*PFD)(char *name, FILE *fp);
typedef struct {
char *name;
PFD rdfunc;
} DFormat;
static DFormat format_tab[] = {
{"hspice", sf_rdhdr_hspice },
{"hsascii", sf_rdhdr_hsascii },
{"hsbinary", sf_rdhdr_hsbin },
{"cazm", sf_rdhdr_cazm },
{"spice3raw", sf_rdhdr_s3raw },
{"spice2raw", sf_rdhdr_s2raw },
{"ascii", sf_rdhdr_ascii },
};
static const int NFormats = sizeof(format_tab)/sizeof(DFormat);
/*
* Open spice waveform file for reading.
* Reads in header with signal names (and sometimes signal types).
* TODO: simple strategies for trying to deduce file type from
* name or contents.
*/
SpiceStream *
ss_open_internal(FILE *fp, char *filename, char *format)
{
SpiceStream *ss;
int i;
for(i = 0; i < NFormats; i++) {
if(0==strcmp(format, format_tab[i].name)) {
ss = (format_tab[i].rdfunc)(filename, fp);
if(ss) {
ss->filetype = i;
return ss;
} else {
ss_msg(DBG, "ss_open", "failed to open %s using format %s", filename, format_tab[i].name);
return NULL;
}
}
}
ss_msg(ERR, "ss_open", "Format \"%s\" unknown", format);
return NULL;
}
SpiceStream *
ss_open(char *filename, char *format)
{
FILE *fp;
fp = fopen64(filename, "r");
if(fp == NULL) {
fprintf(stderr, "fopen(\"%s\"): %s\n", filename, strerror(errno));
return NULL;
}
return ss_open_internal(fp, filename, format);
}
SpiceStream *
ss_open_fp(FILE *fp, char *format)
{
return ss_open_internal(fp, "<spicestream>", format);
}
/*
* Allocate SpiceStream structure and fill in some portions.
* To be called only from format-specific header-reading functions,
* usually after they read and verify the header.
* Caller must still set types and names of ivar and dvars,
* and must set readrow and linebuf items.
*/
SpiceStream *
ss_new(FILE *fp, char *filename, int ndv, int nspar)
{
SpiceStream *ss;
ss = (SpiceStream *) g_new0(sizeof(SpiceStream), 1);
ss->filename = (char *) g_strdup(filename);
ss->fp = fp;
ss->ivar = (SpiceVar *) g_new0(sizeof(SpiceVar), 1);
ss->ndv = ndv;
if(ndv)
ss->dvar = (SpiceVar *) g_new0(sizeof(SpiceVar), ndv);
ss->nsweepparam = nspar;
if(nspar)
ss->spar = (SpiceVar *) g_new0(sizeof(SpiceVar), nspar);
return ss;
}
/*
* Close the file assocated with a SpiceStream.
* No more data can be read, but the header information can still
* be accessed.
*/
void ss_close(SpiceStream *ss)
{
fclose(ss->fp);
ss->fp = NULL;
ss->readrow = ss_readrow_none;
}
/*
* Free all resources associated with a SpiceStream.
*/
void ss_delete(SpiceStream *ss)
{
if(ss->fp)
fclose(ss->fp);
if(ss->filename)
g_free(ss->filename);
if(ss->ivar)
g_free(ss->ivar);
if(ss->dvar)
g_free(ss->dvar);
if(ss->linebuf)
g_free(ss->linebuf);
g_free(ss);
}
/*
* row-reading function that always returns EOF.
*/
static int
ss_readrow_none(SpiceStream *ss, double *ivar, double *dvars)
{
return 0;
}
static char *vartype_names[] = {
"Unknown", "Time", "Voltage", "Current", "Frequency"
};
const int nvartype_names = sizeof(vartype_names)/sizeof(char *);
/*
* return a string corresponding to a SpiceStream VarType.
* the pointer returned is in static or readonly storage,
* and is overwritten with each call.
*/
char *vartype_name_str(VarType type)
{
static char buf[32];
if(type < nvartype_names)
return vartype_names[type];
else {
sprintf(buf, "type-%d", type);
return buf;
}
}
/*
* return pointer to string with printable name for a variable
* or one of the columns of a variable.
* buf is a pointer to a buffer to use. If NULL, one will be allocated.
* n is the maximum number of characters to put in the buffer.
*/
char *ss_var_name(SpiceVar *sv, int col, char *buf, int n)
{
int idx;
if(buf == NULL) {
int l;
l = strlen(sv->name + 3);
buf = (char *) g_new(sizeof(char), l);
n = l;
}
strncpy(buf, sv->name, n-1);
n -= strlen(buf)+1;
if(sv->ncols == 1 || col < 0)
return buf;
if(n>1) {
idx = strlen(buf);
buf[idx++] = '.';
buf[idx++] = '0'+col;
buf[idx] = 0;
}
return(buf);
}
/*
* given a filetype number, return a pointer to a string containing the
* name of the Spicestream file format.
* Valid file type numbers start at 0.
*/
char *ss_filetype_name(int n)
{
if(n >= 0 && n < NFormats)
return format_tab[n].name;
else
return NULL;
}
/*
* utility function to read whole line into buffer, expanding buffer if needed.
* line buffer must be allocated with g_malloc/g_new, or NULL in which case
* we allocate an initial, buffer.
* returns 0 or EOF.
*/
int
fread_line(FILE *fp, char **bufp, int *bufsize)
{
int c;
int n = 0;
if(*bufp == NULL) {
if(*bufsize == 0)
*bufsize = 1024;
*bufp = (char *) g_new(sizeof(char), *bufsize);
}
while(((c = getc(fp)) != EOF) && c != '\n') {
(*bufp)[n++] = c;
if(n >= *bufsize) {
*bufsize *= 2;
*bufp = (char *) g_realloc(*bufp, *bufsize);
}
}
(*bufp)[n] = 0;
if(c == EOF)
return EOF;
else
return 0;
}
FILE *ss_error_file;
SSMsgHook ss_error_hook;
/*
* ss_msg: emit an error message from anything in the spicestream subsystem,
* or anything else that wants to use our routines.
*
* If ss_error_hook is non-NULL, it is a pointer to a function that
* will be called with the error string.
* if ss_error_file is non-NULL, it is a FILE* to write the message to.
* If neither of these are non-null, the message is written to stderr.
*
* args:
* type is one of:
* DBG - Debug, ERR - ERROR, INFO - infomration, WARN - warning
* id is the name of the function, or other identifier
* remaining arguments are printf-like.
*/
void
ss_msg(SSMsgLevel type, const char *id, const char *msg, ...)
{
char *typestr;
va_list args;
int blen = 1024;
char buf[1024];
if(type < spicestream_msg_level)
return;
switch (type) {
case DBG:
typestr = "<<DEBUG>>";
break;
case ERR:
typestr = "<<ERROR>>";
break;
case WARN:
typestr = "<<WARNING>>";
break;
case INFO:
default:
typestr = "";
break;
}
va_start(args, msg);
#ifdef HAVE_SNPRINTF
blen = snprintf(buf, 1024, "[%s]: %s ", id, typestr);
if(blen>0)
blen += vsnprintf(&buf[blen-1], 1024-blen, msg, args);
if(blen>0)
blen += snprintf(&buf[blen-1], 1024-blen, "\n");
#else
sprintf(buf, "[%s]: %s ", id, typestr);
blen = strlen(buf);
vsprintf(&buf[blen], msg, args);
strcat(buf, "\n");
#endif
if(ss_error_hook)
(ss_error_hook)(buf);
if(ss_error_file)
fputs(buf, ss_error_file);
if(ss_error_hook == NULL && ss_error_file == NULL)
fputs(buf, stderr);
va_end(args);
}