Hi Zbynek,
Try this little script:
#include <pjsr/DataType.jsh>
#define LF 0x0a
#define CR 0x0d
function TextFile( filePath )
{
/*
* Reads a text file and returns its contents as a ByteArray.
*/
this.read = function( filePath )
{
var f = new File;
f.openForReading( filePath );
var buffer = f.read( DataType_ByteArray, f.size );
f.close();
return buffer;
};
/*
* Extracts all text lines from a ByteArray and returns them as an array of strings.
*/
this.getTextLines = function( buffer )
{
var lines = new Array;
for ( var bol = 0; ; )
{
// Scan end-of-line sequence.
// Support native EOL sequences on all platforms:
// UNIX (LF), Windows (CR+LF) and old Mac (CR)
var n = 1;
var eol = buffer.linearSearch( LF, bol ); // LF
if ( eol < 0 )
{
eol = buffer.linearSearch( CR, bol ); // CR
if ( eol < 0 )
eol = buffer.length;
}
else
{
if ( eol > 0 && buffer.at( eol-1 ) == CR ) // CR+LF
{
--eol;
n = 2;
}
}
// Add this line to the list, with trailing whitespace removed
lines.push( buffer.utf8ToString( bol, eol-bol ).replace( /\s*$/g, '' ) );
// Prepare for the next line
bol = eol + n;
if ( bol > buffer.upperBound )
break;
}
return lines;
};
this.filePath = filePath;
this.lines = this.getTextLines( this.read( this.filePath ) );
}
var file = new TextFile( "/path/to/text/file" );
for ( var i in file.lines )
console.writeln( "<raw>" + file.lines[i] + "</raw>" );