static int dm5_cylinders(void *param, int columns, char **data, char **column) { UNUSED(columns); UNUSED(column); struct parser_state *state = (struct parser_state *)param; cylinder_start(state); if (data[7] && atoi(data[7]) > 0 && atoi(data[7]) < 350000) state->cur_dive->cylinder[state->cur_cylinder_index].start.mbar = atoi(data[7]); if (data[8] && atoi(data[8]) > 0 && atoi(data[8]) < 350000) state->cur_dive->cylinder[state->cur_cylinder_index].end.mbar = (atoi(data[8])); if (data[6]) { /* DM5 shows tank size of 12 liters when the actual * value is 0 (and using metric units). So we just use * the same 12 liters when size is not available */ if (strtod_flags(data[6], NULL, 0) == 0.0 && state->cur_dive->cylinder[state->cur_cylinder_index].start.mbar) state->cur_dive->cylinder[state->cur_cylinder_index].type.size.mliter = 12000; else state->cur_dive->cylinder[state->cur_cylinder_index].type.size.mliter = lrint((strtod_flags(data[6], NULL, 0)) * 1000); } if (data[2]) state->cur_dive->cylinder[state->cur_cylinder_index].gasmix.o2.permille = atoi(data[2]) * 10; if (data[3]) state->cur_dive->cylinder[state->cur_cylinder_index].gasmix.he.permille = atoi(data[3]) * 10; cylinder_end(state); return 0; }
/* Imperial cylinder volumes need working pressure to be meaningful */ volume_t string_to_volume(const char *str, pressure_t workp) { const char *end; double value = strtod_flags(str, &end, 0); QString rest = QString(end).trimmed(); QString local_l = QObject::tr("l"); QString local_cuft = QObject::tr("cuft"); volume_t volume; if (rest.startsWith("l") || rest.startsWith("ℓ") || rest.startsWith(local_l)) goto l; if (rest.startsWith("cuft") || rest.startsWith(local_cuft)) goto cuft; /* * If we don't have explicit units, and there is no working * pressure, we're going to assume "liter" even in imperial * measurements. */ if (!workp.mbar) goto l; if (prefs.units.volume == prefs.units.LITER) goto l; cuft: if (workp.mbar) value /= bar_to_atm(workp.mbar / 1000.0); value = cuft_to_l(value); l: volume.mliter = rint(value * 1000); return volume; }
//TODO: Move to C. fraction_t string_to_fraction(const char *str) { const char *end; double value = strtod_flags(str, &end, 0); fraction_t fraction; fraction.permille = rint(value * 10); return fraction; }
static int dm5_gaschange(void *param, int columns, char **data, char **column) { UNUSED(columns); UNUSED(column); struct parser_state *state = (struct parser_state *)param; event_start(state); if (data[0]) state->cur_event.time.seconds = atoi(data[0]); if (data[1]) { strcpy(state->cur_event.name, "gaschange"); state->cur_event.value = lrint(strtod_flags(data[1], NULL, 0)); } /* He part of the mix */ if (data[2]) state->cur_event.value += lrint(strtod_flags(data[2], NULL, 0)) << 16; event_end(state); return 0; }
//TODO: Move to C. pressure_t string_to_pressure(const char *str) { const char *end; double value = strtod_flags(str, &end, 0); QString rest = QString(end).trimmed(); QString local_psi = QObject::tr("psi"); QString local_bar = QObject::tr("bar"); pressure_t pressure; if (rest.startsWith("bar") || rest.startsWith(local_bar)) goto bar; if (rest.startsWith("psi") || rest.startsWith(local_psi)) goto psi; if (prefs.units.pressure == prefs.units.PSI) goto psi; bar: pressure.mbar = rint(value * 1000); return pressure; psi: pressure.mbar = psi_to_mbar(value); return pressure; }
//TODO: Move to C. depth_t string_to_depth(const char *str) { const char *end; double value = strtod_flags(str, &end, 0); QString rest = QString(end).trimmed(); QString local_ft = QObject::tr("ft"); QString local_m = QObject::tr("m"); depth_t depth; if (rest.startsWith("m") || rest.startsWith(local_m)) goto m; if (rest.startsWith("ft") || rest.startsWith(local_ft)) goto ft; if (prefs.units.length == prefs.units.FEET) goto ft; m: depth.mm = rint(value * 1000); return depth; ft: depth.mm = feet_to_mm(value); return depth; }
//TODO: Move to C weight_t string_to_weight(const char *str) { const char *end; double value = strtod_flags(str, &end, 0); QString rest = QString(end).trimmed(); QString local_kg = QObject::tr("kg"); QString local_lbs = QObject::tr("lbs"); weight_t weight; if (rest.startsWith("kg") || rest.startsWith(local_kg)) goto kg; // using just "lb" instead of "lbs" is intentional - some people might enter the singular if (rest.startsWith("lb") || rest.startsWith(local_lbs)) goto lbs; if (prefs.units.weight == prefs.units.LBS) goto lbs; kg: weight.grams = rint(value * 1000); return weight; lbs: weight.grams = lbs_to_grams(value); return weight; }
static int dm5_dive(void *param, int columns, char **data, char **column) { UNUSED(columns); UNUSED(column); int i; int tempformat = 0; int interval, retval = 0, block_size; struct parser_state *state = (struct parser_state *)param; sqlite3 *handle = state->sql_handle; unsigned const char *sampleBlob; char *err = NULL; char get_events_template[] = "select * from Mark where DiveId = %d"; char get_tags_template[] = "select Text from DiveTag where DiveId = %d"; char get_cylinders_template[] = "select * from DiveMixture where DiveId = %d"; char get_gaschange_template[] = "select GasChangeTime,Oxygen,Helium from DiveGasChange join DiveMixture on DiveGasChange.DiveMixtureId=DiveMixture.DiveMixtureId where DiveId = %d"; char get_events[512]; dive_start(state); state->cur_dive->number = atoi(data[0]); state->cur_dive->when = (time_t)(atol(data[1])); if (data[2]) utf8_string(data[2], &state->cur_dive->notes); if (data[3]) state->cur_dive->duration.seconds = atoi(data[3]); if (data[15]) state->cur_dive->dc.duration.seconds = atoi(data[15]); /* * TODO: the deviceid hash should be calculated here. */ settings_start(state); dc_settings_start(state); if (data[4]) { utf8_string(data[4], &state->cur_settings.dc.serial_nr); state->cur_settings.dc.deviceid = atoi(data[4]); } if (data[5]) utf8_string(data[5], &state->cur_settings.dc.model); dc_settings_end(state); settings_end(state); if (data[6]) state->cur_dive->dc.maxdepth.mm = lrint(strtod_flags(data[6], NULL, 0) * 1000); if (data[8]) state->cur_dive->dc.airtemp.mkelvin = C_to_mkelvin(atoi(data[8])); if (data[9]) state->cur_dive->dc.watertemp.mkelvin = C_to_mkelvin(atoi(data[9])); if (data[4]) { state->cur_dive->dc.deviceid = atoi(data[4]); } if (data[5]) utf8_string(data[5], &state->cur_dive->dc.model); snprintf(get_events, sizeof(get_events) - 1, get_cylinders_template, state->cur_dive->number); retval = sqlite3_exec(handle, get_events, &dm5_cylinders, state, &err); if (retval != SQLITE_OK) { fprintf(stderr, "%s", "Database query dm5_cylinders failed.\n"); return 1; } if (data[14]) state->cur_dive->dc.surface_pressure.mbar = (atoi(data[14]) / 100); interval = data[16] ? atoi(data[16]) : 0; /* * sampleBlob[0] version number, indicates the size of one sample * * Following ones describe single sample, bugs in interpretation of the binary blob are likely: * * sampleBlob[3] depth * sampleBlob[7-9] pressure * sampleBlob[11] temperature - either full Celsius or float, might be different field for some version of DM */ sampleBlob = (unsigned const char *)data[24]; if (sampleBlob) { switch (sampleBlob[0]) { case 1: // Log is converted from DM4 to DM5 block_size = 16; break; case 2: block_size = 19; break; case 3: block_size = 23; break; case 4: // Temperature is stored in float tempformat = 1; block_size = 26; break; case 5: // Temperature is stored in float tempformat = 1; block_size = 30; break; default: block_size = 16; break; } } for (i = 0; interval && sampleBlob && i * interval < state->cur_dive->duration.seconds; i++) { float *depth = (float *)&sampleBlob[i * block_size + 3]; int32_t pressure = (sampleBlob[i * block_size + 9] << 16) + (sampleBlob[i * block_size + 8] << 8) + sampleBlob[i * block_size + 7]; sample_start(state); state->cur_sample->time.seconds = i * interval; state->cur_sample->depth.mm = lrintf(depth[0] * 1000.0f); if (tempformat == 1) { float *temp = (float *)&(sampleBlob[i * block_size + 11]); state->cur_sample->temperature.mkelvin = C_to_mkelvin(*temp); } else { if ((sampleBlob[i * block_size + 11]) != 0x7F) { state->cur_sample->temperature.mkelvin = C_to_mkelvin(sampleBlob[i * block_size + 11]); } } /* * Limit cylinder pressures to somewhat sensible values */ if (pressure >= 0 && pressure < 350000) state->cur_sample->pressure[0].mbar = pressure; sample_end(state); } /* * Log was converted from DM4, thus we need to parse the profile * from DM4 format */ if (i == 0) { float *profileBlob; unsigned char *tempBlob; int *pressureBlob; profileBlob = (float *)data[17]; tempBlob = (unsigned char *)data[18]; pressureBlob = (int *)data[19]; for (i = 0; interval && i * interval < state->cur_dive->duration.seconds; i++) { sample_start(state); state->cur_sample->time.seconds = i * interval; if (profileBlob) state->cur_sample->depth.mm = lrintf(profileBlob[i] * 1000.0f); else state->cur_sample->depth.mm = state->cur_dive->dc.maxdepth.mm; if (data[18] && data[18][0]) state->cur_sample->temperature.mkelvin = C_to_mkelvin(tempBlob[i]); if (data[19] && data[19][0]) state->cur_sample->pressure[0].mbar = pressureBlob[i]; sample_end(state); } } snprintf(get_events, sizeof(get_events) - 1, get_gaschange_template, state->cur_dive->number); retval = sqlite3_exec(handle, get_events, &dm5_gaschange, state, &err); if (retval != SQLITE_OK) { fprintf(stderr, "%s", "Database query dm5_gaschange failed.\n"); return 1; } snprintf(get_events, sizeof(get_events) - 1, get_events_template, state->cur_dive->number); retval = sqlite3_exec(handle, get_events, &dm4_events, state, &err); if (retval != SQLITE_OK) { fprintf(stderr, "%s", "Database query dm4_events failed.\n"); return 1; } snprintf(get_events, sizeof(get_events) - 1, get_tags_template, state->cur_dive->number); retval = sqlite3_exec(handle, get_events, &dm4_tags, state, &err); if (retval != SQLITE_OK) { fprintf(stderr, "%s", "Database query dm4_tags failed.\n"); return 1; } dive_end(state); return SQLITE_OK; }
static int dm4_dive(void *param, int columns, char **data, char **column) { UNUSED(columns); UNUSED(column); int i; int interval, retval = 0; struct parser_state *state = (struct parser_state *)param; sqlite3 *handle = state->sql_handle; float *profileBlob; unsigned char *tempBlob; int *pressureBlob; char *err = NULL; char get_events_template[] = "select * from Mark where DiveId = %d"; char get_tags_template[] = "select Text from DiveTag where DiveId = %d"; char get_events[64]; dive_start(state); state->cur_dive->number = atoi(data[0]); state->cur_dive->when = (time_t)(atol(data[1])); if (data[2]) utf8_string(data[2], &state->cur_dive->notes); /* * DM4 stores Duration and DiveTime. It looks like DiveTime is * 10 to 60 seconds shorter than Duration. However, I have no * idea what is the difference and which one should be used. * Duration = data[3] * DiveTime = data[15] */ if (data[3]) state->cur_dive->duration.seconds = atoi(data[3]); if (data[15]) state->cur_dive->dc.duration.seconds = atoi(data[15]); /* * TODO: the deviceid hash should be calculated here. */ settings_start(state); dc_settings_start(state); if (data[4]) utf8_string(data[4], &state->cur_settings.dc.serial_nr); if (data[5]) utf8_string(data[5], &state->cur_settings.dc.model); state->cur_settings.dc.deviceid = 0xffffffff; dc_settings_end(state); settings_end(state); if (data[6]) state->cur_dive->dc.maxdepth.mm = lrint(strtod_flags(data[6], NULL, 0) * 1000); if (data[8]) state->cur_dive->dc.airtemp.mkelvin = C_to_mkelvin(atoi(data[8])); if (data[9]) state->cur_dive->dc.watertemp.mkelvin = C_to_mkelvin(atoi(data[9])); /* * TODO: handle multiple cylinders */ cylinder_start(state); if (data[22] && atoi(data[22]) > 0) state->cur_dive->cylinder[state->cur_cylinder_index].start.mbar = atoi(data[22]); else if (data[10] && atoi(data[10]) > 0) state->cur_dive->cylinder[state->cur_cylinder_index].start.mbar = atoi(data[10]); if (data[23] && atoi(data[23]) > 0) state->cur_dive->cylinder[state->cur_cylinder_index].end.mbar = (atoi(data[23])); if (data[11] && atoi(data[11]) > 0) state->cur_dive->cylinder[state->cur_cylinder_index].end.mbar = (atoi(data[11])); if (data[12]) state->cur_dive->cylinder[state->cur_cylinder_index].type.size.mliter = lrint((strtod_flags(data[12], NULL, 0)) * 1000); if (data[13]) state->cur_dive->cylinder[state->cur_cylinder_index].type.workingpressure.mbar = (atoi(data[13])); if (data[20]) state->cur_dive->cylinder[state->cur_cylinder_index].gasmix.o2.permille = atoi(data[20]) * 10; if (data[21]) state->cur_dive->cylinder[state->cur_cylinder_index].gasmix.he.permille = atoi(data[21]) * 10; cylinder_end(state); if (data[14]) state->cur_dive->dc.surface_pressure.mbar = (atoi(data[14]) * 1000); interval = data[16] ? atoi(data[16]) : 0; profileBlob = (float *)data[17]; tempBlob = (unsigned char *)data[18]; pressureBlob = (int *)data[19]; for (i = 0; interval && i * interval < state->cur_dive->duration.seconds; i++) { sample_start(state); state->cur_sample->time.seconds = i * interval; if (profileBlob) state->cur_sample->depth.mm = lrintf(profileBlob[i] * 1000.0f); else state->cur_sample->depth.mm = state->cur_dive->dc.maxdepth.mm; if (data[18] && data[18][0]) state->cur_sample->temperature.mkelvin = C_to_mkelvin(tempBlob[i]); if (data[19] && data[19][0]) state->cur_sample->pressure[0].mbar = pressureBlob[i]; sample_end(state); } snprintf(get_events, sizeof(get_events) - 1, get_events_template, state->cur_dive->number); retval = sqlite3_exec(handle, get_events, &dm4_events, state, &err); if (retval != SQLITE_OK) { fprintf(stderr, "%s", "Database query dm4_events failed.\n"); return 1; } snprintf(get_events, sizeof(get_events) - 1, get_tags_template, state->cur_dive->number); retval = sqlite3_exec(handle, get_events, &dm4_tags, state, &err); if (retval != SQLITE_OK) { fprintf(stderr, "%s", "Database query dm4_tags failed.\n"); return 1; } dive_end(state); /* for (i=0; i<columns;++i) { fprintf(stderr, "%s\t", column[i]); } fprintf(stderr, "\n"); for (i=0; i<columns;++i) { fprintf(stderr, "%s\t", data[i]); } fprintf(stderr, "\n"); //exit(0); */ return SQLITE_OK; }