Ignore all generated/compiled files
[gwave-svn.git] / spicefile / ss_spice2.c
blobb4fca4580ebaa873576ab9d5145d3eeaee92931a
1 /*
2 * ss_spice2.c: routines for SpiceStream that handle the output
3 * format from Berkeley Spice2G6
5 * Copyright 1998,1999 Stephen G. Tell
6 * Copyright 1998 D. Jeff Dionne
8 * Based on rd_spice2.c that Jeff Dione contributed to gwave-0.0.4,
9 * this was largely rewritten by Steve Tell for the spicestream library.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU Library General Public
22 * License along with this library; if not, write to the Free
23 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "ssintern.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <float.h>
34 #include <config.h>
35 #include <glib.h>
36 #include "spice2.h"
37 #include "spicestream.h"
39 static int sf_readrow_s2raw(SpiceStream *sf, double *ivar, double *dvars);
40 static char *msgid = "s2raw";
42 /* Read spice-type file header - Berkeley Spice2G6 "raw" format */
43 SpiceStream *
44 sf_rdhdr_s2raw(char *name, FILE *fp)
46 SpiceStream *sf = NULL;
47 int ndv;
48 int i;
49 char *cp;
50 spice_hdr_t s2hdr;
51 spice_var_name_t s2vname;
52 spice_var_type_t s2vtype;
53 spice_var_loc_t s2vloc;
54 spice_plot_title_t s2title;
55 spice_var_t s2var;
57 if(fread (&s2var,sizeof(s2var),1,fp) != 1)
58 return NULL;
59 if (memcmp(&s2var,SPICE_MAGIC,8)) {
60 ss_msg(DBG, msgid, "%s: not a spice2 rawfile (bad magic number)", name);
61 return NULL;
63 if(fread (&s2hdr,sizeof(s2hdr),1,fp) != 1)
64 return NULL;
65 ss_msg(DBG, msgid, "%s: nvars=%d const=%d analysis mode %d",
66 name, s2hdr.nvars, s2hdr.const4, s2hdr.mode);
68 /* independent variable name */
69 if(fread (&s2vname,sizeof(s2vname),1,fp) != 1)
70 return NULL;
71 s2vname.name[7] = 0;
72 if(cp = strchr(s2vname.name, ' '))
73 *cp = 0;
75 ndv = s2hdr.nvars - 1;
76 sf = ss_new(fp, name, ndv, 0);
77 sf->ncols = ndv;
78 sf->ivar->name = g_strdup(s2vname.name);
79 sf->ivar->type = TIME;
80 sf->ivar->col = 0;
81 sf->ivar->ncols = 1;
83 for (i = 0; i < ndv; i++) {
84 SpiceVar *dvar;
85 if(fread (&s2vname, sizeof(s2vname), 1, fp) != 1)
86 goto err;
87 s2vname.name[7] = 0;
88 if(cp = strchr(s2vname.name, ' '))
89 *cp = 0;
91 /* FIXME:sgt: get correct type */
92 /* FIXME:sgt: handle complex */
93 dvar = ss_spicevar_new(s2vname.name, VOLTAGE, i, 1);
94 g_ptr_array_add(sf->dvarp, dvar);
97 if(fread (&s2vtype, sizeof(s2vtype), 1, fp) != 1)
98 goto err;
99 for (i = 0; i < ndv; i++) {
100 if(fread (&s2vtype, sizeof(s2vtype), 1, fp) != 1)
101 goto err;
104 if(fread (&s2vloc, sizeof(s2vloc), 1, fp) != 1)
105 goto err;
106 for (i = 0; i < ndv; i++) {
107 if(fread (&s2vloc, sizeof(s2vloc), 1, fp) != 1)
108 goto err;
110 if(fread (&s2title, sizeof(s2title), 1, fp) != 1)
111 goto err;
112 s2title.title[23] = 0;
113 ss_msg(DBG, msgid, "title=\"%s\"", s2title.title);
114 ss_msg(DBG, msgid, "done with header at offset=0x%lx", (long) ftello64(fp));
116 sf->readrow = sf_readrow_s2raw;
117 return sf;
118 err:
119 if(sf) {
120 ss_delete(sf);
122 return NULL;
127 * Read row of values from a spice2 rawfile
129 static int
130 sf_readrow_s2raw(SpiceStream *sf, double *ivar, double *dvars)
132 int i, rc;
133 spice_var_t val;
135 /* independent var */
136 if ((rc = fread (&val,sizeof(val),1, sf->fp)) != 1) {
137 if(rc == 0)
138 return 0;
139 else
140 return -1;
142 if (memcmp(&val,SPICE_MAGIC,8) == 0) /* another analysis */
143 return 0;
144 *ivar = val.val;
146 /* dependent vars */
147 for(i = 0; i < sf->ndv; i++) {
148 if(fread(&val, sizeof(val), 1, sf->fp) != 1) {
149 ss_msg(ERR, msgid, "unexpected EOF at dvar %d", i);
150 return -1;
152 dvars[i] = val.val;
154 return 1;