~ubuntu-branches/ubuntu/maverick/swig1.3/maverick

« back to all changes in this revision

Viewing changes to Source/Swig/include.c

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Landschoff
  • Date: 2002-03-29 01:56:07 UTC
  • Revision ID: james.westby@ubuntu.com-20020329015607-c0wt03xu8oy9ioj7
Tags: upstream-1.3.11
ImportĀ upstreamĀ versionĀ 1.3.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ----------------------------------------------------------------------------- 
 
2
 * include.c
 
3
 *
 
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
 
6
 *     are provided.
 
7
 * 
 
8
 * Author(s) : David Beazley (beazley@cs.uchicago.edu)
 
9
 *
 
10
 * Copyright (C) 1999-2000.  The University of Chicago
 
11
 * See the file LICENSE for information on usage and redistribution.    
 
12
 * ----------------------------------------------------------------------------- */
 
13
 
 
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 $";
 
15
 
 
16
#include "swig.h"
 
17
 
 
18
/* Delimeter used in accessing files and directories */
 
19
 
 
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 */
 
24
 
 
25
/* This function sets the name of the configuration file */
 
26
 
 
27
void Swig_set_config_file(const String_or_char *filename) {
 
28
  lang_config = NewString(filename);
 
29
}
 
30
 
 
31
String *Swig_get_config_file() {
 
32
  return lang_config;
 
33
}
 
34
 
 
35
 
 
36
/* -----------------------------------------------------------------------------
 
37
 * Swig_swiglib_set()
 
38
 * Swig_swiglib_get()
 
39
 *
 
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
 * ----------------------------------------------------------------------------- */
 
43
 
 
44
void
 
45
Swig_swiglib_set(const String_or_char *sl) {
 
46
  swiglib = NewString(sl);
 
47
}
 
48
 
 
49
String *
 
50
Swig_swiglib_get() {
 
51
  return swiglib;
 
52
}
 
53
 
 
54
/* -----------------------------------------------------------------------------
 
55
 * Swig_add_directory()
 
56
 *
 
57
 * Adds a directory to the SWIG search path.
 
58
 * ----------------------------------------------------------------------------- */
 
59
 
 
60
void 
 
61
Swig_add_directory(const String_or_char *dirname) {
 
62
  if (!directories) directories = NewList();
 
63
  assert(directories);
 
64
  if (!DohIsString(dirname)) {
 
65
    dirname = NewString((char *) dirname);
 
66
    assert(dirname);
 
67
  }
 
68
  Append(directories, dirname);
 
69
}
 
70
 
 
71
/* -----------------------------------------------------------------------------
 
72
 * Swig_last_file()
 
73
 * 
 
74
 * Returns the full pathname of the last file opened. 
 
75
 * ----------------------------------------------------------------------------- */
 
76
 
 
77
String *
 
78
Swig_last_file() {
 
79
  assert(lastpath);
 
80
  return lastpath;
 
81
}
 
82
 
 
83
/* -----------------------------------------------------------------------------
 
84
 * Swig_search_path() 
 
85
 * 
 
86
 * Returns a list of the current search paths.
 
87
 * ----------------------------------------------------------------------------- */
 
88
 
 
89
List *
 
90
Swig_search_path() {
 
91
  String *filename;
 
92
  String *dirname;
 
93
  List   *slist;
 
94
  int i;
 
95
 
 
96
  slist = NewList();
 
97
  assert(slist);
 
98
  filename = NewString("");
 
99
  assert(filename);
 
100
#ifdef MACSWIG
 
101
  Printf(filename,"%s",SWIG_FILE_DELIMETER);
 
102
#else
 
103
  Printf(filename,".%s", SWIG_FILE_DELIMETER);
 
104
#endif
 
105
  Append(slist,filename);
 
106
  for (i = 0; i < Len(directories); i++) {
 
107
    dirname =  Getitem(directories,i);
 
108
    filename = NewString("");
 
109
    assert(filename);
 
110
    Printf(filename, "%s%s", dirname, SWIG_FILE_DELIMETER);
 
111
    Append(slist,filename);
 
112
  }
 
113
  return slist;
 
114
}  
 
115
 
 
116
/* -----------------------------------------------------------------------------
 
117
 * Swig_open()
 
118
 *
 
119
 * Looks for a file and open it.  Returns an open  FILE * on success.
 
120
 * ----------------------------------------------------------------------------- */
 
121
 
 
122
FILE *
 
123
Swig_open(const String_or_char *name) {
 
124
  FILE        *f;
 
125
  String   *filename;
 
126
  List     *spath = 0;
 
127
  char        *cname;
 
128
  int          i;
 
129
 
 
130
  if (!directories) directories = NewList();
 
131
  assert(directories);
 
132
 
 
133
  cname = Char(name);
 
134
  filename = NewString(cname);
 
135
  assert(filename);
 
136
  f = fopen(Char(filename),"r");
 
137
  if (!f) {
 
138
      spath = Swig_search_path();
 
139
      for (i = 0; i < Len(spath); i++) {
 
140
          Clear(filename);
 
141
          Printf(filename,"%s%s", Getitem(spath,i), cname);
 
142
          f = fopen(Char(filename),"r");
 
143
          if (f) break;
 
144
      } 
 
145
      Delete(spath);
 
146
  }
 
147
  if (f) {
 
148
    Delete(lastpath);
 
149
    lastpath = Copy(filename);
 
150
  }
 
151
  Delete(filename);
 
152
  return f;
 
153
}
 
154
 
 
155
/* -----------------------------------------------------------------------------
 
156
 * Swig_read_file()
 
157
 * 
 
158
 * Reads data from an open FILE * and returns it as a string.
 
159
 * ----------------------------------------------------------------------------- */
 
160
 
 
161
String *
 
162
Swig_read_file(FILE *f) {
 
163
  char       buffer[4096];
 
164
  String *str = NewString("");
 
165
 
 
166
  assert(str);
 
167
  while (fgets(buffer,4095,f)) {
 
168
    Append(str,buffer);
 
169
  }
 
170
  Append(str,"\n");
 
171
  return str;
 
172
}
 
173
 
 
174
/* -----------------------------------------------------------------------------
 
175
 * Swig_include()
 
176
 *
 
177
 * Opens a file and returns it as a string.
 
178
 * ----------------------------------------------------------------------------- */
 
179
 
 
180
String *
 
181
Swig_include(const String_or_char *name) {
 
182
  FILE         *f;
 
183
  String    *str;
 
184
 
 
185
  f = Swig_open(name);
 
186
  if (!f) return 0;
 
187
  str = Swig_read_file(f);
 
188
  fclose(f);
 
189
  Seek(str,0,SEEK_SET);
 
190
  Setfile(str,lastpath);
 
191
  Setline(str,1);
 
192
  return str;
 
193
}
 
194
 
 
195
/* -----------------------------------------------------------------------------
 
196
 * Swig_insert_file()
 
197
 *
 
198
 * Copies the contents of a file into another file
 
199
 * ----------------------------------------------------------------------------- */
 
200
 
 
201
int
 
202
Swig_insert_file(const String_or_char *filename, File *outfile) {
 
203
  char buffer[4096];
 
204
  int  nbytes;
 
205
  FILE *f = Swig_open(filename);
 
206
 
 
207
  if (!f) return -1;
 
208
  while ((nbytes = Read(f,buffer,4096)) > 0) {
 
209
    Write(outfile,buffer,nbytes);
 
210
  }
 
211
  return 0;
 
212
}
 
213
 
 
214
/* -----------------------------------------------------------------------------
 
215
 * Swig_register_filebyname()
 
216
 *
 
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
 
220
 * in SWIG1.1
 
221
 * ----------------------------------------------------------------------------- */
 
222
 
 
223
static Hash *named_files = 0;
 
224
 
 
225
void
 
226
Swig_register_filebyname(const String_or_char *filename, File *outfile) {
 
227
  if (!named_files) named_files = NewHash();
 
228
  Setattr(named_files, filename, outfile);
 
229
}
 
230
 
 
231
/* -----------------------------------------------------------------------------
 
232
 * Swig_filebyname()
 
233
 *
 
234
 * Get a named file
 
235
 * ----------------------------------------------------------------------------- */
 
236
 
 
237
File *
 
238
Swig_filebyname(const String_or_char *filename) {
 
239
  if (!named_files) return 0;
 
240
  return Getattr(named_files,filename);
 
241
}
 
242
 
 
243
/* -----------------------------------------------------------------------------
 
244
 * Swig_file_suffix()
 
245
 *
 
246
 * Returns the suffix of a file
 
247
 * ----------------------------------------------------------------------------- */
 
248
 
 
249
char *
 
250
Swig_file_suffix(const String_or_char *filename) {
 
251
  char *d;
 
252
  char *c = Char(filename);
 
253
  if (strlen(c)) {
 
254
    d = c + strlen(filename) - 1;
 
255
    while (d != c) {
 
256
      if (*d == '.') return d;
 
257
      d--;
 
258
    }
 
259
    return c+strlen(filename);  
 
260
  }
 
261
  return c;
 
262
}
 
263
 
 
264
/* -----------------------------------------------------------------------------
 
265
 * Swig_file_basename()
 
266
 *
 
267
 * Returns the filename with no suffix attached.
 
268
 * ----------------------------------------------------------------------------- */
 
269
 
 
270
char *
 
271
Swig_file_basename(const String_or_char *filename)
 
272
{
 
273
  static char tmp[1024];
 
274
  char *c;
 
275
  strcpy(tmp,Char(filename));
 
276
  c = Swig_file_suffix(tmp);
 
277
  *c = 0;
 
278
  return tmp;
 
279
}
 
280
 
 
281
/* -----------------------------------------------------------------------------
 
282
 * Swig_file_dirname()
 
283
 *
 
284
 * Return the name of the directory associated with a file
 
285
 * ----------------------------------------------------------------------------- */
 
286
char *
 
287
Swig_file_dirname(const String_or_char *filename)
 
288
{
 
289
  static char tmp[1024];
 
290
  const char *delim = SWIG_FILE_DELIMETER;
 
291
  char *c;
 
292
  strcpy(tmp,Char(filename));
 
293
  if (!strstr(tmp,delim)) {
 
294
    return "";
 
295
  }
 
296
  c = tmp + strlen(tmp) -1;
 
297
  while (*c != *delim) c--;
 
298
  *(++c) = 0;
 
299
  return tmp;
 
300
}
 
301
      
 
302
    
 
303
    
 
304