ngspice/src/frontend/dimens.c

217 lines
4.2 KiB
C
Raw Normal View History

2000-04-27 22:03:57 +02:00
/**********
Copyright 1992 Regents of the University of California. All rights reserved.
Author: 1992 David A. Gates, U. C. Berkeley CAD Group
**********/
/*
* Read and write dimension/index arrays via strings.
*/
#include "ngspice.h"
* src/Makefile.am src/main.c src/sconvert.c src/analysis/cktdisto.c src/analysis/cktnoise.c src/analysis/noisean.c: Updates for the new header files. * src/maths/cmaths/cmath1.c src/maths/cmaths/cmath2.c src/maths/cmaths/cmath3.c src/maths/cmaths/cmath4.c: Updates for the new header files. * src/frontend/.cvsignore src/frontend/Makefile.am: Updates for the new files. * src/frontend/agraf.c src/frontend/aspice.c src/frontend/breakp.c src/frontend/breakp2.c src/frontend/circuits.c src/frontend/cpitf.c src/frontend/debugcom.c src/frontend/define.c src/frontend/diff.c src/frontend/dimens.c src/frontend/display.c src/frontend/doplot.c src/frontend/dotcards.c src/frontend/evaluate.c src/frontend/fourier.c src/frontend/graf.c src/frontend/grid.c src/frontend/inp.c src/frontend/inpcom.c src/frontend/interp.c src/frontend/linear.c src/frontend/misccoms.c src/frontend/misccoms.h src/frontend/miscvars.c src/frontend/mw_coms.c src/frontend/newcoms.c src/frontend/nutinp.c src/frontend/options.c src/frontend/outitf.c src/frontend/parse.c src/frontend/plotcurv.c src/frontend/points.c src/frontend/postcoms.c src/frontend/rawfile.c src/frontend/runcoms.c src/frontend/runcoms2.c src/frontend/shyu.c src/frontend/spec.c src/frontend/spiceif.c src/frontend/typesdef.c src/frontend/vectors.c src/frontend/where.c src/frontend/postcoms.c: Updates for the new header files. Some commands have moved into the new files below. * src/frontend/README src/frontend/com_compose.c src/frontend/com_compose.h src/frontend/com_display.c src/frontend/com_display.h src/frontend/com_let.c src/frontend/com_let.h src/frontend/com_setscale.c src/frontend/com_setscale.h src/frontend/commands.c src/frontend/commands.h src/frontend/completion.h src/frontend/streams.h src/frontend/testcommands.c: Separation into different com_* commands. This is a start. The rest of the subdirectory needs doing. * src/include/complex.h src/include/cpdefs.h src/include/cpextern.h src/include/cpstd.h src/include/fteconst.h src/include/ftedata.h src/include/ftedev.h src/include/fteext.h src/include/ftegraph.h src/include/fteparse.h src/include/dvec.h src/include/grid.h src/include/plot.h src/include/pnode.h src/include/sim.h src/include/variable.h src/include/wordlist.h src/include/bool.h: Separation of header files into smaller pieces. This limits recompilation to only the affected source files. The original header files have a warning message embedded to flag obsoleted use. * src/frontend/compose.c src/frontend/compose.h src/frontend/nutctab.c src/frontend/nutctab.h src/frontend/plot5.c src/frontend/plot5.h src/frontend/spcmdtab.c src/frontend/x11.c src/frontend/x11.h src/frontend/xgraph.c src/frontend/xgraph.h: Moved these files into src/frontend/plotting subdirectory. * src/frontend/plotting/.cvsignore src/frontend/plotting/Makefile.am src/frontend/plotting/plot5.c src/frontend/plotting/plot5.h src/frontend/plotting/plotting.c src/frontend/plotting/plotting.h src/frontend/plotting/pvec.c src/frontend/plotting/pvec.h src/frontend/plotting/x11.c src/frontend/plotting/x11.h src/frontend/plotting/xgraph.c src/frontend/plotting/xgraph.h: The new libplotting library with automake and CVS infrastructure.
2000-05-06 16:12:51 +02:00
#include "dvec.h" /* For MAXDIMS */
2000-04-27 22:03:57 +02:00
#include "dimens.h"
/*
* Create a string of the form "12,1,10".
*/
2001-02-09 20:46:36 +01:00
void
dimstring(int *data, int length, char *retstring)
2000-04-27 22:03:57 +02:00
{
int i;
char buf[BSIZE_SP];
2001-02-09 20:46:36 +01:00
2000-04-27 22:03:57 +02:00
if (!data || length < 1)
2001-02-09 20:46:36 +01:00
retstring = "";
2000-04-27 22:03:57 +02:00
buf[0] = '\0';
for (i=0; i < length; i++) {
sprintf(buf + strlen(buf), "%d%s", data[i],
(i < length - 1) ? "," : "");
}
/* XXX Should I return a copy instead? */
2001-02-09 20:46:36 +01:00
/* qui ci devo fare una copia */
strcpy(retstring, buf);
2000-04-27 22:03:57 +02:00
}
/*
* Create a string of the form "[12][1][10]".
*/
2001-02-09 20:46:36 +01:00
void
indexstring(int *data, int length, char *retstring)
2000-04-27 22:03:57 +02:00
{
int i;
char buf[BSIZE_SP];
if (!data || length < 1)
2001-02-09 20:46:36 +01:00
retstring = "";
2000-04-27 22:03:57 +02:00
buf[0] = '\0';
for (i=0; i < length; i++) {
sprintf(buf + strlen(buf), "[%d]", data[i]);
}
2001-02-09 20:46:36 +01:00
strcpy(retstring, buf);
2000-04-27 22:03:57 +02:00
}
/*
* Add one to anstrchr into an array with sizes in dims.
* Return 1 when all counters overflow at once.
*/
int
incindex(int *counts, int numcounts, int *dims, int numdims)
{
int i, start;
if (!counts || numcounts < 1 || !dims || numdims < 1)
return 0;
start = numcounts - 1;
for (i = start; i >= 0; i--) {
if (++counts[i] < dims[i])
break; /* This counter is not maxed out. */
else
counts[i] = 0;
}
if (i == 0)
return(1);
else
return(0);
}
/*
* Count number of empty dimensions in an array.
*/
int
emptydims(int *data, int length)
{
int i, numempty = 0;
for (i=0; i < length; i++) {
if (data[i] == 0)
numempty++;
}
return(numempty);
}
/*
* Read a string of one of the following forms into a dimensions array:
* [12][1][10]
* [12,1,10]
* 12,1,10
* 12, 1, 10
* 12 , 1 , 10
* Basically, we require that all brackets be matched, that all numbers
* be separated by commas or by "][", that all whitespace is ignored, and
* the beginning [ and end ] are ignored if they exist. The only valid
* characters in the string are brackets, commas, spaces, and digits.
* If any dimension is blank, its entry in the array is set to 0.
*
* Return 0 on success, 1 on failure.
*/
int
atodims(char *p, int *data, int *outlength)
{
int length = 0;
int state = 0;
int err = 0;
int needbracket = 0;
char sep = '\0';
if (!data || !outlength)
return 1;
if (!p) {
*outlength = 0;
return 0;
}
while (*p && isspace(*p))
p++;
if (*p == '[') {
p++;
while (*p && isspace(*p))
p++;
needbracket = 1;
}
while (*p && state != 3) {
switch (state) {
case 0: /* p just at or before a number */
if (length >= MAXDIMS) {
if (length == MAXDIMS)
printf("Error: maximum of %d dimensions allowed.\n",
MAXDIMS);
length += 1;
} else if (!isdigit(*p)) {
data[length++] = 0; /* This position was empty. */
} else {
data[length++] = atoi(p);
while (isdigit(*p))
p++;
}
state = 1;
break;
case 1: /* p after a number, looking for ',' or ']' */
if (sep == '\0') {
sep = *p;
}
if (*p == ']' && *p == sep) {
p++;
state = 2;
} else if (*p == ',' && *p == sep) {
p++;
state = 0;
} else /* Funny character following a # */
break;
break;
case 2: /* p after a ']', either at the end or looking for '[' */
if (*p == '[') {
p++;
state = 0;
} else {
state = 3;
}
break;
}
while (*p && isspace(*p))
p++;
}
*outlength = length;
if (length > MAXDIMS) {
return(1);
}
if (state == 3) { /* We finished with a closing bracket */
err = !needbracket;
} else if (*p) { /* We finished by hitting a bad character after a # */
err = 1;
} else { /* We finished by exhausting the string */
err = needbracket;
}
if (err) {
*outlength = 0;
}
return(err);
}
/*
* Skip to the first character that cannot be part of a dimension string.
*/
char *
skipdims(char *p)
{
if (!p)
return NULL;
while(*p && (*p == '[' || *p == ']' || *p == ','
|| isspace(*p) || isdigit(*p)))
p++;
return(p);
}