-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapi_template.c
70 lines (55 loc) · 1.17 KB
/
api_template.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
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include "apilib.h"
api_template_t api_template_create(const char *filename)
{
char *p, *s;
int fd;
struct stat statbuf;
fd = open(filename, O_RDONLY);
if(fd == -1) {
return NULL; // cannot open
}
if(fstat(fd, &statbuf) == -1) {
close(fd);
return NULL; // fstat error
}
if(!S_ISREG(statbuf.st_mode)) {
close(fd);
return NULL; // not a file
}
p = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
close(fd);
if(p == MAP_FAILED) {
return NULL;
}
s = strdup(p);
munmap(p, statbuf.st_size);
return s;
}
void api_template_set(api_template_t *tpl, const char *key, char *fmt, ...)
{
if(!tpl)
return;
va_list v;
char *new_string, *old_string;
va_start(v, fmt);
vasprintf(&new_string, fmt, v);
va_end(v);
// old_string = "<% key %>"
old_string = malloc(strlen(key) + 7);
memset(old_string, 0, strlen(key)+7);
sprintf(old_string, "<%% %s %%>", key);
*tpl = string_replace(*tpl, old_string, new_string);
free(new_string);
free(old_string);
}
void api_template_free(api_template_t tpl)
{
free(tpl);
}