[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-users] FileReader - go to line?



Hiho,

the attached (still some debug posts in there) seems to do the right thing.

a = TabFilePlayer.new( "testfile.txt" );

a.readAtLine( 3 );

a.readAtLine( 10 );

a.currentLine;

a.close;

This could easily be integrated with FileReader (can't write it as extensions, 
as I need some instance variables), or I can quarkify it instead, as a 
separate set of classes.

Some extra catch when getting at the end of the file would be useful, but for 
the rest it seems to work fine.

sincerely,
Marije


On Tuesday 16 June 2009 20:36:23 nescivi wrote:
> On Tuesday 16 June 2009 20:21:56 nescivi wrote:
> > On Tuesday 16 June 2009 20:06:02 Dan Stowell wrote:
> > > FileReader:read has a "startRow" argument.
> >
> > Yeah, but read reads all the data at once, which I want to avoid. Files
> > could be quite big.
> >
> > > If treating a FileReader as
> > > a Stream you can use things like .nextN to jump over a load of
> > > lines...
> >
> > nextN seems to jump over a lot of characters. not really lines as such,
> > it seems.
>
> My bad, it does read the next N lines.
> Still that can be quite a bit to remember... maybe have a skipNextN method
> could be useful.
>
> sincerely,
> Marije
>
> > Hmm... FileReader.new creates a var stream, which is a file.
> > The method next there gets the next line of data, so I guess I could use
> > that to skip forward some lines, by just reading it and not doing
> > anything with the data...
> > I could then keep a track of which line was at what file position, so I
> > can more easily change the position in the file afterwards.
> >
> > I think I could make it :)
> >
> > I just wondered if someone else already did to save me some work :)
> >
> > sincerely,
> > Marije
> >
> > > Dan
> > >
> > > 2009/6/17 nescivi <nescivi@xxxxxxxxx>:
> > > > Hiho,
> > > >
> > > > has anyone done things with FileReaders, like skipping to certain
> > > > lines in the file?
> > > >
> > > > This could be quite useful for controlling file playblack...
> > > >
> > > > sincerely,
> > > > Marije
>
> _______________________________________________
> sc-users mailing list
>
> info (subscription, etc.):
> http://www.beast.bham.ac.uk/research/sc_mailing_lists.shtml archive:
> https://listarc.bham.ac.uk/marchives/sc-users/
> search: https://listarc.bham.ac.uk/lists/sc-users/search/



+ Stream{

	// skips a number of lines and returns the result of the last line.
	skipNextN{ |n,inval|
		var res;
		n.do{ res = this.next(inval) };
		^res;
	}

}
FilePlayer : FileReader {

	var <>currentLine = 0;
	var <>lineMap;

	*new { | pathOrFile, skipEmptyLines=false, skipBlanks=false,  delimiter |
		var stream;
		if (pathOrFile.isKindOf(File) ) { stream = pathOrFile }  { stream =  File(pathOrFile, "r") }; 
		if (stream.isOpen.not) { warn("FileReader: file" + pathOrFile + "not found.") ^nil };
		^super.newCopyArgs(stream, skipEmptyLines, skipBlanks,  delimiter ? this.delim).myInit;
	}

	myInit{
		lineMap = Order.new;
		lineMap.put( 0, 0 );
	}

	reset{
		currentLine = 0;
		super.reset;
	}

	next{
		var res = super.next;
		this.setCurrentLine( currentLine + 1 );
		^res;
	}

	/* should not implement here
	nextN{ |n|
		var res = super.nextN(n);
		this.setCurrentLine( currentLine + n );
		^res;
	}

	skipNextN{ |n|
		var res = super.skipNextN(n);
		this.setCurrentLine( currentLine + n );
		^res;
	}
	*/

	setCurrentLine{ |cl|
		currentLine = cl;
		lineMap.put( currentLine, stream.pos );
		[ currentLine, stream.pos].postln;
	}

	goToLine{ |line|
		var ind, lmap;
		var pos = lineMap.at( line );
		if ( pos.notNil,
			{ 
				stream.pos = pos;
				currentLine = line;
			},
			{ 
				ind = lineMap.slotFor( line );
				// ind is now the index into the indices for the highest line number before the one we know.
				lmap = lineMap.indices.at( ind );
				stream.pos = lineMap.at( lmap );
				[ind, lmap, stream.pos, line, line-lmap ].postln;
				this.skipNextN( line - lmap );
			}
		);
	}

	readAtLine{ |line|
		this.goToLine( line );
		^this.next;
	}

}

TabFilePlayer : FilePlayer { 
	classvar <delim = $\t;
}

CSVFilePlayer : FilePlayer { 
	classvar <delim = $,;
}

SemiColonFilePlayer : FilePlayer { 
	classvar <delim = $;;
}