From 4f9381f052289c8728ba1ff391df59cc283d2f81 Mon Sep 17 00:00:00 2001 From: bumbread Date: Mon, 6 Jun 2022 15:57:25 +1100 Subject: [PATCH] inttypes print specifiers --- bake.cmd | 27 ++++---- code/stdio.c | 4 +- code/string.c | 5 +- inc/_macros.h | 2 + inc/inttypes.h | 180 +++++++++++++++++++++++++++++++++++++++++++++++++ inc/string.h | 20 +++--- test/test.c | 35 ++-------- 7 files changed, 215 insertions(+), 58 deletions(-) diff --git a/bake.cmd b/bake.cmd index dcf336b..45afddf 100644 --- a/bake.cmd +++ b/bake.cmd @@ -1,20 +1,23 @@ @echo off setlocal enabledelayedexpansion -set PLATFORM=win32 set CIABATTA_OPTIONS=-Iinc -g -gcodeview -nodefaultlibs -D_CRT_SECURE_NO_WARNINGS +set PLATFORM=win32 +if NOT "%1"=="fast" ( -del ciabatta.lib -for /R code %%F in (*.c) do ( - echo %%F - clang -c -o build\%%~nF.obj %%F %CIABATTA_OPTIONS% + del ciabatta.lib + for /R code %%F in (*.c) do ( + echo %%F + clang -c -o build\%%~nF.obj %%F %CIABATTA_OPTIONS% + ) + for /R platform\%PLATFORM% %%F in (*.c) do ( + echo %%F + clang -c -o build\%%~nF.obj %%F %CIABATTA_OPTIONS% + ) + llvm-ar rc ciabatta.lib build\*.obj + del build\*.obj ) -for /R platform\%PLATFORM% %%F in (*.c) do ( - echo %%F - clang -c -o build\%%~nF.obj %%F %CIABATTA_OPTIONS% -) -llvm-ar rc ciabatta.lib build\*.obj -clang test\test.c ciabatta.lib -lkernel32 -luser32 -lshell32 -nostdlib %CIABATTA_OPTIONS% -del build\*.obj +echo Compiling test.. +clang test\test.c ciabatta.lib -std=c11 -lkernel32 -luser32 -lshell32 -nostdlib %CIABATTA_OPTIONS% diff --git a/code/stdio.c b/code/stdio.c index c518e3d..bc49a29 100644 --- a/code/stdio.c +++ b/code/stdio.c @@ -2,8 +2,8 @@ #include #include -// Even if the user doesn't enable LIB_EXT1 we still have it existing -size_t strnlen_s(const char *s, size_t maxsize); +#define __STDC_WANT_LIB_EXT1__ 1 +#include typedef void(*OutputFunc)(void* ctx, size_t n, const char str[]); diff --git a/code/string.c b/code/string.c index a2a073b..2f4eda9 100644 --- a/code/string.c +++ b/code/string.c @@ -4,6 +4,7 @@ #include #include #include +#include // TODO: rewrite memmove to not allocate anything @@ -194,8 +195,6 @@ size_t strlen(const char *s) { return i; } -#if __STDC_WANT_LIB_EXT1__ == 1 - size_t strnlen_s(const char *s, size_t maxsize) { if (s == NULL) return 0; @@ -205,5 +204,3 @@ size_t strnlen_s(const char *s, size_t maxsize) { } return i; } - -#endif \ No newline at end of file diff --git a/inc/_macros.h b/inc/_macros.h index dc9df71..962f0ac 100644 --- a/inc/_macros.h +++ b/inc/_macros.h @@ -4,6 +4,8 @@ #define _str_(x) #x #define _str(x) _str_(x) +#define _con(a,b) a ## b + #if !defined(_func) #if defined(_compiler_msvc) #define _func __FUNCTION__ diff --git a/inc/inttypes.h b/inc/inttypes.h index e69de29..b4b8eac 100644 --- a/inc/inttypes.h +++ b/inc/inttypes.h @@ -0,0 +1,180 @@ + +#pragma once + +#include +#include + +// typedef struct imaxdiv_t imaxdiv_t +// It's C's fault not mine + +#define _spec8 "hh" +#define _spec16 "" +#define _spec32 "" + +#if INT64_MAX == LONG_MAX + #define _spec64 "l" +#elif INT64_MAX == LLONG_MAX + #define _spec64 "ll" +#endif + +#if INT8_LEAST_MAX == CHAR_MAX + #define _spec8l "hh" +#elif INT8_LEAST_MAX == SHORT_MAX || INT8_LEAST_MAX==INT_MAX + #define _spec8l "" +#elif INT8_LEAST_MAX == LONG_MAX + #define _spec8l "l" +#elif INT8_LEAST_MAX == LLONG_MAX + #define _spec8l "ll" +#endif + +#if INT16_LEAST_MAX == CHAR_MAX + #define _spec16l "hh" +#elif INT16_LEAST_MAX == SHORT_MAX || INT16_LEAST_MAX==INT_MAX + #define _spec16l "" +#elif INT16_LEAST_MAX == LONG_MAX + #define _spec16l "l" +#elif INT16_LEAST_MAX == LLONG_MAX + #define _spec16l "ll" +#endif + +#if INT32_LEAST_MAX == CHAR_MAX + #define _spec32l "hh" +#elif INT32_LEAST_MAX == SHORT_MAX || INT32_LEAST_MAX==INT_MAX + #define _spec32l "" +#elif INT32_LEAST_MAX == LONG_MAX + #define _spec32l "l" +#elif INT32_LEAST_MAX == LLONG_MAX + #define _spec32l "ll" +#endif + +#if INT64_LEAST_MAX == CHAR_MAX + #define _spec64l "hh" +#elif INT64_LEAST_MAX == SHORT_MAX || INT64_LEAST_MAX==INT_MAX + #define _spec64l "" +#elif INT64_LEAST_MAX == LONG_MAX + #define _spec64l "l" +#elif INT64_LEAST_MAX == LLONG_MAX + #define _spec64l "ll" +#endif + + +#if INT8_FAST_MAX == CHAR_MAX + #define _spec8f "hh" +#elif INT8_FAST_MAX == SHORT_MAX || INT8_FAST_MAX==INT_MAX + #define _spec8f "" +#elif INT8_FAST_MAX == LONG_MAX + #define _spec8f "l" +#elif INT8_FAST_MAX == LLONG_MAX + #define _spec8f "ll" +#endif + +#if INT16_FAST_MAX == CHAR_MAX + #define _spec16f "hh" +#elif INT16_FAST_MAX == SHORT_MAX || INT16_FAST_MAX==INT_MAX + #define _spec16f "" +#elif INT16_FAST_MAX == LONG_MAX + #define _spec16f "l" +#elif INT16_FAST_MAX == LLONG_MAX + #define _spec16f "ll" +#endif + +#if INT32_FAST_MAX == CHAR_MAX + #define _spec32f "hh" +#elif INT32_FAST_MAX == SHORT_MAX || INT32_FAST_MAX==INT_MAX + #define _spec32f "" +#elif INT32_FAST_MAX == LONG_MAX + #define _spec32f "l" +#elif INT32_FAST_MAX == LLONG_MAX + #define _spec32f "ll" +#endif + +#if INT64_FAST_MAX == CHAR_MAX + #define _spec64f "hh" +#elif INT64_FAST_MAX == SHORT_MAX || INT64_FAST_MAX==INT_MAX + #define _spec64f "" +#elif INT64_FAST_MAX == LONG_MAX + #define _spec64f "l" +#elif INT64_FAST_MAX == LLONG_MAX + #define _spec64f "ll" +#endif + +#define PRId8 _spec8 "d" +#define PRId16 _spec16 "d" +#define PRId32 _spec32 "d" +#define PRId64 _spec64 "d" +#define PRIdFAST8 _spec8f "d" +#define PRIdFAST16 _spec16f "d" +#define PRIdFAST32 _spec32f "d" +#define PRIdFAST64 _spec64f "d" +#define PRIdLEAST8 _spec8l "d" +#define PRIdLEAST16 _spec16l "d" +#define PRIdLEAST32 _spec32l "d" +#define PRIdLEAST64 _spec64l "d" +#define PRIdPTR _specptr "d" +#define PRIi8 _spec8 "i" +#define PRIi16 _spec16 "i" +#define PRIi32 _spec32 "i" +#define PRIi64 _spec64 "i" +#define PRIiFAST8 _spec8f "i" +#define PRIiFAST16 _spec16f "i" +#define PRIiFAST32 _spec32f "i" +#define PRIiFAST64 _spec64f "i" +#define PRIiLEAST8 _spec8l "i" +#define PRIiLEAST16 _spec16l "i" +#define PRIiLEAST32 _spec32l "i" +#define PRIiLEAST64 _spec64l "i" +#define PRIiPTR _specptr "i" +#define PRIo8 _spec8 "o" +#define PRIo16 _spec16 "o" +#define PRIo32 _spec16 "o" +#define PRIo64 _spec64 "o" +#define PRIoLEAST8 _spec8l "o" +#define PRIoLEAST16 _spec16l "o" +#define PRIoLEAST32 _spec32l "o" +#define PRIoLEAST64 _spec64l "o" +#define PRIoFAST8 _spec8f "o" +#define PRIoFAST16 _spec16f "o" +#define PRIoFAST32 _spec32f "o" +#define PRIoFAST64 _spec64f "o" +#define PRIoPTR _specptr "o" +#define PRIu8 _spec8 "u" +#define PRIu16 _spec16 "u" +#define PRIu32 _spec16 "u" +#define PRIu64 _spec64 "u" +#define PRIuLEAST8 _spec8l "u" +#define PRIuLEAST16 _spec16l "u" +#define PRIuLEAST32 _spec32l "u" +#define PRIuLEAST64 _spec64l "u" +#define PRIuFAST8 _spec8f "u" +#define PRIuFAST16 _spec16f "u" +#define PRIuFAST32 _spec32f "u" +#define PRIuFAST64 _spec64f "u" +#define PRIuPTR _specptr "u" +#define PRIx8 _spec8 "x" +#define PRIx16 _spec16 "x" +#define PRIx32 _spec32 "x" +#define PRIx64 _spec64 "x" +#define PRIxLEAST8 _spec8l "x" +#define PRIxLEAST16 _spec16l "x" +#define PRIxLEAST32 _spec32l "x" +#define PRIxLEAST64 _spec64l "x" +#define PRIxFAST8 _spec8f "x" +#define PRIxFAST16 _spec16f "x" +#define PRIxFAST32 _spec32f "x" +#define PRIxFAST64 _spec64f "x" +#define PRIxPTR _specptr "x" +#define PRIX8 _spec8 "X" +#define PRIX16 _spec16 "X" +#define PRIX32 _spec32 "X" +#define PRIX64 _spec64 "X" +#define PRIXLEAST8 _spec8l "X" +#define PRIXLEAST16 _spec16l "X" +#define PRIXLEAST32 _spec32l "X" +#define PRIXLEAST64 _spec64l "X" +#define PRIXFAST8 _spec8f "X" +#define PRIXFAST16 _spec16f "X" +#define PRIXFAST32 _spec32f "X" +#define PRIXFAST64 _spec64f "X" +#define PRIXPTR _specptr "X" + +// TODO: scan formats for signed and unsigned integers diff --git a/inc/string.h b/inc/string.h index a062ac3..fa2dfc1 100644 --- a/inc/string.h +++ b/inc/string.h @@ -31,15 +31,15 @@ char *strerror(int errnum); size_t strlen(const char *s); #if __STDC_WANT_LIB_EXT1__ == 1 -errno_t memcpy_s(void * restrict s1, rsize_t s1max, const void * restrict s2, rsize_t n); -errno_t memmove_s(void *s1, rsize_t s1max, const void *s2, rsize_t n); -errno_t strcpy_s(char * restrict s1, rsize_t s1max, const char * restrict s2); -errno_t strncpy_s(char * restrict s1, rsize_t s1max,const char * restrict s2, rsize_t n); -errno_t strcat_s(char * restrict s1, rsize_t s1max, const char * restrict s2); -errno_t strncat_s(char * restrict s1, rsize_t s1max, const char * restrict s2, rsize_t n); -char *strtok_s(char * restrict s1, rsize_t * restrict s1max, const char * restrict s2, char ** restrict ptr); -errno_t memset_s(void *s, rsize_t smax, int c, rsize_t n); -errno_t strerror_s(char *s, rsize_t maxsize, errno_t errnum); -size_t strerrorlen_s(errno_t errnum); +int memcpy_s(void * restrict s1, size_t s1max, const void * restrict s2, size_t n); +int memmove_s(void *s1, size_t s1max, const void *s2, size_t n); +int strcpy_s(char * restrict s1, size_t s1max, const char * restrict s2); +int strncpy_s(char * restrict s1, size_t s1max,const char * restrict s2, size_t n); +int strcat_s(char * restrict s1, size_t s1max, const char * restrict s2); +int strncat_s(char * restrict s1, size_t s1max, const char * restrict s2, size_t n); +char *strtok_s(char * restrict s1, size_t * restrict s1max, const char * restrict s2, char ** restrict ptr); +int memset_s(void *s, size_t smax, int c, size_t n); +int strerror_s(char *s, size_t maxsize, int errnum); +size_t strerrorlen_s(int errnum); size_t strnlen_s(const char *s, size_t maxsize); #endif diff --git a/test/test.c b/test/test.c index 41f6d19..a362590 100644 --- a/test/test.c +++ b/test/test.c @@ -4,35 +4,10 @@ #include #include +#include + int main(int argc, char** argv) { - for (int i = 0; i < argc; i++) { - printf("[%d] = %s\n", i, argv[i]); - } - - printf("%d\n", 583875381); - printf("%.*s", (int)3, "Hello"); - - for (char c = 'a'; c != 'z'; ++c) { - assert(isupper(toupper(c))); - } - - double v0 = strtod("0X1.BC70A3D70A3D7P+6", NULL); - - // parsing with error handling - const char *p = "111.11 -2.22 Nan nan(2) inF 0X1.BC70A3D70A3D7P+6 1.18973e+4932zzz"; - - char *end; - for (double f = strtod(p, &end); p != end; f = strtod(p, &end)) { - printf("'%.*s' -> ", (int)(end - p), p); - p = end; - if (errno == ERANGE){ - printf("range error, got "); - } - - printf("%f\n", f); - } - - // parsing without error handling - double v1 = strtod(" -0.0000000123junk", NULL); - double v2 = strtod("junk", NULL); + uint64_t mynumber = 4; + printf("%"PRIu64"\n", mynumber); + return 0; } \ No newline at end of file