File: enrobage.cpp

package info (click to toggle)
faust 0.9.9.4b-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 7,164 kB
  • ctags: 6,050
  • sloc: cpp: 25,384; xml: 4,102; makefile: 648; yacc: 372; ruby: 247; lex: 161; sh: 50
file content (177 lines) | stat: -rw-r--r-- 3,987 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/************************************************************************
 ************************************************************************
    FAUST compiler
	Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
    ---------------------------------------------------------------------
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 ************************************************************************
 ************************************************************************/
 
 
 
#include "enrobage.hh"
#include <string>
#include <cstdlib>
#include <climits>
#include "compatibility.hh"


//----------------------------------------------------------------


/**
 * Try to open a file within a path (a set of directory).
 */

void streamCopyUntil(istream& src, ostream& dst, const string& until)
{ 
	string	s;
	while ( getline(src,s) && (s != until) ) dst << s << endl;
}


/**
 * Try to open a file within a path (a set of directory).
 */

void streamCopyUntilEnd(istream& src, ostream& dst)
{ 
	string	s;
	while ( getline(src,s) ) dst << s << endl;
}


/**
 * Try to open a file within a path (a set of directory).
 */

ifstream* open_stream(const char* filename)
{
	ifstream* f = new ifstream();
	f->open(filename);
	if (f->good()) return f;
	delete f;
	return 0;
}

#ifdef WIN32
#define PATHSEP ";"
#else
#define PATHSEP ":"
#endif

ifstream* open_path_stream (const char* lofdir, const char* filename)
{
    char        old[PATH_MAX];
    char*       lod = strdup(lofdir);
    char*       dir = strtok(lod, PATHSEP);
    
    if (!dir) dir = lod;
    getcwd (old, PATH_MAX);
    while (dir) {
        if (chdir(dir) == 0) {
            ifstream* f = open_stream(filename);
            if (f) {
                free(lod);
                return f;
            }
            chdir(old);
        }
        dir = strtok(NULL, PATHSEP);
    }
    cerr << "file " << filename << " not found in path " << lofdir << endl;
    free (lod);
    return 0;
}


/**
 * Try to open an architecture file.
 */
ifstream* open_arch_stream (const char* filename)
{	
	ifstream* f = open_stream(filename);
	
	if (f) {
		return f;
	} else {
		const char* p1 = getenv("FAUST_PATH");
		const char* p2 = (p1 != 0) ? p1 : "/usr/local/lib/faust/:/usr/lib/faust/";
		return open_path_stream(p2, filename);
	}
}


	


/*---------------------------------------------*/

		
bool check_file(const char* filename)
{
	FILE* f = fopen(filename, "r");
	
	if (f == NULL) {
		fprintf(stderr, "faust: "); perror(filename);
	} else {
		fclose(f);
	}
	return f != NULL;
}
		

/**
 * Try to open a file.
 */
FILE* fopensearch(const char* filename)
{	
	FILE* f = fopen(filename, "r");
	
	if (f) {
		return f;
	} else {
		const char* p1 = getenv("FAUST_PATH");
		const char* p2 = (p1 != 0) ? p1 : "/usr/local/lib/faust/:/usr/lib/faust/";
		return fopenpath(p2, filename);
	}
}

	
FILE* fopenpath(const char* lofdir, const char* filename)
{
	char		old[PATH_MAX];
	char* 		lod = strdup(lofdir);
	char* 		dir = strtok(lod, ":");
	
	getcwd (old, PATH_MAX);
	while (dir) {
		if (chdir(dir) == 0) {
			
			FILE* f = fopen(filename, "r");
			chdir(old);
			if (f) {
				free(lod);
				return f;
			}
		} else  {
		}
		dir = strtok(NULL, ":");
	}
	cerr << "file " << filename << " not found in path " << lofdir << endl;
	free (lod);
	return 0;
}