Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/stdoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "stdoc/io/stdoc_printf.h"
#include "stdoc/io/stdoc_version.h"
#include "stdoc/io/stdoc_scanf.h"
#include "stdoc/utility/compiler/macros/macros.h"

#endif /* STDOC_H */
7 changes: 3 additions & 4 deletions include/stdoc/io/stdoc_printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@

#if !defined(STDOC_USE_LIBC)

STDOC_STATIC_INLINE long stdoc_syscall_write(int fd, const void* buf,
unsigned long count)
static inline long stdoc_syscall_write(int fd, const void* buf, unsigned long count)
{
long ret;

Expand Down Expand Up @@ -176,10 +175,10 @@ STDOC_STATIC_INLINE long stdoc_syscall_write(int fd, const void* buf,
*
* Notes:
* - Format string is validated at compile-time when supported
* via STDOC_ATTR_PRINTF
* via STDOC_ATTRIBUTE_PRINTF
* - Behavior is undefined for unsupported format specifiers
*/
STDOC_ATTR_PRINTF(1, 2)
STDOC_ATTRIBUTE_PRINTF(1, 2)
int stdoc_printf(const char* format, ...);

#endif /* STDOC_PRINTF_H */
42 changes: 42 additions & 0 deletions include/stdoc/io/stdoc_scanf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef STDOC_SCANF_H
#define STDOC_SCANF_H

#include <stdarg.h>
#include <stdoc/utility/compiler/macros/macros.h>

/*
* stdoc_scanf
*
* Minimal scanf-like function reading from stdin (fd 0).
*
* Parameters:
* format - format string
* ... - pointers to variables to store input
*
* Returns:
* Number of successfully matched and assigned input items.
*
* Supported format specifiers:
* %d - int (decimal)
* %u - unsigned int (decimal)
* %x - unsigned int (hexadecimal, lowercase/uppercase accepted)
* %c - char (no whitespace skipping)
* %s - string (whitespace-delimited, char* must be large enough)
* %p - void* (hexadecimal, optional 0x prefix)
* %% - literal '%'
*
* Behavior:
* - Input is buffered (single syscall per scanf call if possible)
* - Whitespace (space, tab, newline) skips automatically for %d/%u/%x/%s/%p
* - %c does NOT skip whitespace
* - Returns EOF if input fails before any conversion.
*
* Limitations:
* - No width, no assignment suppression, no length modifiers.
* - No floating-point support.
*/

STDOC_ATTRIBUTE_SCANF(1, 2)
int stdoc_scanf(const char* format, ...);

#endif /* STDOC_SCANF_H */
80 changes: 26 additions & 54 deletions include/stdoc/utility/compiler/macros/macros.h
Comment thread
uoctamika marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -56,80 +56,68 @@
#endif /* STDOC_COMPILER_GNU_LIKE */

/*
* STDOC_ATTR_NORETURN
* STDOC_ATTRIBUTE_NORETURN
* Indicates that a function never returns (exit, abort, infinite loop).
*/
#if STDOC_COMPILER_GNU_LIKE
# define STDOC_ATTR_NORETURN __attribute__((noreturn))
# define STDOC_ATTRIBUTE_NORETURN __attribute__((noreturn))
#elif STDOC_COMPILER_MSVC
# define STDOC_ATTR_NORETURN __declspec(noreturn)
# define STDOC_ATTRIBUTE_NORETURN __declspec(noreturn)
#else
# define STDOC_ATTR_NORETURN
#endif /* STDOC_ATTR_NORETURN */
# define STDOC_ATTRIBUTE_NORETURN
#endif /* STDOC_ATTRIBUTE_NORETURN */

/*
* STDOC_ATTR_UNUSED
* STDOC_ATTRIBUTE_UNUSED
* Marks a variable or parameter as intentionally unused.
*/
#if STDOC_COMPILER_GNU_LIKE
# define STDOC_ATTR_UNUSED __attribute__((unused))
# define STDOC_ATTRIBUTE_UNUSED __attribute__((unused))
#else
# define STDOC_ATTR_UNUSED
#endif /* STDOC_ATTR_UNUSED */
# define STDOC_ATTRIBUTE_UNUSED
#endif /* STDOC_ATTRIBUTE_UNUSED */

/*
* STDOC_ATTR_ALWAYS_INLINE
* Forces inlining of a function, even at low optimization levels.
*/
#if STDOC_COMPILER_GNU_LIKE
# define STDOC_ATTR_ALWAYS_INLINE inline __attribute__((always_inline))
#elif STDOC_COMPILER_MSVC
# define STDOC_ATTR_ALWAYS_INLINE __forceinline
#else
# define STDOC_ATTR_ALWAYS_INLINE inline
#endif /* STDOC_ATTR_ALWAYS_INLINE */

/*
* STDOC_ATTR_PRINTF(fmt, arg)
* STDOC_ATTRIBUTE_PRINTF(fmt, arg)
* Enables compile-time format string checking for printf-like functions.
*/
#if STDOC_COMPILER_GNU_LIKE && !STDOC_COMPILER_ICC
# define STDOC_ATTR_PRINTF(fmt, arg) __attribute__((format(printf, fmt, arg)))
# define STDOC_ATTRIBUTE_PRINTF(fmt, arg) __attribute__((format(printf, fmt, arg)))
#else
# define STDOC_ATTR_PRINTF(fmt, arg)
#endif /* STDOC_ATTR_PRINTF */
# define STDOC_ATTRIBUTE_PRINTF(fmt, arg)
#endif /* STDOC_ATTRIBUTE_PRINTF */

/*
* STDOC_ATTR_SCANF(fmt, arg)
* STDOC_ATTRIBUTE_SCANF(fmt, arg)
* Enables compile-time format string checking for scanf-like functions.
*/
#if STDOC_COMPILER_GNU_LIKE && !STDOC_COMPILER_ICC
# define STDOC_ATTR_SCANF(fmt, arg) __attribute__((format(scanf, fmt, arg)))
# define STDOC_ATTRIBUTE_SCANF(fmt, arg) __attribute__((format(scanf, fmt, arg)))
#else
# define STDOC_ATTR_SCANF(fmt, arg)
#endif /* STDOC_ATTR_SCANF */
# define STDOC_ATTRIBUTE_SCANF(fmt, arg)
#endif /* STDOC_ATTRIBUTE_SCANF */

/*
* STDOC_ATTR_WARN_UNUSED
* STDOC_ATTRIBUTE_WARN_UNUSED
* Generates a warning if the function's return value is ignored.
*/
#if STDOC_COMPILER_GNU_LIKE
# define STDOC_ATTR_WARN_UNUSED __attribute__((warn_unused_result))
# define STDOC_ATTRIBUTE_WARN_UNUSED __attribute__((warn_unused_result))
#else
# define STDOC_ATTR_WARN_UNUSED
#endif /* STDOC_ATTR_WARN_UNUSED */
# define STDOC_ATTRIBUTE_WARN_UNUSED
#endif /* STDOC_ATTRIBUTE_WARN_UNUSED */

/*
* STDOC_ATTR_DEPRECATED(msg)
* STDOC_ATTRIBUTE_DEPRECATED(msg)
* Marks a function or variable as deprecated.
*/
#if STDOC_COMPILER_GNU_LIKE
# define STDOC_ATTR_DEPRECATED(msg) __attribute__((deprecated(msg)))
# define STDOC_ATTRIBUTE_DEPRECATED(msg) __attribute__((deprecated(msg)))
#elif STDOC_COMPILER_MSVC
# define STDOC_ATTR_DEPRECATED(msg) __declspec(deprecated(msg))
# define STDOC_ATTRIBUTE_DEPRECATED(msg) __declspec(deprecated(msg))
#else
# define STDOC_ATTR_DEPRECATED(msg)
#endif /* STDOC_ATTR_DEPRECATED */
# define STDOC_ATTRIBUTE_DEPRECATED(msg)
#endif /* STDOC_ATTRIBUTE_DEPRECATED */

/*
* STDOC_PUBLIC / STDOC_LOCAL
Expand Down Expand Up @@ -166,22 +154,6 @@
# define STDOC_UNLIKELY(x) (x)
#endif /* branch prediction */

/*
* STDOC_INLINE / STDOC_STATIC_INLINE
* Portable inline keywords.
*/
#ifndef STDOC_INLINE
# if STDOC_COMPILER_MSVC
# define STDOC_INLINE __inline
# else
# define STDOC_INLINE inline
# endif
#endif /* STDOC_INLINE */

#ifndef STDOC_STATIC_INLINE
# define STDOC_STATIC_INLINE static STDOC_INLINE
#endif /* STDOC_STATIC_INLINE */

/*
* STDOC_ARRAY_SIZE(arr)
* Number of elements in a static array.
Expand Down
Loading
Loading