diff --git a/src/frontend/dimens.c b/src/frontend/dimens.c index d4da5e3f8..f04afa2a8 100644 --- a/src/frontend/dimens.c +++ b/src/frontend/dimens.c @@ -15,27 +15,36 @@ Author: 1992 David A. Gates, U. C. Berkeley CAD Group /* * Create a string of the form "12,1,10". + * + * Parameters + * dim_data: Array of sizes of dimensions, [12, 1, 10] for the example + * n_dim: Number of elements in the array, 3 in the example + * retstring: Address of buffer where the string is returned. + + * Remarks + * It is assumed that the buffer in retstring is large enough, which for + * MAXDIMS, would require MAXDIMS * 11 bytes in the worst case assuming + * 32-bit ints. A looser but more general bound only assuming 8-bit bytes + * would be MAXDIMS * (3 * sizeof(int) + 1). */ void -dimstring(int *data, int length, char *retstring) +dimstring(const int *dim_data, int n_dim, char *retstring) { + /* Handle case of no dimensions */ + if (dim_data == (int *) NULL || n_dim < 1) { + *retstring = '\0'; + return; + } + + /* Append each dimension */ + retstring += sprintf(retstring, "%d", dim_data[0]); /* first */ int i; - char buf[BSIZE_SP]; + for (i = 1; i < n_dim; i++) { /* rest are prefixed by a comma */ + retstring += sprintf(retstring, ",%d", dim_data[i]); + } +} /* end of function dimstring */ - if (!data || length < 1) - retstring = ""; - - 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? */ - /* qui ci devo fare una copia */ - strcpy(retstring, buf); -} - /* * Create a string of the form "[12][1][10]" in retstring. @@ -49,7 +58,7 @@ dimstring(int *data, int length, char *retstring) * It is assumed that the buffer in retstring is large enough, which for * MAXDIMS, would require MAXDIMS * 12 + 1 bytes in the worst case assuming * 32-bit ints. A looser but more general bound only assuming 8-bit bytes - * would be MAXDIMS * 3 * sizeof(int) + 1. + * would be MAXDIMS * (3 * sizeof(int) + 2) + 1. */ void indexstring(const int *dim_data, int n_dim, char *retstring) @@ -74,7 +83,7 @@ indexstring(const int *dim_data, int n_dim, char *retstring) * Return 1 when all counters overflow at once. */ int -incindex(int *counts, int numcounts, int *dims, int numdims) +incindex(int *counts, int numcounts, const int *dims, int numdims) { int i, start; @@ -96,22 +105,6 @@ incindex(int *counts, int numcounts, int *dims, int numdims) } -/* - * 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] @@ -217,6 +210,26 @@ atodims(char *p, int *data, int *outlength) } + +#ifdef COMPILE_UNUSED_FUNCTIONS +/* #ifdef COMPILE_UNUSED_FUNCTIONS added 2019-03-31 */ +/* + * 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); +} + + + /* * Skip to the first character that cannot be part of a dimension string. */ @@ -226,8 +239,10 @@ skipdims(char *p) if (!p) return NULL; - while (*p && (*p == '[' || *p == ']' || *p == ',' || isspace_c(*p) || isdigit_c(*p))) + while (*p && (*p == '[' || *p == ']' || *p == ',' || + isspace_c(*p) || isdigit_c(*p))) p++; return (p); } +#endif /* COMPILE_UNUSED_FUNCTIONS */ diff --git a/src/frontend/dimens.h b/src/frontend/dimens.h index dc1a1455d..49d0c75f7 100644 --- a/src/frontend/dimens.h +++ b/src/frontend/dimens.h @@ -6,11 +6,15 @@ #ifndef ngspice_DIMENS_H #define ngspice_DIMENS_H -void dimstring(int *data, int length, char *retstring); +void dimstring(const int *dim_data, int n_dim, char *retstring); void indexstring(const int *dim_data, int n_dim, char *retstring); -int incindex(int *counts, int numcounts, int *dims, int numdims); -int emptydims(int *data, int length); +int incindex(int *counts, int numcounts, const int *dims, int numdims); int atodims(char *p, int *data, int *outlength); + +#ifdef COMPILE_UNUSED_FUNCTIONS +/* #ifdef COMPILE_UNUSED_FUNCTIONS added 2019-03-31 */ +int emptydims(int *data, int length); char *skipdims(char *p); +#endif #endif