git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@103 626c5289-ae23-0410-ae9c-e8d60b6d4f22
207 lines
5.5 KiB
C
207 lines
5.5 KiB
C
/****************************************************************************
|
|
* Simplified Wrapper and Interface Generator (SWIG)
|
|
*
|
|
* Author : David Beazley
|
|
*
|
|
* Department of Computer Science
|
|
* University of Chicago
|
|
* 1100 E 58th Street
|
|
* Chicago, IL 60637
|
|
* beazley@cs.uchicago.edu
|
|
*
|
|
* Please read the file LICENSE for the copyright and terms by which SWIG
|
|
* can be used and distributed.
|
|
****************************************************************************/
|
|
|
|
static char cvsroot[] = "$Header$";
|
|
|
|
#include "swig.h"
|
|
|
|
/*******************************************************************************
|
|
* $Header$
|
|
*
|
|
* File : include.c
|
|
*
|
|
* Various file manipulation functions.
|
|
*******************************************************************************/
|
|
|
|
/* Delimeter used in accessing files and directories */
|
|
|
|
static DOH *directories = 0; /* List of include directories */
|
|
static DOH *libdir = 0; /* SWIG library directory */
|
|
static DOH *lastpath = 0; /* Last file that was included */
|
|
static int bytes_read = 0; /* Bytes read */
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
* void Swig_add_directory( DOH *dirname)
|
|
*
|
|
* Adds a directory to the SWIG search path.
|
|
* ----------------------------------------------------------------------------- */
|
|
|
|
void
|
|
Swig_add_directory(DOH *dirname)
|
|
{
|
|
if (!directories) directories = NewList();
|
|
assert(directories);
|
|
if (!String_check(dirname)) {
|
|
dirname = NewString((char *) dirname);
|
|
assert(dirname);
|
|
}
|
|
Append(directories, dirname);
|
|
}
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
* void Swig_set_library(DOH *libname)
|
|
*
|
|
* Sets the language specific library name like 'tcl', 'perl5', 'python', etc...
|
|
* ----------------------------------------------------------------------------- */
|
|
|
|
void
|
|
Swig_set_library(DOH *libname)
|
|
{
|
|
Delete(libdir);
|
|
if (!String_check(libname)) {
|
|
libname = NewString((char *) libname);
|
|
assert(libname);
|
|
}
|
|
libdir = libname;
|
|
}
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
* DOH *Swig_get_library()
|
|
*
|
|
* Gets the language specific name like 'tcl', 'perl5', etc...
|
|
* ----------------------------------------------------------------------------- */
|
|
DOH *
|
|
Swig_get_library() {
|
|
assert(libdir);
|
|
return libdir;
|
|
}
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
* DOH *Swig_last_file()
|
|
*
|
|
* Returns the full pathname of the last file opened.
|
|
* ----------------------------------------------------------------------------- */
|
|
|
|
DOH *
|
|
Swig_last_file() {
|
|
assert(lastpath);
|
|
return lastpath;
|
|
}
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
* DOH *Swig_search_path()
|
|
*
|
|
* Returns a list of the current search paths.
|
|
* ----------------------------------------------------------------------------- */
|
|
|
|
DOH *Swig_search_path()
|
|
{
|
|
DOH *filename;
|
|
DOH *dirname;
|
|
DOH *slist;
|
|
int i;
|
|
|
|
slist = NewList();
|
|
assert(slist);
|
|
filename = NewString("");
|
|
assert(filename);
|
|
Printf(filename,".%s", SWIG_FILE_DELIMETER);
|
|
Append(slist,filename);
|
|
for (i = 0; i < Len(directories); i++) {
|
|
dirname = Getitem(directories,i);
|
|
if (libdir) {
|
|
filename = NewString("");
|
|
assert(filename);
|
|
Printf(filename,"%s%s%s%s", dirname, SWIG_FILE_DELIMETER, libdir, SWIG_FILE_DELIMETER);
|
|
Append(slist,filename);
|
|
}
|
|
filename = NewString("");
|
|
assert(filename);
|
|
Printf(filename, "%s%s", dirname, SWIG_FILE_DELIMETER);
|
|
Append(slist,filename);
|
|
}
|
|
return slist;
|
|
}
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
* FILE *Swig_open( DOH *name)
|
|
*
|
|
* Looks for a file and open it.
|
|
* ----------------------------------------------------------------------------- */
|
|
|
|
FILE *
|
|
Swig_open(DOH *name)
|
|
{
|
|
FILE *f;
|
|
DOH *filename;
|
|
DOH *spath = 0;
|
|
char *cname;
|
|
int i;
|
|
|
|
if (!directories) directories = NewList();
|
|
assert(directories);
|
|
|
|
cname = Char(name);
|
|
filename = NewString(cname);
|
|
assert(filename);
|
|
f = fopen(Char(filename),"r");
|
|
if (!f) {
|
|
spath = Swig_search_path();
|
|
for (i = 0; i < Len(spath); i++) {
|
|
Clear(filename);
|
|
Printf(filename,"%s%s", Getitem(spath,i), cname);
|
|
f = fopen(Char(filename),"r");
|
|
if (f) break;
|
|
}
|
|
Delete(spath);
|
|
}
|
|
if (f) {
|
|
Delete(lastpath);
|
|
lastpath = Copy(filename);
|
|
}
|
|
Delete(filename);
|
|
return f;
|
|
}
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
* DOH *Swig_read_file(FILE *f)
|
|
*
|
|
* Reads data from f and returns as a new string
|
|
* ----------------------------------------------------------------------------- */
|
|
|
|
DOH *Swig_read_file(FILE *f) {
|
|
char buffer[4096];
|
|
DOH *str = NewString("");
|
|
assert(str);
|
|
while (fgets(buffer,4095,f)) {
|
|
Append(str,buffer);
|
|
}
|
|
Append(str,"\n");
|
|
return str;
|
|
}
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
* DOH *Swig_include(DOH *name)
|
|
*
|
|
* Open a file and return it as a string.
|
|
* ----------------------------------------------------------------------------- */
|
|
|
|
DOH *
|
|
Swig_include(DOH *name)
|
|
{
|
|
FILE *f;
|
|
DOH *str;
|
|
f = Swig_open(name);
|
|
if (!f) return 0;
|
|
str = Swig_read_file(f);
|
|
bytes_read = bytes_read + Len(str);
|
|
fclose(f);
|
|
Seek(str,0,SEEK_SET);
|
|
Setfile(str,lastpath);
|
|
Setline(str,1);
|
|
/* fprintf(stderr,"%d bytes read\n", bytes_read); */
|
|
return str;
|
|
}
|
|
|