1 /* -----------------------------------------------------------------------------
2 * See the LICENSE file for information on copyright, usage and redistribution
3 * of SWIG, and the README file for authors - http://www.swig.org/release.html.
7 * The functions in this file are used to manage files in the SWIG library.
8 * General purpose functions for opening, including, and retrieving pathnames
10 * ----------------------------------------------------------------------------- */
12 char cvsroot_include_c[] = "$Id: include.c 11080 2009-01-24 13:15:51Z bhy $";
16 static List *directories = 0; /* List of include directories */
17 static String *lastpath = 0; /* Last file that was included */
18 static List *pdirectories = 0; /* List of pushed directories */
19 static int dopush = 1; /* Whether to push directories */
21 /* This functions determine whether to push/pop dirs in the preprocessor */
22 void Swig_set_push_dir(int push) {
26 int Swig_get_push_dir(void) {
30 /* -----------------------------------------------------------------------------
31 * Swig_add_directory()
33 * Adds a directory to the SWIG search path.
34 * ----------------------------------------------------------------------------- */
36 List *Swig_add_directory(const_String_or_char_ptr dirname) {
39 directories = NewList();
42 adirname = NewString(dirname);
43 Append(directories,adirname);
49 /* -----------------------------------------------------------------------------
50 * Swig_push_directory()
52 * Inserts a directory at the front of the SWIG search path. This is used by
53 * the preprocessor to grab files in the same directory as other included files.
54 * ----------------------------------------------------------------------------- */
56 void Swig_push_directory(const_String_or_char_ptr dirname) {
58 if (!Swig_get_push_dir())
61 pdirectories = NewList();
63 pdirname = NewString(dirname);
65 Insert(pdirectories,0,pdirname);
69 /* -----------------------------------------------------------------------------
70 * Swig_pop_directory()
72 * Pops a directory off the front of the SWIG search path. This is used by
74 * ----------------------------------------------------------------------------- */
76 void Swig_pop_directory(void) {
77 if (!Swig_get_push_dir())
81 Delitem(pdirectories, 0);
84 /* -----------------------------------------------------------------------------
87 * Returns the full pathname of the last file opened.
88 * ----------------------------------------------------------------------------- */
90 String *Swig_last_file(void) {
95 /* -----------------------------------------------------------------------------
96 * Swig_search_path_any()
98 * Returns a list of the current search paths.
99 * ----------------------------------------------------------------------------- */
101 static List *Swig_search_path_any(int syspath) {
108 filename = NewStringEmpty();
111 Printf(filename, "%s", SWIG_FILE_DELIMITER);
113 Printf(filename, ".%s", SWIG_FILE_DELIMITER);
115 Append(slist, filename);
118 /* If there are any pushed directories. Add them first */
120 ilen = Len(pdirectories);
121 for (i = 0; i < ilen; i++) {
122 filename = NewString(Getitem(pdirectories,i));
123 Append(filename,SWIG_FILE_DELIMITER);
124 Append(slist,filename);
128 /* Add system directories next */
129 ilen = Len(directories);
130 for (i = 0; i < ilen; i++) {
131 filename = NewString(Getitem(directories,i));
132 Append(filename,SWIG_FILE_DELIMITER);
134 /* If doing a system include, put the system directories first */
135 Insert(slist,i,filename);
137 /* Otherwise, just put the system directories after the pushed directories (if any) */
138 Append(slist,filename);
145 List *Swig_search_path() {
146 return Swig_search_path_any(0);
151 /* -----------------------------------------------------------------------------
154 * open a file, optionally looking for it in the include path. Returns an open
156 * ----------------------------------------------------------------------------- */
158 static FILE *Swig_open_file(const_String_or_char_ptr name, int sysfile, int use_include_path) {
166 directories = NewList();
170 filename = NewString(cname);
172 f = fopen(Char(filename), "r");
173 if (!f && use_include_path) {
174 spath = Swig_search_path_any(sysfile);
176 for (i = 0; i < ilen; i++) {
178 Printf(filename, "%s%s", Getitem(spath, i), cname);
179 f = fopen(Char(filename), "r");
187 lastpath = Swig_filename_escape(filename);
193 /* Open a file - searching the include paths to find it */
194 FILE *Swig_include_open(const_String_or_char_ptr name) {
195 return Swig_open_file(name, 0, 1);
198 /* Open a file - does not use include paths to find it */
199 FILE *Swig_open(const_String_or_char_ptr name) {
200 return Swig_open_file(name, 0, 0);
205 /* -----------------------------------------------------------------------------
208 * Reads data from an open FILE * and returns it as a string.
209 * ----------------------------------------------------------------------------- */
211 String *Swig_read_file(FILE *f) {
214 String *str = NewStringEmpty();
217 while (fgets(buffer, 4095, f)) {
222 char *cstr = Char(str);
223 if (cstr[len - 1] != '\n') {
230 /* -----------------------------------------------------------------------------
233 * Opens a file and returns it as a string.
234 * ----------------------------------------------------------------------------- */
236 static String *Swig_include_any(const_String_or_char_ptr name, int sysfile) {
241 f = Swig_open_file(name, sysfile, 1);
244 str = Swig_read_file(f);
246 Seek(str, 0, SEEK_SET);
247 file = Copy(lastpath);
254 String *Swig_include(const_String_or_char_ptr name) {
255 return Swig_include_any(name, 0);
258 String *Swig_include_sys(const_String_or_char_ptr name) {
259 return Swig_include_any(name, 1);
262 /* -----------------------------------------------------------------------------
265 * Copies the contents of a file into another file
266 * ----------------------------------------------------------------------------- */
268 int Swig_insert_file(const_String_or_char_ptr filename, File *outfile) {
271 FILE *f = Swig_include_open(filename);
275 while ((nbytes = Read(f, buffer, 4096)) > 0) {
276 Write(outfile, buffer, nbytes);
281 /* -----------------------------------------------------------------------------
282 * Swig_register_filebyname()
284 * Register a "named" file with the core. Named files can become targets
285 * for %insert directives and other SWIG operations. This function takes
286 * the place of the f_header, f_wrapper, f_init, and other global variables
288 * ----------------------------------------------------------------------------- */
290 static Hash *named_files = 0;
292 void Swig_register_filebyname(const_String_or_char_ptr filename, File *outfile) {
294 named_files = NewHash();
295 Setattr(named_files, filename, outfile);
298 /* -----------------------------------------------------------------------------
302 * ----------------------------------------------------------------------------- */
304 File *Swig_filebyname(const_String_or_char_ptr filename) {
307 return Getattr(named_files, filename);
310 /* -----------------------------------------------------------------------------
313 * Returns the suffix of a file
314 * ----------------------------------------------------------------------------- */
316 char *Swig_file_suffix(const_String_or_char_ptr filename) {
318 char *c = Char(filename);
319 int len = Len(filename);
332 /* -----------------------------------------------------------------------------
333 * Swig_file_basename()
335 * Returns the filename with no suffix attached.
336 * ----------------------------------------------------------------------------- */
338 char *Swig_file_basename(const_String_or_char_ptr filename) {
339 static char tmp[1024];
341 strcpy(tmp, Char(filename));
342 c = Swig_file_suffix(tmp);
347 /* -----------------------------------------------------------------------------
348 * Swig_file_filename()
350 * Return the file with any leading path stripped off
351 * ----------------------------------------------------------------------------- */
352 char *Swig_file_filename(const_String_or_char_ptr filename) {
353 static char tmp[1024];
354 const char *delim = SWIG_FILE_DELIMITER;
357 strcpy(tmp, Char(filename));
358 c = strrchr(tmp, *delim);
365 /* -----------------------------------------------------------------------------
366 * Swig_file_dirname()
368 * Return the name of the directory associated with a file
369 * ----------------------------------------------------------------------------- */
370 char *Swig_file_dirname(const_String_or_char_ptr filename) {
371 static char tmp[1024];
372 const char *delim = SWIG_FILE_DELIMITER;
374 strcpy(tmp, Char(filename));
375 if (!strstr(tmp, delim)) {
378 c = tmp + strlen(tmp) - 1;