File: PathName.sc

package info (click to toggle)
supercollider 1%3A3.6.6~repack-2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 23,792 kB
  • ctags: 25,269
  • sloc: cpp: 177,129; lisp: 63,421; ansic: 11,297; python: 1,787; perl: 766; yacc: 311; sh: 286; lex: 181; ruby: 173; makefile: 168; xml: 13
file content (339 lines) | stat: -rw-r--r-- 7,381 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
PathName {

	var <fullPath, colonIndices;

	classvar <>scroot;
	classvar <>secondVolume; //needed only for OS9 path conversions
	classvar <>tmp;

	*new { arg path = "";
		^super.new.init(path.standardizePath);
	}
	*fromOS9 { arg path="";
		if(secondVolume.notNil and:
			{ path.copyRange(0,secondVolume.size - 1) == secondVolume },{
				path = "/Volumes/" ++ path;
		});
		^super.new.init(
			String.streamContents({ arg s;
				path.do({ arg char,i;
					if(char == $:,{
						if(i != 0,{ // leading : is not wanted in unix
							s << $/
						})
					},{
						s <<  char
					});
				})
			})
		)
	}
	*initClass {
		scroot = File.getcwd;
		tmp = Platform.defaultTempDir;
		tmp.isNil.if(
			{"No valid temp directory found. Please set this manually using PathName.tmp_".warn});
	}
	init { arg inPath;
		fullPath = inPath;
	}
	colonIndices {
		^colonIndices ?? {
			colonIndices = List.new;
		 	fullPath.do({ arg eachChar, i;
				if (eachChar.isPathSeparator, { colonIndices.add(i) });
			});
			colonIndices
		}
	}
	fileName {
		^fullPath.copyRange((this.lastColonIndex) + 1, (fullPath.size -1).max(0));
	}

	fileNameWithoutExtension {
		var fileName;
		fileName = this.fileName;
		fileName.reverseDo({ arg char,i;
			if(char == $.,{
				^fileName.copyRange(0,fileName.size - (i + 2))
			})
		});
		^fileName
	}

	fileNameWithoutDoubleExtension {
		var fileName, pathName;
		fileName = this.fileNameWithoutExtension;
		pathName = PathName( fileName );
		^pathName.fileNameWithoutExtension;
	}

	extension {
		var fileName;
		fileName = this.fileName;
		fileName.reverseDo({ arg char,i;
			if(char == $.,{
				^fileName.copyRange(fileName.size - i,fileName.size - 1)
			})
		});
		^""
	}
	pathOnly {
		^fullPath.copyRange(0, this.lastColonIndex);
	}

	diskName {  ^fullPath.copyRange(0, this.colonIndices.first - 1) }

	isRelativePath { ^this.isAbsolutePath.not }

	isAbsolutePath {
		^fullPath.at(0).isPathSeparator
	}

	absolutePath{
		^fullPath.absolutePath;
	}

	asRelativePath { |relativeTo|
		var r, a, b, i, mePath;
		mePath = this.fullPath.absolutePath;
		relativeTo = (relativeTo ? scroot ).absolutePath;
		r = thisProcess.platform.pathSeparator;

		a = mePath.split(r);
		b = relativeTo.split(r);

		i=0;
		while{a[i]==b[i] and:{i<a.size}}{
		        i = i + 1;
		};
		^(".."++r).dup(b.size-i).join ++ a[i..].join(r)
	}
	asAbsolutePath {
		if(this.isAbsolutePath,{
			^fullPath
		},{
			// this assumes relative to the sc app
			// deprecated b/c String:absolutePath uses File.getcwd, more robust
//			^scroot ++ "/" ++ fullPath;
			^fullPath.absolutePath
		})
	}
	allFolders {
		var folderNames, pathCopy;
		folderNames = List.new;
		pathCopy = fullPath;

		this.colonIndices.doAdjacentPairs({ arg startColon, endColon;
			folderNames.add( fullPath.copyRange(startColon + 1, endColon - 1) );
		});

		^folderNames
	}

	folderName {
		var indexBeforeFolder,ci;
		ci = this.colonIndices;
		if (ci.isEmpty, { ^"" });

		indexBeforeFolder =
		if (ci.size == 1, 0,
			{ ci.at(ci.size - 2) + 1 });

		^fullPath.copyRange(indexBeforeFolder, this.lastColonIndex - 1);
	}

	lastColonIndex {
		var ci;
		ci = this.colonIndices;
		^if (ci.isEmpty, { -1 }, { ci.last }) ;
	}

	nextName {
		var nextName;
		nextName = if (fullPath.last.isDecDigit,
			{ this.noEndNumbers ++ (this.endNumber + 1).asString;
			},
			{fullPath ++ "1"; }
		);
		^nextName;
	}

	noEndNumbers {
		var result, count = 0, char;

		result = fullPath.copy;
		while(
			{ 	count = count + 1;
				char = fullPath.at(fullPath.size - count);
				char.notNil and: { char.isDecDigit};
			},
			{ result = result.copyRange(0,  result.size - 2) }
			);
		^result;
	}

	endNumber {	// turn consecutive digits at the end of fullPath into a number.

		var reverseNumString = "";
		var count = 0, char, number;

		while(
			{ 	count = count + 1;
				char = fullPath.at(fullPath.size - count);
				char.notNil and: { char.isDecDigit};
			},
			{ reverseNumString = reverseNumString ++ char }
			);

			// convert reverseNumString back to number (digits times powers of 10)
		number = reverseNumString.inject(0,
			{ arg sum, eachChar, i; sum = sum + (eachChar.digit * (10 ** i))
			}
		);
		^number
	}

	/* concatenation */
	+/+ { | path |
		var otherFullPath = path.respondsTo(\fullPath).if({ path.fullPath }, { path.asString });
		^this.class.new(fullPath +/+ otherFullPath)
	}

	/* additional methods jrh */

	entries {
		var path;
		path = fullPath;
		if(path.last.isPathSeparator.not, { path = path ++ thisProcess.platform.pathSeparator });
		^pathMatch(path ++ "*").collect({ arg item; PathName(item) });
	}

	pathMatch {
		^pathMatch(fullPath)
	}

	isFolder {
		var path;
		path = this.pathMatch;
		^if(path.notEmpty, {
			path.at(0).last.isPathSeparator
		}, { false });
	}
	isFile {
		var path;
		path = this.pathMatch;
		^if(path.notEmpty, {
			path.at(0).last.isPathSeparator.not
		}, { false });
	}

	files {
		^this.entries.select({ arg item; item.isFile })
	}
	folders {
		^this.entries.select({ arg item; item.isFolder })
	}
	deepFiles {
		^this.entries.collect({ arg item;
			if(item.isFile,{
				item
			},{
				item.deepFiles
			})
		}).flat
	}

	parentPath {
		var ci = this.colonIndices;

		^if((fullPath.last.isPathSeparator) && (ci.size > 1), {
			fullPath.copyRange(0, ci[ci.size - 2]);
		}, {
			fullPath.copyRange(0, this.lastColonIndex)
		});
	}

	foldersWithoutCVS { arg path;
		^this.folders(path).reject({ arg item; item.isCVS })
	}
	isCVS {
		^this.fileName == "CVS";
	}
	foldersWithoutSVN { arg path;
		^this.folders(path).reject({ arg item; item.isSVN })
	}
	isSVN {
		^this.fileName == ".svn";
	}
	filesDo { arg func;
		this.files.do(func);
		this.foldersWithoutSVN.do { arg pathname;
			pathname.filesDo(func)
		}
	}
	filesDoNoCVS { arg func;
		this.files.do(func);
		this.foldersWithoutCVS.do { arg pathname;
			pathname.filesDo(func)
		}
	}

	filesDoNoSVN { arg func;
		this.files.do(func);
		this.foldersWithoutSVN.do { arg pathname;
			pathname.filesDo(func)
		}
	}

	// Iterates over all files within this path which match criteria for being help files.
	// Doesn't iterate over the help files listed in the Help tree - see Help:do for that.
	helpFilesDo { arg func;
		var extensions    = #['html', 'htm', 'scd', 'rtf', 'rtfd']; // included
		var ignoreFolders = #['ignore', '.svn', '_darcs', 'CVS', '.git']; // excluded
		this.files.select{|afile| extensions.includes(afile.extension.asSymbol) }
			.do(func);
		this.folders.reject{|afolder| ignoreFolders.includes(afolder.folderName.asSymbol) }
			.do{ arg pathname;
				pathname.helpFilesDo(func)
			}
	}
	streamTree { arg str, tabs=0;
		str << this.fullPath << Char.nl;
		this.files.do({ arg item;
			tabs.do({ str << Char.tab });
			str << item.fileNameWithoutExtension  << Char.nl
		});
		this.foldersWithoutSVN.do({ arg item;
			item.streamTree(str, tabs + 1);
		});
	}
	streamTreeNoCVS { arg str, tabs=0;
		str << this.fullPath << Char.nl;
		this.files.do({ arg item;
			tabs.do({ str << Char.tab });
			str << item.fileNameWithoutExtension  << Char.nl
		});
		this.foldersWithoutCVS.do({ arg item;
			item.streamTree(str, tabs + 1);
		});
	}

	dumpTree {
		this.streamTree(Post)
	}

	printOn { arg stream;
		stream << "PathName(" << fullPath <<")"
	}

	dumpToDoc { arg title="Untitled";
		var str, doc;
		doc = Document.new(title);
		str = CollStream.new;
		this.streamTree(str);
		doc.string = str.collection;
		^doc
	}

}