diff options
| -rw-r--r-- | docs/cspan_api.md | 2 | ||||
| -rw-r--r-- | include/c11/fmt.h (renamed from include/c11/print.h) | 31 | ||||
| -rw-r--r-- | src/singleupdate.sh | 2 |
3 files changed, 24 insertions, 11 deletions
diff --git a/docs/cspan_api.md b/docs/cspan_api.md index f0c6babd..3a811ebf 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -137,7 +137,7 @@ int main() { Slicing cspan without and with reducing the rank: ```c #define i_implement -#include <c11/print.h> +#include <c11/fmt.h> #include <stc/cspan.h> using_cspan3(Span, int); // Shorthand to define Span, Span2, and Span3 diff --git a/include/c11/print.h b/include/c11/fmt.h index ee0d8151..6c1388af 100644 --- a/include/c11/print.h +++ b/include/c11/fmt.h @@ -2,13 +2,10 @@ #define FMT_H_INCLUDED
/*
VER 2.2: NEW API:
-void print(fmt, ...);
-void println(fmt, ...);
-void printd(dest, fmt, ...);
-
void fmt_print(fmt, ...);
void fmt_println(fmt, ...);
void fmt_printd(dest, fmt, ...);
+const char* fmt_tm(fmt, struct tm* tp);
void fmt_close(fmt_stream* ss);
dest - destination, one of:
@@ -18,7 +15,7 @@ void fmt_close(fmt_stream* ss); Set ss->overwrite=1 for overwrite-mode.
Call fmt_close(ss) after usage.
- fmt - format string
+ fmt - format string (const char*)
{} Auto-detected format. If :MOD is not specified,
float will use ".8g" format, and double ".16g".
{:MODS} Format modifiers: '<' left align (replaces '-'). Default for char* and char.
@@ -29,12 +26,12 @@ void fmt_close(fmt_stream* ss); * C11 or higher required.
* MAX 255 chars fmt string by default. MAX 12 arguments after fmt string.
* Define FMT_IMPLEMENT or i_implement prior to #include in one translation unit.
-* Define FMT_SHORTS to define print(), println() and printd() macros, without fmt_ prefix.
+* Define FMT_SHORTS to add print(), println() and printd() macros, without fmt_ prefix.
* (c) operamint, 2022, MIT License.
-----------------------------------------------------------------------------------
#define FMT_IMPLEMENT
#define FMT_SHORTS
-#include "c11/print.h"
+#include "c11/fmt.h"
int main() {
const double pi = 3.141592653589793;
@@ -62,6 +59,12 @@ int main() { printd(ss, "{} {}", ", Pi squared is:", pi*pi);
print("{}, len={}, cap={}\n", ss->data, ss->len, ss->cap);
fmt_close(ss);
+
+ time_t now = time(NULL);
+ struct tm t1 = *localtime(&now), t2 = t1;
+ t2.tm_year += 2;
+ fmt_print(1, "Dates:\n {}\n {}\n", fmt_tm("%Y-%m-%d %X %Z", &t1),
+ fmt_tm("%Y-%m-%d %X %Z", &t2));
}
*/
#include <stdio.h>
@@ -92,12 +95,14 @@ typedef struct { _Bool overwrite;
} fmt_stream;
+#define fmt_tm(fmt, tmptr) _fmt_strftime(fmt, tmptr) /* Max 2 usages. Buffer = 64 chars. */
void fmt_close(fmt_stream* ss);
int _fmt_parse(char* p, int nargs, const char *fmt, ...);
void _fmt_bprint(fmt_stream*, const char* fmt, ...);
+struct tm; const char* _fmt_strftime(const char *fmt, const struct tm *tp);
#ifndef FMT_MAX
-#define FMT_MAX 256
+#define FMT_MAX 128
#endif
#ifdef FMT_SHORTS
@@ -195,11 +200,19 @@ void _fmt_bprint(fmt_stream*, const char* fmt, ...); #include <stdlib.h>
#include <stdarg.h>
#include <string.h>
+#include <time.h>
void fmt_close(fmt_stream* ss) {
free(ss->data);
}
+const char* _fmt_strftime(const char *fmt, const struct tm *tp) {
+ static char buf[2][64], i = 0;
+ i = !i;
+ strftime(buf[i], 64, fmt, tp);
+ return buf[i];
+}
+
void _fmt_bprint(fmt_stream* ss, const char* fmt, ...) {
va_list args, args2;
va_start(args, fmt);
@@ -271,4 +284,4 @@ int _fmt_parse(char* p, int nargs, const char *fmt, ...) { }
#endif
#endif
-#undef i_implement
\ No newline at end of file +#undef i_implement
diff --git a/src/singleupdate.sh b/src/singleupdate.sh index 9b1d37a0..8a621e57 100644 --- a/src/singleupdate.sh +++ b/src/singleupdate.sh @@ -1,6 +1,6 @@ d=$(git rev-parse --show-toplevel) mkdir -p $d/../stcsingle/c11 $d/../stcsingle/stc -python singleheader.py $d/include/c11/print.h > $d/../stcsingle/c11/print.h +python singleheader.py $d/include/c11/fmt.h > $d/../stcsingle/c11/fmt.h python singleheader.py $d/include/stc/calgo.h > $d/../stcsingle/stc/calgo.h python singleheader.py $d/include/stc/carc.h > $d/../stcsingle/stc/carc.h python singleheader.py $d/include/stc/cbits.h > $d/../stcsingle/stc/cbits.h |
