Line data Source code
1 :
2 : /******************************************************************************
3 : * $Id$
4 : *
5 : * Project: GDAL/OGR Java bindings
6 : * Purpose: Add javadoc located in a special file into generated SWIG Java files
7 : * Author: Even Rouault <even dot rouault at spatialys.com>
8 : *
9 : *******************************************************************************
10 : * Copyright (c) 2009, Even Rouault <even dot rouault at spatialys.com>
11 : *
12 : * Permission is hereby granted, free of charge, to any person obtaining a
13 : * copy of this software and associated documentation files (the "Software"),
14 : * to deal in the Software without restriction, including without limitation
15 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 : * and/or sell copies of the Software, and to permit persons to whom the
17 : * Software is furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be included
20 : * in all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 : * DEALINGS IN THE SOFTWARE.
29 : *******************************************************************************/
30 :
31 : /* NOTE : this is really a quick and very dirty hack to put the javadoc contained */
32 : /* in a special formatted file, javadoc.java, into the SWIG generated java files */
33 : /* This program leaks memory and would crash easily on unexpected inputs */
34 :
35 : #include <stdio.h>
36 : #include <stdlib.h>
37 : #include <string.h>
38 :
39 : #define TRUE 1
40 : #define FALSE 0
41 :
42 : typedef struct
43 : {
44 : char* methodName;
45 : char* compactMethodName;
46 : char* javadoc;
47 : int bUsed;
48 : int bHide;
49 : } JavaDocInstance;
50 :
51 5242 : char* stripline(char* pszBuf)
52 : {
53 5242 : int i = 0;
54 10454 : while(pszBuf[i] == ' ' && pszBuf[i] != 0)
55 5212 : i ++;
56 5242 : memmove(pszBuf, pszBuf + i, strlen(pszBuf) - i + 1);
57 :
58 5242 : i = strlen(pszBuf) - 1;
59 13330 : while(i > 0 && (pszBuf[i] == '{' || pszBuf[i] == '\n' || pszBuf[i] == ' '))
60 : {
61 8088 : pszBuf[i] = 0;
62 8088 : i--;
63 : }
64 :
65 5242 : return pszBuf;
66 : }
67 :
68 : /* Remove argument names from function prototype, so that we used the */
69 : /* argument names from javadoc.java instead of the ones provided by default by the */
70 : /* bindings */
71 3498 : char* removeargnames(char* pszBuf)
72 : {
73 3498 : if (strstr(pszBuf, "="))
74 : {
75 701 : *strstr(pszBuf, "=") = 0;
76 701 : stripline(pszBuf);
77 : }
78 :
79 3498 : if (strstr(pszBuf, "(") == NULL)
80 846 : return pszBuf;
81 :
82 : //fprintf(stderr, "%s\n", pszBuf);
83 :
84 2652 : if (strstr(pszBuf, "{"))
85 : {
86 6 : *strstr(pszBuf, "{") = 0;
87 6 : stripline(pszBuf);
88 : }
89 : else
90 : {
91 2646 : int i = strstr(pszBuf, ")") - pszBuf - 1;
92 2646 : char* lastOK = strstr(pszBuf, ")");
93 2669 : while(i>0 && pszBuf[i] == ' ')
94 23 : i --;
95 46683 : while(i>0)
96 : {
97 46683 : if (pszBuf[i] == '(')
98 782 : break;
99 45901 : if (pszBuf[i] == ' ' || pszBuf[i] == ']')
100 : {
101 5723 : if (pszBuf[i] == ' ')
102 5723 : memmove(pszBuf + i + 1, lastOK, strlen(lastOK) + 1);
103 : else
104 0 : memmove(pszBuf + i, lastOK, strlen(lastOK) + 1);
105 48313 : while(pszBuf[i] != ',' && pszBuf[i] != '(')
106 42590 : i --;
107 5723 : if (pszBuf[i] == ',')
108 3859 : lastOK = pszBuf + i;
109 : else
110 1864 : break;
111 : }
112 44037 : i --;
113 : }
114 2646 : i = strstr(pszBuf, "(") - pszBuf;
115 54388 : while(pszBuf[i])
116 : {
117 51742 : if (pszBuf[i] == ' ')
118 9821 : memmove(pszBuf + i, pszBuf + i + 1, strlen(pszBuf + i + 1) + 1);
119 : else
120 41921 : i++;
121 : }
122 : }
123 : //fprintf(stderr, "after : %s\n", pszBuf);
124 2652 : return pszBuf;
125 : }
126 :
127 2693 : static void ignore_ret(char* x)
128 : {
129 : (void)x;
130 2693 : }
131 :
132 1 : int main(int argc, char* argv[])
133 : {
134 1 : const char* patch_filename = argv[1];
135 :
136 1 : FILE* fSrc = fopen(patch_filename, "rt");
137 1 : if( fSrc == NULL )
138 : {
139 0 : fprintf(stderr, "Cannot open %s\n", patch_filename);
140 0 : return 1;
141 : }
142 : FILE* fDst;
143 1 : JavaDocInstance* instances = (JavaDocInstance*)calloc(sizeof(JavaDocInstance), 3000);
144 1 : int nInstances = 0;
145 : char szLine[1024];
146 : char szClass[256];
147 : char javadoc[16384];
148 1 : szClass[0] = 0;
149 74 : while(fgets(szLine, 1023, fSrc))
150 : {
151 73 : if (strstr(szLine, "/**") == NULL) continue;
152 22 : begin:
153 888 : strcpy(javadoc, szLine);
154 9306 : while(fgets(szLine, 1023, fSrc))
155 : {
156 9306 : strcat(javadoc, szLine);
157 9306 : if (strstr(szLine, "*/"))
158 888 : break;
159 : }
160 2893 : while(fgets(szLine, 1023, fSrc))
161 : {
162 2892 : if (szLine[0] == 10)
163 1069 : continue;
164 1823 : else if (strstr(szLine, "*") == NULL)
165 : {
166 936 : instances[nInstances].javadoc = strdup(javadoc);
167 :
168 936 : char* pszLine = szLine;
169 936 : if (strncmp(pszLine, "@hide ", 6) == 0)
170 : {
171 41 : instances[nInstances].bHide = 1;
172 41 : pszLine += 6;
173 : }
174 : else
175 895 : instances[nInstances].bHide = 0;
176 :
177 936 : instances[nInstances].methodName = strdup(stripline(pszLine));
178 936 : instances[nInstances].compactMethodName = strdup(removeargnames(stripline(pszLine)));
179 936 : nInstances++;
180 : }
181 : else
182 887 : break;
183 : }
184 888 : if (strstr(szLine, "/**") != NULL)
185 866 : goto begin;
186 : }
187 : //fprintf(stderr, "nInstances=%d\n", nInstances);
188 1 : fclose(fSrc);
189 :
190 : int i;
191 90 : for(i=3;i<argc;i++)
192 : {
193 89 : if( strstr(argv[i], "AsyncReader.java") )
194 : {
195 0 : fprintf(stderr, "Skipping %s\n", argv[i]);
196 0 : continue;
197 : }
198 89 : fSrc = fopen(argv[i], "rt");
199 89 : if (fSrc == NULL)
200 : {
201 0 : fprintf(stderr, "Cannot open %s\n", argv[i]);
202 0 : continue;
203 : }
204 : char szDstName[1024];
205 89 : sprintf(szDstName, "%s/%s", argv[2], argv[i]);
206 89 : fDst = fopen(szDstName, "wt");
207 89 : if (fDst == NULL)
208 : {
209 0 : fprintf(stderr, "Cannot write %s\n", szDstName);
210 0 : continue;
211 : }
212 89 : szClass[0] = 0;
213 : char szPackage[256];
214 89 : szPackage[0] = 0;
215 :
216 12062 : while(fgets(szLine, 1023, fSrc))
217 : {
218 : char szMethodName[2048];
219 11973 : char* szOriLine = strdup(szLine);
220 11973 : if (strstr(szLine, "package"))
221 : {
222 89 : strcpy(szPackage, szLine);
223 : }
224 11884 : else if (strstr(szLine, "public class") || strstr(szLine, "public interface"))
225 : {
226 81 : strcpy(szClass, stripline(szLine));
227 81 : if (strstr(szClass, "extends"))
228 : {
229 11 : *strstr(szClass, "extends") = 0;
230 11 : stripline(szClass);
231 : }
232 81 : if (strstr(szClass, "implements"))
233 : {
234 9 : *strstr(szClass, "implements") = 0;
235 9 : stripline(szClass);
236 : }
237 81 : if (strstr(szLine, "Driver"))
238 : {
239 2 : if (strstr(szPackage, "org.gdal.gdal"))
240 1 : strcpy(szLine, "public class org.gdal.gdal.Driver");
241 : else
242 1 : strcpy(szLine, "public class org.gdal.ogr.Driver");
243 2 : strcpy(szClass, szLine);
244 : }
245 : }
246 11973 : if (strstr(szLine, "synchronized "))
247 : {
248 56 : char* c = strstr(szLine, "synchronized ");
249 56 : *c = 0;
250 56 : memmove(c, c + 13, strlen(c + 13) + 1);
251 : }
252 11973 : if (strstr(szLine, "public") && !strstr(szLine, "native"))
253 2649 : {
254 2649 : if (strchr(szLine, '(') && !strchr(szLine,')'))
255 : {
256 6 : strcpy(szMethodName, szLine);
257 : do
258 : {
259 6 : ignore_ret(fgets(szLine, 1023, fSrc));
260 6 : strcpy(szMethodName + strlen(szMethodName) - 1, szLine);
261 6 : } while (!strchr(szMethodName,')'));
262 6 : strcpy(szLine, szMethodName);
263 6 : free(szOriLine);
264 6 : szOriLine = strdup(szMethodName);
265 : //fprintf(stderr, "%s\n", szOriLine);
266 : }
267 2649 : if (strchr(szLine, '(') || strchr(szLine, '='))
268 2562 : sprintf(szMethodName, "%s:%s", szClass, removeargnames(stripline(szLine)));
269 : else
270 87 : strcpy(szMethodName, szClass);
271 : //fprintf(stderr, "%s\n", szMethodName);
272 : int j;
273 2052590 : for(j=0;j<nInstances;j++)
274 : {
275 2050860 : if (strcmp(instances[j].compactMethodName, szMethodName) == 0)
276 : {
277 924 : instances[j].bUsed = TRUE;
278 :
279 : //fprintf(stderr, "found match for %s\n", szMethodName);
280 924 : if (instances[j].bHide)
281 : {
282 39 : if (strstr(szLine, "final static") == NULL)
283 : {
284 : do
285 : {
286 74 : ignore_ret(fgets(szLine, 1023, fSrc));
287 74 : } while (!strchr(szLine,'}'));
288 : }
289 39 : break;
290 : }
291 :
292 885 : fprintf(fDst, "%s", instances[j].javadoc);
293 885 : if (strchr(szMethodName, '('))
294 : {
295 738 : fprintf(fDst, "%s;\n", strchr(instances[j].methodName, ':')+1);
296 738 : int nBrackets = 0;
297 738 : int bFoundOpen = FALSE;
298 738 : strcpy(szLine, szOriLine);
299 : do
300 : {
301 : int j;
302 127441 : for(j=0;szLine[j];j++)
303 : {
304 124828 : if (szLine[j] == '{')
305 : {
306 788 : bFoundOpen = TRUE;
307 788 : nBrackets ++;
308 : }
309 124040 : else if (szLine[j] == '}')
310 : {
311 788 : nBrackets --;
312 : }
313 : }
314 2613 : ignore_ret(fgets(szLine, 1023, fSrc));
315 2613 : } while(bFoundOpen == FALSE || nBrackets > 0);
316 : }
317 : else
318 147 : fprintf(fDst, "%s", szOriLine);
319 885 : break;
320 : }
321 : }
322 2649 : if (j == nInstances)
323 : {
324 1725 : if (strstr(szOriLine, "public") && (strstr(szOriLine, "getCPtr") || strstr(szOriLine, "long cPtr")))
325 21 : {
326 21 : char* c = strstr(szOriLine, "public");
327 21 : *c = 0;
328 21 : fprintf(fDst, "%s private %s", szOriLine, c + 6);
329 : }
330 : else
331 1704 : fprintf(fDst, "%s", szOriLine);
332 : }
333 : }
334 : else
335 9324 : fprintf(fDst, "%s", szOriLine);
336 11973 : free(szOriLine);
337 : }
338 :
339 89 : fclose(fSrc);
340 89 : fclose(fDst);
341 : }
342 :
343 : int j;
344 937 : for(j=0;j<nInstances;j++)
345 : {
346 936 : if (!instances[j].bUsed)
347 18 : fprintf(stderr, "WARNING: did not find occurrence of %s\n", instances[j].methodName);
348 : }
349 :
350 1 : return 0;
351 : }
|