TA-REF
vsnprintf.c File Reference
#include <stdbool.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
+ Include dependency graph for vsnprintf.c:

Classes

struct  out_fct_wrap_type
 

Macros

#define PRINTF_NTOA_BUFFER_SIZE   32U
 
#define PRINTF_FTOA_BUFFER_SIZE   32U
 
#define PRINTF_SUPPORT_FLOAT
 
#define PRINTF_SUPPORT_LONG_LONG
 
#define PRINTF_SUPPORT_PTRDIFF_T
 
#define FLAGS_ZEROPAD   (1U << 0U)
 
#define FLAGS_LEFT   (1U << 1U)
 
#define FLAGS_PLUS   (1U << 2U)
 
#define FLAGS_SPACE   (1U << 3U)
 
#define FLAGS_HASH   (1U << 4U)
 
#define FLAGS_UPPERCASE   (1U << 5U)
 
#define FLAGS_CHAR   (1U << 6U)
 
#define FLAGS_SHORT   (1U << 7U)
 
#define FLAGS_LONG   (1U << 8U)
 
#define FLAGS_LONG_LONG   (1U << 9U)
 
#define FLAGS_PRECISION   (1U << 10U)
 
#define _putchar   putchar
 

Typedefs

typedef void(* out_fct_type) (char character, void *buffer, size_t idx, size_t maxlen)
 

Functions

int putchar (char ch)
 
static void _out_buffer (char character, void *buffer, size_t idx, size_t maxlen)
 
static void _out_null (char character, void *buffer, size_t idx, size_t maxlen)
 
static void _out_char (char character, void *buffer, size_t idx, size_t maxlen)
 
static void _out_fct (char character, void *buffer, size_t idx, size_t maxlen)
 
static unsigned int _strlen (const char *str)
 
static bool _is_digit (char ch)
 
static unsigned int _atoi (const char **str)
 
static size_t _ntoa_format (out_fct_type out, char *buffer, size_t idx, size_t maxlen, char *buf, size_t len, bool negative, unsigned int base, unsigned int prec, unsigned int width, unsigned int flags)
 
static size_t _ntoa_long (out_fct_type out, char *buffer, size_t idx, size_t maxlen, unsigned long value, bool negative, unsigned long base, unsigned int prec, unsigned int width, unsigned int flags)
 
static size_t _ntoa_long_long (out_fct_type out, char *buffer, size_t idx, size_t maxlen, unsigned long long value, bool negative, unsigned long long base, unsigned int prec, unsigned int width, unsigned int flags)
 
static size_t _ftoa (out_fct_type out, char *buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags)
 
static int _vsnprintf (out_fct_type out, char *buffer, const size_t maxlen, const char *format, va_list va)
 
int sprintf (char *buffer, const char *format,...)
 
int snprintf (char *buffer, size_t count, const char *format,...)
 
int vsnprintf (char *buffer, size_t count, const char *format, va_list va)
 
int fctprintf (void(*out)(char character, void *arg), void *arg, const char *format,...)
 

Macro Definition Documentation

◆ _putchar

#define _putchar   putchar

◆ FLAGS_CHAR

#define FLAGS_CHAR   (1U << 6U)

◆ FLAGS_HASH

#define FLAGS_HASH   (1U << 4U)

◆ FLAGS_LEFT

#define FLAGS_LEFT   (1U << 1U)

◆ FLAGS_LONG

#define FLAGS_LONG   (1U << 8U)

◆ FLAGS_LONG_LONG

#define FLAGS_LONG_LONG   (1U << 9U)

◆ FLAGS_PLUS

#define FLAGS_PLUS   (1U << 2U)

◆ FLAGS_PRECISION

#define FLAGS_PRECISION   (1U << 10U)

◆ FLAGS_SHORT

#define FLAGS_SHORT   (1U << 7U)

◆ FLAGS_SPACE

#define FLAGS_SPACE   (1U << 3U)

◆ FLAGS_UPPERCASE

#define FLAGS_UPPERCASE   (1U << 5U)

◆ FLAGS_ZEROPAD

#define FLAGS_ZEROPAD   (1U << 0U)

◆ PRINTF_FTOA_BUFFER_SIZE

#define PRINTF_FTOA_BUFFER_SIZE   32U

◆ PRINTF_NTOA_BUFFER_SIZE

#define PRINTF_NTOA_BUFFER_SIZE   32U

◆ PRINTF_SUPPORT_FLOAT

#define PRINTF_SUPPORT_FLOAT

◆ PRINTF_SUPPORT_LONG_LONG

#define PRINTF_SUPPORT_LONG_LONG

◆ PRINTF_SUPPORT_PTRDIFF_T

#define PRINTF_SUPPORT_PTRDIFF_T

Typedef Documentation

◆ out_fct_type

typedef void(* out_fct_type) (char character, void *buffer, size_t idx, size_t maxlen)

Function Documentation

◆ _atoi()

static unsigned int _atoi ( const char **  str)
static

_atoi() - Converts the internal ASCII string into an unsigned integer.

This function is to convert the internal ASCII string into unsigned integer.

Parameters
strstring representation of an integral number.
Returns
i unsigned integer value.

◆ _ftoa()

static size_t _ftoa ( out_fct_type  out,
char *  buffer,
size_t  idx,
size_t  maxlen,
double  value,
unsigned int  prec,
unsigned int  width,
unsigned int  flags 
)
static

_ftoa() - Converts a given floating-point number or a double to a string with the use of standard library functions.

This function checks whether the value is negative or not, then it checks with if condtion default precision to 6, if it not set it will set explicitly. Using the while loop it limits the precision to 9,because it causes a overflow error when precision crosses above 10. Using the if condition rollover or round If the precsion value is greater than 0.5 up the precision value.it round up to

  1. Using the while_loop condition adding extra zeros and append decimal value to the lenghth. Finally using the conditional statement executes pad leading zeros, handling the hash value, padding spaces up to given width and reverses the string.
Parameters
outtype of out_fct_type
bufferPointer to a character string to write the result.
idxidx bytes of size_t
maxlenMaximum number of characters to write.
negativeboolean type
basean unsigned long data type
precan unsigned integral data type
widthan unsigned integral data type
flagsan unsigned integral data type
Returns
non integer value if success else error occur

◆ _is_digit()

static bool _is_digit ( char  ch)
inlinestatic

_is_digit() - Is for the internal test if char is a digit from 0 to 9

Parameters
chThis is the character to be checked.
Returns
true if char is a digit and internal test if char is a digit from 0 to 9

◆ _ntoa_format()

static size_t _ntoa_format ( out_fct_type  out,
char *  buffer,
size_t  idx,
size_t  maxlen,
char *  buf,
size_t  len,
bool  negative,
unsigned int  base,
unsigned int  prec,
unsigned int  width,
unsigned int  flags 
)
static

_ntoa_format() - Converts the string into the defined format structure.

This function uses the while condition for padding the leading zeroes and also applies the if conditions to handle the hash. Using the if condtion pad spaces up to given width what specifies in that. It reverse the string and again append pad spaces up to given width.

Parameters
outtype of out_fct_type
bufferPointer to a character string to write the result.
idxidx bytes of size_t
maxlenMaximum number of characters to write.
negativeboolean type
basean unsigned long data type
precan unsigned integer data type
widthan unsigned integer data type
flagsan unsigned integer data type
Returns
idx non integer value if success else error occur.

◆ _ntoa_long()

static size_t _ntoa_long ( out_fct_type  out,
char *  buffer,
size_t  idx,
size_t  maxlen,
unsigned long  value,
bool  negative,
unsigned long  base,
unsigned int  prec,
unsigned int  width,
unsigned int  flags 
)
static

_ntoa_long() - Converts string into long value.

This function begins with an if condition value then it assigns ~FLAGS_HASH into flags & value. Later it uses the if condition and do while write if precision not equal to zero and value is not equals to zero.

Parameters
outtype of out_fct_type
bufferPointer to a character string to write the result.
ididx bytes of size_t
maxlenMaximum number of characters to write.
negativeboolean type
basean unsigned long data type
precan unsigned integral data type
widthan unsigned integral data type
flagsan unsigned integral data type
Returns
idx non integer value if success else error occur.

◆ _ntoa_long_long()

static size_t _ntoa_long_long ( out_fct_type  out,
char *  buffer,
size_t  idx,
size_t  maxlen,
unsigned long long  value,
bool  negative,
unsigned long long  base,
unsigned int  prec,
unsigned int  width,
unsigned int  flags 
)
static

_ntoa_long_long() - Function to convert string to long value.

This function begins with an if condition then it assigns ~FLAGS_HASH into flags & value. Later it uses the if condition and do while write if precision not equal to zero and value is not equals to zero.

Parameters
outtype of out_fct_type
bufferPointer to a character string to write the result.
idxidx bytes of size_t
maxlenMaximum number of characters to write.
negativeboolean type
basean unsigned long data type
precan unsigned integral data type
widthan unsigned integral data type
flagan unsigned integral data type
Returns
idx non integer value if success else error occur.

◆ _out_buffer()

static void _out_buffer ( char  character,
void *  buffer,
size_t  idx,
size_t  maxlen 
)
inlinestatic

_out_buffer() - Internal buffer output

This function compares the idx and maxlen, If "idx" is less than "maxlen" then it will assign "character" value into the typecasting char "buffer[idx]"

Parameters
charactercharacter type string
bufferPointer to a character string to write the result.
idxbytes of size_t
maxlenMaximum number of characters to write.

◆ _out_char()

static void _out_char ( char  character,
void *  buffer,
size_t  idx,
size_t  maxlen 
)
inlinestatic

_out_char() - Internal putchar wrapper

The typecasting of arguments with void is to avoid unused variable warnings in some compilers. Checks the character value once the if condtion is success then putchar() writes a character into stdout.

Parameters
charactercharacter type string
bufferPointer to a character string to write the result.
idxbytes of size_t
maxlenMaximum number of characters to write.

◆ _out_fct()

static void _out_fct ( char  character,
void *  buffer,
size_t  idx,
size_t  maxlen 
)
inlinestatic

_out_fct() - Internal output function wrapper

This function typecasting idx and maxlen arguments is to avoid compiler error. And then output function wrapper and the buffer is the output fct pointer.

Parameters
charactercharacter type string
bufferPointer to a character string to write the result.
idxbytes of size_t
maxlenMaximum number of characters to write.

◆ _out_null()

static void _out_null ( char  character,
void *  buffer,
size_t  idx,
size_t  maxlen 
)
inlinestatic

_out_null() - Internal null output.

The typecasting of arguments with void is applied to avoid unused variable warnings in some compilers.

Parameters
charactercharacter type string
bufferPointer to a character string to write the result.
idxbytes of size_t
maxlenMaximum number of characters to write.

◆ _strlen()

static unsigned int _strlen ( const char *  str)
inlinestatic

_strlen() - calculates the length of the string.

Parameters
strstr is an argument of type pointer.
Returns
string length if successfully executed,else error occured.

◆ _vsnprintf()

static int _vsnprintf ( out_fct_type  out,
char *  buffer,
const size_t  maxlen,
const char *  format,
va_list  va 
)
static

_vsnprintf() - Function writes formatted output to a character array, up to a maximum number of characters.

The _vsnprintf fucntion firstly initializes the varibles of format specifers like flags, width, precsion in this they evaluate all the specifiers invidually.First it checks the buffer equal to zero or not for null output function. After that flags evaluation will start using the switch case, then width field evalucation take process using if condition.

Parameters
outtype of out_fct_type.
bufferpointer to the buffer where you want to function to store the formatted string.
maxlenmaximum number of characters to store in the buffer.
formatstring that specifies the format of the output.
vavariable-argument list of the additional argument.
Returns
Its return the typecasted int of idx if success otherwise error occured.

◆ fctprintf()

int fctprintf ( void(*)(char character, void *arg)  out,
void *  arg,
const char *  format,
  ... 
)

fctprintf() - Function is using the libary macros of variable aruguments like vastart and vaend.

This function initializes the va_list variable and invokes the va_start(). Invokes _vsnprintf function and stores the value into ret. It applies the functions va_start and va_end on va and returns ret.

Parameters
outAn output function which takes one character and an argument pointer.
argAn argument pointer for user data passed to output function.
formatA string that specifies the format of the output.
Returns
The number of characters that are sent to the output function, not counting the terminating null character.

◆ putchar()

int putchar ( char  ch)

◆ snprintf()

int snprintf ( char *  buffer,
size_t  count,
const char *  format,
  ... 
)

snprintf() - Places the generated output into the character array pointed to by buf, instead of writing it to a file

This function initializes the va_list variable and invokes the va_start(). Invokes _vsnprintf function and stores the value into ret. It applies the functions va_start and va_end on va and returns ret.

Parameters
bufferpointer to buffer where you want to function to store the formatted string.
countmaximum number of characters to store in the buffer.
formatstring that specifies the format of the output.
Returns
ret returns the ret value as an integer type.

◆ sprintf()

int sprintf ( char *  buffer,
const char *  format,
  ... 
)

sprintf() - Sends formatted output to a string pointed to by the argument buffer.

This function initialize the va_list variable and invokes the va_start(). Invokes _vsnprintf function and store the value into ret. It applies the functions va_start and va_end on va and returns ret.

Parameters
bufferpointer to an array of char elements resulting string will store.
formatstring that contains the text to be written to buffer.
Returns
ret It returns the ret value as an integer type.

◆ vsnprintf()

int vsnprintf ( char *  buffer,
size_t  count,
const char *  format,
va_list  va 
)

vsnprintf() - Invokes another function called _vsnprintf(). with some arguments.

Parameters
bufferPointer to the buffer where you want to function to store the formatted string.
countmaximum number of characters to store in the buffer.
formatstring that specifies the format of the output.
Returns
Its return the typecasted int of idx if success otherwise error occured.