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 err = chdir(old);
00100 if (chdir("/usr/local/share/faust")==0) {
00101 ifstream* f = new ifstream();
00102 f->open(filename);
00103 if (f->good()) return f; else delete f;
00104 }
00105 err = chdir(old);
00106 if (chdir("/usr/share/faust")==0) {
00107 ifstream* f = new ifstream();
00108 f->open(filename);
00109 if (f->good()) return f; else delete f;
00110 }
00111
00112 return 0;
00113 }
00114
00115
00116
00117
00118
00124 bool check_file(const char* filename)
00125 {
00126 FILE* f = fopen(filename, "r");
00127
00128 if (f == NULL) {
00129 fprintf(stderr, "faust: "); perror(filename);
00130 } else {
00131 fclose(f);
00132 }
00133 return f != NULL;
00134 }
00135
00136
00141 static FILE* fopenat(string& fullpath, const char* dir, const char* filename)
00142 {
00143 int err;
00144 char olddirbuffer[FAUST_PATH_MAX];
00145 char newdirbuffer[FAUST_PATH_MAX];
00146
00147 char* olddir = getcwd (olddirbuffer, FAUST_PATH_MAX);
00148
00149 if (chdir(dir) == 0) {
00150 FILE* f = fopen(filename, "r");
00151 fullpath = getcwd (newdirbuffer, FAUST_PATH_MAX);
00152 fullpath += '/';
00153 fullpath += filename;
00154 err = chdir(olddir);
00155 return f;
00156 }
00157 err = chdir(olddir);
00158 return 0;
00159 }
00160
00165 static FILE* fopenat(string& fullpath, const string& dir, const char* filename)
00166 {
00167 return fopenat(fullpath, dir.c_str(), filename);
00168 }
00169
00174 static FILE* fopenat(string& fullpath, const string& dir, const char* path, const char* filename)
00175 {
00176 int err;
00177 char olddirbuffer[FAUST_PATH_MAX];
00178 char newdirbuffer[FAUST_PATH_MAX];
00179
00180 char* olddir = getcwd (olddirbuffer, FAUST_PATH_MAX);
00181 if (chdir(dir.c_str()) == 0) {
00182 if (chdir(path) == 0) {
00183 FILE* f = fopen(filename, "r");
00184 fullpath = getcwd (newdirbuffer, FAUST_PATH_MAX);
00185 fullpath += '/';
00186 fullpath += filename;
00187 err = chdir(olddir);
00188 return f;
00189 }
00190 }
00191 err = chdir(olddir);
00192 return 0;
00193 }
00194
00195
00196
00200 static bool isAbsolutePathname(const string& filename)
00201 {
00202
00203 if (filename.size()>1 && filename[1] == ':') return true;
00204
00205
00206 if (filename.size()>0 && filename[0] == '/') return true;
00207
00208 return false;
00209 }
00210
00211
00216 static void buildFullPathname(string& fullpath, const char* filename)
00217 {
00218 char old[FAUST_PATH_MAX];
00219
00220 if (isAbsolutePathname(filename)) {
00221 fullpath = filename;
00222 } else {
00223 fullpath = getcwd (old, FAUST_PATH_MAX);
00224 fullpath += '/';
00225 fullpath += filename;
00226 }
00227 }
00228
00234 #ifdef WIN32
00235 FILE* fopensearch(const char* filename, string& fullpath)
00236 {
00237 FILE* f;
00238
00239 if ((f = fopen(filename, "r"))) {
00240 buildFullPathname(fullpath, filename);
00241 return f;
00242 }
00243 if ((f = fopenat(fullpath, gMasterDirectory, filename))) {
00244 return f;
00245 }
00246 if ((f = fopenat(fullpath, gFaustDirectory, "architecture", filename))) {
00247 return f;
00248 }
00249 if ((f = fopenat(fullpath, gFaustSuperDirectory, "architecture", filename))) {
00250 return f;
00251 }
00252 if ((f = fopenat(fullpath, gFaustSuperSuperDirectory, "architecture", filename))) {
00253 return f;
00254 }
00255 return 0;
00256 }
00257 #else
00258 FILE* fopensearch(const char* filename, string& fullpath)
00259 {
00260 FILE* f;
00261
00262
00263 if ((f = fopen(filename, "r"))) {
00264 buildFullPathname(fullpath, filename);
00265 return f;
00266 }
00267 if ((f = fopenat(fullpath, gMasterDirectory, filename))) {
00268 return f;
00269 }
00270 if ((f = fopenat(fullpath, gFaustDirectory, "architecture", filename))) {
00271 return f;
00272 }
00273 if ((f = fopenat(fullpath, gFaustSuperDirectory, "architecture", filename))) {
00274 return f;
00275 }
00276 if ((f = fopenat(fullpath, gFaustSuperSuperDirectory, "architecture", filename))) {
00277 return f;
00278 }
00279 if ((f = fopenat(fullpath, "/usr/local/share/faust", filename))) {
00280 return f;
00281 }
00282 if ((f = fopenat(fullpath, "/usr/share/faust", filename))) {
00283 return f;
00284 }
00285 return 0;
00286 }
00287 #endif
00288
00289
00297 #ifndef DIR_SEPARATOR
00298 #define DIR_SEPARATOR '/'
00299 #endif
00300
00301 #ifdef WIN32
00302 #define HAVE_DOS_BASED_FILE_SYSTEM
00303 #ifndef DIR_SEPARATOR_2
00304 #define DIR_SEPARATOR_2 '\\'
00305 #endif
00306 #endif
00307
00308
00309 #ifndef DIR_SEPARATOR_2
00310 # define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
00311 #else
00312 # define IS_DIR_SEPARATOR(ch) \
00313 (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
00314 #endif
00315
00316 const char* filebasename(const char* name)
00317 {
00318 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
00319
00320 if (isalpha(name[0]) && name[1] == ':')
00321 name += 2;
00322 #endif
00323
00324 const char* base;
00325 for (base = name; *name; name++)
00326 {
00327 if (IS_DIR_SEPARATOR (*name))
00328 {
00329 base = name + 1;
00330 }
00331 }
00332 return base;
00333 }
00334