table_read(): skip initial empty lines, allow 2 or more blank lines for new dataset

This commit is contained in:
stefan schippers 2023-02-10 03:36:29 +01:00
parent 33c2c2d1b2
commit c51f007da3
4 changed files with 39 additions and 18 deletions

View File

@ -3366,9 +3366,12 @@ void draw(void)
int cc, c, i = 0;
xSymbol *symptr;
int textlayer;
#if HAS_CAIRO==1
const char *textfont;
#endif
if(!xctx || xctx->no_draw) return;
#if HAS_CAIRO==1
#ifndef __unix__
clear_cairo_surface(xctx->cairo_save_ctx,
xctx->xrect[0].x, xctx->xrect[0].y, xctx->xrect[0].width, xctx->xrect[0].height);
@ -3376,7 +3379,6 @@ void draw(void)
xctx->xrect[0].x, xctx->xrect[0].y, xctx->xrect[0].width, xctx->xrect[0].height);
#endif
#endif
if(xctx->no_draw) return;
xctx->show_hidden_texts = tclgetboolvar("show_hidden_texts");
rebuild_selected_array();
if(has_x) {

View File

@ -57,6 +57,21 @@ int my_strncasecmp(const char *s1, const char *s2, size_t n)
return tolower(*s1) - tolower(*s2);
}
/* return lenght of line and skip */
size_t my_fgets_skip(FILE *fd)
{
enum { SIZE = 1024 };
char buf[SIZE];
size_t line_len = 0, len;
while(fgets(buf, SIZE, fd)) {
len = strlen(buf);
line_len += len;
if(buf[len - 1] == '\n') break;
}
return line_len;
}
/* caller should free allocated storage for s */
char *my_fgets(FILE *fd)
{

View File

@ -766,7 +766,7 @@ int table_read(const char *f)
if(xctx->graph_values || xctx->graph_npoints || xctx->graph_nvars || xctx->graph_datasets) {
dbg(0, "table_read(): must clear current data file before loading new\n");
return res;
return 0;
}
int_hash_init(&xctx->graph_raw_table, HASHSIZE);
fd = fopen(f, fopen_read_mode);
@ -775,31 +775,31 @@ int table_read(const char *f)
int field;
int npoints = 0;
int dataset_points = 0;
int prev_empty = 0;
int prev_prev_empty = 0, prev_empty = 0;
res = 1;
/* read data line by line */
while((line = my_fgets(fd))) {
int empty = 1;
if(line[0] == '#') {
my_free(_ALLOC_ID_, &line);
continue; /* skip comments */
goto clear;
}
line_ptr = line;
while(*line_ptr) { /* non empty line ? */
if(*line_ptr != ' ' && *line_ptr != '\t' && *line_ptr != '\n') empty = 0;
line_ptr++;
}
if(empty && prev_empty) { /* second empty line: start new dataset */
xctx->graph_datasets++;
dataset_points = 0;
my_free(_ALLOC_ID_, &line);
continue;
} else if(empty) {
if(empty) {
prev_prev_empty = prev_empty;
prev_empty = 1;
my_free(_ALLOC_ID_, &line);
continue;
goto clear;
}
prev_empty = 0;
if(prev_prev_empty == 1 && prev_empty == 1) {
xctx->graph_datasets++;
my_realloc(_ALLOC_ID_, &xctx->graph_npoints, xctx->graph_datasets * sizeof(int));
dataset_points = 0;
}
if(!xctx->graph_datasets) xctx->graph_datasets++;
prev_prev_empty = prev_empty = 0;
line_ptr = line;
field = 0;
while( (line_tok = my_strtok_r(line_ptr, " \t\n", "", &line_save)) ) {
@ -826,11 +826,11 @@ int table_read(const char *f)
nline++;
if(nline == 1) {
xctx->graph_values = my_calloc(_ALLOC_ID_, xctx->graph_nvars + 1, sizeof(SPICE_DATA *));
my_realloc(_ALLOC_ID_, &xctx->graph_npoints, (xctx->graph_datasets+1) * sizeof(int));
xctx->graph_datasets++;
my_realloc(_ALLOC_ID_, &xctx->graph_npoints, xctx->graph_datasets * sizeof(int));
}
clear:
my_free(_ALLOC_ID_, &line);
}
} /* while(line ....) */
xctx->graph_allpoints = 0;
if(res == 1) {
int i;
@ -844,9 +844,12 @@ int table_read(const char *f)
dbg(0, "Table file data read: %s\n", f);
dbg(0, "points=%d, vars=%d, datasets=%d\n",
xctx->graph_allpoints, xctx->graph_nvars, xctx->graph_datasets);
/* allocate extra column for custom calculated data (expressions) */
my_realloc(_ALLOC_ID_, &xctx->graph_values[xctx->graph_nvars], npoints * sizeof(SPICE_DATA));
} else {
dbg(0, "table_read(): no useful data found\n");
}
fclose(fd);
return res;
}

View File

@ -1373,6 +1373,7 @@ extern size_t my_strdup(int id, char **dest, const char *src);
extern void my_strndup(int id, char **dest, const char *src, size_t n);
extern size_t my_strdup2(int id, char **dest, const char *src);
extern char *my_fgets(FILE *fd);
extern size_t my_fgets_skip(FILE *fd);
extern char *my_strtok_r(char *str, const char *delim, const char *quote, char **saveptr);
extern char **parse_cmd_string(const char *cmd, int *argc);
extern int my_strncpy(char *d, const char *s, size_t n);