FAUST compiler  0.9.9.6b8
Classes | Macros | Functions | Variables
enrobage.cpp File Reference
#include "enrobage.hh"
#include <vector>
#include <list>
#include <set>
#include <string>
#include <ctype.h>
#include <limits.h>
#include <stdlib.h>
#include "compatibility.hh"
#include "sourcefetcher.hh"
#include <errno.h>
#include <climits>
Include dependency graph for enrobage.cpp:

Go to the source code of this file.

Classes

class  myparser
 A minimalistic parser used to recognize '#include <faust/...>' patterns when copying architecture files. More...
 

Macros

#define TRY_OPEN(filename)
 
#define DIR_SEPARATOR   '/'
 filebasename returns the basename of a path. More...
 
#define IS_DIR_SEPARATOR(ch)   ((ch) == DIR_SEPARATOR)
 

Functions

static bool isBlank (const string &s)
 Returns true is a line is blank (contains only white caracters) More...
 
static string & replaceOccurences (string &str, const string &oldstr, const string &newstr)
 Replace every occurrence of oldstr by newstr inside str. More...
 
static string & replaceClassName (string &str)
 Used when copying architecture files to replace default mydsp class name with the user specified one. More...
 
void streamCopyLicense (istream &src, ostream &dst, const string &exceptiontag)
 Copy or remove license header. More...
 
bool isFaustInclude (const string &s, string &fname)
 True if string s match '#include <faust/fname>'. More...
 
void inject (ostream &dst, const string fname)
 
void streamCopyUntil (istream &src, ostream &dst, const string &until)
 Copy src to dst until specific line. More...
 
void streamCopy (istream &src, ostream &dst)
 Copy src to dst. More...
 
void streamCopyUntilEnd (istream &src, ostream &dst)
 Copy src to dst until end. More...
 
ifstream * open_arch_stream (const char *filename)
 Try to open an architecture file searching in various directories. More...
 
const char * strip_start (const char *filename)
 
bool check_url (const char *filename)
 Check if an URL exists. More...
 
static FILE * fopenat (string &fullpath, const char *dir, const char *filename)
 Try to open the file '<dir>/<filename>'. More...
 
static FILE * fopenat (string &fullpath, const string &dir, const char *filename)
 Try to open the file '<dir>/<filename>'. More...
 
static FILE * fopenat (string &fullpath, const string &dir, const char *path, const char *filename)
 Try to open the file '<dir>/<path>/<filename>'. More...
 
static bool isAbsolutePathname (const string &filename)
 Test absolute pathname. More...
 
static void buildFullPathname (string &fullpath, const char *filename)
 Build a full pathname of <filename>. More...
 
FILE * fopensearch (const char *filename, string &fullpath)
 Try to open the file <filename> searching in various directories. More...
 
const char * filebasename (const char *name)
 returns a pointer on the basename part of name More...
 
string filedirname (const string &name)
 returns a string containing the dirname of name If no dirname, returns "." More...
 

Variables

string gFaustSuperSuperDirectory
 
string gFaustSuperDirectory
 
string gFaustDirectory
 
string gMasterDirectory
 
string gClassName
 
bool gInlineArchSwitch
 
list< string > gImportDirList
 
set< string > alreadyIncluded
 Inject file fname into dst ostream if not already done. More...
 

Macro Definition Documentation

#define DIR_SEPARATOR   '/'

filebasename returns the basename of a path.

(adapted by kb from basename.c)

Parameters
[in]Thepath to parse.
Returns
The last component of the given path.

Definition at line 522 of file enrobage.cpp.

#define IS_DIR_SEPARATOR (   ch)    ((ch) == DIR_SEPARATOR)

Definition at line 534 of file enrobage.cpp.

Referenced by filebasename().

#define TRY_OPEN (   filename)
Value:
ifstream* f = new ifstream(); \
f->open(filename, ifstream::in); \
if (f->is_open()) return f; else delete f; \
if(!(yy_init))
Definition: faustlexer.cpp:993

Definition at line 212 of file enrobage.cpp.

Referenced by open_arch_stream().

Function Documentation

static void buildFullPathname ( string &  fullpath,
const char *  filename 
)
static

Build a full pathname of <filename>.

<fullpath> = <currentdir>/<filename>

Definition at line 412 of file enrobage.cpp.

References FAUST_PATH_MAX, and isAbsolutePathname().

Referenced by fopensearch().

413 {
414  char old[FAUST_PATH_MAX];
415 
416  if (isAbsolutePathname(filename)) {
417  fullpath = filename;
418  } else {
419  fullpath = getcwd (old, FAUST_PATH_MAX);
420  fullpath += '/';
421  fullpath += filename;
422  }
423 }
static bool isAbsolutePathname(const string &filename)
Test absolute pathname.
Definition: enrobage.cpp:396
#define FAUST_PATH_MAX

Here is the call graph for this function:

Here is the caller graph for this function:

bool check_url ( const char *  filename)

Check if an URL exists.

Returns
true if the URL exist, false otherwise

Definition at line 296 of file enrobage.cpp.

References http_fetch(), and http_strerror().

Referenced by process_cmdline().

297 {
298  char* fileBuf = 0;
299 
300  // Tries to open as an URL for a local file
301  if (strstr(filename, "file://") != 0) {
302  // Tries to open as a regular file after removing 'file://'
303  FILE* f = fopen(&filename[7], "r");
304  if (f) {
305  fclose(f);
306  return true;
307  } else {
308  cerr << "ERROR : cannot open file '" << filename << "' : " << strerror(errno) << "; for help type \"faust --help\"" << endl;
309  return false;
310  }
311 
312  // Tries to open as a http URL
313  } else if (strstr(filename, "http://") != 0) {
314  if (http_fetch(filename, &fileBuf) != -1) {
315  return true;
316  } else {
317  cerr << "ERROR : unable to access URL '" << filename << "' : " << http_strerror() << "; for help type \"faust --help\"" << endl;
318  return false;
319  }
320  } else {
321  // Otherwise tries to open as a regular file
322  FILE* f = fopen(filename, "r");
323  if (f) {
324  fclose(f);
325  return true;
326  } else {
327  cerr << "ERROR : cannot open file '" << filename << "' : " << strerror(errno) << "; for help type \"faust --help\"" << endl;
328  return false;
329  }
330  }
331 }
const char * http_strerror()
int http_fetch(const char *url_tmp, char **fileBuf)

Here is the call graph for this function:

Here is the caller graph for this function:

const char* filebasename ( const char *  name)

returns a pointer on the basename part of name

Definition at line 544 of file enrobage.cpp.

References IS_DIR_SEPARATOR, and name().

Referenced by copyFaustSources(), filedirname(), and printfaustlisting().

545 {
546 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
547  /* Skip over the disk name in MSDOS pathnames. */
548  if (isalpha(name[0]) && name[1] == ':')
549  name += 2;
550 #endif
551 
552  const char* base;
553  for (base = name; *name; name++)
554  {
555  if (IS_DIR_SEPARATOR (*name))
556  {
557  base = name + 1;
558  }
559  }
560  return base;
561 }
#define IS_DIR_SEPARATOR(ch)
Definition: enrobage.cpp:534
const char * name(Symbol *sym)
Returns the name of a symbol.
Definition: symbol.hh:98

Here is the call graph for this function:

Here is the caller graph for this function:

string filedirname ( const string &  name)

returns a string containing the dirname of name If no dirname, returns "."

Definition at line 568 of file enrobage.cpp.

References filebasename().

Referenced by initFaustDirectories().

569 {
570  const char* base = filebasename(name.c_str());
571  const unsigned int size = (const unsigned int)(base-name.c_str());
572  string dirname;
573 
574  if (size==0) {
575  dirname += '.';
576 
577  } else if (size==1) {
578  dirname += name[0];
579 
580  } else {
581  for (unsigned int i=0; i<size-1; i++) {
582  dirname += name[i];
583  }
584  }
585  return dirname;
586 }
const char * filebasename(const char *name)
returns a pointer on the basename part of name
Definition: enrobage.cpp:544
const char * name(Symbol *sym)
Returns the name of a symbol.
Definition: symbol.hh:98

Here is the call graph for this function:

Here is the caller graph for this function:

static FILE* fopenat ( string &  fullpath,
const char *  dir,
const char *  filename 
)
static

Try to open the file '<dir>/<filename>'.

If it succeed, it stores the full pathname of the file into <fullpath>

Definition at line 337 of file enrobage.cpp.

References FAUST_PATH_MAX.

Referenced by fopenat(), and fopensearch().

338 {
339  int err;
340  char olddirbuffer[FAUST_PATH_MAX];
341  char newdirbuffer[FAUST_PATH_MAX];
342 
343  char* olddir = getcwd (olddirbuffer, FAUST_PATH_MAX);
344 
345  if (chdir(dir) == 0) {
346  FILE* f = fopen(filename, "r");
347  fullpath = getcwd (newdirbuffer, FAUST_PATH_MAX);
348  fullpath += '/';
349  fullpath += filename;
350  err = chdir(olddir);
351  return f;
352  }
353  err = chdir(olddir);
354  return 0;
355 }
#define FAUST_PATH_MAX

Here is the caller graph for this function:

static FILE* fopenat ( string &  fullpath,
const string &  dir,
const char *  filename 
)
static

Try to open the file '<dir>/<filename>'.

If it succeed, it stores the full pathname of the file into <fullpath>

Definition at line 362 of file enrobage.cpp.

References fopenat().

363 {
364  return fopenat(fullpath, dir.c_str(), filename);
365 }
static FILE * fopenat(string &fullpath, const char *dir, const char *filename)
Try to open the file '/'.
Definition: enrobage.cpp:337

Here is the call graph for this function:

static FILE* fopenat ( string &  fullpath,
const string &  dir,
const char *  path,
const char *  filename 
)
static

Try to open the file '<dir>/<path>/<filename>'.

If it succeed, it stores the full pathname of the file into <fullpath>

Definition at line 371 of file enrobage.cpp.

References FAUST_PATH_MAX.

372 {
373  int err;
374  char olddirbuffer[FAUST_PATH_MAX];
375  char newdirbuffer[FAUST_PATH_MAX];
376 
377  char* olddir = getcwd (olddirbuffer, FAUST_PATH_MAX);
378 
379  if (chdir(dir.c_str()) == 0) {
380  if (chdir(path) == 0) {
381  FILE* f = fopen(filename, "r");
382  fullpath = getcwd (newdirbuffer, FAUST_PATH_MAX);
383  fullpath += '/';
384  fullpath += filename;
385  err = chdir(olddir);
386  return f;
387  }
388  }
389  err = chdir(olddir);
390  return 0;
391 }
#define FAUST_PATH_MAX
FILE* fopensearch ( const char *  filename,
string &  fullpath 
)

Try to open the file <filename> searching in various directories.

If succesful place its full pathname in the string <fullpath>

Definition at line 458 of file enrobage.cpp.

References buildFullPathname(), fopenat(), gFaustDirectory, gFaustSuperDirectory, gFaustSuperSuperDirectory, gImportDirList, and gMasterDirectory.

Referenced by SourceReader::parse().

459 {
460  FILE* f;
461  char* envpath;
462 
463 
464  // search in current directory
465 
466  if ((f = fopen(filename, "r"))) {
467  buildFullPathname(fullpath, filename);
468  return f;
469  }
470 
471  // search file in user supplied directory path
472 
473  for (list< string >::iterator i = gImportDirList.begin(); i != gImportDirList.end(); i++) {
474  if ((f = fopenat(fullpath, *i, filename))) {
475  //std::cerr << "found file : " << fullpath << std::endl;
476  return f;
477  }
478  }
479 
480 
481  // search in default directories
482 
483  if ((f = fopenat(fullpath, gMasterDirectory, filename))) {
484  return f;
485  }
486  if ((envpath = getenv("FAUST_LIB_PATH")) && (f = fopenat(fullpath, envpath, filename))) {
487  return f;
488  }
489  if ((f = fopenat(fullpath, gFaustDirectory, "architecture", filename))) {
490  return f;
491  }
492  if ((f = fopenat(fullpath, gFaustSuperDirectory, "architecture", filename))) {
493  return f;
494  }
495  if ((f = fopenat(fullpath, gFaustSuperSuperDirectory, "architecture", filename))) {
496  return f;
497  }
498 #ifdef INSTALL_PREFIX
499  if ((f = fopenat(fullpath, INSTALL_PREFIX "/lib64/faust", filename))) {
500  return f;
501  }
502 #endif
503  if ((f = fopenat(fullpath, "/usr/local/lib64/faust", filename))) {
504  return f;
505  }
506  if ((f = fopenat(fullpath, "/usr/lib64/faust", filename))) {
507  return f;
508  }
509  return 0;
510 }
string gMasterDirectory
Definition: main.cpp:105
string gFaustSuperSuperDirectory
Definition: main.cpp:101
string gFaustDirectory
Definition: main.cpp:103
static void buildFullPathname(string &fullpath, const char *filename)
Build a full pathname of .
Definition: enrobage.cpp:412
static FILE * fopenat(string &fullpath, const char *dir, const char *filename)
Try to open the file '/'.
Definition: enrobage.cpp:337
list< string > gImportDirList
Definition: main.cpp:161
string gFaustSuperDirectory
Definition: main.cpp:102

Here is the call graph for this function:

Here is the caller graph for this function:

void inject ( ostream &  dst,
const string  fname 
)

Definition at line 167 of file enrobage.cpp.

References alreadyIncluded, open_arch_stream(), and streamCopy().

Referenced by streamCopyUntil().

168 {
169  if (alreadyIncluded.find(fname) == alreadyIncluded.end()) {
170  alreadyIncluded.insert(fname);
171  istream* src = open_arch_stream( fname.c_str());
172  if (src) {
173  streamCopy(*src, dst);
174  } else {
175  cerr << "NOT FOUND " << fname << endl;
176  }
177  }
178 }
set< string > alreadyIncluded
Inject file fname into dst ostream if not already done.
Definition: enrobage.cpp:165
ifstream * open_arch_stream(const char *filename)
Try to open an architecture file searching in various directories.
Definition: enrobage.cpp:220
void streamCopy(istream &src, ostream &dst)
Copy src to dst.
Definition: enrobage.cpp:199

Here is the call graph for this function:

Here is the caller graph for this function:

static bool isAbsolutePathname ( const string &  filename)
static

Test absolute pathname.

Definition at line 396 of file enrobage.cpp.

Referenced by buildFullPathname().

397 {
398  //test windows absolute pathname "x:xxxxxx"
399  if (filename.size()>1 && filename[1] == ':') return true;
400 
401  // test unix absolute pathname "/xxxxxx"
402  if (filename.size()>0 && filename[0] == '/') return true;
403 
404  return false;
405 }

Here is the caller graph for this function:

static bool isBlank ( const string &  s)
static

Returns true is a line is blank (contains only white caracters)

Definition at line 48 of file enrobage.cpp.

Referenced by streamCopyLicense().

48  {
49  for (size_t i=0; i<s.size(); i++) {
50  if (s[i] != ' ' && s[i] != '\t') return false;
51  }
52  return true;
53 }

Here is the caller graph for this function:

bool isFaustInclude ( const string &  s,
string &  fname 
)

True if string s match '#include <faust/fname>'.

Definition at line 149 of file enrobage.cpp.

References myparser::filename(), myparser::parse(), and myparser::skip().

Referenced by streamCopyUntil().

150 {
151  myparser P(s);
152  if ( P.skip() && P.parse("#include") && P.skip() && P.filename(fname) ) {
153  myparser Q(fname);
154  return Q.parse("faust/");
155  } else {
156  return false;
157  }
158 }
A minimalistic parser used to recognize '#include ' patterns when copying architecture fil...
Definition: enrobage.cpp:121

Here is the call graph for this function:

Here is the caller graph for this function:

ifstream* open_arch_stream ( const char *  filename)

Try to open an architecture file searching in various directories.

Definition at line 220 of file enrobage.cpp.

References FAUST_PATH_MAX, gFaustDirectory, gFaustSuperDirectory, gFaustSuperSuperDirectory, and TRY_OPEN.

Referenced by inject(), main(), and openArchFile().

221 {
222  char buffer[FAUST_PATH_MAX];
223  char* old = getcwd (buffer, FAUST_PATH_MAX);
224  int err;
225 
226  TRY_OPEN(filename);
227 
228  char *envpath = getenv("FAUST_LIB_PATH");
229  if (envpath!=NULL) {
230  if (chdir(envpath)==0) {
231  TRY_OPEN(filename);
232  }
233  }
234  err = chdir(old);
235  if ( (chdir(gFaustDirectory.c_str())==0) && (chdir("architecture")==0) ) {
236  //cout << "enrobage.cpp : 'architecture' directory found in gFaustDirectory" << endl;
237  TRY_OPEN(filename);
238  }
239  err = chdir(old);
240  if ((chdir(gFaustSuperDirectory.c_str())==0) && (chdir("architecture")==0) ) {
241  //cout << "enrobage.cpp : 'architecture' directory found in gFaustSuperDirectory" << endl;
242  TRY_OPEN(filename);
243  }
244  err = chdir(old);
245  if ((chdir(gFaustSuperSuperDirectory.c_str())==0) && (chdir("architecture")==0) ) {
246  //cout << "enrobage.cpp : 'architecture' directory found in gFaustSuperSuperDirectory" << endl;
247  TRY_OPEN(filename);
248  }
249 #ifdef INSTALL_PREFIX
250  err = chdir(old);
251  if (chdir(INSTALL_PREFIX "/lib64/faust")==0) {
252  TRY_OPEN(filename);
253  }
254  err = chdir(old);
255  if (chdir(INSTALL_PREFIX "/include")==0) {
256  TRY_OPEN(filename);
257  }
258 #endif
259  err = chdir(old);
260  if (chdir("/usr/local/lib64/faust")==0) {
261  TRY_OPEN(filename);
262  }
263  err = chdir(old);
264  if (chdir("/usr/lib64/faust")==0) {
265  TRY_OPEN(filename);
266  }
267  err = chdir(old);
268  if (chdir("/usr/local/include")==0) {
269  TRY_OPEN(filename);
270  }
271  err = chdir(old);
272  if (chdir("/usr/include")==0) {
273  TRY_OPEN(filename);
274  }
275 
276  return 0;
277 }
string gFaustSuperSuperDirectory
Definition: main.cpp:101
string gFaustDirectory
Definition: main.cpp:103
#define TRY_OPEN(filename)
Definition: enrobage.cpp:212
string gFaustSuperDirectory
Definition: main.cpp:102
#define FAUST_PATH_MAX

Here is the caller graph for this function:

static string& replaceClassName ( string &  str)
static

Used when copying architecture files to replace default mydsp class name with the user specified one.

Definition at line 76 of file enrobage.cpp.

References gClassName, and replaceOccurences().

Referenced by streamCopyUntil().

77 {
78  return replaceOccurences(str, "mydsp", gClassName);
79 }
string gClassName
Definition: main.cpp:158
static string & replaceOccurences(string &str, const string &oldstr, const string &newstr)
Replace every occurrence of oldstr by newstr inside str.
Definition: enrobage.cpp:59

Here is the call graph for this function:

Here is the caller graph for this function:

static string& replaceOccurences ( string &  str,
const string &  oldstr,
const string &  newstr 
)
static

Replace every occurrence of oldstr by newstr inside str.

str is modified and returned as reference for convenience

Definition at line 59 of file enrobage.cpp.

Referenced by replaceClassName().

60 {
61  string::size_type l1 = oldstr.length();
62  string::size_type l2 = newstr.length();
63 
64  string::size_type pos = str.find(oldstr);
65  while (pos != string::npos) {
66  str.replace(pos, l1, newstr);
67  pos = str.find(oldstr, pos + l2);
68  }
69  return str;
70 }

Here is the caller graph for this function:

void streamCopy ( istream &  src,
ostream &  dst 
)

Copy src to dst.

Definition at line 199 of file enrobage.cpp.

References streamCopyUntil().

Referenced by inject(), and main().

200 {
201  streamCopyUntil(src, dst, "<<<FOBIDDEN LINE IN A FAUST ARCHITECTURE FILE>>>");
202 }
void streamCopyUntil(istream &src, ostream &dst, const string &until)
Copy src to dst until specific line.
Definition: enrobage.cpp:183

Here is the call graph for this function:

Here is the caller graph for this function:

void streamCopyLicense ( istream &  src,
ostream &  dst,
const string &  exceptiontag 
)

Copy or remove license header.

Architecture files can contain a header specifying the license. If this header contains an exception tag (for example "FAUST COMPILER EXCEPTION") it is an indication for the compiler to remove the license header from the resulting code. A header is the first non blank line that begins a comment.

Definition at line 87 of file enrobage.cpp.

References isBlank().

88 {
89  string s;
90  vector<string> H;
91 
92  // skip blank lines
93  while (getline(src,s) && isBlank(s)) dst << s << endl;
94 
95  // first non blank should start a comment
96  if (s.find("/*")==string::npos) { dst << s << endl; return; }
97 
98  // copy the header into H
99  bool remove = false;
100  H.push_back(s);
101 
102  while (getline(src,s) && s.find("*/") == string::npos) {
103  H.push_back(s);
104  if (s.find(exceptiontag) != string::npos) remove=true;
105  }
106 
107  // copy the header unless explicitely granted to remove it
108  if (!remove) {
109  // copy the header
110  for (unsigned int i=0; i<H.size(); i++) {
111  dst << H[i] << endl;
112  }
113  dst << s << endl;
114  }
115 }
static bool isBlank(const string &s)
Returns true is a line is blank (contains only white caracters)
Definition: enrobage.cpp:48

Here is the call graph for this function:

void streamCopyUntil ( istream &  src,
ostream &  dst,
const string &  until 
)

Copy src to dst until specific line.

Definition at line 183 of file enrobage.cpp.

References gInlineArchSwitch, inject(), isFaustInclude(), and replaceClassName().

Referenced by main(), streamCopy(), and streamCopyUntilEnd().

184 {
185  string s;
186  string fname;
187  while ( getline(src,s) && (s != until) ) {
188  if (gInlineArchSwitch && isFaustInclude(s, fname)) {
189  inject(dst, fname);
190  } else {
191  dst << replaceClassName(s) << endl;
192  }
193  }
194 }
bool gInlineArchSwitch
Definition: main.cpp:156
bool isFaustInclude(const string &s, string &fname)
True if string s match '#include '.
Definition: enrobage.cpp:149
void inject(ostream &dst, const string fname)
Definition: enrobage.cpp:167
static string & replaceClassName(string &str)
Used when copying architecture files to replace default mydsp class name with the user specified one...
Definition: enrobage.cpp:76

Here is the call graph for this function:

Here is the caller graph for this function:

void streamCopyUntilEnd ( istream &  src,
ostream &  dst 
)

Copy src to dst until end.

Definition at line 207 of file enrobage.cpp.

References streamCopyUntil().

Referenced by main().

208 {
209  streamCopyUntil(src, dst, "<<<FOBIDDEN LINE IN A FAUST ARCHITECTURE FILE>>>");
210 }
void streamCopyUntil(istream &src, ostream &dst, const string &until)
Copy src to dst until specific line.
Definition: enrobage.cpp:183

Here is the call graph for this function:

Here is the caller graph for this function:

const char* strip_start ( const char *  filename)

Definition at line 281 of file enrobage.cpp.

282 {
283  const char* start = "file://";
284  if (strstr(filename, start) != NULL) {
285  return &filename[strlen(start)];
286  } else {
287  return filename;
288  }
289 }

Variable Documentation

set<string> alreadyIncluded

Inject file fname into dst ostream if not already done.

Definition at line 165 of file enrobage.cpp.

Referenced by inject().

string gClassName

Definition at line 158 of file main.cpp.

Referenced by main(), process_cmdline(), and replaceClassName().

string gFaustDirectory

Definition at line 103 of file main.cpp.

Referenced by fopensearch(), initFaustDirectories(), and open_arch_stream().

string gFaustSuperDirectory

Definition at line 102 of file main.cpp.

Referenced by fopensearch(), initFaustDirectories(), and open_arch_stream().

string gFaustSuperSuperDirectory

Definition at line 101 of file main.cpp.

Referenced by fopensearch(), initFaustDirectories(), and open_arch_stream().

list<string> gImportDirList

Definition at line 161 of file main.cpp.

Referenced by fopensearch(), and process_cmdline().

bool gInlineArchSwitch

Definition at line 156 of file main.cpp.

Referenced by process_cmdline(), and streamCopyUntil().

string gMasterDirectory

Definition at line 105 of file main.cpp.

Referenced by fopensearch(), and initFaustDirectories().