1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3 * Copyright 2012 Couchbase, Inc
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 #include "config.h"
18 #include <algorithm>
19 #include <cerrno>
20 #include <cstdlib>
21 #include <string.h>
22 #include <strings.h>
23 #include <cerrno>
24 #include <getopt.h>
25 #include <sys/stat.h>
26 #include <iostream>
27 #include <fstream>
28 #include <vector>
29 #include <string>
30 #include <ctype.h>
31
32 #include "cJSON.h"
33
34 using namespace std;
35
36 /**
37 * Print the JSON object quoted
38 */
operator <<(ostream &out, cJSON *json)39 static ostream& operator <<(ostream &out, cJSON *json)
40 {
41 char *data = cJSON_PrintUnformatted(json);
42 int ii = 0;
43
44 out << '"';
45 while (data[ii] != '\0') {
46 if (data[ii] == '"') {
47 out << '\\';
48 }
49 out << data[ii];
50 ++ii;
51 }
52
53 out << '"';
54 free(data);
55 return out;
56 }
57
usage(void)58 static void usage(void)
59 {
60 cerr << "Usage: gencode -j JSON -c cfile -h headerfile -f function"
61 << endl
62 << "\tThe JSON file will be read to generate the c and h file."
63 << endl;
64 exit(EXIT_FAILURE);
65 }
66
main(int argc, char **argv)67 int main(int argc, char **argv) {
68 int cmd;
69 const char *json = NULL;
70 const char *hfile = NULL;
71 const char *cfile = NULL;
72 const char *function = NULL;
73
74 while ((cmd = getopt(argc, argv, "j:c:h:f:")) != -1) {
75 switch (cmd) {
76 case 'j' :
77 json = optarg;
78 break;
79 case 'c' :
80 cfile = optarg;
81 break;
82 case 'h':
83 hfile = optarg;
84 break;
85 case 'f':
86 function = optarg;
87 break;
88 default:
89 usage();
90 }
91 }
92
93 if (json == NULL || hfile == NULL || cfile == NULL || function == NULL) {
94 usage();
95 }
96
97 struct stat st;
98 if (stat(json, &st) == -1) {
99 cerr << "Failed to look up \"" << json << "\": "
100 << strerror(errno) << endl;
101 exit(EXIT_FAILURE);
102 }
103
104 char *data = new char[st.st_size + 1];
105 data[st.st_size] = 0;
106 ifstream input(json);
107 input.read(data, st.st_size);
108 input.close();
109
110 cJSON *c = cJSON_Parse(data);
111 if (c == NULL) {
112 cerr << "Failed to parse JSON.. probably syntax error" << endl;
113 exit(EXIT_FAILURE);
114 }
115
116 ofstream headerfile(hfile);
117
118 std::string macro(hfile);
119 std::replace(macro.begin(), macro.end(), ' ', '_');
120 std::replace(macro.begin(), macro.end(), '/', '_');
121 std::replace(macro.begin(), macro.end(), '-', '_');
122 std::replace(macro.begin(), macro.end(), '.', '_');
123 std::replace(macro.begin(), macro.end(), ':', '_');
124 std::transform(macro.begin(), macro.end(), macro.begin(), ::toupper);
125
126 headerfile
127 << "/*" << endl
128 << " * Copyright 2014 Couchbase, Inc" << endl
129 << " *" << endl
130 << " * Licensed under the Apache License, Version 2.0 (the \"License\");" << endl
131 << " * you may not use this file except in compliance with the License." << endl
132 << " * You may obtain a copy of the License at" << endl
133 << " *" << endl
134 << " * http://www.apache.org/licenses/LICENSE-2.0" << endl
135 << " *" << endl
136 << " * Unless required by applicable law or agreed to in writing, software" << endl
137 << " * distributed under the License is distributed on an \"AS IS\" BASIS," << endl
138 << " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied." << endl
139 << " * See the License for the specific language governing permissions and" << endl
140 << " * limitations under the License." << endl
141 << " */" << endl
142 << endl
143 << "/********************************" << endl
144 << "** Generated file, do not edit **" << endl
145 << "*********************************/" << endl
146 << "#ifndef " << macro << endl
147 << "#define " << macro << endl
148 << endl
149 << "#include \"config.h\"" << endl
150 << endl
151 << "#ifdef __cplusplus" << endl
152 << "extern \"C\" {" << endl
153 << "#endif" << endl
154 << endl
155 << "const char *" << function << "(void);" << endl
156 << endl
157 << "#ifdef __cplusplus" << endl
158 << "}" << endl
159 << "#endif" << endl
160 << "#endif /* " << macro << "*/" << endl;
161 headerfile.close();
162
163 ofstream sourcefile(cfile);
164 sourcefile
165 << "/*" << endl
166 << " * Copyright 2014 Couchbase, Inc" << endl
167 << " *" << endl
168 << " * Licensed under the Apache License, Version 2.0 (the \"License\");" << endl
169 << " * you may not use this file except in compliance with the License." << endl
170 << " * You may obtain a copy of the License at" << endl
171 << " *" << endl
172 << " * http://www.apache.org/licenses/LICENSE-2.0" << endl
173 << " *" << endl
174 << " * Unless required by applicable law or agreed to in writing, software" << endl
175 << " * distributed under the License is distributed on an \"AS IS\" BASIS," << endl
176 << " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied." << endl
177 << " * See the License for the specific language governing permissions and" << endl
178 << " * limitations under the License." << endl
179 << " */" << endl
180 << endl
181 << "/********************************" << endl
182 << "** Generated file, do not edit **" << endl
183 << "*********************************/" << endl
184 << "#include \"config.h\"" << endl
185 << "#include \"" << hfile << "\"" << endl
186 << endl
187 << "const char *" << function << "(void)" << endl
188 << "{" << endl
189 << " return " << c << ";" << endl
190 << "}" << endl;
191
192 cJSON_Delete(c);
193
194 return 0;
195 }
196