Author Topic: reading text file in PJSR  (Read 5404 times)

Offline zvrastil

  • PixInsight Addict
  • ***
  • Posts: 179
    • Astrophotography
reading text file in PJSR
« 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

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: reading text file in PJSR
« Reply #1 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>" );
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline zvrastil

  • PixInsight Addict
  • ***
  • Posts: 179
    • Astrophotography
Re: reading text file in PJSR
« Reply #2 on: 2011 August 28 09:58:30 »
Thanks Juan. Little bit longer than I expected, but still useful  8).

Zbynek

ruediger

  • Guest
Re: reading text file in PJSR
« Reply #3 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

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: reading text file in PJSR
« Reply #4 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 :)
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline Nocturnal

  • PixInsight Jedi Council Member
  • *******
  • Posts: 2727
    • http://www.carpephoton.com
Re: reading text file in PJSR
« Reply #5 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.
Best,

    Sander
---
Edge HD 1100
QHY-8 for imaging, IMG0H mono for guiding, video cameras for occulations
ASI224, QHY5L-IIc
HyperStar3
WO-M110ED+FR-III/TRF-2008
Takahashi EM-400
PIxInsight, DeepSkyStacker, PHD, Nebulosity