-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandle_string.c
More file actions
61 lines (59 loc) · 1.69 KB
/
handle_string.c
File metadata and controls
61 lines (59 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <format_specifier.h>
#include <handlers.h>
#include <stdarg.h>
#include <stdlib.h>
#include <utils.h>
/**
* write_string - Writes a string to the buffer
* @string: The string to write
* @buf: Buffer where the string will be written
* @bufsize: Pointer to an integer that keeps track of the current length of the
* buffer
* @num_written: Number of characters to write from the string
*/
static void write_string(const char *string, char *buf, int *bufsize,
int num_written)
{
while (num_written-- > 0)
{
if (*bufsize == LENGTH)
flush(buf, bufsize);
buf[(*bufsize)++] = *string++;
}
}
/**
* handle_string - Handles the string format specifier
* @fs: Pointer to the FormatSpecifier structure
* @args: Pointer to the va_list containing the arguments
* @buf: Buffer where the formatted output will be stored
* @bufsize: Pointer to an integer that keeps track of the current length of the
* buffer
*
* Description: This function retrieves a string argument from the va_list and
* appends it to the buffer, flushing it when necessary.
*/
int handle_string(struct FormatSpecifier *fs, va_list *args, char *buf,
int *bufsize)
{
int string_len, num_written, num_pad;
char *string = (char *)va_arg(*args, char *);
if (string == NULL)
string = "(null)";
string_len = _strlen(string);
if (fs->precision >= 0)
num_written = min(fs->precision, string_len);
else
num_written = string_len;
num_pad = max(fs->width - num_written, 0);
if (fs->flags & FLAG_LEFT)
{
write_string(string, buf, bufsize, num_written);
write_space(buf, bufsize, num_pad);
}
else
{
write_space(buf, bufsize, num_pad);
write_string(string, buf, bufsize, num_written);
}
return num_written + num_pad;
}