PixInsight Forum (historical)

Software Development => PCL and PJSR Development => Topic started by: zvrastil on 2011 August 28 04:31:32

Title: reading text file in PJSR
Post by: zvrastil on 2011 August 28 04:31:32
Hi,

is it possible to read text file, line by line, in PJSR? I can see methods for text output on File class, but not for text input.

thanks, Zbynek
Title: Re: reading text file in PJSR
Post by: Juan Conejero on 2011 August 28 07:41:45
Hi Zbynek,

Try this little script:

Code: [Select]
#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>" );
Title: Re: reading text file in PJSR
Post by: zvrastil on 2011 August 28 09:58:30
Thanks Juan. Little bit longer than I expected, but still useful  8).

Zbynek
Title: Re: reading text file in PJSR
Post by: ruediger on 2011 August 28 10:28:27
After reading the whole file content into "buffer", you can also use:
var lines = buffer.toString().split( '\n' );
and then lines.trim() (or trimLeft/trimRight) to get rid of additional, unwanted white space.

RĂ¼diger
Title: Re: reading text file in PJSR
Post by: Juan Conejero on 2011 August 29 02:26:48
Quote
Little bit longer than I expected, but still useful  8).

Well, bear in mind that my answers necessarily have to be pedagogical many times; It's my job here :)
Title: Re: reading text file in PJSR
Post by: Nocturnal on 2011 September 07 12:40:34
PixInsight motto: "Anything worth doing is worth overdoing".

Seriously though. There are lots of javascript sites online so a google/bing search will typically find you an answer quicker than anyone can post it here. Naturally the pixinsight aspects of javascript can only be found here. How to use the PJSR libraries etc.

This is a site I've used quite a bit for my C# work, lots of practical examples:

http://www.java2s.com/

It includes large sections on javascript although it's rather web centered as that's where 99.% of javascript runs.