-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_string.c
69 lines (59 loc) · 1.98 KB
/
format_string.c
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
62
63
64
65
66
67
68
69
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* format_string.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alischyn <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/22 19:22:42 by alischyn #+# #+# */
/* Updated: 2017/03/22 19:58:21 by alischyn ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void format_string_normal(t_fmt *fmt, char *string)
{
int len;
len = STRLEN(string);
if (fmt->has_precision && fmt->precision < len)
len = fmt->precision;
if (fmt->has_width && fmt->width > len && !fmt->f_minus)
APPEND_CHAR_N(fmt->f_zero ? '0' : ' ', fmt->width - len);
APPEND_STRING_N(string, len);
if (fmt->has_width && fmt->width > len && fmt->f_minus)
APPEND_CHAR_N(' ', fmt->width - len);
}
char *wstring_to_string(const wchar_t *wstring, int len)
{
char *string;
int i;
string = (char *)malloc(len + 1);
i = 0;
while (i < len)
{
string[i] = wstring[i];
i++;
}
string[i] = '\0';
return (string);
}
void format_wstring(t_fmt *fmt, va_list ap)
{
wchar_t *wstring;
char *string;
int len;
wstring = va_arg(ap, wchar_t *);
wstring = wstring ?: L"(null)";
len = 0;
while (wstring[len] != '\0')
len++;
string = wstring_to_string(wstring, len);
format_string_normal(fmt, string);
free(string);
}
void format_string(t_fmt *fmt, va_list ap)
{
char *string;
string = va_arg(ap, char *);
string = string ?: "(null)";
format_string_normal(fmt, string);
}