Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
min_printf.h
Go to the documentation of this file.
1/**
2 * @file
3 * @brief Implementation of a [function](https://www.geeksforgeeks.org/variable-length-argument-c) similar to `printf`
4 * @details
5 * `printf` statement rewritten (as `min_printf`) in C without using the `stdio.h` library
6 * Syntax of `min_printf` is same as `printf`
7 * Currently min_printf handles:
8 * Integers, Doubles, floats, characters and strings
9 * The format specifiers and escape sequence is the same as for `printf`
10 * User can also specify the width and precision if required, just like in the case of `printf`
11 * How to use it:
12 * - First include min_printf.h in your code
13 * - Then type `min_printf()`, and pass required parameters to it
14 * - As already specified, it's syntax is same as printf
15 * @author [Jaskarn Singh](https://github.com/Jaskarn7)
16*/
17
18#ifndef MIN_PRINTF_H
19#define MIN_PRINTF_H
20
21#include <stdlib.h> /// for `malloc` and `free` functions
22#include <unistd.h> /// for `write` function
23#include <stdarg.h> /// for `va_start` and `va_arg` functions
24
25#define INT_MAX_LENGTH 10 /// used as standard length of string to store integers
26#define PRECISION_FOR_FLOAT 8 /// default precision for float or double if not specified
27
28/**
29 * @brief struct used to store character in certain times
30*/
31typedef struct buffer {
32 char buffr_char; // Character will be stored in this variable
33 int buf_size; // Checks if character is present in buffr_char or not, 0 if no and 1 if yes
35
36/**
37 * @details
38 * This function return ten to the power a(The parameter specified to it) like:
39 * if the parameter specified is 4 i.e. -> power_of_ten(4) is called then
40 * this function will return ten to the power four (10000);
41 * @param a The power of ten which is to be returned
42 * @return Ten to the power a
43 */
44int power_of_ten(int a)
45{
46 int n = 1; ///< This number will be returned as ten to power of a
47 for (int i = 1; i <= a; ++i)
48 n *= 10 ;
49 return n;
50}
51
52/**
53 * @brief Checks if a character is a number
54 * @param c character to be checked if it's a number or not
55 * @return `true`(1) if the character is a number
56 * @return `false`(0) if the character is NOT a number
57*/
58int is_number(char *c)
59{
60 return (*c >= '0' && *c <= '9') ? 1 : 0;
61}
62
63/**
64 * @brief Returns specific required next character
65 * @param p pointer to a format string of `min_printf()`
66 * @param buffer struct for checking if buffr_char character is present or not
67 * @return character inside `buffer->buffr_char`, if `buffer->buf_size` is one
68 * @return character at which p is pointing, if `buffer->buf_size` is zero
69 */
70char get_ch(char *p, Buffer *buffer)
71{
72 if (buffer->buf_size) {
73 buffer->buf_size = 0; ///< Since character is used, this sets `buffer->buf_size` to zero
74 return buffer->buffr_char; // Returns character inside buffer->buffr_char
75 }
76 return *p++;
77}
78
79/**
80 * @brief Stores character to the `buffer->buffr_char`
81 * @param c character to be stored in the `buffer->buffr_char`
82 * @param buffer struct where character will be stored
83*/
84void unget_ch(char *c, Buffer *buffer)
85{
86 buffer->buffr_char = *c; // Character initializes inside buffer->buffr_char
87 buffer->buf_size = 1; // Sets bufsize to one as new character is stored in buffr_char
88}
89
90
91/**
92 * @brief Calculates the number of digits in a number
93 * @param n number whose digits are to be counted
94 * @return number of digits in n
95*/
97{
98 int digits = 0; // Stores encountered number of digits
99 while (n > 0) {
100 ++digits; // Since number still contains a digit, so increment digit variable
101 n /= 10; // Removes last digit from number
102 }
103 return digits;
104}
105
106/**
107 * @brief Prints one character on screen
108 * @param s character to be printed on the screen
109*/
110void put_char(char s)
111{
112 /* buf used for storing character to be printed in an array (+1 for '\0')*/
113 char *buf = (char *) malloc(sizeof(char) + 1);
114 *buf = s;
115 *(buf + 1) = '\0';
116 write(1, buf, 1);
117 free(buf);
118}
119
120/**
121 * @brief Reverses a string using [two pointer algorithm](https://www.geeksforgeeks.org/program-reverse-array-using-pointers/?ref=rp)
122 * @param p pointer to the string which is to be reversed
123*/
124void reverse_str(char *p)
125{
126 char *l = p; // Points to first character of p
127 char *h = p; // Will be used to point to last character of p
128 char temp; // Temporarily stores a character, Used in swapping
129
130 while (*h != '\0')
131 ++h;
132 --h; // Now h point to last valid character of string
133
134 /* Swap character which lower and higher are pointing until lower < higher. At that point string will be reversed.*/
135 while (l < h) {
136 temp = *l;
137 *l = *h;
138 *h = temp;
139 ++l; // Increment lower to next character
140 --h; // Decrement higher to previous character from current character
141 }
142}
143
144/**
145 * @details
146 * The algorithm here is to first convert the number into
147 * string and then reverse it be passing it to reverse_str function
148 * and then printing on the screen
149 * @param n Number to be printed
150 * @param width Total characters to be printed (Prints ' ' if (size < width)
151 * @param precision Total character of number to be printed (prints 0 before number if size of number < precision)
152 *
153*/
154void print_int_value(int n, int width, int precision)
155{
156 char *p = (char *) malloc(INT_MAX_LENGTH * sizeof(char) + 1); /* +1 for '\0' */
157 char *s = p; // Temporary pointer
158 int size = 0; //!< Used to store number of digits in number
159
160 while (n > 0) {
161 *s++ = n % 10 + '0'; // Converts last digit of number to character and store it in p
162 ++size; // Increment size variable as one more digit is occurred
163 n /= 10; // Removes the last digit from the number n as we have successfully stored it in p
164 }
165 *s = '\0';
166
167 s = p; // Again point back s to starting of p
168
169 reverse_str(p);
170
171 /*!
172 * The next two conditions check weather it is required to
173 * add blanks before printing the number (ie: width)and is it specified how many
174 * zeros to be printed before the number is printed (ie: precision)
175 */
176 if (width > 0 && size < width)
177 for (int i = 0; i < (width - precision); ++i)
178 put_char(' ');
179
180 if (precision > 0 && precision > size)
181 for (int i = 0; i < (precision - size); ++i)
182 put_char('0');
183
184 /* Prints the number.*/
185 while (*s != '\0')
186 put_char(*s++);
187
188 free(p);
189}
190
191/**
192* @brief The algorithm here is also the same as the `print_int_value` function
193 *
194 * @details
195 * First, the digits before decimal is printed by converting the double
196 * to int. Then after printed a `.`, the double number is subtracted with
197 * the integer value of the number, leaving us with 0 before the decimal.
198 * Then, we multiply the number with 10 raised to the power precision (
199 * precision means how many digits to be printed after the decimal.)
200 * By default, the precision is 8 if it is not specified.
201 * Then, the remaining number is printed on the screen.
202 * @param dval double number to be printed
203 * @param width similar to width parameter of print_int_value()
204 * @param precision tells the number of digits to be printed after the decimal (By default it is 8)
205 */
206void print_double_value(double dval, int width, int precision)
207{
208 int ndigits = get_number_of_digits((int) dval); // Store number of digits before decimal in dval
209 int reqd_blanks = width - (precision + 1) - ndigits; // Blanks to be printed before printing dval, just to cover the width
210
211 print_int_value((int) dval, reqd_blanks, 0); // Prints the part before decimal
212
213 put_char('.'); // Print decimal
214
215 /*Deletes digits before decimal and makes them zero. For example:
216 if dval = 1923.79022, them this will make dval = 0.79022
217 */
218 dval = dval - (int) dval;
219
220 dval *= power_of_ten(precision); // Brings precision number of digits after decimal to before decimal
221
222 print_int_value((int) dval, 0, precision); // Prints the remaining number
223}
224
225/**
226 * @details
227* First size of the string is calculated to check whether
228* width and precision are to be taken into account or not.
229* Then, the string is printed in accordingly.
230* @param p pointer to string to be printed
231* @param width if (width > sizeof string) then, blanks will be printed before sting to cover up the width
232* @param precision total characters of the string to be printed (prints the whole string if 0 or greater than size of string)
233*/
234void print_string(char *p, int width, int precision)
235{
236 int size = 0; // Stores number of character in string
237 char *s = p; // Temporary pointer
238
239 /* Calculates size of string p*/
240 while (*s != '\0') {
241 ++size;
242 ++s;
243 }
244
245 s = p; // Point s to starting of p
246
247 /* Checks how many characters to be printed.
248 if precision is defined then size variable is changed to precision so that only precision
249 number of characters were printed.
250 */
251 if (precision != 0 && precision < size)
252 size = precision;
253
254 /* Prints blanks to cover the width if required*/
255 for (int i = 0; i < (width - size); ++i)
256 put_char(' ');
257
258 /* Print the string.*/
259 for (int i = 0; i < size; ++i)
260 put_char(*s++);
261
262}
263
264/**
265* @brief Takes width and precision specified from the format of the string
266* @param p pointer of the format string
267* @param width variable in which width will be stored
268* @param precision variable in which precision will be stored
269* @return character pointer to the current pointer of string p (used to update value of p)
270*/
271char *get_width_and_precision(char *p, Buffer *buffer, int *width, int *precision)
272{
273 /* Skip % if p is pointing to it.*/
274 if (*p == '%')
275 ++p;
276
277 /* Calculates the width specified. */
278 while (*p != '.' && is_number(p))
279 *width = *width * 10 + (*p++ - '0');
280
281 /* Calculates the precision specified.*/
282 if (*p == '.' /* Since a precision is always specified after a '.'. */) {
283 while (is_number(++p))
284 *precision = *precision * 10 + (*p - '0');
285 unget_ch(p, buffer); // The non number will be stored in `buffer->buffr`
286 }
287 return p;
288}
289
290/**
291 * min_printf is the function same as printf
292 * @param fmt format of string
293 * @param ... arguments passed according to the format
294*/
295void min_printf(char *fmt, ...)
296{
297 va_list ap; // Points to each unnamed arg in turn
298 char *p, *sval; // p will be used to point to fmt and sval will store string value
299 char cval; // Stores character value
300 int ival; // For integer values
301 double dval; // For double or float values
302 va_start(ap, fmt); // Makes ap points to first unnames argument
303
304 /* Initializing the buffer for storing character. */
305 Buffer *buffer = (Buffer *) malloc(sizeof(Buffer));
306 buffer->buf_size = 0; // Initially set buffer size to zero as no character is inserted
307
308 for (p = fmt; *p != '\0'; ++p) {
309
310 /* If p != '%' then the character is printed to screen. */
311 if (*p != '%') {
312 put_char(*p);
313 continue;
314 }
315
316 int width = 0; // Stores width specified
317 int precision = 0; // Stores precision specified
318
319 /* Updates values of width, precision and p. */
320 p = get_width_and_precision(p, buffer, &width, &precision);
321
322 /* Checks format of next argument.*/
323 switch (get_ch(p, buffer)) {
324 case 'd': // Integer
325 ival = va_arg(ap, int);
326 print_int_value(ival, width, precision);
327 break;
328 case 'c': // Character
329 cval = va_arg(ap, int);
330 put_char(cval);
331 break;
332 case 'f': // Float or Double
333 dval = va_arg(ap, double);
334
335 // If precision is not specified then default value is applied
336 if (precision == 0)
337 precision = PRECISION_FOR_FLOAT;
338 print_double_value(dval, width, precision);
339 break;
340 case 's': // String pointer
341 sval = va_arg(ap, char *);
342 print_string(sval, width, precision);
343 break;
344 default:
345 put_char(*p);
346 break;
347 }
348 }
349 va_end(ap);
350}
351
352#endif /* MIN_PRINTF_H */
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition: malloc_dbg.h:18
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition: malloc_dbg.h:26
void print_int_value(int n, int width, int precision)
Definition: min_printf.h:154
void min_printf(char *fmt,...)
min_printf is the function same as printf
Definition: min_printf.h:295
char get_ch(char *p, Buffer *buffer)
Returns specific required next character.
Definition: min_printf.h:70
void print_double_value(double dval, int width, int precision)
The algorithm here is also the same as the print_int_value function.
Definition: min_printf.h:206
int power_of_ten(int a)
Definition: min_printf.h:44
void reverse_str(char *p)
Reverses a string using two pointer algorithm
Definition: min_printf.h:124
char * get_width_and_precision(char *p, Buffer *buffer, int *width, int *precision)
Takes width and precision specified from the format of the string.
Definition: min_printf.h:271
void unget_ch(char *c, Buffer *buffer)
Stores character to the buffer->buffr_char
Definition: min_printf.h:84
#define INT_MAX_LENGTH
for malloc and free functions for write function for va_start and va_arg functions
Definition: min_printf.h:25
struct buffer Buffer
struct used to store character in certain times
void put_char(char s)
Prints one character on screen.
Definition: min_printf.h:110
int is_number(char *c)
Checks if a character is a number.
Definition: min_printf.h:58
int get_number_of_digits(int n)
Calculates the number of digits in a number.
Definition: min_printf.h:96
void print_string(char *p, int width, int precision)
Definition: min_printf.h:234
struct used to store character in certain times
Definition: min_printf.h:31