-
Notifications
You must be signed in to change notification settings - Fork 4
/
format.c
64 lines (62 loc) · 1.97 KB
/
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
#include <string.h>
#include <stdio.h>
int format_replace(const char *input, const char *pattern,
const char *str, char *output, size_t len)
{
size_t slen = strlen(str);
size_t spat = strlen(pattern);
size_t sinp = strlen(input);
size_t req = sinp + slen - spat;
if (req < len) {
char *pos = strstr(input, pattern);
if (pos) {
size_t plen = pos - input;
// copy text after the pattern, leave room for str:
memmove(output + plen + slen, pos + spat,
1 + sinp - plen - spat);
// replace the pattern:
memmove(output + plen, str, slen);
if (plen > 0 && output != input) {
// copy text before the pattern:
memcpy(output, input, plen);
}
return 1;
}
}
return 0;
}
char *format_list(int argc, const char *argv[],
char *buffer, size_t len,
const char *two,
const char *start,
const char *middle,
const char *end)
{
if (argc == 1) {
size_t slen = strlen(argv[0]);
if (slen < len) {
memcpy(buffer, argv[0], slen + 1);
return buffer;
}
}
else if (argc == 2) {
if (format_replace(two, "{0}", argv[0], buffer, len)) {
if (format_replace(buffer, "{1}", argv[1], buffer, len)) {
return buffer;
}
}
}
else if (format_replace(start, "{0}", argv[0], buffer, len)) {
int i;
for (i = 1; i < argc - 2; ++i) {
if (!format_replace(buffer, "{1}", middle, buffer, len)) return NULL;
if (!format_replace(buffer, "{0}", argv[i], buffer, len)) return NULL;
}
if (!format_replace(buffer, "{1}", end, buffer, len)) return NULL;
if (!format_replace(buffer, "{0}", argv[argc - 2], buffer, len)) return NULL;
if (format_replace(buffer, "{1}", argv[argc - 1], buffer, len)) {
return buffer;
}
}
return NULL;
}