00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "enrobage.hh"
00025 #include <string>
00026 #include <limits.h>
00027 #include <stdlib.h>
00028 #include "compatibility.hh"
00029 #include <climits>
00030
00031 extern string gFaustSuperSuperDirectory;
00032 extern string gFaustSuperDirectory;
00033 extern string gFaustDirectory;
00034 extern string gMasterDirectory;
00035
00036
00037
00041 void streamCopyUntil(istream& src, ostream& dst, const string& until)
00042 {
00043 string s;
00044 while ( getline(src,s) && (s != until) ) dst << s << endl;
00045 }
00046
00050 void streamCopy(istream& src, ostream& dst)
00051 {
00052 string s;
00053 while ( getline(src,s)) dst << s << endl;
00054 }
00055
00059 void streamCopyUntilEnd(istream& src, ostream& dst)
00060 {
00061 string s;
00062 while ( getline(src,s) ) dst << s << endl;
00063 }
00064
00065
00069 ifstream* open_arch_stream(const char* filename)
00070 {
00071 char buffer[FAUST_PATH_MAX];
00072 char* old = getcwd (buffer, FAUST_PATH_MAX);
00073 int err;
00074
00075 {
00076 ifstream* f = new ifstream();
00077 f->open(filename, ifstream::in); if (f->is_open()) return f; else delete f;
00078 }
00079 if ( (chdir(gFaustDirectory.c_str())==0) && (chdir("architecture")==0) ) {
00080
00081 ifstream* f = new ifstream();
00082 f->open(filename, ifstream::in);
00083 if (f->good()) return f; else delete f;
00084 }
00085 err = chdir(old);
00086 if ((chdir(gFaustSuperDirectory.c_str())==0) && (chdir("architecture")==0) ) {
00087
00088 ifstream* f = new ifstream();
00089 f->open(filename, ifstream::in);
00090 if (f->good()) return f; else delete f;
00091 }
00092 err = chdir(old);
00093 if ((chdir(gFaustSuperSuperDirectory.c_str())==0) && (chdir("architecture")==0) ) {
00094
00095 ifstream* f = new ifstream();
00096 f->open(filename, ifstream::in);
00097 if (f->good()) return f; else delete f;
00098 }
00099 #ifdef INSTALL_PREFIX
00100 err = chdir(old);
00101 if (chdir(INSTALL_PREFIX "/lib/faust")==0) {
00102 ifstream* f = new ifstream();
00103 f->open(filename);
00104 if (f->good()) return f; else delete f;
00105 }
00106 #endif
00107 err = chdir(old);
00108 if (chdir("/usr/local/lib/faust")==0) {
00109 ifstream* f = new ifstream();
00110 f->open(filename);
00111 if (f->good()) return f; else delete f;
00112 }
00113 err = chdir(old);
00114 if (chdir("/usr/lib/faust")==0) {
00115 ifstream* f = new ifstream();
00116 f->open(filename);
00117 if (f->good()) return f; else delete f;
00118 }
00119
00120 return 0;
00121 }
00122
00123
00124
00125
00126
00132 bool check_file(const char* filename)
00133 {
00134 FILE* f = fopen(filename, "r");
00135
00136 if (f == NULL) {
00137 fprintf(stderr, "faust: "); perror(filename);
00138 } else {
00139 fclose(f);
00140 }
00141 return f != NULL;
00142 }
00143
00144
00149 static FILE* fopenat(string& fullpath, const char* dir, const char* filename)
00150 {
00151 int err;
00152 char olddirbuffer[FAUST_PATH_MAX];
00153 char newdirbuffer[FAUST_PATH_MAX];
00154
00155 char* olddir = getcwd (olddirbuffer, FAUST_PATH_MAX);
00156
00157 if (chdir(dir) == 0) {
00158 FILE* f = fopen(filename, "r");
00159 fullpath = getcwd (newdirbuffer, FAUST_PATH_MAX);
00160 fullpath += '/';
00161 fullpath += filename;
00162 err = chdir(olddir);
00163 return f;
00164 }
00165 err = chdir(olddir);
00166 return 0;
00167 }
00168
00173 static FILE* fopenat(string& fullpath, const string& dir, const char* filename)
00174 {
00175 return fopenat(fullpath, dir.c_str(), filename);
00176 }
00177
00182 static FILE* fopenat(string& fullpath, const string& dir, const char* path, const char* filename)
00183 {
00184 int err;
00185 char olddirbuffer[FAUST_PATH_MAX];
00186 char newdirbuffer[FAUST_PATH_MAX];
00187
00188 char* olddir = getcwd (olddirbuffer, FAUST_PATH_MAX);
00189 if (chdir(dir.c_str()) == 0) {
00190 if (chdir(path) == 0) {
00191 FILE* f = fopen(filename, "r");
00192 fullpath = getcwd (newdirbuffer, FAUST_PATH_MAX);
00193 fullpath += '/';
00194 fullpath += filename;
00195 err = chdir(olddir);
00196 return f;
00197 }
00198 }
00199 err = chdir(olddir);
00200 return 0;
00201 }
00202
00203
00204
00208 static bool isAbsolutePathname(const string& filename)
00209 {
00210
00211 if (filename.size()>1 && filename[1] == ':') return true;
00212
00213
00214 if (filename.size()>0 && filename[0] == '/') return true;
00215
00216 return false;
00217 }
00218
00219
00224 static void buildFullPathname(string& fullpath, const char* filename)
00225 {
00226 char old[FAUST_PATH_MAX];
00227
00228 if (isAbsolutePathname(filename)) {
00229 fullpath = filename;
00230 } else {
00231 fullpath = getcwd (old, FAUST_PATH_MAX);
00232 fullpath += '/';
00233 fullpath += filename;
00234 }
00235 }
00236
00242 #ifdef WIN32
00243 FILE* fopensearch(const char* filename, string& fullpath)
00244 {
00245 FILE* f;
00246
00247 if ((f = fopen(filename, "r"))) {
00248 buildFullPathname(fullpath, filename);
00249 return f;
00250 }
00251 if ((f = fopenat(fullpath, gMasterDirectory, filename))) {
00252 return f;
00253 }
00254 if ((f = fopenat(fullpath, gFaustDirectory, "architecture", filename))) {
00255 return f;
00256 }
00257 if ((f = fopenat(fullpath, gFaustSuperDirectory, "architecture", filename))) {
00258 return f;
00259 }
00260 if ((f = fopenat(fullpath, gFaustSuperSuperDirectory, "architecture", filename))) {
00261 return f;
00262 }
00263 return 0;
00264 }
00265 #else
00266 FILE* fopensearch(const char* filename, string& fullpath)
00267 {
00268 FILE* f;
00269
00270
00271 if ((f = fopen(filename, "r"))) {
00272 buildFullPathname(fullpath, filename);
00273 return f;
00274 }
00275 if ((f = fopenat(fullpath, gMasterDirectory, filename))) {
00276 return f;
00277 }
00278 if ((f = fopenat(fullpath, gFaustDirectory, "architecture", filename))) {
00279 return f;
00280 }
00281 if ((f = fopenat(fullpath, gFaustSuperDirectory, "architecture", filename))) {
00282 return f;
00283 }
00284 if ((f = fopenat(fullpath, gFaustSuperSuperDirectory, "architecture", filename))) {
00285 return f;
00286 }
00287 #ifdef INSTALL_PREFIX
00288 if ((f = fopenat(fullpath, INSTALL_PREFIX "/lib/faust", filename))) {
00289 return f;
00290 }
00291 #endif
00292 if ((f = fopenat(fullpath, "/usr/local/lib/faust", filename))) {
00293 return f;
00294 }
00295 if ((f = fopenat(fullpath, "/usr/lib/faust", filename))) {
00296 return f;
00297 }
00298 return 0;
00299 }
00300 #endif
00301
00302
00310 #ifndef DIR_SEPARATOR
00311 #define DIR_SEPARATOR '/'
00312 #endif
00313
00314 #ifdef WIN32
00315 #define HAVE_DOS_BASED_FILE_SYSTEM
00316 #ifndef DIR_SEPARATOR_2
00317 #define DIR_SEPARATOR_2 '\\'
00318 #endif
00319 #endif
00320
00321
00322 #ifndef DIR_SEPARATOR_2
00323 # define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
00324 #else
00325 # define IS_DIR_SEPARATOR(ch) \
00326 (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
00327 #endif
00328
00329 const char* filebasename(const char* name)
00330 {
00331 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
00332
00333 if (isalpha(name[0]) && name[1] == ':')
00334 name += 2;
00335 #endif
00336
00337 const char* base;
00338 for (base = name; *name; name++)
00339 {
00340 if (IS_DIR_SEPARATOR (*name))
00341 {
00342 base = name + 1;
00343 }
00344 }
00345 return base;
00346 }
00347