]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wxSWIG/SWIG/include.cxx
1 /*******************************************************************************
2 * Simplified Wrapper and Interface Generator (SWIG)
4 * Author : David Beazley
6 * Department of Computer Science
7 * University of Chicago
10 * beazley@cs.uchicago.edu
12 * Please read the file LICENSE for the copyright and terms by which SWIG
13 * can be used and distributed.
14 *******************************************************************************/
21 /*******************************************************************************
26 * Code for including files into a wrapper file.
28 *******************************************************************************/
30 /* Delimeter used in accessing files and directories */
38 /* Linked list containing search directories */
48 /* Linked list containing included files */
55 Inode
*include_list
= 0;
57 // -----------------------------------------------------------------------------
58 // void add_directory(char *dirname)
60 // Adds a directory to the SWIG search path.
62 // Inputs : dirname = Pathname
66 // Side Effects : Adds dirname to linked list of pathnames.
67 // -----------------------------------------------------------------------------
69 void add_directory(char *dirname
) {
75 iz
.dirname
= new char[2];
81 d
->dirname
= new char[strlen(dirname
)+1];
82 strcpy(d
->dirname
,dirname
);
88 // -----------------------------------------------------------------------------
89 // int add_iname(char *name)
91 // Adds an include file to the list of processed files. If already present,
94 // Inputs : name = filename
96 // Output : 0 on success, 1 on failure.
98 // Side Effects : Adds name to linked list.
99 // -----------------------------------------------------------------------------
101 int add_iname(char *name
) {
105 // if (WrapExtern) return 0; // Still thinking about this patch.
110 if (strcmp(i
->name
, name
) == 0) return 1;
116 newi
->name
= new char[strlen(name
)+1];
117 strcpy(newi
->name
, name
);
118 newi
->next
= include_list
;
123 // -----------------------------------------------------------------------------
124 // void check_suffix(char *name)
126 // Checks the suffix of an include file to see if we need to handle it
127 // differently. C and C++ source files need a little extra help.
129 // Inputs : name = include file name.
134 // Sets ForceExtern status variable if a C/C++ source file
137 // -----------------------------------------------------------------------------
139 void check_suffix(char *name
) {
143 if (strlen(name
) == 0) return;
144 c
= name
+strlen(name
)-1;
146 if (*c
== '.') break;
149 if (c
== name
) return;
153 if (strcmp(c
,".c") == 0) {
155 } else if (strcmp(c
,".C") == 0) {
157 } else if (strcmp(c
,".cc") == 0) {
159 } else if (strcmp(c
,".cxx") == 0) {
161 } else if (strcmp(c
,".c++") == 0) {
163 } else if (strcmp(c
,".cpp") == 0) {
169 // -----------------------------------------------------------------------------
170 // int include_file(char *name)
172 // Includes a new SWIG wrapper file. Returns -1 if file not found.
174 // Inputs : name = filename
176 // Output : 0 on success. -1 on failure.
178 // Side Effects : sets scanner to read from new file.
179 // -----------------------------------------------------------------------------
181 int include_file(char *name
) {
187 extern void scanner_file(FILE *);
189 if (!include_init
) return -1; // Not initialized yet
190 if (add_iname(name
)) {
191 if (Verbose
) fprintf(stderr
,"file %s already included.\n", name
);
192 return -1; // Already included this file
196 fprintf(stderr
,"Wrapping %s...\n", name
);
197 fprintf(stderr
,"Looking for ./%s\n", name
);
200 if ((f
= fopen(name
,"r")) != NULL
) {
201 input_file
= new char[strlen(name
)+1];
202 strcpy(input_file
,name
);
208 // Now start searching libraries
210 d
= ihead
.next
; // Start of search list
212 // Look for the wrap file in language directory
213 sprintf(filename
,"%s%c%s%c%s", d
->dirname
,DELIMETER
,LibDir
,DELIMETER
,name
);
214 if (Verbose
) fprintf(stderr
,"Looking for %s\n", filename
);
215 if((f
= fopen(filename
,"r")) != NULL
) {
218 sprintf(filename
,"%s%c%s", d
->dirname
, DELIMETER
,name
);
219 if (Verbose
) fprintf(stderr
,"Looking for %s\n", filename
);
220 if ((f
= fopen(filename
,"r")) != NULL
) {
225 // Found it, open it up for input
226 input_file
= new char[strlen(filename
)+1];
227 strcpy(input_file
,filename
);
235 if (!found
) fprintf(stderr
,"%s : Line %d. Unable to find include file %s (ignored).\n",input_file
, line_number
, name
);
241 static char buffer
[1024];
243 // -----------------------------------------------------------------------------
244 // void copy_data(FILE *f1, FILE *f2)
246 // Copies data from file f1 to file f2.
248 // Inputs : f1 = FILE 1
253 // Side Effects : Closes file f1 upon exit.
254 // -----------------------------------------------------------------------------
256 void copy_data(FILE *f1
, FILE *f2
) {
258 while (fgets(buffer
,1023,f1
)) {
264 // -----------------------------------------------------------------------------
265 // void copy_data(FILE *f1, String *s2)
267 // Copies data from file f1 to String s2.
269 // Inputs : f1 = FILE 1
274 // Side Effects : Closes file f1 upon exit.
275 // -----------------------------------------------------------------------------
277 void copy_data(FILE *f1
, String
&s2
) {
279 while (fgets(buffer
,1023,f1
)) {
285 // -----------------------------------------------------------------------------
286 // int insert_file(char *name, FILE *f)
288 // Looks for a file and inserts into file f.
290 // Inputs : name = filename
293 // Output : 0 on success, -1 on failure.
295 // Side Effects : None
296 // -----------------------------------------------------------------------------
298 int insert_file(char *name
, FILE *f_out
) {
305 if (!include_init
) return -1; // Not initialized yet
306 if (add_iname(name
)) {
307 if (Verbose
) fprintf(stderr
,"file %s already included.\n", name
);
308 return -1; // Already included this file
311 if (Verbose
) fprintf(stderr
,"Looking for ./%s\n", name
);
312 if ((f
= fopen(name
,"r")) != NULL
) {
317 // Now start searching libraries
319 d
= ihead
.next
; // Start of search list
321 // Look for the wrap file in language directory
322 sprintf(filename
,"%s%c%s%c%s", d
->dirname
,DELIMETER
,LibDir
,DELIMETER
,name
);
323 if (Verbose
) fprintf(stderr
,"Looking for %s\n", filename
);
324 if((f
= fopen(filename
,"r")) != NULL
) {
327 sprintf(filename
,"%s%c%s", d
->dirname
, DELIMETER
, name
);
328 if (Verbose
) fprintf(stderr
,"Looking for %s\n", filename
);
329 if ((f
= fopen(filename
,"r")) != NULL
) {
340 if ((!found
) && (Verbose
)) fprintf(stderr
,"unable to find %s. (Ignored)\n",name
);
344 // -----------------------------------------------------------------------------
345 // void swig_append(char *filename, FILE *f)
347 // Appends the contents of filename to stream f.
350 // filename = File to append
355 // Side Effects : None
356 // -----------------------------------------------------------------------------
358 void swig_append(char *filename
, FILE *f
) {
362 if ((in_file
= fopen(filename
,"r")) == NULL
) {
363 fprintf(stderr
,"** SWIG ERROR ** file %s not found.\n",filename
);
367 while (fgets(buffer
,1023,in_file
)) {
374 // -----------------------------------------------------------------------------
375 // int get_file(char *name, String &str)
377 // Looks for a file and converts it into a String.
379 // Inputs : name = filename
382 // Output : 0 on success, -1 on failure.
384 // Side Effects : None
385 // -----------------------------------------------------------------------------
387 int get_file(char *name
, String
&str
) {
394 if (!include_init
) return -1; // Not initialized yet
396 if (Verbose
) fprintf(stderr
,"Looking for %s\n", name
);
397 if ((f
= fopen(name
,"r")) != NULL
) {
402 // Now start searching libraries
404 d
= ihead
.next
; // Start of search list
406 // Look for the wrap file in language directory
407 sprintf(filename
,"%s%c%s%c%s", d
->dirname
,DELIMETER
,LibDir
,DELIMETER
,name
);
408 if (Verbose
) fprintf(stderr
,"Looking for %s\n", filename
);
409 if((f
= fopen(filename
,"r")) != NULL
) {
412 sprintf(filename
,"%s%c%s", d
->dirname
, DELIMETER
, name
);
413 if (Verbose
) fprintf(stderr
,"Looking for %s\n", filename
);
414 if ((f
= fopen(filename
,"r")) != NULL
) {
425 if ((!found
)) fprintf(stderr
,"SWIG Error. Unable to find %s. Possible installation problem.\n",name
);
430 static char *libs
[1000];
431 static int nlibs
= 0;
433 // -----------------------------------------------------------------------------
434 // void library_add(char *name)
436 // Adds a filename to the list of libraries. This is usually only called by
437 // the SWIG main program.
439 // Inputs : name = library name
443 // Side Effects : Adds the library name to the libs array above
444 // -----------------------------------------------------------------------------
446 void library_add(char *name
) {
449 // Check to make sure it's not already added
451 if (!(*name
)) return;
453 for (i
= 0; i
< nlibs
; i
++) {
454 if (strcmp(libs
[i
],name
) == 0) return;
457 libs
[nlibs
] = copy_string(name
);
461 // -----------------------------------------------------------------------------
462 // void library_insert()
464 // Starts parsing all of the SWIG library files.
470 // Side Effects : Opens and attaches all of the specified library files to
473 // Bugs : Opens all of the files. Will fail if there are too many open files.
475 // -----------------------------------------------------------------------------
477 void library_insert() {
482 include_file(libs
[i
]);
487 // -----------------------------------------------------------------------------
488 // int checkout(char *filename,char *dest)
490 // Tries to check a file out of the SWIG library. If found, it will save it in
491 // the current directory. This is a useful mechanism for using SWIG as a code
492 // manager and for extracting library files.
494 // Inputs : filename = Library file
495 // dest = Destination file
497 // Output : 0 on success
500 // Side Effects : None
501 // -----------------------------------------------------------------------------
503 int checkout_file(char *filename
,char *dest
) {
508 // First check to see if the file already exists in current directory
509 f1
= fopen(dest
,"r");
512 fprintf(stderr
,"Warning. Unable to check-out %s. File already exists.\n", filename
);
518 sprintf(tempn
,"%s%d",dest
,rand());
519 f1
= fopen(tempn
,"r");
524 f1
= fopen(tempn
,"w");
526 fprintf(stderr
,"Unable to open %s for writing\n", tempn
);
532 // Now try to insert the library file into the destination file
533 if ((insert_file(filename
,f1
)) == -1) {
534 fprintf(stderr
,"Unable to check-out '%s'. File does not exist in SWIG library.\n",filename
);
536 remove(tempn
); // Unsuccessful, remove file we created
546 // -----------------------------------------------------------------------------
547 // int checkin_file(char *dir, char *lang, char *source,char *dest)
549 // Attempts to check a file into the SWIG library.
551 // Inputs : dir = Location of the SWIG library.
552 // lang = Language specific subdirectory.
553 // source = Source file.
554 // dest = Destination file.
556 // Output : 0 on success
559 // Side Effects : None
560 // -----------------------------------------------------------------------------
562 int checkin_file(char *dir
, char *lang
, char *source
, char *dest
) {
568 // First check to see if the file exists
570 f1
= fopen(source
,"r");
575 // Now try to open the destination file
576 sprintf(tempn
,"%s/%s/%s", dir
,lang
,dest
);
577 f1
= fopen(tempn
,"w");
579 fprintf(f1
,"%s",s
.get());