mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Create simple non-thread-safe "gasname()" function
Every single user of the get_gas_name() function wanted to just use the result to create a message or printout, and that made the interface a bit cumbersome. This adds a "gasname()" wrapper that returns a static buffer with the result in it, which is neither pretty nor thread-safe, but is a much simpler interface to work with. Our dive planning isn't multi-threaded, so nothing should care. [ This also converts a few open-coded gasname debug printouts to use the helper function, resulting in prettier printouts and less code ] Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
d825d689dd
commit
ee36bf8bf3
1 changed files with 21 additions and 24 deletions
45
planner.c
45
planner.c
|
@ -103,6 +103,14 @@ static void get_gas_string(const struct gasmix *gasmix, char *text, int len)
|
||||||
snprintf(text, len, "(%d/%d)", (get_o2(gasmix) + 5) / 10, (get_he(gasmix) + 5) / 10);
|
snprintf(text, len, "(%d/%d)", (get_o2(gasmix) + 5) / 10, (get_he(gasmix) + 5) / 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Returns a static char buffer - only good for immediate use by printf etc */
|
||||||
|
static const char *gasname(const struct gasmix *gasmix)
|
||||||
|
{
|
||||||
|
static char gas[64];
|
||||||
|
get_gas_string(gasmix, gas, sizeof(gas));
|
||||||
|
return gas;
|
||||||
|
}
|
||||||
|
|
||||||
double interpolate_transition(struct dive *dive, int t0, int t1, int d0, int d1, const struct gasmix *gasmix, int ppo2)
|
double interpolate_transition(struct dive *dive, int t0, int t1, int d0, int d1, const struct gasmix *gasmix, int ppo2)
|
||||||
{
|
{
|
||||||
int j;
|
int j;
|
||||||
|
@ -143,9 +151,7 @@ double tissue_at_end(struct dive *dive, char **cached_datap)
|
||||||
t1 = sample->time.seconds;
|
t1 = sample->time.seconds;
|
||||||
get_gas_from_events(&dive->dc, t0, &gas);
|
get_gas_from_events(&dive->dc, t0, &gas);
|
||||||
if ((gasidx = get_gasidx(dive, &gas)) == -1) {
|
if ((gasidx = get_gasidx(dive, &gas)) == -1) {
|
||||||
char gas_string[50];
|
report_error(translate("gettextFromC", "Can't find gas %s"), gasname(&gas));
|
||||||
get_gas_string(&gas, gas_string, sizeof(gas_string));
|
|
||||||
report_error(translate("gettextFromC", "Can't find gas %s"), gas_string);
|
|
||||||
gasidx = 0;
|
gasidx = 0;
|
||||||
}
|
}
|
||||||
if (i > 0)
|
if (i > 0)
|
||||||
|
@ -200,9 +206,7 @@ static int verify_gas_exists(struct dive *dive, struct gasmix mix_in)
|
||||||
if (gasmix_distance(&cyl->gasmix, &mix_in) < 200)
|
if (gasmix_distance(&cyl->gasmix, &mix_in) < 200)
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
char gas[50];
|
fprintf(stderr, "this gas %s should have been on the cylinder list\nThings will fail now\n", gasname(&mix_in));
|
||||||
get_gas_string(&mix_in, gas, sizeof(gas));
|
|
||||||
fprintf(stderr, "this gas %s should have been on the cylinder list\nThings will fail now\n", gas);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -439,10 +443,11 @@ static struct gaschanges *analyze_gaslist(struct diveplan *diveplan, struct dive
|
||||||
}
|
}
|
||||||
*gaschangenr = nr;
|
*gaschangenr = nr;
|
||||||
#if DEBUG_PLAN & 16
|
#if DEBUG_PLAN & 16
|
||||||
for (nr = 0; nr < *gaschangenr; nr++)
|
for (nr = 0; nr < *gaschangenr; nr++) {
|
||||||
printf("gaschange nr %d: @ %5.2lfm gasidx %d (%d/%d)\n", nr, gaschanges[nr].depth / 1000.0,
|
int idx = gaschanges[nr].gasidx;
|
||||||
gaschanges[nr].gasidx, (get_o2(&dive->cylinder[gaschanges[nr].gasidx].gasmix) + 5) / 10,
|
printf("gaschange nr %d: @ %5.2lfm gasidx %d (%s)\n", nr, gaschanges[nr].depth / 1000.0,
|
||||||
(get_he(&dive->cylinder[gaschanges[nr].gasidx].gasmix) + 5) / 10);
|
idx, gasname(&dive->cylinder[idx].gasmix));
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
return gaschanges;
|
return gaschanges;
|
||||||
}
|
}
|
||||||
|
@ -521,7 +526,6 @@ static void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool
|
||||||
do {
|
do {
|
||||||
struct gasmix gasmix, newgasmix;
|
struct gasmix gasmix, newgasmix;
|
||||||
const char *depth_unit;
|
const char *depth_unit;
|
||||||
char gas[64];
|
|
||||||
double depthvalue;
|
double depthvalue;
|
||||||
int decimals;
|
int decimals;
|
||||||
struct divedatapoint *nextdp;
|
struct divedatapoint *nextdp;
|
||||||
|
@ -546,7 +550,6 @@ static void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool
|
||||||
dp->depth != lastdepth &&
|
dp->depth != lastdepth &&
|
||||||
nextdp->depth != dp->depth)
|
nextdp->depth != dp->depth)
|
||||||
continue;
|
continue;
|
||||||
get_gas_string(&gasmix, gas, sizeof(gas));
|
|
||||||
gasidx = get_gasidx(dive, &gasmix);
|
gasidx = get_gasidx(dive, &gasmix);
|
||||||
len = strlen(buffer);
|
len = strlen(buffer);
|
||||||
if (dp->depth != lastdepth)
|
if (dp->depth != lastdepth)
|
||||||
|
@ -554,18 +557,16 @@ static void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool
|
||||||
decimals, depthvalue, depth_unit,
|
decimals, depthvalue, depth_unit,
|
||||||
FRACTION(dp->time - lasttime, 60),
|
FRACTION(dp->time - lasttime, 60),
|
||||||
FRACTION(dp->time, 60),
|
FRACTION(dp->time, 60),
|
||||||
gas);
|
gasname(&gasmix));
|
||||||
else
|
else
|
||||||
snprintf(buffer + len, sizeof(buffer) - len, translate("gettextFromC", "Stay at %.*f %s for %d:%02d min - runtime %d:%02u on %s\n"),
|
snprintf(buffer + len, sizeof(buffer) - len, translate("gettextFromC", "Stay at %.*f %s for %d:%02d min - runtime %d:%02u on %s\n"),
|
||||||
decimals, depthvalue, depth_unit,
|
decimals, depthvalue, depth_unit,
|
||||||
FRACTION(dp->time - lasttime, 60),
|
FRACTION(dp->time - lasttime, 60),
|
||||||
FRACTION(dp->time, 60),
|
FRACTION(dp->time, 60),
|
||||||
gas);
|
gasname(&gasmix));
|
||||||
if (nextdp && gasmix_distance(&gasmix, &newgasmix)) {
|
if (nextdp && gasmix_distance(&gasmix, &newgasmix)) {
|
||||||
// gas switch at this waypoint
|
// gas switch at this waypoint
|
||||||
get_gas_string(&newgasmix, gas, sizeof(gas));
|
snprintf(buffer + len, sizeof(buffer) - len, translate("gettextFromC", "Switch gas to %s\n"), gasname(&newgasmix));
|
||||||
len = strlen(buffer);
|
|
||||||
snprintf(buffer + len, sizeof(buffer) - len, translate("gettextFromC", "Switch gas to %s\n"), gas);
|
|
||||||
gasmix = newgasmix;
|
gasmix = newgasmix;
|
||||||
}
|
}
|
||||||
lasttime = dp->time;
|
lasttime = dp->time;
|
||||||
|
@ -576,13 +577,11 @@ static void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool
|
||||||
for (gasidx = 0; gasidx < MAX_CYLINDERS; gasidx++) {
|
for (gasidx = 0; gasidx < MAX_CYLINDERS; gasidx++) {
|
||||||
double volume;
|
double volume;
|
||||||
const char *unit;
|
const char *unit;
|
||||||
char gas[64];
|
|
||||||
const char *warning = "";
|
const char *warning = "";
|
||||||
cylinder_t *cyl = &dive->cylinder[gasidx];
|
cylinder_t *cyl = &dive->cylinder[gasidx];
|
||||||
if (cylinder_none(cyl))
|
if (cylinder_none(cyl))
|
||||||
break;
|
break;
|
||||||
len = strlen(buffer);
|
len = strlen(buffer);
|
||||||
get_gas_string(&cyl->gasmix, gas, sizeof(gas));
|
|
||||||
volume = get_volume_units(cyl->gas_used.mliter, NULL, &unit);
|
volume = get_volume_units(cyl->gas_used.mliter, NULL, &unit);
|
||||||
if (cyl->type.size.mliter) {
|
if (cyl->type.size.mliter) {
|
||||||
/* Warn if the plan uses more gas than is available in a cylinder
|
/* Warn if the plan uses more gas than is available in a cylinder
|
||||||
|
@ -591,7 +590,7 @@ static void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool
|
||||||
if (cyl->end.mbar < 10000)
|
if (cyl->end.mbar < 10000)
|
||||||
warning = translate("gettextFromC", "WARNING: this is more gas than available in the specified cylinder!");
|
warning = translate("gettextFromC", "WARNING: this is more gas than available in the specified cylinder!");
|
||||||
}
|
}
|
||||||
snprintf(buffer + len, sizeof(buffer) - len, translate("gettextFromC", "%.0f%s of %s%s\n"), volume, unit, gas, warning);
|
snprintf(buffer + len, sizeof(buffer) - len, translate("gettextFromC", "%.0f%s of %s%s\n"), volume, unit, gasname(&cyl->gasmix), warning);
|
||||||
}
|
}
|
||||||
dive->notes = strdup(buffer);
|
dive->notes = strdup(buffer);
|
||||||
}
|
}
|
||||||
|
@ -651,9 +650,7 @@ void plan(struct diveplan *diveplan, char **cached_datap, struct dive **divep, s
|
||||||
get_gas_from_events(&dive->dc, sample->time.seconds, &gas);
|
get_gas_from_events(&dive->dc, sample->time.seconds, &gas);
|
||||||
po2 = dive->dc.sample[dive->dc.samples - 1].po2;
|
po2 = dive->dc.sample[dive->dc.samples - 1].po2;
|
||||||
if ((current_cylinder = get_gasidx(dive, &gas)) == -1) {
|
if ((current_cylinder = get_gasidx(dive, &gas)) == -1) {
|
||||||
char gas_string[50];
|
report_error(translate("gettextFromC", "Can't find gas %s"), gasname(&gas));
|
||||||
get_gas_string(&gas, gas_string, sizeof(gas_string));
|
|
||||||
report_error(translate("gettextFromC", "Can't find gas %s"), gas_string);
|
|
||||||
current_cylinder = 0;
|
current_cylinder = 0;
|
||||||
}
|
}
|
||||||
depth = dive->dc.sample[dive->dc.samples - 1].depth.mm;
|
depth = dive->dc.sample[dive->dc.samples - 1].depth.mm;
|
||||||
|
@ -675,7 +672,7 @@ void plan(struct diveplan *diveplan, char **cached_datap, struct dive **divep, s
|
||||||
tissue_tolerance = tissue_at_end(dive, cached_datap);
|
tissue_tolerance = tissue_at_end(dive, cached_datap);
|
||||||
|
|
||||||
#if DEBUG_PLAN & 4
|
#if DEBUG_PLAN & 4
|
||||||
printf("gas %d/%d\n", gas.o2.permille, gas.he.permille);
|
printf("gas %s\n", gasname(&gas));
|
||||||
printf("depth %5.2lfm ceiling %5.2lfm\n", depth / 1000.0, ceiling / 1000.0);
|
printf("depth %5.2lfm ceiling %5.2lfm\n", depth / 1000.0, ceiling / 1000.0);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue