-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmake_cvector.py
155 lines (115 loc) · 3.34 KB
/
make_cvector.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import sys, os, glob, re
# In the middle of the night I get really lazy, even though I already had it hard coded
# https://stackoverflow.com/questions/1883980/find-the-nth-occurrence-of-substring-in-a-string
def find_nth(haystack, needle, n):
start = haystack.find(needle)
while start >= 0 and n > 1:
start = haystack.find(needle, start+len(needle))
n -= 1
return start
def get_header(filename):
header_text = open(filename).read()
# 3rd because 1st is the CVEC_SIZE_T #endif, 2nd is CVEC_SZ #endif
start = find_nth(header_text, "#endif", 3) + 6 # #endif of extern "C"
end = header_text.find("#ifdef", start) #ifdef close of extern "C"
return header_text[start:end]
def get_c_file(filename):
c_text = open(filename).read()
return c_text[c_text.find("cvec_sz"):]
cvector_str = """
/* header starts */
#ifndef CVECTOR_H
#define CVECTOR_H
#if defined(CVEC_ONLY_INT) || defined(CVEC_ONLY_DOUBLE) || defined(CVEC_ONLY_STR) \
|| defined(CVEC_ONLY_VOID)
#ifndef CVEC_ONLY_INT
#define CVEC_NO_INT
#endif
#ifndef CVEC_ONLY_DOUBLE
#define CVEC_NO_DOUBLE
#endif
#ifndef CVEC_ONLY_STR
#define CVEC_NO_STR
#endif
#ifndef CVEC_ONLY_VOID
#define CVEC_NO_VOID
#endif
#endif
#if defined(CVEC_MALLOC) && defined(CVEC_FREE) && defined(CVEC_REALLOC)
/* ok */
#elif !defined(CVEC_MALLOC) && !defined(CVEC_FREE) && !defined(CVEC_REALLOC)
/* ok */
#else
#error "Must define all or none of CVEC_MALLOC, CVEC_FREE, and CVEC_REALLOC."
#endif
#ifndef CVEC_MALLOC
#include <stdlib.h>
#define CVEC_MALLOC(sz) malloc(sz)
#define CVEC_REALLOC(p, sz) realloc(p, sz)
#define CVEC_FREE(p) free(p)
#endif
#ifndef CVEC_MEMMOVE
#include <string.h>
#define CVEC_MEMMOVE(dst, src, sz) memmove(dst, src, sz)
#endif
#ifndef CVEC_ASSERT
#include <assert.h>
#define CVEC_ASSERT(x) assert(x)
#endif
#ifndef CVEC_SIZE_T
#include <stdlib.h>
#define CVEC_SIZE_T size_t
#endif
#ifndef CVEC_SZ
#define CVEC_SZ
typedef CVEC_SIZE_T cvec_sz;
#endif
#ifdef __cplusplus
extern "C" {
#endif
"""
cvector_str += "#ifndef CVEC_NO_INT\n\n"
cvector_str += get_header("cvector_i.h")
cvector_str += "#endif\n\n"
cvector_str += "#ifndef CVEC_NO_DOUBLE\n\n"
cvector_str += get_header("cvector_d.h")
cvector_str += "#endif\n\n"
cvector_str += "#ifndef CVEC_NO_STR\n\n"
cvector_str += get_header("cvector_str.h")
cvector_str += "#endif\n\n"
cvector_str += "#ifndef CVEC_NO_VOID\n\n"
cvector_str += get_header("cvector_void.h")
cvector_str += "#endif\n\n"
cvector_str += """
#ifdef __cplusplus
}
#endif
"""
c_text = open("cvector_macro.h").read()
cvector_str += c_text[c_text.find("#define CVEC_NEW_DECLS(TYPE)"):c_text.rfind("#endif")]
cvector_str += """
/* header ends */
#endif
#ifdef CVECTOR_IMPLEMENTATION
"""
cvector_str += "#ifndef CVEC_NO_INT\n\n"
cvector_str += get_c_file("cvector_i.c")
cvector_str += "#endif\n\n"
cvector_str += "#ifndef CVEC_NO_DOUBLE\n\n"
cvector_str += get_c_file("cvector_d.c")
cvector_str += "#endif\n\n"
cvector_str += "#ifndef CVEC_NO_STR\n\n"
cvector_str += get_c_file("cvector_str.c")
cvector_str += "#endif\n\n"
cvector_str += "#ifndef CVEC_NO_VOID\n\n"
cvector_str += get_c_file("cvector_void.c")
cvector_str += "#endif\n\n"
cvector_str += """
#endif
"""
cvector_h = open("cvector.h", "w")
cvector_h.write("/*\n")
cvector_h.write(open("header_docs.txt").read())
cvector_h.write("*/\n")
cvector_h.write(cvector_str)
cvector_h.close()