tgrekov-ft_printf
HIVE printf Feb 2024
Loading...
Searching...
No Matches
int.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* int.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: tgrekov <tgrekov@student.hive.fi> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2023/11/01 05:18:23 by tgrekov #+# #+# */
9/* Updated: 2024/02/13 08:37:14 by tgrekov ### ########.fr */
10/* */
11/* ************************************************************************** */
12
20#include <stdarg.h>
21#include <unistd.h>
22#include <stdlib.h>
23#include "../sequence.h"
24#include "../../utils/utils.h"
25#include "../../utils/internal_types.h"
26
27int process_uint(t_sequence seq, int *fd, int total);
28
39static t_biggest signed_arg(va_list args, t_lenmod lenmod)
40{
41 if (lenmod == hh)
42 return ((signed char) va_arg(args, int));
43 if (lenmod == h)
44 return ((short) va_arg(args, int));
45 if (lenmod == l)
46 return (va_arg(args, long));
47 if (lenmod == ll)
48 return (va_arg(args, long long));
49 if (lenmod == j)
50 return (va_arg(args, intmax_t));
51 if (lenmod == z)
52 return (va_arg(args, size_t));
53 if (lenmod == t)
54 return (va_arg(args, t_ptrdiff_t));
55 return (va_arg(args, int));
56}
57
65{
66 if (n == -T_BIGGEST_MAX - 1LL)
67 return ((t_ubiggest)(-(n + 1)) + 1ULL);
68 if (n < 0)
69 n = -n;
70 return (n);
71}
72
88void pre_int(va_list args, t_sequence *seq)
89{
90 t_biggest n;
91
92 n = signed_arg(args, seq->subspec.lenmod);
93 if (n < 0)
94 seq->sign = "-";
95 seq->data = lltull(n);
96 seq->total_len = u_len_base(seq->data, 10)
97 - (!seq->subspec.precision && !seq->data);
98 if (seq->subspec.precision > seq->total_len)
99 seq->total_len = seq->subspec.precision;
100 if (seq->subspec.precision != -1)
101 seq->subspec.pad_str = " ";
102 if (seq->subspec.forced_sign && !seq->sign)
103 seq->sign = seq->subspec.forced_sign;
104 seq->process = process_uint;
105}
int process_uint(t_sequence seq, int *fd, int total)
Processes the unsigned integer specifier by printing the number in base 10.
Definition uint.c:36
static t_ubiggest lltull(t_biggest n)
Safely converts a signed integer to an unsigned one.
Definition int.c:64
void pre_int(va_list args, t_sequence *seq)
Preprocess the signed integer specifier by retrieving the argument in the appropriate size,...
Definition int.c:88
static t_biggest signed_arg(va_list args, t_lenmod lenmod)
Retrieves the next signed variable argument in the appropriate size dependent on the length modifier.
Definition int.c:39
long long t_biggest
Convenience typedef for largest signed type I need to handle.
#define T_BIGGEST_MAX
Largest value that fits in t_biggest.
unsigned long long t_ubiggest
Convenience typedef for largest unsigned type I need to handle.
long t_ptrdiff_t
If _PTRDIFF_T is defined, set to ptrdiff_t. Set to long otherwise.
int u_len_base(t_ubiggest n, int base)
Determine the length of unsigned number n in base base.
Definition u_len_base.c:29
Holds information regarding the current format specifier sequence.
Definition sequence.h:49
int(* process)(struct s_sequence, int *, int)
Function to use for processing this sequence's value.
Definition sequence.h:56
t_ubiggest data
Retrieved variable argument.
Definition sequence.h:53
t_subspec subspec
Holds subspecifier options for a format specifier.
Definition sequence.h:51
int total_len
Total number of characters after padding.
Definition sequence.h:54
char * sign
String containing the prefix for the value. Set during preprocessing for a format handler.
Definition sequence.h:52
int precision
Minimum number of digits to be printed, or maximum characters for strings.
Definition subspec.h:71
char * forced_sign
String to print before the value. Set before preprocessing for a format handler. Will be overriden,...
Definition subspec.h:67
t_lenmod lenmod
Represents the length modifiers for a format specifier's variable argument.
Definition subspec.h:72
char * pad_str
String used when padding values.
Definition subspec.h:69
enum e_lenmod t_lenmod
Represents the length modifiers for a format specifier's variable argument.