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
This commit is contained in:
Dave Beazley 2006-12-19 03:49:17 +00:00
commit 79451aa1a7
4 changed files with 264 additions and 127 deletions

185
Doc/Devel/file.html Normal file
View file

@ -0,0 +1,185 @@
<html>
<head>
<title>SWIG File Handling</title>
</head>
<body>
<center>
<h1>SWIG File Handling</h1>
<p>
David M. Beazley <br>
dave-swig@dabeaz.com<br>
</b>
</center>
<h2>Introduction</h2>
This document describes the functions related to file and filename handling in the SWIG core. These functions are
defined in the header file <tt>Source/Swig/swigfile.h</tt>. This API is considered to be stable.
<h2>File Search Path</h2>
These functions manipulate the search path for locating files.
<p>
<b><tt>List *Swig_add_directory(const String_or_char *dirname)</tt></b>
<blockquote>
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.
</blockquote>
<p>
<b><tt>void Swig_push_directory(const String_or_char *dirname)</tt></b>
<blockquote>
Pushs a temporary directory onto the search path. This directory is searched before
directories added with <tt>Swig_add_directory()</tt> except when including a system
file explicitly (either using #include &lt;file&gt; or calling <tt>Swig_include_sys()</tt>).
This function is normally used by the preprocessor to add temporary directories when
processing #include statements.
</blockquote>
<p>
<b><tt>void Swig_pop_directory()</tt></b>
<blockquote>
Pops off the last pushed directory with <tt>Swig_push_directory()</tt>
</blockquote>
<p>
<b><tt>int Swig_get_push_dir()</tt></b>
<blockquote>
Returns a flag that indicates whether directory pushing is enabled or not.
</blockquote>
<p>
<b><tt>void Swig_set_push_dir(int dopush)</tt></b>
<blockquote>
Enables or disables directory pushing. By default, it is turned on. However, the <tt>-I-</tt> command line
option to SWIG disables it.
</blockquote>
<p>
<b><tt>List *Swig_search_path()</tt></b>
<blockquote>
Returns the current search path.
</blockquote>
<h2>File access functions</h2>
<p>
<b><tt>FILE *Swig_open(const String_or_char *name)</tt></b>
<blockquote>
Opens a file, using the applicable search paths, and returns an open <tt>FILE *</tt> object if found. Returns NULL if the file is not found.
</blockquote>
<p>
<b><tt>String *Swig_read_file(FILE *f)</tt></b>
<blockquote>
Reads all of the data from an open file into a string which is returned.
</blockquote>
<p>
<b><tt>String *Swig_include(const String_or_char *name)</tt></b>
<blockquote>
Searches for an include file <tt>name</tt> 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.
</blockquote>
<p>
<b><tt>String *Swig_include_sys(const String_or_char *name)</tt></b>
<blockquote>
Searches for an include file <tt>name</tt> 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 <tt>#include &lt;file&gt;</tt> in the preprocessor.
</blockquote>
<p>
<b><tt>int Swig_insert_file(const String_or_char *name, File *outfile)</tt></b>
<blockquote>
Searches for a file <tt>name</tt> and dumps its contents to <tt>outfile</tt> if found.
Returns 0 on sucesss, -1 if the file couldn't be found.
</blockquote>
<h2>Query functions</h2>
<p>
<b><tt>String *Swig_last_file()</tt></b>
<blockquote>
Returns the full pathname of the file last opened or included.
</blockquote>
<h2>Named files</h2>
<p>
<b><tt>void *Swig_register_filebyname(const String_or_char *filename, File *outfile)</tt></b>
<blockquote>
Registers a file object <tt>outfile</tt> with a specific name <tt>filename</tt>. 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.
</blockquote>
<p>
<b><tt>File *Swig_filebyname(const String_or_char *filename)</tt></b>
<blockquote>
This looks up a file object previously registered using <tt>Swig_register_filebyname()</tt>. This
is used to implement the %insert directive.
</blockquote>
<h2>Filename utilities</h2>
<p>
<b><tt>char *Swig_file_suffix(const String_or_char *filename)</tt></b>
<blockquote>
Returns the suffix of a filename. For instance, if the filename is "foo.txt", it returns ".txt".
</blockquote>
<p>
<b><tt>char *Swig_file_basename(const String_or_char *filename)</tt></b>
<blockquote>
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.
</blockquote>
<p>
<b><tt>char *Swig_file_filename(const String_or_char *filename)</tt></b>
<blockquote>
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.
</blockquote>
<p>
<b><tt>char *Swig_file_dirname(const String_or_char *filename)</tt></b>
<blockquote>
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.
</blockquote>
<p>
<b><tt>SWIG_FILE_DELIMETER</tt></b>
<blockquote>
This macro contains the file delimeter string for the local machine. On unix it is "/", on Windows it is "\\".
</blockquote>
</body>
</html>

View file

@ -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()
*

View file

@ -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 --- */

37
Source/Swig/swigfile.h Normal file
View file

@ -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