parsing text files
//a Textfile parser utility class
//jrh 2002 with help by epic
Text {
var <>string;
*new { arg string;
^super.new.string_(string);
}
separate { arg func;
var array, indices, i0= -1;
array = [];
indices = [];
string.do({ arg char, i;
if(func.value(char, i), {
indices = indices.add(i);
});
});
indices = indices.add(string.size-1);
(indices.size - 1).do({ arg i;
var substring;
substring = string.copyRange(i0+1, indices.at(i)-1);
i0 = indices.at(i);
array = array.add(substring);
});
array = array.add(string.copyRange(i0+1, string.size-1));
^array;
}
stringToFloat { arg string;
string = string.select({ arg char; char.isDecDigit || (char == $.) });
^string.interpret;
}
replace { arg that, by;
string = string.collect({ arg char; if(char == that ,{ by }, {char}) });
}
convertDecimalCommas {
this.replace($, ,$. )
}
asSignal { arg delimiterFunc;
var signal, substring, float;
delimiterFunc = delimiterFunc ?? {{arg char; char !== Char.blank}};
signal = Signal(512);
substring = String.new;
string.do({ arg char;
if(delimiterFunc.value.not, {
substring = substring ++ char;
}, {
float = this.stringToFloat(substring);
if(float.notNil, { signal = signal.add(float) });
substring = String.new;
});
});
float = this.stringToFloat(substring);
if(float.notNil, { signal = signal.add(float) });
^signal;
}
}
______
//test: extract all numbers out of a text and convert it to a signal
var file, signal, text;
GetFileDialog({ arg ok, path;
if(ok, {
file = File(path, "rb");
text = Text(file.readAllString);
text.convertDecimalCommas;
signal = text.asSignal.asCompileString.postln;
file.close;
})
});
Links to this Page
- Public Library 3 last edited on 27 October 2006 at 9:41 pm by host81-159-217-15.range81-159.btcentralplus.com
- enter. last edited on 29 January 2003 at 10:46 pm by 203.14.169.19
- sc-zep-materialien (2002) last edited on 13 November 2003 at 7:27 pm by max2-007.dialin.uni-hamburg.de
- Public Library SC2 last edited on 2 March 2005 at 2:20 pm by max2-223.dialin.uni-hamburg.de