MentOS  0.8.0
The Mentoring Operating System
Macros | Functions | Variables
vsprintf.c File Reference

Print formatting routines. More...

Macros

#define __DEBUG_HEADER__   "[PRINTF]"
 Change header.
 
#define __DEBUG_LEVEL__   LOGLEVEL_NOTICE
 Set log level.
 
#define CVTBUFSIZE   500
 Size of the buffer used to call cvt functions.
 
#define FLAGS_ZEROPAD   (1U << 0U)
 Fill zeros before the number.
 
#define FLAGS_LEFT   (1U << 1U)
 Left align the value.
 
#define FLAGS_PLUS   (1U << 2U)
 Print the plus sign.
 
#define FLAGS_SPACE   (1U << 3U)
 If positive add a space instead of the plus sign.
 
#define FLAGS_HASH   (1U << 4U)
 Preceed with 0x or 0X, x or X respectively.
 
#define FLAGS_UPPERCASE   (1U << 5U)
 Print uppercase.
 
#define FLAGS_SIGN   (1U << 6U)
 Print the sign.
 

Functions

static int skip_atoi (const char **s)
 Returns the integer value parsed from the beginning of the string until a non-integer character is found. More...
 
static char * number (char *str, long num, int base, int size, int32_t precision, unsigned flags)
 Transforms the number into a string. More...
 
static char * eaddr (char *str, unsigned char *addr, int size, int precision, unsigned flags)
 Converts a MAC address into a human-readable string format. More...
 
static char * iaddr (char *str, unsigned char *addr, int size, int precision, unsigned flags)
 Converts an IPv4 address into a human-readable string format. More...
 
static void cfltcvt (double value, char *buffer, char fmt, int precision)
 Converts a floating-point number to a string with a specified format. More...
 
static void forcdecpt (char *buffer)
 Ensures that a decimal point is present in the given number string. More...
 
static void cropzeros (char *buffer)
 Removes trailing zeros after the decimal point in a number string. More...
 
static char * flt (char *str, double num, int size, int precision, char fmt, unsigned flags)
 Formats a floating-point number into a string with specified options. More...
 
int vsprintf (char *str, const char *fmt, va_list args)
 Write formatted data from variable argument list to string. More...
 
int printf (const char *fmt,...)
 Write formatted output to stdout. More...
 
int sprintf (char *str, const char *fmt,...)
 Write formatted output to str. More...
 
int vfprintf (int fd, const char *fmt, va_list args)
 Write formatted data from variable argument list to a file. More...
 
int fprintf (int fd, const char *fmt,...)
 Write formatted output to a file. More...
 

Variables

static char * _digits = "0123456789abcdefghijklmnopqrstuvwxyz"
 The list of digits.
 
static char * _upper_digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 The list of uppercase digits.
 

Detailed Description

Print formatting routines.

Function Documentation

◆ cfltcvt()

static void cfltcvt ( double  value,
char *  buffer,
char  fmt,
int  precision 
)
static

Converts a floating-point number to a string with a specified format.

Parameters
valueThe floating-point value to be converted.
bufferThe output buffer to store the resulting string.
fmtThe format specifier ('e', 'f', or 'g').
precisionThe number of digits to be displayed after the decimal point.

◆ cropzeros()

static void cropzeros ( char *  buffer)
static

Removes trailing zeros after the decimal point in a number string.

Parameters
bufferThe string representation of a number.

◆ eaddr()

static char* eaddr ( char *  str,
unsigned char *  addr,
int  size,
int  precision,
unsigned  flags 
)
static

Converts a MAC address into a human-readable string format.

Parameters
strThe output string where the MAC address will be written.
addrThe 6-byte MAC address to be formatted.
sizeThe minimum field width for the output (pads with spaces if necessary).
precisionUnused in this function (for compatibility with similar functions).
flagsControl flags that affect the format (e.g., uppercase and left alignment).
Returns
Pointer to the end of the output string.

◆ flt()

static char* flt ( char *  str,
double  num,
int  size,
int  precision,
char  fmt,
unsigned  flags 
)
static

Formats a floating-point number into a string with specified options.

This function converts a floating-point number into a string representation based on the specified format, precision, and flags. It handles alignment, padding, and sign appropriately.

Parameters
strPointer to the output string where the formatted number will be stored.
numThe floating-point number to format.
sizeThe total size of the output string, including padding.
precisionThe number of digits to display after the decimal point.
fmtThe format specifier for the output ('f', 'g', 'e', etc.).
flagsControl flags that modify the output format (e.g., left alignment, zero padding).
Returns
Pointer to the next position in the output string after the formatted number.

If the FLAGS_LEFT is set, clear the FLAGS_ZEROPAD flag. Left alignment implies no zero padding.

Determine the padding character (c) and the sign of the number. If FLAGS_ZEROPAD is set, the padding will be '0', otherwise it will be a space (' ').

Check the FLAGS_SIGN flag to determine if the sign should be added.

If the number is negative, set sign to '-' and make the number positive.

If FLAGS_PLUS is set, prepend a '+' to positive numbers.

If FLAGS_SPACE is set, prepend a space to positive numbers.

Set the default precision if no precision is provided.

Convert the floating-point number num into a string workbuf using the given format fmt.

If the FLAGS_HASH is set and precision is 0, force a decimal point in the output.

For format 'g', remove trailing zeros unless FLAGS_HASH is set.

Calculate the length of the resulting string workbuf.

Adjust size to account for the length of the output string.

Add padding spaces before the number if neither FLAGS_ZEROPAD nor FLAGS_LEFT are set.

Add the sign character (if any) before the number.

Add padding characters (either '0' or spaces) before the number if FLAGS_ZEROPAD is set.

Copy the formatted number string to the output str.

Add padding spaces after the number if FLAGS_LEFT is set (left-aligned output).

Return the resulting string after formatting the number.

◆ forcdecpt()

static void forcdecpt ( char *  buffer)
static

Ensures that a decimal point is present in the given number string.

Parameters
bufferThe string representation of a number.

◆ fprintf()

int fprintf ( int  fd,
const char *  fmt,
  ... 
)

Write formatted output to a file.

Parameters
fdThe file descriptor associated with the file.
fmtFormat string, following the same specifications as printf.
...The list of arguments.
Returns
On success, the total number of characters written is returned. On failure, a negative number is returned.

◆ iaddr()

static char* iaddr ( char *  str,
unsigned char *  addr,
int  size,
int  precision,
unsigned  flags 
)
static

Converts an IPv4 address into a human-readable string format.

Parameters
strThe output string where the IPv4 address will be written.
addrThe 4-byte IPv4 address to be formatted.
sizeThe minimum field width for the output (pads with spaces if necessary).
precisionUnused in this function (for compatibility with similar functions).
flagsControl flags that affect the format (e.g., left alignment).
Returns
Pointer to the end of the output string.

◆ number()

static char* number ( char *  str,
long  num,
int  base,
int  size,
int32_t  precision,
unsigned  flags 
)
static

Transforms the number into a string.

Parameters
strthe output string.
numthe number to transform to string.
basethe base to use for number transformation (e.g., 10 for decimal, 16 for hex).
sizethe minimum size of the output string (pads with '0' or spaces if necessary).
precisionthe precision for number conversion (affects floating point numbers and zero padding).
flagscontrol flags (e.g., for padding, sign, and case sensitivity).
Returns
the resulting string after number transformation.

◆ printf()

int printf ( const char *  fmt,
  ... 
)

Write formatted output to stdout.

Parameters
fmtThe format string.
...The list of arguments.
Returns
On success, the total number of characters written is returned. On failure, a negative number is returned.

◆ skip_atoi()

static int skip_atoi ( const char **  s)
inlinestatic

Returns the integer value parsed from the beginning of the string until a non-integer character is found.

Parameters
sthe string we need to analyze.
Returns
the integer value represented by the initial digits in the string.
Note
This function assumes that s points to a valid string and will stop parsing at the first non-integer character.

◆ sprintf()

int sprintf ( char *  str,
const char *  fmt,
  ... 
)

Write formatted output to str.

Parameters
strThe buffer where the formatted string will be placed.
fmtFormat string, following the same specifications as printf.
...The list of arguments.
Returns
On success, the total number of characters written is returned. On failure, a negative number is returned.

◆ vfprintf()

int vfprintf ( int  fd,
const char *  fmt,
va_list  args 
)

Write formatted data from variable argument list to a file.

Parameters
fdThe file descriptor associated with the file.
fmtFormat string, following the same specifications as printf.
argsA variable arguments list.
Returns
On success, the total number of characters written is returned. On failure, a negative number is returned.

◆ vsprintf()

int vsprintf ( char *  str,
const char *  fmt,
va_list  args 
)

Write formatted data from variable argument list to string.

Parameters
strPointer to a buffer where the resulting C-string is stored.
fmtFormat string, following the same specifications as printf.
argsA variable arguments list.
Returns
On success, the total number of characters written is returned. On failure, a negative number is returned.