refactor table_read(), less pointer indirections

This commit is contained in:
stefan schippers 2023-10-15 10:24:13 +02:00
parent 53286346e1
commit f87ab46310
2 changed files with 29 additions and 27 deletions

View File

@ -832,14 +832,16 @@ int table_read(const char *f)
size_t lines, bytes; size_t lines, bytes;
char *line = NULL, *line_ptr, *line_save; char *line = NULL, *line_ptr, *line_save;
const char *line_tok; const char *line_tok;
Raw *raw;
if(xctx->raw) { if(xctx->raw) {
dbg(0, "table_read(): must clear current data file before loading new\n"); dbg(0, "table_read(): must clear current data file before loading new\n");
return 0; return 0;
} }
xctx->raw = my_calloc(_ALLOC_ID_, 1, sizeof(Raw)); xctx->raw = my_calloc(_ALLOC_ID_, 1, sizeof(Raw));
xctx->raw->level = -1; raw = xctx->raw;
xctx->raw->annot_p = -1; raw->level = -1;
raw->annot_p = -1;
/* quick inspect file and get upper bound of number of data lines */ /* quick inspect file and get upper bound of number of data lines */
ufd = open(f, O_RDONLY); ufd = open(f, O_RDONLY);
@ -847,7 +849,7 @@ int table_read(const char *f)
count_lines_bytes(ufd, &lines, &bytes); count_lines_bytes(ufd, &lines, &bytes);
close(ufd); close(ufd);
int_hash_init(&xctx->raw->table, HASHSIZE); int_hash_init(&raw->table, HASHSIZE);
fd = fopen(f, fopen_read_mode); fd = fopen(f, fopen_read_mode);
if(fd) { if(fd) {
int nline = 0; int nline = 0;
@ -872,9 +874,9 @@ int table_read(const char *f)
prev_empty = 1; prev_empty = 1;
goto clear; goto clear;
} }
if(!xctx->raw->datasets || (prev_prev_empty == 1 && prev_empty == 1) ) { if(!raw->datasets || (prev_prev_empty == 1 && prev_empty == 1) ) {
xctx->raw->datasets++; raw->datasets++;
my_realloc(_ALLOC_ID_, &xctx->raw->npoints, xctx->raw->datasets * sizeof(int)); my_realloc(_ALLOC_ID_, &raw->npoints, raw->datasets * sizeof(int));
dataset_points = 0; dataset_points = 0;
} }
prev_prev_empty = prev_empty = 0; prev_prev_empty = prev_empty = 0;
@ -888,17 +890,17 @@ int table_read(const char *f)
line_ptr = NULL; line_ptr = NULL;
/* dbg(1,"%s ", line_tok); */ /* dbg(1,"%s ", line_tok); */
if(nline == 0) { /* header line */ if(nline == 0) { /* header line */
my_realloc(_ALLOC_ID_, &xctx->raw->names, (field + 1) * sizeof(char *)); my_realloc(_ALLOC_ID_, &raw->names, (field + 1) * sizeof(char *));
xctx->raw->names[field] = NULL; raw->names[field] = NULL;
my_strcat(_ALLOC_ID_, &xctx->raw->names[field], line_tok); my_strcat(_ALLOC_ID_, &raw->names[field], line_tok);
int_hash_lookup(&xctx->raw->table, xctx->raw->names[field], field, XINSERT_NOREPLACE); int_hash_lookup(&raw->table, raw->names[field], field, XINSERT_NOREPLACE);
xctx->raw->nvars = field + 1; raw->nvars = field + 1;
} else { /* data line */ } else { /* data line */
if(field >= xctx->raw->nvars) break; if(field >= raw->nvars) break;
#if SPICE_DATA_TYPE == 1 /* float */ #if SPICE_DATA_TYPE == 1 /* float */
xctx->raw->values[field][npoints] = (SPICE_DATA)my_atof(line_tok); raw->values[field][npoints] = (SPICE_DATA)my_atof(line_tok);
#else /* double */ #else /* double */
xctx->raw->values[field][npoints] = (SPICE_DATA)my_atod(line_tok); raw->values[field][npoints] = (SPICE_DATA)my_atod(line_tok);
#endif #endif
} }
++field; ++field;
@ -907,32 +909,32 @@ int table_read(const char *f)
++npoints; ++npoints;
dataset_points++; dataset_points++;
} }
xctx->raw->npoints[xctx->raw->datasets - 1] = dataset_points; raw->npoints[raw->datasets - 1] = dataset_points;
/* dbg(1, "\n"); */ /* dbg(1, "\n"); */
++nline; ++nline;
if(nline == 1) { if(nline == 1) {
int f; int f;
xctx->raw->values = my_calloc(_ALLOC_ID_, xctx->raw->nvars + 1, sizeof(SPICE_DATA *)); raw->values = my_calloc(_ALLOC_ID_, raw->nvars + 1, sizeof(SPICE_DATA *));
for(f = 0; f <= xctx->raw->nvars; f++) { /* one extra column for wave expressions */ for(f = 0; f <= raw->nvars; f++) { /* one extra column for wave expressions */
my_realloc(_ALLOC_ID_, &xctx->raw->values[f], lines * sizeof(SPICE_DATA)); my_realloc(_ALLOC_ID_, &raw->values[f], lines * sizeof(SPICE_DATA));
} }
} }
clear: clear:
my_free(_ALLOC_ID_, &line); my_free(_ALLOC_ID_, &line);
} /* while(line ....) */ } /* while(line ....) */
xctx->raw->allpoints = 0; raw->allpoints = 0;
if(res == 1) { if(res == 1) {
int i; int i;
my_strdup2(_ALLOC_ID_, &xctx->raw->schname, xctx->sch[xctx->currsch]); my_strdup2(_ALLOC_ID_, &raw->schname, xctx->sch[xctx->currsch]);
my_strncpy(xctx->raw->filename, f, S(xctx->raw->filename)); my_strncpy(raw->filename, f, S(raw->filename));
xctx->raw->level = xctx->currsch; raw->level = xctx->currsch;
xctx->raw->allpoints = 0; raw->allpoints = 0;
for(i = 0; i < xctx->raw->datasets; ++i) { for(i = 0; i < raw->datasets; ++i) {
xctx->raw->allpoints += xctx->raw->npoints[i]; raw->allpoints += raw->npoints[i];
} }
dbg(0, "Table file data read: %s\n", f); dbg(0, "Table file data read: %s\n", f);
dbg(0, "points=%d, vars=%d, datasets=%d\n", dbg(0, "points=%d, vars=%d, datasets=%d\n",
xctx->raw->allpoints, xctx->raw->nvars, xctx->raw->datasets); raw->allpoints, raw->nvars, raw->datasets);
} else { } else {
dbg(0, "table_read(): no useful data found\n"); dbg(0, "table_read(): no useful data found\n");
} }

View File

@ -778,7 +778,7 @@ typedef struct {
/* when descending hierarchy xctx->current_name changes, xctx->raw_schname /* when descending hierarchy xctx->current_name changes, xctx->raw_schname
* holds the name of the top schematic from which the raw file was loaded */ * holds the name of the top schematic from which the raw file was loaded */
char *schname; char *schname;
int level; /* hierarchy level where raw file has been read MIRRORED IN TCL*/ int level; /* hierarchy level where raw file has been read */
} Raw; } Raw;