1
/* -----------------------------------------------------------------------------
4
* The functions in this file are used to manage files in the SWIG library.
5
* General purpose functions for opening, including, and retrieving pathnames
8
* Author(s) : David Beazley (beazley@cs.uchicago.edu)
10
* Copyright (C) 1999-2000. The University of Chicago
11
* See the file LICENSE for information on usage and redistribution.
12
* ----------------------------------------------------------------------------- */
14
static char cvsroot[] = "$Header: /cvs/projects/SWIG/Source/Swig/include.c,v 1.10.4.4 2002/01/07 04:08:51 beazley Exp $";
18
/* Delimeter used in accessing files and directories */
20
static List *directories = 0; /* List of include directories */
21
static String *lastpath = 0; /* Last file that was included */
22
static String *swiglib = 0; /* Location of SWIG library */
23
static String *lang_config = 0; /* Language configuration file */
25
/* This function sets the name of the configuration file */
27
void Swig_set_config_file(const String_or_char *filename) {
28
lang_config = NewString(filename);
31
String *Swig_get_config_file() {
36
/* -----------------------------------------------------------------------------
40
* Set the location of the SWIG library. This isn't really used, by the
41
* include mechanism, but rather as a query interface for language modules.
42
* ----------------------------------------------------------------------------- */
45
Swig_swiglib_set(const String_or_char *sl) {
46
swiglib = NewString(sl);
54
/* -----------------------------------------------------------------------------
55
* Swig_add_directory()
57
* Adds a directory to the SWIG search path.
58
* ----------------------------------------------------------------------------- */
61
Swig_add_directory(const String_or_char *dirname) {
62
if (!directories) directories = NewList();
64
if (!DohIsString(dirname)) {
65
dirname = NewString((char *) dirname);
68
Append(directories, dirname);
71
/* -----------------------------------------------------------------------------
74
* Returns the full pathname of the last file opened.
75
* ----------------------------------------------------------------------------- */
83
/* -----------------------------------------------------------------------------
86
* Returns a list of the current search paths.
87
* ----------------------------------------------------------------------------- */
98
filename = NewString("");
101
Printf(filename,"%s",SWIG_FILE_DELIMETER);
103
Printf(filename,".%s", SWIG_FILE_DELIMETER);
105
Append(slist,filename);
106
for (i = 0; i < Len(directories); i++) {
107
dirname = Getitem(directories,i);
108
filename = NewString("");
110
Printf(filename, "%s%s", dirname, SWIG_FILE_DELIMETER);
111
Append(slist,filename);
116
/* -----------------------------------------------------------------------------
119
* Looks for a file and open it. Returns an open FILE * on success.
120
* ----------------------------------------------------------------------------- */
123
Swig_open(const String_or_char *name) {
130
if (!directories) directories = NewList();
134
filename = NewString(cname);
136
f = fopen(Char(filename),"r");
138
spath = Swig_search_path();
139
for (i = 0; i < Len(spath); i++) {
141
Printf(filename,"%s%s", Getitem(spath,i), cname);
142
f = fopen(Char(filename),"r");
149
lastpath = Copy(filename);
155
/* -----------------------------------------------------------------------------
158
* Reads data from an open FILE * and returns it as a string.
159
* ----------------------------------------------------------------------------- */
162
Swig_read_file(FILE *f) {
164
String *str = NewString("");
167
while (fgets(buffer,4095,f)) {
174
/* -----------------------------------------------------------------------------
177
* Opens a file and returns it as a string.
178
* ----------------------------------------------------------------------------- */
181
Swig_include(const String_or_char *name) {
187
str = Swig_read_file(f);
189
Seek(str,0,SEEK_SET);
190
Setfile(str,lastpath);
195
/* -----------------------------------------------------------------------------
198
* Copies the contents of a file into another file
199
* ----------------------------------------------------------------------------- */
202
Swig_insert_file(const String_or_char *filename, File *outfile) {
205
FILE *f = Swig_open(filename);
208
while ((nbytes = Read(f,buffer,4096)) > 0) {
209
Write(outfile,buffer,nbytes);
214
/* -----------------------------------------------------------------------------
215
* Swig_register_filebyname()
217
* Register a "named" file with the core. Named files can become targets
218
* for %insert directives and other SWIG operations. This function takes
219
* the place of the f_header, f_wrapper, f_init, and other global variables
221
* ----------------------------------------------------------------------------- */
223
static Hash *named_files = 0;
226
Swig_register_filebyname(const String_or_char *filename, File *outfile) {
227
if (!named_files) named_files = NewHash();
228
Setattr(named_files, filename, outfile);
231
/* -----------------------------------------------------------------------------
235
* ----------------------------------------------------------------------------- */
238
Swig_filebyname(const String_or_char *filename) {
239
if (!named_files) return 0;
240
return Getattr(named_files,filename);
243
/* -----------------------------------------------------------------------------
246
* Returns the suffix of a file
247
* ----------------------------------------------------------------------------- */
250
Swig_file_suffix(const String_or_char *filename) {
252
char *c = Char(filename);
254
d = c + strlen(filename) - 1;
256
if (*d == '.') return d;
259
return c+strlen(filename);
264
/* -----------------------------------------------------------------------------
265
* Swig_file_basename()
267
* Returns the filename with no suffix attached.
268
* ----------------------------------------------------------------------------- */
271
Swig_file_basename(const String_or_char *filename)
273
static char tmp[1024];
275
strcpy(tmp,Char(filename));
276
c = Swig_file_suffix(tmp);
281
/* -----------------------------------------------------------------------------
282
* Swig_file_dirname()
284
* Return the name of the directory associated with a file
285
* ----------------------------------------------------------------------------- */
287
Swig_file_dirname(const String_or_char *filename)
289
static char tmp[1024];
290
const char *delim = SWIG_FILE_DELIMETER;
292
strcpy(tmp,Char(filename));
293
if (!strstr(tmp,delim)) {
296
c = tmp + strlen(tmp) -1;
297
while (*c != *delim) c--;