hometiscriptStream

Stream object

Properties

isInput read only, true | false

reports if this is an input stream or not.

isInput read only, true | false

reports if this is an output stream or not.

isPipe read only, true | false

reports if this is a pipe stream or not.

proxy object | null

(Pipe only)

Gets/sets proxy object used as an interface of the thread for other thread - proxy of this thread on the remote side (other thread). Each instance of the pipe can have only one proxy object on either side of the pipe so duplex inter-thread communications require two pipe streams.

pending read only, integer

(Pipe only)

Returns number of pending requests (not processed at the moment) issued by post() and print**() methods.

encoding read/write - gets/sets encoding used by the stream.

For now it supports either "none" (raw byte stream) and "utf-8" encodings.

name read only, string

Name of the stream - either file name or url where the data came from. Can be an empty string, e.g. for in memory streams.

Methods

Stream object has no public constructors so it is impossible to create it using new operator.
To create streams use static open*** methods.
openFile

(file-name [,mode]) : stream | null

Static method. Opens the file which name is stored in the file-name string and returns an instance of Stream object. Operations allowed to the stream returned are defined by the mode parameter-string.

Script engine uses C/C++ runtime for opening file streams. See fopen function definition for the meaning of the mode string.

openSocket

("address:port" [, timeout[, numberOfAttempts:1 ] ] ) :  stream | null

Static method. Opens the socket stream which address and port is stored in the address-port string and returns an instance of Stream object. Opens socket stream in read-write mode. Address can be either domain name or IP address. Format of address:port string is "domain:NNN" or "NNN.NNN.NNN.NNN:NNN" where N is a decimal digit. timeout is a number of seconds to wait on any operations on this socket. If execution of operation on this socket will take more than this limit then exception will be thrown by runtime system.

numberOfAttempts is an integer - number of attempts to connect, 1 by default.

Example: following code will print out http server response of terrainformatica.com server:

var sock = Stream.openSocket( "www.terrainformatica.com:80" , 5 /*seconds timeout*/ );
if( !sock ) return;

sock.println("GET http://www.terrainformatica.com/main.whtm HTTP/1.0");
sock.println("User-Agent: TIScript [en]");
sock.println("");

while( true )
{
  var s = sock.readln();
  if(s == undefined) break;
  stdout.println(s);
}
openString
([initialSize:int | initialValue:string]) : stream

Static method. Opens in-memory string output stream with initialSize (integer) of its buffer. Use string streams when you plan to update some string frequently or compose string from many components. String streams are also known as StringBuffer/StringBuilder in Java or .NET. To get current content of the string stream use its method toString.

openPipe
( [receiver: function [, proxy: object]] ) : stream

Creates so called pipe - stream used for inter-thread communications. Pipe is shared between two threads allowing two threads to  communicate. See methods send(), post() and properties pending and proxy.

The receiver is the function that has following signature: function recv(value). This function will be called when data printed by print(), println() or printf() will arrive from other side of the pipe.

The proxy object is an object that is used as an interface with functions accessible from the alien thread (VM) sitting on other side of the pipe, See methods send() and post() below.

Example, following fragment (from ScIDE/facades.tis) creates three pipes that are used as stdin, stdout and stderr streams in the alien VM (other ScIDE window running is separate thread).

  function receiverOut(data) { Log.print(data,false); }
  function receiverErr(data) { Log.print(data,true); }

  // callback interface
  var iface =
  {
    foo: function(v1,v2) { stdout.printf("got a call of 'foo' with %V,%V\n",v1,v2); return v1 + v2; }
  };

  view.open(f,
      Stream.openPipe( null, iface ), // call interface for the alien
      Stream.openPipe( receiverOut ), // stdout
      Stream.openPipe( receiverErr )  // stderr
  );
toString

( ) :  stream

Returns content of string buffer if this is a string stream or name/address of the stream if it was open as file or socket stream.

close

( [returnString:true|false] ) : string | true | false

Closes this stream - file, socket or string stream buffer. If returnString is true then return value is a result of toString() method.

print
( string ) : true | false

Outputs string into the stream.

print is an equivalent of: stream << string; operation.

$
( ... ) : true | false

Stringizer method, outputs content in brackets into the stream.

println
( string ) : true | false

Outputs string appended by \r\n into the stream.

$n
( string ) : true | false

Stringizer method, outputs content in brackets appended by \r\n into the stream.

printf
( format:string, [value1[, value2[, ...[, valueN]]]]) : true | false

Prints formatted text by the rules of printf C/C++ function.

Additional format types:

    %v and %V - these format types accept any value as an argument and produce source code representation of the value suitable for later parsing by eval() method. Thus if value is an array of values it will be printed as "[element1, element2, element3... elementN]" and object (instance of Object class) will be printed as "{key1:value1, key2:value2,..., keyN:valueN}". %v produces one line output and %V tries to produce human readable output with line feeds and tabulations.
    Use it if you need to serialize objects in AJAX/JSON fashion.

    %S - this format type converts its argument into string and outputs it with HTML escapement. So characters like '<' will be converted to "&lt;" sequences in the output.

scanf
( format: string ) : array

Scans the stream for values according to the rules of scanf C/C++ function with wildcard (like %[a-z] ) extensions. See definition of  scanf in MSDN.

Additional format types:

    %N - scans for either float or integer number.

Function returns array of successfully parsed values.

putc
( char-code ) : true | false

Outputs character into the stream. Character defined by its integer code.

putc is an equivalent of: stream << charcode; operation.

getc
( ) : integer | undefined

Reads one character from the stream. Returns its code as integer or undefined if stream was closed or got EOF.

readln
( ) : string | undefined

Reads sequence of characters from stream until '\n' character. Returns string read without trailing '\r' and '\n'.

send
(methodName: symbol [, .. ] ) : value

(Pipe only)

Sends call request to the proxy interface object on other side of the pipe and waits its completion. Returns result of the remote method. Additional parameters that follow the methodName are passed to the call of remote method as normal parameters.

post
(methodName: symbol [, .. ] ) : undefined

(Pipe only)

Posts call request to the proxy interface object on other side of the pipe. This method does not wait for the completion of the remote method it just increases the pending counter. Additional parameters that follow the methodName are passed to the call of remote method as normal parameters.

TIScript

Classes
Download