cleanup: don't allocate device_data_t structure

These can all just be local objects.

Also, don't overwrite them with 0. We later want to convert the
string to std::string, where this would be very sketchy.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-05-02 09:50:02 +02:00 committed by bstoeger
parent b74703b61d
commit 0915c1ce43
4 changed files with 32 additions and 37 deletions

View file

@ -104,21 +104,21 @@ static int read_file_header(unsigned char *buffer)
* Returns libdc's equivalent model number (also from g_models) or zero if * Returns libdc's equivalent model number (also from g_models) or zero if
* this a manual dive. * this a manual dive.
*/ */
static int dtrak_prepare_data(int model, device_data_t *dev_data) static int dtrak_prepare_data(int model, device_data_t &dev_data)
{ {
dc_descriptor_t *d = NULL; dc_descriptor_t *d = NULL;
int i = 0; int i = 0;
while (model != g_models[i].model_num && g_models[i].model_num != 0xEE) while (model != g_models[i].model_num && g_models[i].model_num != 0xEE)
i++; i++;
dev_data->model = copy_string(g_models[i].name); dev_data.model = copy_string(g_models[i].name);
dev_data->vendor = (const char *)malloc(strlen(g_models[i].name) + 1); dev_data.vendor = (const char *)malloc(strlen(g_models[i].name) + 1);
sscanf(g_models[i].name, "%[A-Za-z] ", (char *)dev_data->vendor); sscanf(g_models[i].name, "%[A-Za-z] ", (char *)dev_data.vendor);
dev_data->product = copy_string(strchr(g_models[i].name, ' ') + 1); dev_data.product = copy_string(strchr(g_models[i].name, ' ') + 1);
d = get_descriptor(g_models[i].type, g_models[i].libdc_num); d = get_descriptor(g_models[i].type, g_models[i].libdc_num);
if (d) if (d)
dev_data->descriptor = d; dev_data.descriptor = d;
else else
return 0; return 0;
return g_models[i].libdc_num; return g_models[i].libdc_num;
@ -174,8 +174,8 @@ static char *dt_dive_parser(unsigned char *runner, struct dive *dt_dive, struct
struct dive_site *ds; struct dive_site *ds;
char is_nitrox = 0, is_O2 = 0, is_SCR = 0; char is_nitrox = 0, is_O2 = 0, is_SCR = 0;
device_data_t *devdata = (device_data_t *)calloc(1, sizeof(device_data_t)); device_data_t devdata;
devdata->log = log; devdata.log = log;
/* /*
* Parse byte to byte till next dive entry * Parse byte to byte till next dive entry
@ -520,7 +520,7 @@ static char *dt_dive_parser(unsigned char *runner, struct dive *dt_dive, struct
libdc_model = dtrak_prepare_data(tmp_1byte, devdata); libdc_model = dtrak_prepare_data(tmp_1byte, devdata);
if (!libdc_model) if (!libdc_model)
report_error(translate("gettextFromC", "[Warning] Manual dive # %d\n"), dt_dive->number); report_error(translate("gettextFromC", "[Warning] Manual dive # %d\n"), dt_dive->number);
dt_dive->dc.model = copy_string(devdata->model); dt_dive->dc.model = copy_string(devdata.model);
/* /*
* Air usage, unknown use. Probably allows or deny manually entering gas * Air usage, unknown use. Probably allows or deny manually entering gas
@ -543,7 +543,7 @@ static char *dt_dive_parser(unsigned char *runner, struct dive *dt_dive, struct
compl_buffer = (unsigned char *) calloc(18 + profile_length, 1); compl_buffer = (unsigned char *) calloc(18 + profile_length, 1);
rc = dt_libdc_buffer(membuf, profile_length, libdc_model, compl_buffer); rc = dt_libdc_buffer(membuf, profile_length, libdc_model, compl_buffer);
if (rc == DC_STATUS_SUCCESS) { if (rc == DC_STATUS_SUCCESS) {
libdc_buffer_parser(dt_dive, devdata, compl_buffer, profile_length + 18); libdc_buffer_parser(dt_dive, &devdata, compl_buffer, profile_length + 18);
} else { } else {
report_error(translate("gettextFromC", "[Error] Out of memory for dive %d. Abort parsing."), dt_dive->number); report_error(translate("gettextFromC", "[Error] Out of memory for dive %d. Abort parsing."), dt_dive->number);
free(compl_buffer); free(compl_buffer);
@ -570,11 +570,9 @@ static char *dt_dive_parser(unsigned char *runner, struct dive *dt_dive, struct
get_cylinder(dt_dive, 0)->end.mbar = get_cylinder(dt_dive, 0)->start.mbar - get_cylinder(dt_dive, 0)->end.mbar = get_cylinder(dt_dive, 0)->start.mbar -
((get_cylinder(dt_dive, 0)->gas_used.mliter / get_cylinder(dt_dive, 0)->type.size.mliter) * 1000); ((get_cylinder(dt_dive, 0)->gas_used.mliter / get_cylinder(dt_dive, 0)->type.size.mliter) * 1000);
} }
free(devdata);
return (char *)membuf; return (char *)membuf;
bail: bail:
free(locality); free(locality);
free(devdata);
return NULL; return NULL;
} }

View file

@ -202,7 +202,6 @@ void show_computer_list()
DCDeviceData::DCDeviceData() DCDeviceData::DCDeviceData()
{ {
memset(&data, 0, sizeof(data));
data.log = nullptr; data.log = nullptr;
data.diveid = 0; data.diveid = 0;
#if defined(BT_SUPPORT) #if defined(BT_SUPPORT)

View file

@ -145,7 +145,6 @@ ConfigureDiveComputerDialog::ConfigureDiveComputerDialog(const QString &filename
ui.connectBluetoothButton->setVisible(false); ui.connectBluetoothButton->setVisible(false);
#endif #endif
memset(&device_data, 0, sizeof(device_data));
fill_computer_list(); fill_computer_list();
unsigned int selectedDiveComputerIndex = 0; unsigned int selectedDiveComputerIndex = 0;
@ -267,7 +266,7 @@ ConfigureDiveComputerDialog::ConfigureDiveComputerDialog(const QString &filename
OstcFirmwareCheck::OstcFirmwareCheck(const QString &product) : parent(0) OstcFirmwareCheck::OstcFirmwareCheck(const QString &product) : parent(0)
{ {
QUrl url; QUrl url;
memset(&devData, 1, sizeof(devData)); devData = device_data_t();
if (product == "OSTC 3" || product == "OSTC 3+" || product == "OSTC cR" || product == "OSTC Plus") { if (product == "OSTC 3" || product == "OSTC 3+" || product == "OSTC cR" || product == "OSTC Plus") {
url = QUrl("http://www.heinrichsweikamp.net/autofirmware/ostc3_changelog.txt"); url = QUrl("http://www.heinrichsweikamp.net/autofirmware/ostc3_changelog.txt");
latestFirmwareHexFile = QString("http://www.heinrichsweikamp.net/autofirmware/ostc3_firmware.hex"); latestFirmwareHexFile = QString("http://www.heinrichsweikamp.net/autofirmware/ostc3_firmware.hex");

View file

@ -819,34 +819,33 @@ static dc_descriptor_t *get_data_descriptor(int data_model, dc_family_t data_fam
* DC. dc_family_t is certainly known *only* if it is Aladin/Memomouse family * DC. dc_family_t is certainly known *only* if it is Aladin/Memomouse family
* otherwise it will be known after get_data_descriptor call. * otherwise it will be known after get_data_descriptor call.
*/ */
static dc_status_t prepare_data(int data_model, const char *serial, dc_family_t dc_fam, device_data_t *dev_data) static dc_status_t prepare_data(int data_model, const char *serial, dc_family_t dc_fam, device_data_t &dev_data)
{ {
dev_data->device = NULL; dev_data.device = NULL;
dev_data->context = NULL; dev_data.context = NULL;
if (!data_model) { if (!data_model) {
dev_data->model = copy_string(manual_dc_name); dev_data.model = copy_string(manual_dc_name);
dev_data->descriptor = NULL; dev_data.descriptor = NULL;
return DC_STATUS_NODEVICE; return DC_STATUS_NODEVICE;
} }
dev_data->descriptor = get_data_descriptor(data_model, dc_fam); dev_data.descriptor = get_data_descriptor(data_model, dc_fam);
if (dev_data->descriptor) { if (dev_data.descriptor) {
dev_data->vendor = dc_descriptor_get_vendor(dev_data->descriptor); dev_data.vendor = dc_descriptor_get_vendor(dev_data.descriptor);
dev_data->product = dc_descriptor_get_product(dev_data->descriptor); dev_data.product = dc_descriptor_get_product(dev_data.descriptor);
concat(&dev_data->model, "", format_string_std("%s %s", dev_data->vendor, dev_data->product)); concat(&dev_data.model, "", format_string_std("%s %s", dev_data.vendor, dev_data.product));
dev_data->devinfo.serial = (uint32_t) lrint(strtod(serial, NULL)); dev_data.devinfo.serial = (uint32_t) lrint(strtod(serial, NULL));
return DC_STATUS_SUCCESS; return DC_STATUS_SUCCESS;
} else { } else {
dev_data->model = copy_string("unsupported dive computer"); dev_data.model = copy_string("unsupported dive computer");
dev_data->devinfo.serial = (uint32_t) lrint(strtod(serial, NULL)); dev_data.devinfo.serial = (uint32_t) lrint(strtod(serial, NULL));
return DC_STATUS_UNSUPPORTED; return DC_STATUS_UNSUPPORTED;
} }
} }
static void device_data_free(device_data_t *dev_data) static void device_data_free(device_data_t &dev_data)
{ {
free((void *) dev_data->model); free((void *) dev_data.model);
dc_descriptor_free(dev_data->descriptor); dc_descriptor_free(dev_data.descriptor);
free(dev_data);
} }
/* /*
@ -931,7 +930,7 @@ extern "C" void smartrak_import(const char *file, struct divelog *log)
return; return;
} }
while (mdb_table.fetch_row()) { while (mdb_table.fetch_row()) {
device_data_t *devdata = (device_data_t *)calloc(1, sizeof(device_data_t)); device_data_t devdata;
dc_family_t dc_fam = DC_FAMILY_NULL; dc_family_t dc_fam = DC_FAMILY_NULL;
unsigned char *prf_buffer, *hdr_buffer; unsigned char *prf_buffer, *hdr_buffer;
struct dive *smtkdive = alloc_dive(); struct dive *smtkdive = alloc_dive();
@ -953,18 +952,18 @@ extern "C" void smartrak_import(const char *file, struct divelog *log)
dc_fam = DC_FAMILY_UWATEC_ALADIN; dc_fam = DC_FAMILY_UWATEC_ALADIN;
} }
rc = prepare_data(dc_model, (char *)col[coln(DCNUMBER)]->bind_ptr, dc_fam, devdata); rc = prepare_data(dc_model, (char *)col[coln(DCNUMBER)]->bind_ptr, dc_fam, devdata);
smtkdive->dc.model = copy_string(devdata->model); smtkdive->dc.model = copy_string(devdata.model);
if (rc == DC_STATUS_SUCCESS && mdb_table.get_len(coln(PROFILE))) { if (rc == DC_STATUS_SUCCESS && mdb_table.get_len(coln(PROFILE))) {
prf_buffer = static_cast<unsigned char *>(mdb_ole_read_full(mdb, col[coln(PROFILE)], &prf_length)); prf_buffer = static_cast<unsigned char *>(mdb_ole_read_full(mdb, col[coln(PROFILE)], &prf_length));
if (prf_length > 0) { if (prf_length > 0) {
if (dc_descriptor_get_type(devdata->descriptor) == DC_FAMILY_UWATEC_ALADIN || dc_descriptor_get_type(devdata->descriptor) == DC_FAMILY_UWATEC_MEMOMOUSE) if (dc_descriptor_get_type(devdata.descriptor) == DC_FAMILY_UWATEC_ALADIN || dc_descriptor_get_type(devdata.descriptor) == DC_FAMILY_UWATEC_MEMOMOUSE)
hdr_length = 18; hdr_length = 18;
std::vector<unsigned char> compl_buffer(hdr_length+prf_length); std::vector<unsigned char> compl_buffer(hdr_length+prf_length);
rc = libdc_buffer_complete(devdata, hdr_buffer, hdr_length, prf_buffer, prf_length, compl_buffer.data()); rc = libdc_buffer_complete(&devdata, hdr_buffer, hdr_length, prf_buffer, prf_length, compl_buffer.data());
if (rc != DC_STATUS_SUCCESS) { if (rc != DC_STATUS_SUCCESS) {
report_error("[Error][smartrak_import]\t- %s - for dive %d", errmsg(rc), smtkdive->number); report_error("[Error][smartrak_import]\t- %s - for dive %d", errmsg(rc), smtkdive->number);
} else { } else {
rc = libdc_buffer_parser(smtkdive, devdata, compl_buffer.data(), hdr_length + prf_length); rc = libdc_buffer_parser(smtkdive, &devdata, compl_buffer.data(), hdr_length + prf_length);
if (rc != DC_STATUS_SUCCESS) if (rc != DC_STATUS_SUCCESS)
report_error("[Error][libdc]\t\t- %s - for dive %d", errmsg(rc), smtkdive->number); report_error("[Error][libdc]\t\t- %s - for dive %d", errmsg(rc), smtkdive->number);
} }