Inspecting some of my "C" source code here I noticed a construct that
I dislike...
static char buf[32]; // sufficient space for ansi sequence
...
sprintf (buf, "\033[38;5;%dm%c\033[0m", ...);
In case of known or deducible string sizes I'm preferring and using
some of the alloc() functions. In the sample I can at least deduce an upper-bound for the buffer-size. But it's anyway still an undesired hard-coded buffer size that I'd like to avoid.
I recall that in C++ I used "String-Streams" for dynamically extended strings. But in "C" my reflex (and habit) is to write things like the
above code.
Is there something in "C" that allows dynamic flexibility of strings?
(Or are there other options that an experienced "C" programmer would
use instead?)
(If there's something available only with newer C-standards I'd also appreciate a hint.)
Is there something in "C" that allows dynamic flexibility of strings?
Inspecting some of my "C" source code here I noticed a construct that
I dislike...
static char buf[32]; // sufficient space for ansi sequence
...
sprintf (buf, "\033[38;5;%dm%c\033[0m", ...);
The ‘asprintf’ subroutine is standardized by POSIX.1-2024, meaning that you can use it now and blame somebody else if it doesn't work. If you
can't target POSIX, the subroutine is also _theoretically_ available
through the feature-test macro ‘__STDC_WANT_LIB_EXT2__’, assuming that ‘__STDC_ALLOC_LIB__’ is a predefined macro, but gLibC does not pay
On 2024-12-19, BlueManedHawk <bluemanedhawk@invalid.invalid> wrote:Don't you mean, vsnprintf ?
The ‘asprintf’ subroutine is standardized by POSIX.1-2024, meaning
that you can use it now and blame somebody else if it doesn't work.
If you
Regardless of how it is made visible, you can detect it via a compile
test in a configure script, and provide your own if it wasn't found:
#if !HAVE_ASPRINTF
int asprintf(char **out, const char *fmt, ...)
{
... // more or less trivial to implement using malloc, realloc and
vsprintf
}
#endif
BTW, is there no wchar_t version of this?
can't target POSIX, the subroutine is also _theoretically_
available through the feature-test macro ‘__STDC_WANT_LIB_EXT2__’, assuming that ‘__STDC_ALLOC_LIB__’ is a predefined macro, but gLibC does not pay
When would it be the case that you can't target POSIX, but *can* mess
around with some the internal feature test macros of some specific
POSIX vendor? :)
On Thu, 19 Dec 2024 19:47:28 -0000 (UTC)
Kaz Kylheku <643-408-1753@kylheku.com> wrote:
On 2024-12-19, BlueManedHawk <bluemanedhawk@invalid.invalid> wrote:
The ‘asprintf’ subroutine is standardized by POSIX.1-2024, meaning
that you can use it now and blame somebody else if it doesn't work.
If you
Regardless of how it is made visible, you can detect it via a compile
test in a configure script, and provide your own if it wasn't found:
#if !HAVE_ASPRINTF
int asprintf(char **out, const char *fmt, ...)
{
... // more or less trivial to implement using malloc, realloc and
vsprintf
Don't you mean, vsnprintf ?
On 2024-12-19, Michael S <already5chosen@yahoo.com> wrote:
On Thu, 19 Dec 2024 19:47:28 -0000 (UTC)
Kaz Kylheku <643-408-1753@kylheku.com> wrote:
On 2024-12-19, BlueManedHawk <bluemanedhawk@invalid.invalid> wrote:
The ‘asprintf’ subroutine is standardized by POSIX.1-2024, meaning >>>> that you can use it now and blame somebody else if it doesn't work.
If you
Regardless of how it is made visible, you can detect it via a compile
test in a configure script, and provide your own if it wasn't found:
#if !HAVE_ASPRINTF
int asprintf(char **out, const char *fmt, ...)
{
... // more or less trivial to implement using malloc, realloc and
vsprintf
Don't you mean, vsnprintf ?
That detail will become obvious when you try to implement it.
[...]
On 2024-12-19, Michael S <already5chosen@yahoo.com> wrote:
On Thu, 19 Dec 2024 19:47:28 -0000 (UTC)
Kaz Kylheku <643-408-1753@kylheku.com> wrote:
On 2024-12-19, BlueManedHawk <bluemanedhawk@invalid.invalid>
wrote:
The ‘asprintf’ subroutine is standardized by POSIX.1-2024,
meaning that you can use it now and blame somebody else if it
doesn't work. If you
Regardless of how it is made visible, you can detect it via a
compile test in a configure script, and provide your own if it
wasn't found:
#if !HAVE_ASPRINTF
int asprintf(char **out, const char *fmt, ...)
{
... // more or less trivial to implement using malloc, realloc
and vsprintf
Don't you mean, vsnprintf ?
That detail will become obvious when you try to implement it.
Em 12/19/2024 7:06 PM, Kaz Kylheku escreveu:
On 2024-12-19, Michael S <already5chosen@yahoo.com> wrote:
On Thu, 19 Dec 2024 19:47:28 -0000 (UTC)
Kaz Kylheku <643-408-1753@kylheku.com> wrote:
On 2024-12-19, BlueManedHawk <bluemanedhawk@invalid.invalid>
wrote:
The ‘asprintf’ subroutine is standardized by POSIX.1-2024,
meaning that you can use it now and blame somebody else if it
doesn't work. If you
Regardless of how it is made visible, you can detect it via a
compile test in a configure script, and provide your own if it
wasn't found:
#if !HAVE_ASPRINTF
int asprintf(char **out, const char *fmt, ...)
{
... // more or less trivial to implement using malloc, realloc
and vsprintf
Don't you mean, vsnprintf ?
That detail will become obvious when you try to implement it.
I did on implementation in 2020 (not using it)
http://thradams.com/vadsprintf.html
The standard should have a string stream compatible with FILE because
- differently of asprintf - if cannot be implemented separately.
On Thu, 19 Dec 2024 23:14:17 -0300
Thiago Adams <thiago.adams@gmail.com> wrote:
Em 12/19/2024 7:06 PM, Kaz Kylheku escreveu:
On 2024-12-19, Michael S <already5chosen@yahoo.com> wrote:
On Thu, 19 Dec 2024 19:47:28 -0000 (UTC)
Kaz Kylheku <643-408-1753@kylheku.com> wrote:
On 2024-12-19, BlueManedHawk <bluemanedhawk@invalid.invalid>
wrote:
The ‘asprintf’ subroutine is standardized by POSIX.1-2024,
meaning that you can use it now and blame somebody else if it
doesn't work. If you
Regardless of how it is made visible, you can detect it via a
compile test in a configure script, and provide your own if it
wasn't found:
#if !HAVE_ASPRINTF
int asprintf(char **out, const char *fmt, ...)
{
... // more or less trivial to implement using malloc, realloc
and vsprintf
Don't you mean, vsnprintf ?
That detail will become obvious when you try to implement it.
I did on implementation in 2020 (not using it)
http://thradams.com/vadsprintf.html
You mean, you don't use asprintf() that you implemented?
That's understandable. The API is rather badly designed. Can be handy
in toy examples, less so in production software.
The standard should have a string stream compatible with FILE because
- differently of asprintf - if cannot be implemented separately.
What level of compatibility?
IMHO, the level that makes sense is where compatibility excludes
fopen, fclose and fflush. I.e. you have new functions, mem_fopen()
and mem_fclose() and do not allow fflush(). Pluse, you add few more
functions or macros for direct access to buffer.
A function printing in a FILE* fprint also should be able to print in
a string stream.
On Thu, 19 Dec 2024 23:14:17 -0300[...]
Thiago Adams <thiago.adams@gmail.com> wrote:
The standard should have a string stream compatible with FILE because
- differently of asprintf - if cannot be implemented separately.
What level of compatibility?
IMHO, the level that makes sense is where compatibility excludes
fopen, fclose and fflush. I.e. you have new functions, mem_fopen()
and mem_fclose() and do not allow fflush(). Pluse, you add few more
functions or macros for direct access to buffer.
For my needs a string stream is better I am using this https://github.com/thradams/cake/blob/main/src/osstream.c Unfortunately
it is not compatible with FILE*.
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 1,007 |
Nodes: | 10 (0 / 10) |
Uptime: | 196:54:48 |
Calls: | 13,143 |
Files: | 186,574 |
D/L today: |
511 files (113M bytes) |
Messages: | 3,310,149 |