Parser: make parser (mostly) reentrant

Introduce a parser_state structure, which describes (most) of the
global parser state. Create such a structure in the entry routines
to the parser and pass it down to the individual functions. The
parser state is initialized and freed with the init_parser_state()
and free_parser_state() functions.

The main benefits are:
1) Isolation of parser state.
2) Keeping the global name space tidy.
3) Prevent memory leaks which could happen in truncated files by
   freeing all the parser state after parse.

A somewhat controversial point might be that the individual
parsing functions are split in those that need parser-state and
those that don't. This means that there are now two versions of
the MATCH macro, viz. one for the former and one for the latter.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-10-17 18:45:22 +02:00 committed by Dirk Hohndel
parent 343808271c
commit 138f27f65d
7 changed files with 1022 additions and 957 deletions

View file

@ -13,57 +13,57 @@
#include "membuffer.h"
#include "gettext.h"
extern int cobalt_profile_sample(void *handle, int columns, char **data, char **column)
static int cobalt_profile_sample(void *param, int columns, char **data, char **column)
{
UNUSED(handle);
UNUSED(columns);
UNUSED(column);
struct parser_state *state = (struct parser_state *)param;
sample_start();
sample_start(state);
if (data[0])
cur_sample->time.seconds = atoi(data[0]);
state->cur_sample->time.seconds = atoi(data[0]);
if (data[1])
cur_sample->depth.mm = atoi(data[1]);
state->cur_sample->depth.mm = atoi(data[1]);
if (data[2])
cur_sample->temperature.mkelvin = metric ? C_to_mkelvin(strtod_flags(data[2], NULL, 0)) : F_to_mkelvin(strtod_flags(data[2], NULL, 0));
sample_end();
state->cur_sample->temperature.mkelvin = state->metric ? C_to_mkelvin(strtod_flags(data[2], NULL, 0)) : F_to_mkelvin(strtod_flags(data[2], NULL, 0));
sample_end(state);
return 0;
}
extern int cobalt_cylinders(void *handle, int columns, char **data, char **column)
static int cobalt_cylinders(void *param, int columns, char **data, char **column)
{
UNUSED(handle);
UNUSED(columns);
UNUSED(column);
struct parser_state *state = (struct parser_state *)param;
cylinder_start();
cylinder_start(state);
if (data[0])
cur_dive->cylinder[cur_cylinder_index].gasmix.o2.permille = atoi(data[0]) * 10;
state->cur_dive->cylinder[state->cur_cylinder_index].gasmix.o2.permille = atoi(data[0]) * 10;
if (data[1])
cur_dive->cylinder[cur_cylinder_index].gasmix.he.permille = atoi(data[1]) * 10;
state->cur_dive->cylinder[state->cur_cylinder_index].gasmix.he.permille = atoi(data[1]) * 10;
if (data[2])
cur_dive->cylinder[cur_cylinder_index].start.mbar = psi_to_mbar(atoi(data[2]));
state->cur_dive->cylinder[state->cur_cylinder_index].start.mbar = psi_to_mbar(atoi(data[2]));
if (data[3])
cur_dive->cylinder[cur_cylinder_index].end.mbar = psi_to_mbar(atoi(data[3]));
state->cur_dive->cylinder[state->cur_cylinder_index].end.mbar = psi_to_mbar(atoi(data[3]));
if (data[4])
cur_dive->cylinder[cur_cylinder_index].type.size.mliter = atoi(data[4]) * 100;
state->cur_dive->cylinder[state->cur_cylinder_index].type.size.mliter = atoi(data[4]) * 100;
if (data[5])
cur_dive->cylinder[cur_cylinder_index].gas_used.mliter = atoi(data[5]) * 1000;
cylinder_end();
state->cur_dive->cylinder[state->cur_cylinder_index].gas_used.mliter = atoi(data[5]) * 1000;
cylinder_end(state);
return 0;
}
extern int cobalt_buddies(void *handle, int columns, char **data, char **column)
static int cobalt_buddies(void *param, int columns, char **data, char **column)
{
UNUSED(handle);
UNUSED(columns);
UNUSED(column);
struct parser_state *state = (struct parser_state *)param;
if (data[0])
utf8_string(data[0], &cur_dive->buddy);
utf8_string(data[0], &state->cur_dive->buddy);
return 0;
}
@ -73,20 +73,20 @@ extern int cobalt_buddies(void *handle, int columns, char **data, char **column)
* Subsurface star rating.
*/
extern int cobalt_visibility(void *handle, int columns, char **data, char **column)
static int cobalt_visibility(void *param, int columns, char **data, char **column)
{
UNUSED(handle);
UNUSED(param);
UNUSED(columns);
UNUSED(column);
UNUSED(data);
return 0;
}
extern int cobalt_location(void *handle, int columns, char **data, char **column)
static int cobalt_location(void *param, int columns, char **data, char **column)
{
UNUSED(handle);
UNUSED(columns);
UNUSED(column);
struct parser_state *state = (struct parser_state *)param;
static char *location = NULL;
if (data[0]) {
@ -97,7 +97,7 @@ extern int cobalt_location(void *handle, int columns, char **data, char **column
sprintf(tmp, "%s / %s", location, data[0]);
free(location);
location = NULL;
cur_dive->dive_site_uuid = find_or_create_dive_site_with_name(tmp, cur_dive->when);
state->cur_dive->dive_site_uuid = find_or_create_dive_site_with_name(tmp, state->cur_dive->when);
free(tmp);
} else {
location = strdup(data[0]);
@ -107,13 +107,14 @@ extern int cobalt_location(void *handle, int columns, char **data, char **column
}
extern int cobalt_dive(void *param, int columns, char **data, char **column)
static int cobalt_dive(void *param, int columns, char **data, char **column)
{
UNUSED(columns);
UNUSED(column);
int retval = 0;
sqlite3 *handle = (sqlite3 *)param;
struct parser_state *state = (struct parser_state *)param;
sqlite3 *handle = state->sql_handle;
char *err = NULL;
char get_profile_template[] = "select runtime*60,(DepthPressure*10000/SurfacePressure)-10000,p.Temperature from Dive AS d JOIN TrackPoints AS p ON d.Id=p.DiveId where d.Id=%d";
char get_cylinder_template[] = "select FO2,FHe,StartingPressure,EndingPressure,TankSize,TankPressure,TotalConsumption from GasMixes where DiveID=%d and StartingPressure>0 and EndingPressure > 0 group by FO2,FHe";
@ -123,13 +124,13 @@ extern int cobalt_dive(void *param, int columns, char **data, char **column)
char get_site_template[] = "select l.Data from Items AS i, List AS l ON i.Value1=l.Id where i.DiveId=%d and l.Type=1";
char get_buffer[1024];
dive_start();
cur_dive->number = atoi(data[0]);
dive_start(state);
state->cur_dive->number = atoi(data[0]);
cur_dive->when = (time_t)(atol(data[1]));
state->cur_dive->when = (time_t)(atol(data[1]));
if (data[4])
utf8_string(data[4], &cur_dive->notes);
utf8_string(data[4], &state->cur_dive->notes);
/* data[5] should have information on Units used, but I cannot
* parse it at all based on the sample log I have received. The
@ -137,79 +138,79 @@ extern int cobalt_dive(void *param, int columns, char **data, char **column)
* that.
*/
metric = 0;
state->metric = 0;
/* Cobalt stores the pressures, not the depth */
if (data[6])
cur_dive->dc.maxdepth.mm = atoi(data[6]);
state->cur_dive->dc.maxdepth.mm = atoi(data[6]);
if (data[7])
cur_dive->dc.duration.seconds = atoi(data[7]);
state->cur_dive->dc.duration.seconds = atoi(data[7]);
if (data[8])
cur_dive->dc.surface_pressure.mbar = atoi(data[8]);
state->cur_dive->dc.surface_pressure.mbar = atoi(data[8]);
/*
* TODO: the deviceid hash should be calculated here.
*/
settings_start();
dc_settings_start();
settings_start(state);
dc_settings_start(state);
if (data[9]) {
utf8_string(data[9], &cur_settings.dc.serial_nr);
cur_settings.dc.deviceid = atoi(data[9]);
cur_settings.dc.model = strdup("Cobalt import");
utf8_string(data[9], &state->cur_settings.dc.serial_nr);
state->cur_settings.dc.deviceid = atoi(data[9]);
state->cur_settings.dc.model = strdup("Cobalt import");
}
dc_settings_end();
settings_end();
dc_settings_end(state);
settings_end(state);
if (data[9]) {
cur_dive->dc.deviceid = atoi(data[9]);
cur_dive->dc.model = strdup("Cobalt import");
state->cur_dive->dc.deviceid = atoi(data[9]);
state->cur_dive->dc.model = strdup("Cobalt import");
}
snprintf(get_buffer, sizeof(get_buffer) - 1, get_cylinder_template, cur_dive->number);
retval = sqlite3_exec(handle, get_buffer, &cobalt_cylinders, 0, &err);
snprintf(get_buffer, sizeof(get_buffer) - 1, get_cylinder_template, state->cur_dive->number);
retval = sqlite3_exec(handle, get_buffer, &cobalt_cylinders, state, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query cobalt_cylinders failed.\n");
return 1;
}
snprintf(get_buffer, sizeof(get_buffer) - 1, get_buddy_template, cur_dive->number);
retval = sqlite3_exec(handle, get_buffer, &cobalt_buddies, 0, &err);
snprintf(get_buffer, sizeof(get_buffer) - 1, get_buddy_template, state->cur_dive->number);
retval = sqlite3_exec(handle, get_buffer, &cobalt_buddies, state, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query cobalt_buddies failed.\n");
return 1;
}
snprintf(get_buffer, sizeof(get_buffer) - 1, get_visibility_template, cur_dive->number);
retval = sqlite3_exec(handle, get_buffer, &cobalt_visibility, 0, &err);
snprintf(get_buffer, sizeof(get_buffer) - 1, get_visibility_template, state->cur_dive->number);
retval = sqlite3_exec(handle, get_buffer, &cobalt_visibility, state, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query cobalt_visibility failed.\n");
return 1;
}
snprintf(get_buffer, sizeof(get_buffer) - 1, get_location_template, cur_dive->number);
retval = sqlite3_exec(handle, get_buffer, &cobalt_location, 0, &err);
snprintf(get_buffer, sizeof(get_buffer) - 1, get_location_template, state->cur_dive->number);
retval = sqlite3_exec(handle, get_buffer, &cobalt_location, state, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query cobalt_location failed.\n");
return 1;
}
snprintf(get_buffer, sizeof(get_buffer) - 1, get_site_template, cur_dive->number);
retval = sqlite3_exec(handle, get_buffer, &cobalt_location, 0, &err);
snprintf(get_buffer, sizeof(get_buffer) - 1, get_site_template, state->cur_dive->number);
retval = sqlite3_exec(handle, get_buffer, &cobalt_location, state, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query cobalt_location (site) failed.\n");
return 1;
}
snprintf(get_buffer, sizeof(get_buffer) - 1, get_profile_template, cur_dive->number);
retval = sqlite3_exec(handle, get_buffer, &cobalt_profile_sample, 0, &err);
snprintf(get_buffer, sizeof(get_buffer) - 1, get_profile_template, state->cur_dive->number);
retval = sqlite3_exec(handle, get_buffer, &cobalt_profile_sample, state, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query cobalt_profile_sample failed.\n");
return 1;
}
dive_end();
dive_end(state);
return SQLITE_OK;
}
@ -223,11 +224,16 @@ int parse_cobalt_buffer(sqlite3 *handle, const char *url, const char *buffer, in
int retval;
char *err = NULL;
target_table = table;
struct parser_state state;
init_parser_state(&state);
state.target_table = table;
state.sql_handle = handle;
char get_dives[] = "select Id,strftime('%s',DiveStartTime),LocationId,'buddy','notes',Units,(MaxDepthPressure*10000/SurfacePressure)-10000,DiveMinutes,SurfacePressure,SerialNumber,'model' from Dive where IsViewDeleted = 0";
retval = sqlite3_exec(handle, get_dives, &cobalt_dive, handle, &err);
retval = sqlite3_exec(handle, get_dives, &cobalt_dive, &state, &err);
free_parser_state(&state);
if (retval != SQLITE_OK) {
fprintf(stderr, "Database query failed '%s'.\n", url);
@ -236,4 +242,3 @@ int parse_cobalt_buffer(sqlite3 *handle, const char *url, const char *buffer, in
return 0;
}