-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr_format.c
92 lines (83 loc) · 2.65 KB
/
str_format.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "shell.h"
/**
* Format a string with %s, %d, %c, %f, and %x specifiers.
* @param str The buffer to store the formatted string.
* @param size The size of the buffer.
* @param format The format string.
* @param args Additional arguments to be formatted.
* @return The number of characters written (excluding null byte).
*/
int str_format(char *str, size_t size, const char *format, void *args) {
size_t written = 0;
const char *src = format;
char *dest = str;
size_t i = 0;
char *s;
int d;
char c;
double f;
unsigned int x;
int frac;
for (src = format; *src != '\0' && written < size - 1; src++) {
if (*src == '%' && *(src + 1) != '\0') {
src++;
switch (*src) {
case 's':
s = (char *)args;
while (*s != '\0' && written < size - 1) {
*dest++ = *s++;
written++;
}
break;
case 'd':
d = *((int *)args);
while (d != 0 && written < size - 1) {
*dest++ = '0' + d % 10;
d /= 10;
written++;
}
break;
case 'c':
c = *((int *)args);
*dest++ = c;
written++;
break;
case 'f':
f = *((double *)args);
d = (int)f;
while (d != 0 && written < size - 1) {
*dest++ = '0' + d % 10;
d /= 10;
written++;
}
if (written < size - 1) {
*dest++ = '.';
written++;
}
frac = (int)((f - (int)f) * 1000000);
while (frac != 0 && written < size - 1) {
*dest++ = '0' + frac % 10;
frac /= 10;
written++;
}
break;
case 'x':
x = *((unsigned int *)args);
while (x != 0 && written < size - 1) {
*dest++ = "0123456789abcdef"[x % 16];
x /= 16;
written++;
}
break;
default:
break;
}
i++;
} else {
*dest++ = *src;
written++;
}
}
*dest = '\0';
return written;
}