-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplateString.c
More file actions
95 lines (87 loc) · 2.59 KB
/
Copy pathtemplateString.c
File metadata and controls
95 lines (87 loc) · 2.59 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
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
93
94
95
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void render_template(const char *template, const char *title, const char *body)
{
char buffer[1024];
// replace template variables with printf-style format specifiers
snprintf(buffer, sizeof(buffer), template, title, body);
printf("%s", buffer);
}
// example of how to "maybe" parse the HTML and add for loop specifiers or boolean logic, and other logic
// i will add the new operators to the if below
// I will be missing modularity if I use prinft-style format specifiers, because with a template engine
// it can divided into "components" but how to create the idea of html component with string in c
// well you could "merge" strings with printf-style format specifiers? :thinking_face:
// mustache existe en c!!!! yay!!! - https://gitlab.com/jobol/mustach
void my_putchar(char c)
{
putchar(c); // Use the standard library function to print a character
}
// Custom printf function
void my_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
for (const char *p = format; *p != '\0'; p++)
{
if (*p == '%')
{
p++;
switch (*p)
{
case 'd':
{
int i = va_arg(args, int);
char buffer[20];
snprintf(buffer, sizeof(buffer), "%d", i);
for (char *b = buffer; *b != '\0'; b++)
{
my_putchar(*b);
}
break;
}
case 'c':
{
int c = va_arg(args, int);
my_putchar(c);
break;
}
case 's':
{
const char *s = va_arg(args, const char *);
while (*s)
{
my_putchar(*s++);
}
break;
}
case '%':
{
my_putchar('%');
break;
}
default:
{
my_putchar('%');
my_putchar(*p);
break;
}
}
}
else
{
my_putchar(*p);
}
}
va_end(args);
}
int main()
{
// the template contains printf-style format specifiers
const char *template = "<html><head><title>%s</title></head><body>%s</body></html>";
render_template(template, "Hello, World!", "<h1>Welcome to my website</h1>");
my_printf("\n");
my_printf("Hello, %s! You have %d new messages. Your grade is %c.\n", "Alice", 5, 'A');
return 0;
}