From 79451aa1a708b0bd3bf4ec0f11ea7a1052a5f0a1 Mon Sep 17 00:00:00 2001 From: Dave Beazley Date: Tue, 19 Dec 2006 03:49:17 +0000 Subject: [PATCH] File management cleanup. Split API into separate header. Removed unused functions. Added documentation git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9622 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Doc/Devel/file.html | 185 +++++++++++++++++++++++++++++++++++++++++ Source/Swig/include.c | 137 +++++++++--------------------- Source/Swig/swig.h | 32 +------ Source/Swig/swigfile.h | 37 +++++++++ 4 files changed, 264 insertions(+), 127 deletions(-) create mode 100644 Doc/Devel/file.html create mode 100644 Source/Swig/swigfile.h diff --git a/Doc/Devel/file.html b/Doc/Devel/file.html new file mode 100644 index 000000000..86dcbb52a --- /dev/null +++ b/Doc/Devel/file.html @@ -0,0 +1,185 @@ + + +SWIG File Handling + + + +
+

SWIG File Handling

+ +

+David M. Beazley
+dave-swig@dabeaz.com
+ + +

+ +

Introduction

+ +This document describes the functions related to file and filename handling in the SWIG core. These functions are +defined in the header file Source/Swig/swigfile.h. This API is considered to be stable. + +

File Search Path

+ +These functions manipulate the search path for locating files. + +

+List *Swig_add_directory(const String_or_char *dirname) + +

+Adds a new directory to the system search path. The directory is appended to +the end of the search path. Returns a list containing the current +system search path. +
+ +

+void Swig_push_directory(const String_or_char *dirname) +

+Pushs a temporary directory onto the search path. This directory is searched before +directories added with Swig_add_directory() except when including a system +file explicitly (either using #include <file> or calling Swig_include_sys()). +This function is normally used by the preprocessor to add temporary directories when +processing #include statements. +
+ +

+void Swig_pop_directory() +

+Pops off the last pushed directory with Swig_push_directory() +
+ +

+int Swig_get_push_dir() + +

+Returns a flag that indicates whether directory pushing is enabled or not. +
+ +

+void Swig_set_push_dir(int dopush) +

+Enables or disables directory pushing. By default, it is turned on. However, the -I- command line +option to SWIG disables it. +
+ +

+List *Swig_search_path() +

+Returns the current search path. +
+ + +

File access functions

+ +

+FILE *Swig_open(const String_or_char *name) + +

+Opens a file, using the applicable search paths, and returns an open FILE * object if found. Returns NULL if the file is not found. +
+ +

+String *Swig_read_file(FILE *f) + +

+Reads all of the data from an open file into a string which is returned. +
+ +

+String *Swig_include(const String_or_char *name) + +

+Searches for an include file name and returns its contents as +a string if found. Returns NULL if not found. All of the applicable +search paths are searched when trying to locate the file. +
+ +

+String *Swig_include_sys(const String_or_char *name) + +

+Searches for an include file name and returns its contents as +a string if found. Returns NULL if not found. All of the applicable +search paths are searched when trying to locate the file, but +preference is given to system paths first. This mimics the behavior +of #include <file> in the preprocessor. +
+ +

+int Swig_insert_file(const String_or_char *name, File *outfile) + +

+Searches for a file name and dumps its contents to outfile if found. +Returns 0 on sucesss, -1 if the file couldn't be found. +
+ +

Query functions

+ +

+String *Swig_last_file() + +

+Returns the full pathname of the file last opened or included. +
+ +

Named files

+ +

+void *Swig_register_filebyname(const String_or_char *filename, File *outfile) + +

+Registers a file object outfile with a specific name filename. This function is +used to implement the SWIG %insert directive and to manage different sections of the output +file such as "runtime","header","wrapper","init", etc. Different language modules may add their own +sections for generating Python code, Perl code, etc. +
+ +

+File *Swig_filebyname(const String_or_char *filename) +

+This looks up a file object previously registered using Swig_register_filebyname(). This +is used to implement the %insert directive. +
+ +

Filename utilities

+ +

+char *Swig_file_suffix(const String_or_char *filename) +

+Returns the suffix of a filename. For instance, if the filename is "foo.txt", it returns ".txt". +
+ +

+char *Swig_file_basename(const String_or_char *filename) +

+Returns the filename without the suffix attached to it. For instance, if the filename is "foo.txt", it returns +"foo". The result is stored in a static variable. If you need to save it, make your own copy. +
+ +

+char *Swig_file_filename(const String_or_char *filename) +

+Returns the filename without any leading directories. For instance, if the filename is "/bar/spam/foo.txt", it +returns "foo.txt". This function is aware of local naming conventions on the machine (e.g., forward versus back slashes on Unix and Windows). The result is stored in a static variable. If you need to save the value, make a copy. +
+ +

+char *Swig_file_dirname(const String_or_char *filename) +

+Returns the directory name (if any). For instance, if the filename is "/bar/spam/foo.txt", it +returns "/bar/spam/". This function is aware of local naming conventions on the machine (e.g., forward versus back slashes on Unix and Windows). The result is stored in a static variable. If you need to save the value, make a copy. +
+ +

+SWIG_FILE_DELIMETER +

+This macro contains the file delimeter string for the local machine. On unix it is "/", on Windows it is "\\". +
+ + + + + + + + diff --git a/Source/Swig/include.c b/Source/Swig/include.c index 76878e3c4..9d522ab8b 100644 --- a/Source/Swig/include.c +++ b/Source/Swig/include.c @@ -12,15 +12,13 @@ char cvsroot_include_c[] = "$Id$"; #include "swig.h" -#include "swigkeys.h" /* Delimeter used in accessing files and directories */ -static List *directories = 0; /* List of include directories */ -static String *lastpath = 0; /* Last file that was included */ -static String *swiglib = 0; /* Location of SWIG library */ -static String *lang_config = 0; /* Language configuration file */ -static int dopush = 1; /* Whether to push directories */ +static List *directories = 0; /* List of include directories */ +static String *lastpath = 0; /* Last file that was included */ +static List *pdirectories = 0; /* List of pushed directories */ +static int dopush = 1; /* Whether to push directories */ /* This functions determine whether to push/pop dirs in the preprocessor */ void Swig_set_push_dir(int push) { @@ -31,34 +29,6 @@ int Swig_get_push_dir(void) { return dopush; } - -/* This function sets the name of the configuration file */ - -void Swig_set_config_file(const String_or_char *filename) { - lang_config = NewString(filename); -} - -String *Swig_get_config_file() { - return lang_config; -} - - -/* ----------------------------------------------------------------------------- - * Swig_swiglib_set() - * Swig_swiglib_get() - * - * Set the location of the SWIG library. This isn't really used, by the - * include mechanism, but rather as a query interface for language modules. - * ----------------------------------------------------------------------------- */ - -void Swig_swiglib_set(const String_or_char *sl) { - swiglib = NewString(sl); -} - -String *Swig_swiglib_get() { - return swiglib; -} - /* ----------------------------------------------------------------------------- * Swig_add_directory() * @@ -66,24 +36,18 @@ String *Swig_swiglib_get() { * ----------------------------------------------------------------------------- */ List *Swig_add_directory(const String_or_char *dirname) { + String *adirname; if (!directories) directories = NewList(); assert(directories); if (dirname) { - String *sdir = NewString(dirname); - Hash *dir = NewHash(); - assert(dir); - SetFlag(dir, k_sysdir); - Setattr(dir, k_name, sdir); - Append(directories, dir); - Delete(dir); - Delete(sdir); + adirname = NewString(dirname); + Append(directories,adirname); + Delete(adirname); } return directories; } - - /* ----------------------------------------------------------------------------- * Swig_push_directory() * @@ -92,23 +56,16 @@ List *Swig_add_directory(const String_or_char *dirname) { * ----------------------------------------------------------------------------- */ void Swig_push_directory(const String_or_char *dirname) { - String *tmp = 0; + String *pdirname; if (!Swig_get_push_dir()) return; - if (!directories) - directories = NewList(); - assert(directories); - if (!DohIsString(dirname)) { - dirname = tmp = NewString(dirname); - assert(dirname); - } - if (dirname) { - Hash *dir = NewHash(); - Setattr(dir, k_name, dirname); - Insert(directories, 0, dir); - if (tmp) - Delete(tmp); - } + if (!pdirectories) + pdirectories = NewList(); + assert(pdirectories); + pdirname = NewString(dirname); + assert(pdirname); + Insert(pdirectories,0,pdirname); + Delete(pdirname); } /* ----------------------------------------------------------------------------- @@ -121,9 +78,9 @@ void Swig_push_directory(const String_or_char *dirname) { void Swig_pop_directory() { if (!Swig_get_push_dir()) return; - if (!directories) + if (!pdirectories) return; - Delitem(directories, 0); + Delitem(pdirectories, 0); } /* ----------------------------------------------------------------------------- @@ -145,11 +102,9 @@ String *Swig_last_file() { static List *Swig_search_path_any(int syspath) { String *filename; - String *dirname; - List *slist, *llist; - int i, ilen; + List *slist; + int i, ilen; - llist = 0; slist = NewList(); assert(slist); filename = NewStringEmpty(); @@ -159,43 +114,33 @@ static List *Swig_search_path_any(int syspath) { #else Printf(filename, ".%s", SWIG_FILE_DELIMETER); #endif - if (syspath) { - llist = NewList(); - assert(llist); - Append(llist, filename); - } else { - Append(slist, filename); + Append(slist, filename); + Delete(filename); + + /* If there are any pushed directories. Add them first */ + if (pdirectories) { + ilen = Len(pdirectories); + for (i = 0; i < ilen; i++) { + filename = NewString(Getitem(pdirectories,i)); + StringAppend(filename,SWIG_FILE_DELIMETER); + Append(slist,filename); + Delete(filename); + } } + /* Add system directories next */ ilen = Len(directories); for (i = 0; i < ilen; i++) { - int issimple = 0; - dirname = Getitem(directories, i); - filename = NewStringEmpty(); - assert(filename); - if (DohIsString(dirname)) { - filename = Copy(dirname); - issimple = 1; + filename = NewString(Getitem(directories,i)); + StringAppend(filename,SWIG_FILE_DELIMETER); + if (syspath) { + /* If doing a system include, put the system directories first */ + Insert(slist,i,filename); } else { - filename = Copy(Getattr(dirname, k_name)); - } - StringAppend(filename, SWIG_FILE_DELIMETER); - - if (syspath && (issimple || !GetFlag(dirname, k_sysdir))) { - Append(llist, filename); - } else { - Append(slist, filename); - /* Insert(slist,0,filename); */ + /* Otherwise, just put the system directories after the pushed directories (if any) */ + Append(slist,filename); } Delete(filename); } - if (syspath) { - int ilen = Len(llist); - for (i = 0; i < ilen; i++) { - Append(slist, Getitem(llist, i)); - } - Delete(llist); - } - return slist; } @@ -278,7 +223,6 @@ String *Swig_read_file(FILE *f) { StringAppend(str, "\n"); } } - return str; } @@ -314,7 +258,6 @@ String *Swig_include_sys(const String_or_char *name) { return Swig_include_any(name, 1); } - /* ----------------------------------------------------------------------------- * Swig_insert_file() * diff --git a/Source/Swig/swig.h b/Source/Swig/swig.h index babf9a5f5..9cd6b7335 100644 --- a/Source/Swig/swig.h +++ b/Source/Swig/swig.h @@ -92,38 +92,10 @@ extern "C" { #define T_SYMBOL 98 #define T_ERROR 99 + /* --- File interface --- */ - extern List *Swig_add_directory(const String_or_char *dirname); - extern void Swig_push_directory(const String_or_char *dirname); - extern void Swig_pop_directory(); - extern String *Swig_last_file(); - extern List *Swig_search_path(); - extern FILE *Swig_open(const String_or_char *name); - extern String *Swig_read_file(FILE *f); - extern String *Swig_include(const String_or_char *name); - extern String *Swig_include_sys(const String_or_char *name); - extern int Swig_insert_file(const String_or_char *name, File *outfile); - extern void Swig_set_config_file(const String_or_char *filename); - extern String *Swig_get_config_file(void); - extern void Swig_set_push_dir(int dopush); - extern int Swig_get_push_dir(void); - extern void Swig_swiglib_set(const String_or_char *); - extern String *Swig_swiglib_get(); - extern void Swig_register_filebyname(const String_or_char *filename, File *outfile); - extern File *Swig_filebyname(const String_or_char *filename); - extern char *Swig_file_suffix(const String_or_char *filename); - extern char *Swig_file_basename(const String_or_char *filename); - extern char *Swig_file_filename(const String_or_char *filename); - extern char *Swig_file_dirname(const String_or_char *filename); - -#if defined(MACSWIG) -# define SWIG_FILE_DELIMETER ":" -#elif defined(_WIN32) -# define SWIG_FILE_DELIMETER "\\" -#else -# define SWIG_FILE_DELIMETER "/" -#endif +#include "swigfile.h" /* --- Command line parsing --- */ diff --git a/Source/Swig/swigfile.h b/Source/Swig/swigfile.h new file mode 100644 index 000000000..5bfd81615 --- /dev/null +++ b/Source/Swig/swigfile.h @@ -0,0 +1,37 @@ +/* ----------------------------------------------------------------------------- + * See the LICENSE file for information on copyright, usage and redistribution + * of SWIG, and the README file for authors - http://www.swig.org/release.html. + * + * swigfile.h + * + * File handling functions in the SWIG core + * ----------------------------------------------------------------------------- */ + +/* $Id: swig.h 9603 2006-12-05 21:47:01Z beazley $ */ + +extern List *Swig_add_directory(const String_or_char *dirname); +extern void Swig_push_directory(const String_or_char *dirname); +extern void Swig_pop_directory(); +extern String *Swig_last_file(); +extern List *Swig_search_path(); +extern FILE *Swig_open(const String_or_char *name); +extern String *Swig_read_file(FILE *f); +extern String *Swig_include(const String_or_char *name); +extern String *Swig_include_sys(const String_or_char *name); +extern int Swig_insert_file(const String_or_char *name, File *outfile); +extern void Swig_set_push_dir(int dopush); +extern int Swig_get_push_dir(void); +extern void Swig_register_filebyname(const String_or_char *filename, File *outfile); +extern File *Swig_filebyname(const String_or_char *filename); +extern char *Swig_file_suffix(const String_or_char *filename); +extern char *Swig_file_basename(const String_or_char *filename); +extern char *Swig_file_filename(const String_or_char *filename); +extern char *Swig_file_dirname(const String_or_char *filename); + +#if defined(MACSWIG) +# define SWIG_FILE_DELIMETER ":" +#elif defined(_WIN32) +# define SWIG_FILE_DELIMETER "\\" +#else +# define SWIG_FILE_DELIMETER "/" +#endif