Commit graph

2870 commits

Author SHA1 Message Date
Berthold Stoeger
e29a0c1b29 import: free dc_descriptor in new device_data_t destructor
It seems that smartrak was the only part of the code that cared
about freeing the dc_descriptor. Make that a general feature
of the new device_data_t destructor, which we could implement
now that things are in C++.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
01306224ff import: turn C-string in device_data_t into std::strings
It was never clear what was a pointer to a static string from
libdivecomputer and what was allocated.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
0915c1ce43 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>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
b74703b61d core: convert ostctools as C++
Replace some of the memory management by C++ idioms.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
fe0bb905f3 core: convert pref.c and units.c to C++
Convert both files simultanously, because the SI_UNITS define works
either under C or under C++.

This was painful, because initialization of struct-members has to
be done in order of definition in C++. And it was completely out
of order. However, as long as not all is C++, we can't use
default initialization directly in the struct definition. :(

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
6cda13a9fe core: replace core/timer.c by std::chrono
No point in reimplementing the wheel.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
1daa4f0584 core: C++-ify statistics.c
The old code was wild: For the yearly statistics it would allocate
one entry per dive in the log. Of course, it would also leak
C-style strings.

Convert the whole thing to somewhat idiomatic C++.

Somewhat wasted work, because I'd like to convert the whole thing
to the new statistics code. But let's finish the conversion to C++
first.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
84219641de core: remove MIN() and MAX() macros
All have been converted to std::min() and std::max().

Moreover, this was Windows only and since we cross-compile, it
is not even clear if this is needed.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
edc0c69943 core: convert divecomputer.c to C++
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
a244e71256 core: replace MIN() by type-sage std::min() in divelist.cpp
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
7b79bc954b core: convert divelist.c to C++
Fortunately, not much to do in this file.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
a6815661d5 uemis: replace a defines by numeric constants
Always good to use the type system of the language instead of
text substitution.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
52fb77da69 uemis: replace UEMIS_CHECK_* defines by enum
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
018884dfde uemis: turn UEMIS_MEM_* defines into enum
One value (UEMIS_MEM_CRITICAL) wasn't ever used. Remove it.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
6d8a904981 uemis: replace divespot_mapping by hash-map
This was the umpteenth inefficient reinvention of a trivial
map. Replace by a hash-map (std::unordered_map). Might just
as well use a balanced binary tree or a sorted array. In the
end, it probably doesn't matter at all.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
f3a36b62ac uemis: unglobalize mindiveid
uemis_get_divenr() returns maxdiveid and passes mindiveid as a
global variable.

Make this more reasonable by returning a min, max pair.

The way mindiveid is an unsigned int and then reinterpreted as
int is very sketchy. This commit attempts to not change that
behavior.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
5554acb2c5 uemis: unglobalize next_table_index
This is only initialized and used in one loop. Very mysterious
why this should be a global variable.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
ad33d498c7 uemis: unglobalize reqtxt_file
This global variable is used in two function independently of
each other. I don't see how there should be transport of this
value from one function to the other. Ominous.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
9e7b98024e uemis: unglobalize response buffer
uemis_get_answer() would put the raw response into a global variable.
This could be anywhere in the call stack and thus you never knew
when the existing buffer was removed under your feet.

Instead, return the buffer explicitly from uemis_get_answer().

I'm nit perfectly happy about the new interface: an error is
indicated by an empty buffer, which is awkward to test for.
If an empty buffer turns out to be a valid response, this
should be replaced by an std::optional<> or std::expected<>.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
58b3583b3b uemis: replace C-strings by std::string and std::string_view
The string code of uemis-downloader.cpp was broken in more ways
than can be listed here. Notably, it brazenly refused to free any
memory allocated for the parameters buffer.

Using std::string and std::string_view should plug all those
memory holes. That made it necessary to do some major refactoring.

This was done blind and therefore will break.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
16e19b550b uemis-downloader: use std::string for constructing strings
This extends work started by Richard Fuchs <dfx@dfx.at>

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
ad3be20c9f uemis-downloader: make newmax an integer variable
newmax was an integer variable kept as a string. Very ominous.

Moreover, memory management seems to be broken:

1) The string is never freed.

2) The string is passed as value from do_uemis_import() to
   get_matching_dives(), which passes it as reference to
   process_raw_buffer(), which may reallocate it, which means
   that do_uemis_import() now possesses a pointer to a free()d
   string.

Simplify all that by making newmax an integer variable and
passing it as a reference from do_uemis_import() to
get_matching_dives().

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
bf92407a4a uemis downloader: use report_info() for debugging
There were a number of fprintf()s that escaped the conversion
to report_info(), because they used "debugfile" instead of
"stderr" as target. However, debugfile was just #defined to
be stderr, so we might just use report_info() for consistency.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
b28d6cf0fc core: convert uemis.c to C++
The uemis code is wild. It simply doesn't deallocate memory
and uses global variables. To get this under control, create
a "struct uemis" and make the functions exported by "uemis.h"
members of "struct uemis". Thus, we don't have to carry around
a parameter for the state of the importing process.

Turn a linked list of "helper" structures (one per imported dive)
into a std::unordered_map, to fix leaking of the helper structures.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
cdc87618da uemis: pass dive pointer as dive pointer, not void pointer
This was very obscure: the function that parses into a struct
dive was passed a void-pointer instead of a struct dive-pointer.

Why? Just pass the correct type.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
21f68387ae core: C++-ify SHA1 interface
All callers of the SHA1 code are C++. Might just as well use
a C++ like interface.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
729356e0b1 cleanup: fix typo in comment
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
742193a5e3 core: convert divesite.c to C++
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
b24f37fb4f core: replace SHA1() function by SHA1_uint32()
The SHA1() helper function was only used when calculating a
SHA1 hash and taking the first four bytes of it as uint32.

Make that explicit by renaming the function into SHA1_uint32()
and directly returning an uint32_t.

Note that the usage in cochran.cpp is sketchy: it generates
a four-byte hash out of two-byte data. Why!?

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
0b817e468a core: convert version.c to C++
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
e3f6496f59 core: convert strtod.c to C++
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
092035d883 core: simplify custom strtod() interface
The strtod_flags() function allowed for fine control of how to
parse strings. However, only two different modes were actually
used: ascii mode ("C" locale) and permissive mode (accept ","
and "." as decimal separator).

The former had already its own function name (ascii_strtod).
Make the latter a separatge function as well (permissive_strtod)
and remove all the flags rigmarole.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Berthold Stoeger
628e2fe933 parser: remove int_or_float union & other small float-parsing cleanups
Owing to bitrot, this union only contained a float and therefore
is pointless. Let's remove it.

That makes the function name "integer_or_float()" non-sensical.
Call it "parse_float()" instead.

Moreover, change the output-arguments of "parse_float()" from
pointers to references, as null-pointers are not supported.

Finally, remove the "errno" check after "ascii_strtod()". As far as
I can tell, errno is not set in "ascii_strtod()" and using a global
variable for error-reporting it is an incredibly silly interface
anyway.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-08-13 19:28:30 +02:00
Michael Keller
b3ff706c5f Filtering: Usability Improvements to Filter Constraints.
Some improvements:
- for constraints where the condition is applied to any one of a list of
  items, show the condition as 'any X';
- when filtering for tags, do not include divemode as a 'tag' - this is
  hard to impossible for users to understand, and we do have a dedicated
  condition for the dive mode.

Signed-off-by: Michael Keller <github@ike.ch>
2024-07-07 12:20:31 +12:00
Berthold Stoeger
3257dbfc63 cleanup: remove unused function get_dive_id_closest_to()
The last caller was removed in e700920e8e.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-06-19 06:55:36 +02:00
Michael Keller
31fca0f676 Filter: Fix Loading of Negated Conditions from git.
Fix loading of the negation of filter conditions. Unlike other
conditions that are persisted as `<key>="<value>"`, this is persisted
to git as `negate`.
This fix remediates this for all cases where the condition has already
been saved to the cloud storage.

Saving to XML takes a different approach and indicates negated
conditions with `negate="1"`, making it identical to all other
attributes. The question is if this approach should be implemented in
addition to the above fix, in order to unify the storage format.

Fixes #4246.

Signed-off-by: Michael Keller <github@ike.ch>
2024-06-15 22:49:44 +02:00
Michael Keller
10fc3bfd47 Bugfix: Fix Incorrect Volumes Displayed for Tank Types.
Fix an issue introduced in #4148.
Essentially the refactoring missed the fact that in the imperial system
tank size is tracked as the free gas volume, but in the metric system
(which is the one used in most of Subsurface's calculations) tank size
is tracked as water capacity.
So when updating a tank template tracking imperial measurements, the
given (metric) volume in l has to be multiplied by the working pressure,
and vice versa.
This also combines all the logic dealing with `tank_info` data in one
place, hopefully making it less likely that this will be broken by
inconsistencies in the future.

Fixes #4239.

Signed-off-by: Michael Keller <github@ike.ch>
2024-06-09 11:15:59 +02:00
Berthold Stoeger
a8c9781205 cleanup: remove unused function create_and_hookup_trip_from_dive()
It seems that the last user was removed 5 years ago: ff9506b21?

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-06-08 15:59:53 +02:00
Michael Keller
f65afaf5d2 Desktop: Fix Gas Editing for Manually Added Dives.
- show the correct gasmix in the profile;
- make gases available for gas switches in the profile after they have
  been added;
- persist gas changes;
- add air as a default gas when adding a dive.

This still has problems when undoing a gas switch - instead of
completely removing the gas switch it is just moved to the next point in the
profile.

Signed-off-by: Michael Keller <github@ike.ch>
2024-06-01 23:22:40 +12:00
Berthold Stoeger
e7d486982f core: remove put_format_loc()
This was replaced by C++ functions in ae299d5e66.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-05-31 18:55:47 +02:00
Michael Keller
5b941ea34e Mobile: Fix Build Warnings.
Fix build warnings from building functions not used in the mobile
version.

Signed-off-by: Michael Keller <mikeller@042.ch>
2024-05-30 11:54:51 +02:00
Berthold Stoeger
e39b42df53 cleanup: remove disfunct add_cloned_weightsystem_at()
Clearly, a development artifact.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-05-29 09:33:23 +12:00
Berthold Stoeger
398cc2b639 cleanup: remove localized snprintf() functions
The last use of these functions was removed in ae299d5e66.

And that's a good thing, because snprintf-style interfaces
make zero sense in times of variable-length character
encodings.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-05-29 09:33:23 +12:00
Berthold Stoeger
2776a2fe48 import: fix memory leak when importing dives
A long standing issue: the dives_to_add, etc. tables need to be
manually freed. This kind of problem wouldn't arise with proper
C++ data structures.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-05-27 20:11:37 +12:00
Michael Keller
1aa5438b2d Cleanup: Improve the Use of 'Planned dive' and 'Manually added dive'.
- standardise the naming;
- use it consistently;
- apply the 'samples < 50' only when putting manually added dives into
  edit mode - everywhere else manually added dives should be treated as
  such;
- do not show a warning before editing a manually added dive in planner.

Signed-off-by: Michael Keller <github@ike.ch>
2024-05-25 20:13:45 +02:00
Michael Keller
b579342639 Cleanup: Remove 'context' Reference from Logging Defines.
Remove the reference to `context` from the defines used for logging, as
this is not used.

Signed-off-by: Michael Keller <mikeller@042.ch>
2024-05-16 16:39:48 +02:00
Berthold Stoeger
6880937838 core: fix INFO() and ERROR() macros in core/serial_ftdi.cpp
Error introduced in da7ea17b66: the INFO() and ERROR() macros
pass stdout instead of the format string as first parameter
to report_error(). Ooooops. How did this ever pass the
compile tests!?

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-05-15 09:19:05 +12:00
Berthold Stoeger
e20ec9248c core: fix leak when clearing dive log
46cf2fc086 fixed a bug where clearing of a divelog, such as the one
used for import, would erase dives in the global(!) divelog.

However, the new code used the function clear_dive_table(), which
only cleared the table without unregistering the dives. In particular,
the dives were not removed from the trips, which means that the trips
were not free()d.

This reinstates the old code, but now passes a divelog paremeter
to delete_single_dive() instead of accessing the global divelog.
Moreover, delete dives from the back to avoid unnecessary
copying.

An alternative and definitely simpler solution might be to just
add a "clear_trip_table()" after "clear_dive_table()".

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-05-13 08:58:03 +12:00
Michael Keller
528532572f Planner: Fix Editing of Plans in Multi-Divecomputer Dives.
Currently editing of planned dives that have been merged with actual
(logged) dives only works if the 'Planned dive' divecomputer is the
first divecomputer, and this divecomputer is selected when clicking
'Edit planned dive'. In other cases the profile of the first
divecomputer is overlaid with the profile of the planned dive, and the
first divecomputer's profile is overwritten when saving the dive plan.
Fix this problem.

Triggered by @SeppoTakalo's comment (https://github.com/subsurface/subsurface/issues/1913#issuecomment-2075562119): Users don't like that planned dives show up as their own entries in the dive list, so being able to merge them with the actual dive after it has been executed is a good feature - but this wasn't working well until now.

Signed-off-by: Michael Keller <github@ike.ch>
2024-05-11 12:51:45 +12:00
Richard Fuchs
edb771e8e6 uemis-downloader: convert strings to std::string
Convert some C-style strings in uemis-downloader.cpp to std::string.
This has the side effect of fixing builds on Debian Trixie, which
currently fail with the (rather silly) error:

/build/subsurface-beta-202405060411/core/uemis-downloader.cpp: In function 'char* build_ans_path(const char*, int)':
/build/subsurface-beta-202405060411/core/uemis-downloader.cpp:290:32: error: '%s' directive output between 0 and 12 bytes may cause result to exceed 'INT_MAX' [-Werror=format-truncation=]
  290 |         snprintf(buf, len, "%s/%s", path, name);
      |                                ^~
......
  529 |         ans_path = build_filename(intermediate, fl);
      |                                                 ~~
cc1plus: some warnings being treated as errors

Signed-off-by: Richard Fuchs <dfx@dfx.at>
2024-05-07 22:34:00 +12:00