mirror of
https://github.com/troglobit/libite.git
synced 2026-11-24 20:21:38 +00:00
New helper function, vfopenf(), inspired by the Infix project
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
1 parent
9eb847e56c
commit
1f6ff90d9e
2 files changed
+40
-15
No files matched your search
+39
-15
@@ -27,10 +27,43 @@
|
|||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Similar to fopenf() except it takes a @a va_list argument
|
||||||
|
* @param mode An fopen() mode string, e.g. "w+".
|
||||||
|
* @param fmt Formatted string to be composed into a pathname.
|
||||||
|
* @param ap List of variable arguemnts from va_start().
|
||||||
|
*
|
||||||
|
* See fopenf() for details.
|
||||||
|
*
|
||||||
|
* @returns a FILE pointer, or @c NULL on error.
|
||||||
|
*/
|
||||||
|
FILE *vfopenf(const char *mode, const char *fmt, va_list ap)
|
||||||
|
{
|
||||||
|
va_list apc;
|
||||||
|
char *file;
|
||||||
|
int len;
|
||||||
|
|
||||||
|
va_copy(apc, ap);
|
||||||
|
len = vsnprintf(NULL, 0, fmt, apc);
|
||||||
|
va_end(apc);
|
||||||
|
|
||||||
|
file = alloca(len + 1);
|
||||||
|
if (!file) {
|
||||||
|
errno = ENOMEM;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
va_copy(apc, ap);
|
||||||
|
vsnprintf(file, len + 1, fmt, apc);
|
||||||
|
va_end(apc);
|
||||||
|
|
||||||
|
return fopen(file, mode);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Open a file based on the formatted string and optional arguments
|
* Open a file based on the formatted string and optional arguments
|
||||||
* @param mode An fopen() mode string, e.g. "w+"
|
* @param mode An fopen() mode string, e.g. "w+".
|
||||||
* @param fmt Formatted string to be composed into a pathname
|
* @param fmt Formatted string to be composed into a pathname.
|
||||||
*
|
*
|
||||||
* This function is an extension to the fopen() family, lessening the burden
|
* This function is an extension to the fopen() family, lessening the burden
|
||||||
* of first having to compose the filename from parts in a seprate buffer.
|
* of first having to compose the filename from parts in a seprate buffer.
|
||||||
@@ -42,24 +75,15 @@
|
|||||||
FILE *fopenf(const char *mode, const char *fmt, ...)
|
FILE *fopenf(const char *mode, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
va_list ap;
|
va_list ap;
|
||||||
char *file;
|
FILE *fp;
|
||||||
int len;
|
|
||||||
|
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
len = vsnprintf(NULL, 0, fmt, ap);
|
fp = vfopenf(mode, fmt, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
if (!fp)
|
||||||
file = alloca(len + 1);
|
|
||||||
if (!file) {
|
|
||||||
errno = ENOMEM;
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
|
|
||||||
va_start(ap, fmt);
|
return fp;
|
||||||
vsnprintf(file, len + 1, fmt, ap);
|
|
||||||
va_end(ap);
|
|
||||||
|
|
||||||
return fopen(file, mode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -79,6 +79,7 @@ char *fparseln (FILE *, size_t *, size_t *, const char[3], int);
|
|||||||
int fexist (const char *file);
|
int fexist (const char *file);
|
||||||
int fisdir (const char *path);
|
int fisdir (const char *path);
|
||||||
|
|
||||||
|
FILE *vfopenf (const char *mode, const char *fmt, va_list ap);
|
||||||
FILE *fopenf (const char *mode, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
|
FILE *fopenf (const char *mode, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
|
||||||
int fremove (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
|
int fremove (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
|
||||||
FILE *tempfile (void);
|
FILE *tempfile (void);
|
||||||
|
|||||||
Reference in new issue
Block a user