2022-08-31 16:23:48 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* libs/libc/obstack/lib_obstack_vprintf.c
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <obstack.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <nuttx/streams.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct obstack_stream
|
|
|
|
{
|
2023-09-10 21:32:34 +02:00
|
|
|
struct lib_outstream_s common;
|
2022-08-31 16:23:48 +02:00
|
|
|
FAR struct obstack *h;
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-09-10 21:32:34 +02:00
|
|
|
static int obstack_puts(FAR struct lib_outstream_s *self,
|
2022-08-31 16:23:48 +02:00
|
|
|
FAR const void *buf, int len)
|
|
|
|
{
|
2023-09-10 21:32:34 +02:00
|
|
|
FAR struct obstack_stream *stream = (FAR struct obstack_stream *)self;
|
2022-08-31 16:23:48 +02:00
|
|
|
|
2023-09-10 21:32:34 +02:00
|
|
|
DEBUGASSERT(self);
|
2022-08-31 16:23:48 +02:00
|
|
|
|
2023-09-10 21:32:34 +02:00
|
|
|
obstack_grow(stream->h, buf, len);
|
2022-08-31 16:23:48 +02:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2023-09-10 21:32:34 +02:00
|
|
|
static void obstack_putc(FAR struct lib_outstream_s *self, int ch)
|
2022-08-31 16:23:48 +02:00
|
|
|
{
|
2024-08-21 13:10:55 +02:00
|
|
|
FAR struct obstack_stream *stream = (FAR struct obstack_stream *)self;
|
|
|
|
|
|
|
|
DEBUGASSERT(self);
|
|
|
|
|
|
|
|
obstack_1grow(stream->h, ch);
|
2022-08-31 16:23:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: obstack_vprintf
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is similar to the vasprintf except it uses obstack to allocate
|
|
|
|
* string on. The characters are written onto the end of the currently
|
|
|
|
* growing object and terminated by null byte.
|
|
|
|
*
|
|
|
|
* The same remarks are applied here as for obstack_printf regarding the
|
|
|
|
* definition location in GlibC.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* h: pointer to the handle used to grow the object.
|
|
|
|
* fmt: format string
|
|
|
|
* ap: format string input as a variable argument list
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Number of characters added to the obstack excluding the null byte.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int obstack_vprintf(FAR struct obstack *h, FAR const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
struct obstack_stream outstream;
|
|
|
|
|
2023-09-10 21:32:34 +02:00
|
|
|
outstream.common.putc = obstack_putc;
|
|
|
|
outstream.common.puts = obstack_puts;
|
|
|
|
outstream.common.flush = lib_noflush;
|
|
|
|
outstream.common.nput = 0;
|
2022-08-31 16:23:48 +02:00
|
|
|
outstream.h = h;
|
|
|
|
|
2024-08-21 13:10:55 +02:00
|
|
|
int nbytes = lib_vsprintf(&outstream.common, fmt, ap);
|
|
|
|
|
|
|
|
if (nbytes < 0)
|
|
|
|
{
|
|
|
|
obstack_free(h, obstack_finish(h));
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
obstack_1grow(h, '\0');
|
|
|
|
return nbytes;
|
2022-08-31 16:23:48 +02:00
|
|
|
}
|