mirror of
https://github.com/troglobit/libite.git
synced 2026-11-11 16:40:42 +00:00
Add support for fopenf(), formatted string fopen()
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
1 parent
28c11f010a
commit
02a42a1cad
7 files changed
+93
-1
No files matched your search
@@ -181,6 +181,15 @@ Generic Functions
|
|||||||
|
|
||||||
Check for the existence of a directory, returns True(1) or False(0).
|
Check for the existence of a directory, returns True(1) or False(0).
|
||||||
|
|
||||||
|
- `fopenf(mode, fmt, ...)`
|
||||||
|
|
||||||
|
Like `fopen()`, but takes a formatted string as argument. This
|
||||||
|
greatly simplifies operations that usually consist of composing a
|
||||||
|
filename from parts into a dynamic buffer before actually opening
|
||||||
|
the file.
|
||||||
|
|
||||||
|
> **Notice:** the swapped order of `pathname` and `mode`!
|
||||||
|
|
||||||
- `fsendfile(src, dst, len)`
|
- `fsendfile(src, dst, len)`
|
||||||
|
|
||||||
Copy data between file streams, very similar to `fcopyfile()`, but
|
Copy data between file streams, very similar to `fcopyfile()`, but
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@ libite_la_CFLAGS = -W -Wall -Wextra
|
|||||||
libite_la_LDFLAGS = $(AM_LDFLAGS) -version-info 5:1:0
|
libite_la_LDFLAGS = $(AM_LDFLAGS) -version-info 5:1:0
|
||||||
libite_la_SOURCES = chomp.c copyfile.c \
|
libite_la_SOURCES = chomp.c copyfile.c \
|
||||||
conio.c conio.h \
|
conio.c conio.h \
|
||||||
dir.c \
|
dir.c fopenf.c \
|
||||||
fexist.c fisdir.c \
|
fexist.c fisdir.c \
|
||||||
fparseln.c fsendfile.c \
|
fparseln.c fsendfile.c \
|
||||||
ifconfig.c lfile.c \
|
ifconfig.c lfile.c \
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
/* Formatted fopen()
|
||||||
|
*
|
||||||
|
* Copyright (c) 2020 Joachim Nilsson <troglobit@gmail.com>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* fopenf - Open a file based on the formatted string and optional arguments
|
||||||
|
* @mode: Last argument in optional list, if omitted EINVAL
|
||||||
|
* @fmt: Formatted string to be composed into a pathname
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* Upon successful completion fopenf() a FILE pointer. Otherwise, NULL
|
||||||
|
* is returned and errno is set to indicate the error.
|
||||||
|
*/
|
||||||
|
FILE *fopenf(const char *mode, const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
FILE *fp = NULL;
|
||||||
|
char *file;
|
||||||
|
int len;
|
||||||
|
|
||||||
|
va_start(ap, fmt);
|
||||||
|
len = vsnprintf(NULL, 0, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
|
va_start(ap, fmt);
|
||||||
|
file = alloca(len + 1);
|
||||||
|
if (file) {
|
||||||
|
vsnprintf(file, len + 1, fmt, ap);
|
||||||
|
fp = fopen(file, mode);
|
||||||
|
}
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
|
return fp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
@@ -65,6 +65,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 *fopenf (const char *mode, const char *fmt, ...);
|
||||||
FILE *tempfile (void);
|
FILE *tempfile (void);
|
||||||
ssize_t copyfile (const char *src, const char *dst, int len, int opt);
|
ssize_t copyfile (const char *src, const char *dst, int len, int opt);
|
||||||
int movefile (const char *src, const char *dst);
|
int movefile (const char *src, const char *dst);
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
*.o
|
*.o
|
||||||
*.log
|
*.log
|
||||||
*.trs
|
*.trs
|
||||||
|
.deps/
|
||||||
chomp
|
chomp
|
||||||
copyfile
|
copyfile
|
||||||
dir
|
dir
|
||||||
fexist
|
fexist
|
||||||
fisdir
|
fisdir
|
||||||
|
fopenf
|
||||||
fsendfile
|
fsendfile
|
||||||
lfile
|
lfile
|
||||||
makepath
|
makepath
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ TESTS += copyfile
|
|||||||
TESTS += dir
|
TESTS += dir
|
||||||
TESTS += fexist
|
TESTS += fexist
|
||||||
TESTS += fisdir
|
TESTS += fisdir
|
||||||
|
TESTS += fopenf
|
||||||
TESTS += fsendfile
|
TESTS += fsendfile
|
||||||
TESTS += lfile
|
TESTS += lfile
|
||||||
TESTS += makepath
|
TESTS += makepath
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#include "check.h"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
FILE *fp;
|
||||||
|
char *dir = "/tmp";
|
||||||
|
char *file = "__foo__";
|
||||||
|
|
||||||
|
fp = fopenf("w", "%s/%s", dir, file);
|
||||||
|
if (fp) {
|
||||||
|
fputs("foo", fp);
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
fail_unless(fexist("/tmp/__foo__"));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user