Commit graph

30 commits

Author SHA1 Message Date
Berthold Stoeger
04149623c1 core: don't construct std::string from null in device.cpp
Recently the QStrings were replaced by std::strings in device.cpp
so that they can be accessed from C-code. However, libstd being
modelled after C, constructing a std::string from a NULL pointer
leads to a crash.

Fix one case where this was overlooked.

Moreover, replace a null-pointer check by empty_string(), to
treat NULL and "" equally.

Reported-by: Salvador Cuñat <salvador.cunat@gmail.com>
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-11-04 15:53:15 -08:00
Berthold Stoeger
0e196310f9 cleanup: split out divecomputer functions from dive.c
Since dive.c is so huge, split out divecomputer-related functions
into divecomputer.[c|h], sample.[c|h] and extradata.[c|h].

This does not give huge compile time improvements, since
struct dive contains a struct divecomputer and therefore
dive.h has to include divecomputer.h. However, it make things
distinctly more clear.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-25 13:59:52 -07:00
Berthold Stoeger
3a2f1b17b6 devices: add index based device removal function
The undo machinery will need a method to remove devices based
on their index instead of their name. Add it.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-25 13:59:04 -07:00
Berthold Stoeger
faebb53909 undo: add device related undo commands
Add commands for deleting devices and editing device nicknames
to include the device-handling in the undo system.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-25 13:59:04 -07:00
Berthold Stoeger
694776eed1 cleanup: rename set_dc_nickname() to add_devices_of_dive()
The function was misnamed in that it doesn't set the nickname
of a device. Instead, it adds all (unknown) devices of a
dive to the/a device-table. Let's call it appropriately.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-24 09:51:37 -07:00
Berthold Stoeger
39a4090c0a devices: add devices in Command::importTable()
Add a device_table parameters to Command::importTable() and
add_imported_dives(). The content of this table will be added
to the global device list (respectively removed on undo).

This is currently a no-op, as the parser doesn't yet fill
out the device table, but adds devices directly to the global
device table.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-24 09:51:37 -07:00
Berthold Stoeger
fadea413cd devices: return index from function adding / removing devices
This will be used to keep the model representing the device-list
up to date.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-24 09:51:37 -07:00
Berthold Stoeger
53118be1f9 devices: add functions to add / remove / check for devices
To include the device code in the undo system, we need functions
to check for the existence of devices and to add or remove them.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-24 09:51:37 -07:00
Berthold Stoeger
6b1be8c4f6 devices: use case-insensitive comparison for model
Recently, the sorting of the devices was changed to be
case-insensitive for models for consistency reasons. However,
then the equality-comparison should also be case-insensitive.

Break it out into its own function, to avoid that mistake
in the future.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-17 09:04:20 -07:00
Berthold Stoeger
c046742288 cleanup: rename clear_device_nodes() to clear_device_table()
For consistency with all the other clear_*_table functions
(dive, trip, dive_site, ...)

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-17 09:04:20 -07:00
Berthold Stoeger
1d6c1db4a5 core: use case-insensitive comparison for device models
The code in core/libdivecomputer.c used string insensitive
comparison for device models, before being merged into core/device.c.

Let's reinstate that behavior, since it appears to be more logical.
On would assume that two different vendors will not use the same
model with different casing (and the same device-ids), so that
should be safe.

This uses strcoll to correctly sort unicode, which will hopefully
never be needed!

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-16 14:26:37 -07:00
Berthold Stoeger
0c769b04b7 cleanup: replace std::find_if by std::any_of
To search for devices with the same model, we used find_if().
However, that was only to check whether such a thing exists,
not to actually do something with said device.

Therefore, change this to std::any_of() to make it clear what
the purpose of the statement is.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-16 14:26:37 -07:00
Berthold Stoeger
8549f24c91 core: add device_table parameter to device table functions
Instead of accessing the global device table directly, add a parameter
to all device-table accessing functions. This makes all places in
the code that access the global device table grep-able, which is
necessary to include the device-table code in the undo system.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-16 14:26:37 -07:00
Berthold Stoeger
7b06349be5 core: remove call_for_each_dc()
The core now loops over the devices directly - no need for this
callback.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-16 14:26:37 -07:00
Berthold Stoeger
d93b261e89 core: factor out device_is_used_by_selected_dive() function
We have a callback for all devices with a twist: it can loop
over those devices that are used by a selected dive. This is
used for exporting a subset of the dive log.

Factor out the "is device used by selected dive" part of the
function and make it available to C. The goal is to make
the whole callback thing unnecessary and let C code loop
directly over the device list.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-16 14:26:37 -07:00
Berthold Stoeger
7aca64bfca cleanup: remove device::operator!=()
This was not used. Moreover, mark device::operator==() for removal.
This is used for detecting changes in the DiveComputerModel. This
can be removed once that is integrated into the undo system.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-16 14:26:37 -07:00
Berthold Stoeger
74b8d13672 core: make get_device_for_dc() accessible from C
The function getDCExact() was used to search for a device structure
matching a divecomputer. Since C code can now access struct device,
we can export that function to C. Rename it to get_device_for_dc()
for consistency with naming of the core functions.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-16 14:26:37 -07:00
Berthold Stoeger
c4bfecce1b core: add C struct device and struct device_table accessors
Up to now, "struct device" and "struct device_table" were C++
only, because they used C++ strings for convenience. Since we
switched from QString to std::string, we can create accessors
for these structs. For the C code, we simply declare them as
opaque structs and give the full definition only for C++.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-16 14:26:37 -07:00
Berthold Stoeger
4a50badb57 cleanup: use std::vector in struct device_table
Since we converted from QString to std::string, let's also use
std::vector instead of QVector. We don't need COW semantics
and all the rigmarole. Let's try to keep Qt data structures
out of the core.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-16 14:26:37 -07:00
Berthold Stoeger
fd8bd9d5c7 cleanup: use std::string in struct device
struct device is a core data structure and therefore shouldn't use QString.
QString stores as UTF-16 (which is a very questionable choice in itself).
However, the real problem is that this puts us in lifetime-management
hell when interfacing with C code: The UTF-16 has to be converted to
UTF-8, but when returning such a string, this puts burden on the caller
who has to free it. In fact, instead of looping over devices from C-code
we had a callback that sent down temporary C-strings with qPrintable.

In contrast, std::string is guaranteed to store its data as
contiguous null-terminated and C-compatible strings. Therefore,
replace the QString by std::string. Keep the QString just in
one place that formats a hexadecimal number to avoid any
potential change.

The disadvantage of using std::string is that it will crash
when constructed with a NULL argument, consistent with C-style
functions such as strcmp, etc. Arguably, NULL is different
from the empty string even though we treat both as the same.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-16 14:26:37 -07:00
Berthold Stoeger
4e479677a0 cleanup: fix tiny memory hole in device.cpp
empty_string() returns true for "". Thus, we can't simply overwrite
the pointer if empyt_string() returns true, but must free the string
regardless. The joys of C memory management!

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-16 14:26:37 -07:00
Berthold Stoeger
2e5913d2ba core: fix detection of duplicate device names
Recently (c9b8584bd2) the sort criteria of the device-table
was changed from  (model/id) to (id/model). However, that
messed with the detection of duplicate device names: there,
the code searched for the first element greater or equal
to (model / 0).

With the reversal of the sort criteria, this would now
always give the first element.

Therefore, do a simple non-binary search, which is much
more robust. The binary search was a silly and pointless
premature optimization anyway - don't do such things
if not necessary!

Since only one place in the code search for existence
for a model-name, fold the corresponding function into
that place.

Moreover, change the code to do a case-insensitive compare.
This is consistent with the dc_match_serial() code in
core/libdivecomputer.c, where matching models is
case-insensitive!

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-13 16:26:42 -07:00
Berthold Stoeger
ff6c1a34ad cleanup: remove unused function is_default_dive_computer()
The last actual user was apparently removed back in 2013(!):
34db6dc2be

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-13 20:15:41 +02:00
Berthold Stoeger
c9b8584bd2 core: sort device-table by id/model instead of model/id
The device table is accessed by core via a callback using
call_for_each_dc(). This sorts the table by device-id. It
is unclear whether this is needed - since currently all it
does is make sure that the devices have a fixed order in XML
and git log files.

In any case, this means that the table had to be copied and
sorted in call_for_each_dc(). Since the frontend now does
its own sorting, we can just keep the core table sorted
as it needs it. This in turn will ultimately make it possible
to replace the callback by a simple loop.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-11 08:35:20 -07:00
Berthold Stoeger
5bc6f5d36c cleanup: make device code more consistent with core
We keep track of device, i.e. distinct dive computers with id in the core.
The corresponding code stuck out like a sore thumb. Firstly, because it
is C++. But more importantly, because it used inconsistent nameing conventions.

Notably it defined a "DiveComputerNode" when this is something very different
from "struct dive_computer", the latter being the dive-computer related
data of a single dive.

Since the whole thing is defined in "device.h" and the function to create
such an entry is called "create_device_node", call the structure "device".
Use snake_case for consistency with the other core structures.

Moreover, call the collection of devices "device_table" in analogy
with "dive_table", etc.

Overall, this should make the core code more consistent style-wise.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-03 10:53:26 -07:00
Berthold Stoeger
90ca635316 cleanup: use getDCExact() instead of callback in set_dc_deviceid()
core/device.c used to be a C file, which couldn't access the C++
divecomputer list directly. Therefore, instead of a simple loop,
searching for a matching DC was implemented via a callback with
void * user data parameter. Wild. Since the file is now C++, let's
just use direct access to the C++ data structures to make this
readable by mere humans.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-03 10:53:26 -07:00
Berthold Stoeger
ce7e74f62f cleanup: pass divecomputer to getDC() and getDCExact() helpers
These are used to search for device nodes and were passed model
and device id (for the exact version). However, all callers used
them to search for the node corresponding to a specific struct
divecomputer, so let's just pass that instead to make the caller
site less complex.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-03 10:53:26 -07:00
Berthold Stoeger
2b557f567a cleanup: hide DiveComputerList implementation details
Remove the declaration of helper functions needed only in
core/device.cpp. To this goal, turn the member functions
into free functions.

Cosmetics: turn the DiveComputer[Node|List] "class"es into
"struct"s, since all members were public anyway.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-03 10:53:26 -07:00
Berthold Stoeger
00abc04913 cleanup: use getDiveSelection() to loop over selected dives
getDiveSelection() returns a vector of the selected dives.
Use that instead of looping over the dive table and checking
manually.

This removes a few lines of code.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-03 10:01:13 -07:00
Berthold Stoeger
a01ab81713 cleanup: fold core/divecomputer.cpp into core/device.c
core/device.h was declaring a number of functions that were related
to divecomputers (dcs): creating a fake dc for manually entered dives
and registering / accessing dc nicknames. On could argue whether
these should be lumped together, but it is what it is.

However, part of that was implemented in C++/Qt code in a separate
core/divecomputer.cpp file. Some function therein where only
accessible to C++ and declared in core/divecomputer.h.

All in all, a big mess. Let's simply combine the files and
conditionally compile the C++-only functions depending on
the __cplusplus define.

Yes, that means turning device.c into device.cpp. A brave soul
might turn the C++/Qt code into C code if they whish later on.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-09-13 13:54:59 -07:00
Renamed from core/device.c (Browse further)