add string creating functions to tmpbuf
* lumiera_tmpbuf_strndup() duplicates a string * lumiera_tmpbuf_sprintf() creates a formatted string
This commit is contained in:
parent
d32c74361b
commit
3a8b3feb96
2 changed files with 53 additions and 3 deletions
|
|
@ -26,6 +26,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
/**
|
||||
* @file Portable and safe wrapers around some clib functions and some tools
|
||||
|
|
@ -179,3 +180,35 @@ lumiera_tmpbuf_provide (size_t size)
|
|||
return buf->buffers[buf->idx];
|
||||
}
|
||||
|
||||
|
||||
char*
|
||||
lumiera_tmpbuf_strndup (const char* src, size_t size)
|
||||
{
|
||||
size_t len = strlen (src);
|
||||
len = len > size ? size : len;
|
||||
|
||||
char* buf = lumiera_tmpbuf_provide (len + 1);
|
||||
strncpy (buf, src, len);
|
||||
buf[len] = '\0';
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
||||
char*
|
||||
lumiera_tmpbuf_sprintf (size_t size, const char* fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
va_start (args, fmt);
|
||||
size_t len = vsnprintf (NULL, 0, fmt, args);
|
||||
va_end (args);
|
||||
|
||||
len = len > size ? size : len;
|
||||
char* buf = lumiera_tmpbuf_provide (len);
|
||||
va_start (args, fmt);
|
||||
vsnprintf (buf, len, fmt, args);
|
||||
va_end (args);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,9 +21,7 @@
|
|||
|
||||
#include "error.h"
|
||||
|
||||
//#include <string.h>
|
||||
//#include <stdlib.h>
|
||||
//#include <pthread.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* @file Portable and safe wrapers around some clib functions and some tools
|
||||
|
|
@ -95,3 +93,22 @@ lumiera_tmpbuf_freeall (void);
|
|||
void*
|
||||
lumiera_tmpbuf_provide (size_t size);
|
||||
|
||||
/**
|
||||
* Duplicate string to a tmpbuf.
|
||||
* @param src string to be duplicated
|
||||
* @param size maximal length to be copied
|
||||
* @return temporary buffer containing a copy of the string
|
||||
*/
|
||||
char*
|
||||
lumiera_tmpbuf_strndup (const char* src, size_t size);
|
||||
|
||||
/**
|
||||
* Construct a string in a tmpbuf.
|
||||
* @param size maximal length for the string
|
||||
* @param fmt printf like formatstring
|
||||
* @param ... parameters
|
||||
* @return temporary buffer containing the constructed of the string
|
||||
*/
|
||||
char*
|
||||
lumiera_tmpbuf_sprintf (size_t size, const char* fmt, ...);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue