Add option to allocate the samples in fake_dc()

We (ab)use fake_dc() to create a pleasing profile for a manually added
dive. Based on it's intended use, fake_dc() simply handed back a dc
structure that pointed at staticly allocated samples - that's obviously
(now that I think about it) going to blow up in my face if I edit a
manually added dive more than once.

So now we have an option for fake_dc() to actually allocate the samples -
this way the rest of the code can treat these samples as we would treat
samples created any other way. We can free them and replace them with a
new set.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2016-02-20 09:36:14 -08:00
parent 0b0e56edeb
commit 38ab11a6c0
5 changed files with 11 additions and 7 deletions

View file

@ -600,7 +600,7 @@ void ProfileWidget2::plotDive(struct dive *d, bool force)
struct divecomputer *currentdc = select_dc(&displayed_dive);
Q_ASSERT(currentdc);
if (!currentdc || !currentdc->samples) {
currentdc = fake_dc(currentdc);
currentdc = fake_dc(currentdc, false);
}
bool setpointflag = (currentdc->divemode == CCR) && prefs.pp_graphs.po2 && current_dive;

View file

@ -556,7 +556,7 @@ QString QMLManager::commitChanges(QString diveId, QString date, QString location
// first clear out the mean depth (or the fake_dc() function tries
// to be too clever
d->meandepth.mm = d->dc.meandepth.mm = 0;
d->dc = *fake_dc(&d->dc);
d->dc = *fake_dc(&d->dc, true);
}
DiveListModel::instance()->updateDive(oldIdx, d);
}

View file

@ -108,12 +108,16 @@ static void fill_samples_no_avg(struct sample *s, int max_d, int max_t, double s
}
}
struct divecomputer *fake_dc(struct divecomputer *dc)
struct divecomputer *fake_dc(struct divecomputer *dc, bool alloc)
{
static struct sample fake[6];
static struct sample fake_samples[6];
static struct divecomputer fakedc;
struct sample *fake = fake_samples;
fakedc = (*dc);
if (alloc)
fake = malloc(sizeof(fake_samples));
fakedc.sample = fake;
fakedc.samples = 6;
@ -122,7 +126,7 @@ struct divecomputer *fake_dc(struct divecomputer *dc)
int max_d = dc->maxdepth.mm;
int avg_d = dc->meandepth.mm;
memset(fake, 0, sizeof(fake));
memset(fake, 0, sizeof(fake_samples));
fake[5].time.seconds = max_t;
if (!max_t || !max_d)
return &fakedc;

View file

@ -6,7 +6,7 @@
extern "C" {
#endif
extern struct divecomputer *fake_dc(struct divecomputer *dc);
extern struct divecomputer *fake_dc(struct divecomputer *dc, bool alloc);
extern void create_device_node(const char *model, uint32_t deviceid, const char *serial, const char *firmware, const char *nickname);
extern void call_for_each_dc(void *f, void (*callback)(void *, const char *, uint32_t,
const char *, const char *, const char *), bool select_only);

View file

@ -762,7 +762,7 @@ void per_cylinder_mean_depth(struct dive *dive, struct divecomputer *dc, int *me
return;
}
if (!dc->samples)
dc = fake_dc(dc);
dc = fake_dc(dc, false);
for (i = 0; i < dc->samples; i++) {
struct sample *sample = dc->sample + i;
int time = sample->time.seconds;