Commit graph

11 commits

Author SHA1 Message Date
Berthold Stoeger
f142e9a9c6 core: C++-ify membuffer
C-style memory management is a pain and nearly nobody seems to get
it right. Add a C++-version of membuffer that frees the buffer
when it gets out-of-scope. Originally, I was thinking about
conditionally adding a constructor/destructor pair when compiling
with C++. But then decided to create a derived class membufferpp,
because it would be extremely confusing to have behavioral change
when changing a source from from C to C++ or vice-versa.

Also add a comment about the dangers of returned pointer: They
become dangling on changes to the membuffer.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-07-23 11:22:43 -07:00
Linus Torvalds
a9b1fbdcc5 Don't expose 'detach_buffer()' to membuffer users
The native buffer of a membuffer is not NUL-terminated, so when you want
to detach it and use it as a C string, you had to first do
'mb_cstring()' that adds the proper termination/

This was all documented in the header files, and all but two users did
it correctly.

But there were those two users, and the exported interface was
unnecessarily hard to use.  We do want the "just detach the raw buffer"
internally in the membuffer code, but let's not make the exported
interface be that hard to use.

So this switches the exported interface to be 'detach_cstring()', which
does that 'mb_cstring()' for you, and avoids the possibility that you'd
use a non-terminated memory buffer as a C string.

The old 'detach_buffer()' is now purely the internal membuffer
implementation, and not used by others.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-10-27 12:42:54 -07:00
Berthold Stoeger
fe7c0b3085 Cleanup: make location argument to put_location() const
Since this is an output function, the pointed-to value is not
modified.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-09-21 16:07:24 -07:00
Berthold Stoeger
46c69fccb7 Cleanup: fix includes in membuffer.h
Headers should not have to be included in a certain order.
Therefore include stdarg.h and stdio.h in membuffer.h, since
the latter uses FILE and va_list.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-06-19 13:11:10 -07:00
Berthold Stoeger
643a964d09 Cleanup: unconstify string argument to add_to_string()
add_to_string() frees the original string that is passed in. This
should therefore not be of "const char *" type, as the contents
of the string *will* be modified (or more precisely: destroyed).

Same for the congener smtk_concat_str().

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-03-27 13:58:15 +01:00
Linus Torvalds
28e3413ff6 Add 'location_t' data structure
Instead of having people treat latitude and longitude as separate
things, just add a 'location_t' data structure that contains both.

Almost all cases want to always act on them together.

This is really just prep-work for adding a few more locations that we
track: I want to add a entry/exit location to each dive (independent of
the dive site) because of how the Garmin Descent gives us the
information (and hopefully, some day, other dive computers too).

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-10-21 19:55:09 +03:00
Jeremie Guichard
5d96d4af0c Add usage documentation for membuffer helper functions
Added a comment block on top of membuffer.h describing common usage
of membuffer helper functions

Signed-off-by: Jeremie Guichard <djebrest@gmail.com>
2018-04-10 08:18:32 -07:00
Berthold Stoeger
bec3e80683 Localization: Make Qt's locale-aware formatting accessible from C
This commit introduces functions:
 - QString asprintf_loc(const char *cformat, ...);
 - int snprintf_loc(char *dst, size_t size, const char *cformat, ...);
 - put_format_loc(struct membuffer *, const char *fmt, ...);
and their va_arg equivalents, which use Qt's QString::arg() formatting
options to render the strings.

The snprintf_loc() function takes care not to truncate multi-byte
UTF-8 encodings. Thus, on overflow, the resulting string might be
shorter than size-1.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-03-05 16:23:55 +01:00
Miika Turkia
02c80a60b0 Refactore parse-xml.c into parse.c and parse-xml.c
This should help us to move parsing that is not XML related to other
files, hopefully making the code cleaner.

Signed-off-by: Miika Turkia <miika.turkia@gmail.com>
2017-11-27 13:42:10 -08:00
Dirk Hohndel
6399eaf271 Add SPDX header to core C files
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-04-29 13:32:55 -07:00
Dirk Hohndel
7be962bfc2 Move subsurface-core to core and qt-mobile to mobile-widgets
Having subsurface-core as a directory name really messes with
autocomplete and is obviously redundant. Simmilarly, qt-mobile caused an
autocomplete conflict and also was inconsistent with the desktop-widget
name for the directory containing the "other" UI.

And while cleaning up the resulting change in the path name for include
files, I decided to clean up those even more to make them consistent
overall.

This could have been handled in more commits, but since this requires a
make clean before the build, it seemed more sensible to do it all in one.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2016-04-04 22:33:58 -07:00
Renamed from subsurface-core/membuffer.h (Browse further)