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 <cstdlib>
00027 #include <climits>
00028 #include "compatibility.hh"
00029
00030
00031
00032
00033
00038 void streamCopyUntil(istream& src, ostream& dst, const string& until)
00039 {
00040 string s;
00041 while ( getline(src,s) && (s != until) ) dst << s << endl;
00042 }
00043
00044
00049 void streamCopyUntilEnd(istream& src, ostream& dst)
00050 {
00051 string s;
00052 while ( getline(src,s) ) dst << s << endl;
00053 }
00054
00055
00060 ifstream* open_stream(const char* filename)
00061 {
00062 ifstream* f = new ifstream();
00063 f->open(filename);
00064 if (f->good()) return f;
00065 delete f;
00066 return 0;
00067 }
00068
00069 #ifdef WIN32
00070 #define PATHSEP ";"
00071 #else
00072 #define PATHSEP ":"
00073 #endif
00074
00075 ifstream* open_path_stream (const char* lofdir, const char* filename)
00076 {
00077 char old[PATH_MAX];
00078 char* lod = strdup(lofdir);
00079 char* dir = strtok(lod, PATHSEP);
00080
00081 if (!dir) dir = lod;
00082 getcwd (old, PATH_MAX);
00083 while (dir) {
00084 if (chdir(dir) == 0) {
00085 ifstream* f = open_stream(filename);
00086 if (f) {
00087 free(lod);
00088 return f;
00089 }
00090 chdir(old);
00091 }
00092 dir = strtok(NULL, PATHSEP);
00093 }
00094 cerr << "file " << filename << " not found in path " << lofdir << endl;
00095 free (lod);
00096 return 0;
00097 }
00098
00099
00103 ifstream* open_arch_stream (const char* filename)
00104 {
00105 ifstream* f = open_stream(filename);
00106
00107 if (f) {
00108 return f;
00109 } else {
00110 const char* p1 = getenv("FAUST_PATH");
00111 const char* p2 = (p1 != 0) ? p1 : "/usr/local/share/faust/:/usr/share/faust/";
00112 return open_path_stream(p2, filename);
00113 }
00114 }
00115
00116
00117
00118
00119
00120
00121
00122
00123 bool check_file(const char* filename)
00124 {
00125 FILE* f = fopen(filename, "r");
00126
00127 if (f == NULL) {
00128 fprintf(stderr, "faust: "); perror(filename);
00129 } else {
00130 fclose(f);
00131 }
00132 return f != NULL;
00133 }
00134
00135
00139 FILE* fopensearch(const char* filename)
00140 {
00141 FILE* f = fopen(filename, "r");
00142
00143 if (f) {
00144 return f;
00145 } else {
00146 const char* p1 = getenv("FAUST_PATH");
00147 const char* p2 = (p1 != 0) ? p1 : "/usr/local/share/faust/:/usr/share/faust/";
00148 return fopenpath(p2, filename);
00149 }
00150 }
00151
00152
00153 FILE* fopenpath(const char* lofdir, const char* filename)
00154 {
00155 char old[PATH_MAX];
00156 char* lod = strdup(lofdir);
00157 char* dir = strtok(lod, ":");
00158
00159 getcwd (old, PATH_MAX);
00160 while (dir) {
00161 if (chdir(dir) == 0) {
00162
00163 FILE* f = fopen(filename, "r");
00164 chdir(old);
00165 if (f) {
00166 free(lod);
00167 return f;
00168 }
00169 } else {
00170 }
00171 dir = strtok(NULL, ":");
00172 }
00173 cerr << "file " << filename << " not found in path " << lofdir << endl;
00174 free (lod);
00175 return 0;
00176 }
00177