8. I/O

  CLASS Stdio.File

Description

This is the basic I/O object, it provides socket communication as well as file access. It does not buffer reads and writes or provide line-by-line reading, that is done with Stdio.FILE object. All functions available in Stdio.Fd are available here as well.


Method tcgetattr
Method tcsetattr

mapping tcgetattr()
int tcsetattr(mapping attr)
int tcsetattr(mapping attr, string when)

Description

gets/sets term attributes

the returned value/the parameter is a mapping on the form

        "ispeed":    baud rate
        "ospeed":    baud rate
        "csize":     character size (5,6,7 or 8)
        "rows":      terminal rows
        "columns":   terminal columns
        flag:        0 or 1
        control char:value
        ...
where 'flag' is the string describing the termios (input, output, control and local mode) flags (see the manpage for termios or the tutorial).

Note that tcsetattr always _changes_ the attributes; correct use to set a flag would be something like: fd->tcsetattr((["myflag":1]));

the argument 'when' to tcsetattr describes when the changes are to take effect: "TCSANOW": the change occurs immediately (default); "TCSADRAIN": the change occurs after all output has been written; "TCSAFLUSH": the change occurs after all output has been written, and empties input buffers.

Example for setting the terminal in raw mode: Stdio.stdin->tcsetattr((["ECHO":0,"ICANON":0,"VMIN":0,"VTIME":0]));

Note: Unknown flags are ignored by tcsetattr(). Terminal rows and columns settring by tcsetattr() is not currently supported.

Returns

0 if failed.


Method errno

int errno()

Description

Returns the error code for the last command on this file. Error code is normally cleared when a command is successful.


Method open

int open(string filename, string mode)
int open(string filename, string mode, int mask)

Description

Open a file for read, write or append. The parameter mode should contain one or more of the following letters:

'r'

Open file for reading.

'w'

Open file for writing.

'a'

Open file for append (use with 'w').

't'

Truncate file at open (use with 'w').

'c'

Create file if it doesn't exist (use with 'w').

'x'

Fail if file already exists (use with 'c').


mode should always contain at least one of the letters 'r' or 'w'.

The parameter mask is protection bits to use if the file is created. Default is 0666 (read+write for all in octal notation).

Returns

This function returns 1 for success, 0 otherwise.

See also

Stdio.File.close, Stdio.File.create


Method open_socket

int open_socket(int|void port, string|void address)

Description

This makes this file into a socket ready for connections. The reason for this function is so that you can set the socket to nonblocking or blocking (default is blocking) before you call Stdio.File.connect.

If you give a port number to this function, the socket will be bound to this port locally before connecting anywhere. This is only useful for some silly protocols like FTP. You may also specify an address to bind to if your machine has many IP numbers.

Returns

This function returns 1 for success, 0 otherwise.

See also

Stdio.File.connect, Stdio.File.set_nonblocking, Stdio.File.set_blocking


Method connect

int connect(string host, int port, void|string client, void|int client_port)

Description

This function connects a socket previously created with Stdio.File.open_socket to a remote socket through TCP/IP. The host argument is the hostname or IP number of the remote machine. A local IP and port can be explicitly bound by specifying client and client_port.

Returns

This function returns 1 for success, 0 otherwise.

Note

In nonblocking mode 0 (zero) may be returned and Stdio.File.errno set to EWOULDBLOCK or WSAEWOULDBLOCK. This should not be regarded as a connection failure. In nonblocking mode you need to wait for a write or close callback before you know if the connection failed or not.

See also

query_address, Stdio.File.async_connect, Stdio.File.connect_unix


Method connect_unix

int connect_unix(string path)

Description

Open a UNIX domain socket connection to the specified destination.

Returns

Returns 1 on success, and 0 on failure.

Note

Nonblocking mode is not supported while connecting


Method read_function

function(:string) read_function(int nbytes)

Description

Returns a function that when called will call read with nbytes as argument. Can be used to get various callback functions, as an example the fourth argument to String.SplitIterator.


Method line_iterator

object line_iterator(int|void trim)

Description

Returns an iterator that will loop over the lines in this file. If trim is true, all '\r' characters will be removed from the input.


Method async_connect

int async_connect(string host, int port, function(int:void) callback, mixed ... args)

Description

Open a TCP/IP connection asynchronously.

This function is similar to Stdio.File.connect, but works asynchronously.

Parameter host

Hostname or IP to connect to.

Parameter port

Port number to connect to.

Parameter callback

Function to be called on completion. The first argument will be 1 if a connection was successfully estabished, and 0 (zero) on failure. The rest of the arguments to callback are passed verbatim from args.

Parameter args

Extra arguments to pass to callback.

Returns

Returns 0 on failure, and 1 if callback will be used.

Note

The socket may be opened with Stdio.File.open_socket ahead of the call to this function, but it is not required.

For callback to be called, the backend must be active (ie main must have returned -1).

The socket will be in non-blocking state if 1 has been returned, and any callbacks will be cleared.

See also

Stdio.File.connect, Stdio.File.open_socket, Stdio.File.set_nonblocking


Method pipe

File pipe(void|int how)

Description

This function creates a bi-directional pipe between the object it was called in and an object that is returned. The two ends of the pipe are indistinguishable. If the File object this function is called in was open to begin with, it will be closed before the pipe is created.

FIXME

Document the PROP_ properties.

See also

Process.create_process


Method create

void Stdio.File()
void Stdio.File(string filename)
void Stdio.File(string filename, string mode)
void Stdio.File(string filename, string mode, int mask)
void Stdio.File(string descriptorname)
void Stdio.File(int fd)
void Stdio.File(int fd, string mode)

Description

There are four basic ways to create a Stdio.File object. The first is calling it without any arguments, in which case the you'd have to call Stdio.File.open, Stdio.File.connect or some other method which connects the File object with a stream.

The second way is calling it with a filename and open mode. This is the same thing as cloning and then calling Stdio.File.open, except shorter and faster.

The third way is to call it with descriptorname of "stdin", "stdout" or "stderr". This will open the specified standard stream.

For the advanced users, you can use the file descriptors of the systems (note: emulated by pike on some systems - like NT). This is only useful for streaming purposes on unix systems. This is not recommended at all if you don't know what you're into. Default mode for this is "rw".

Note

Open mode will be filtered through the system UMASK. You might need to use chmod later.

See also

Stdio.File.open, Stdio.File.connect, Stdio.FILE,


Method assign

int assign(File|Fd o)

Description

This function takes a clone of Stdio.File and assigns all variables of this file from it. It can be used together with Stdio.File.dup to move files around.

See also

Stdio.File.dup


Method dup

File dup()

Description

This function returns a clone of Stdio.File with all variables copied from this file.

Note

All variables, even id, are copied.

See also

Stdio.File.assign


Method close

int close()
int close(string direction)

Description

Close the file. Optionally, specify "r", "w" or "rw" to close just the read, just the write or both read and write directions of the file respectively.

Note

This function will not call the close_callback.

See also

Stdio.File.open, Stdio.File.open_socket


Method set_read_callback

void set_read_callback(function(mixed:void) read_cb)

Description

This function sets the read_callback for the file. The read_callback is called whenever there is data to read from the file.

The callback is called with the id of the file as first argument and some or all of its data as second.

Note

This function does not set the file nonblocking.

See also

Stdio.File.set_nonblocking, read, Stdio.File.query_read_callback, Stdio.File.set_write_callback, Stdio.File.set_close_callback, Stdio.File.set_read_oob_callback Stdio.File.set_write_oob_callback, Stdio.File.set_id


Method query_read_callback

function(mixed:void) query_read_callback()

Description

This function returns the read_callback, which has been set with Stdio.File.set_nonblocking or Stdio.File.set_read_callback.

See also

Stdio.File.set_nonblocking, Stdio.File.set_read_callback


Method set_write_callback

void set_write_callback(function(mixed:void) write_cb)

Description

This function sets the write_callback for the file. The write_callback is called whenever there is buffer space available to write to for the file.

The callback is called with the id of the file as argument.

Note

This function does not set the file nonblocking.

See also

Stdio.File.set_nonblocking, write, Stdio.File.query_write_callback, Stdio.File.set_read_callback, Stdio.File.set_close_callback, Stdio.File.set_read_oob_callback Stdio.File.set_write_oob_callback, Stdio.File.set_id


Method query_write_callback

function(mixed:void) query_write_callback()

Description

This function returns the write_callback, which has been set with Stdio.File.set_nonblocking or Stdio.File.set_write_callback.

See also

Stdio.File.set_nonblocking, Stdio.File.set_write_callback


Method set_read_oob_callback

void set_read_oob_callback(function(mixed:void) read_oob_cb)

FIXME

Document this function.


Method query_read_oob_callback

function(mixed:void) query_read_oob_callback()

FIXME

Document this function.


Method set_write_oob_callback

void set_write_oob_callback(function(mixed:void) write_oob_cb)

FIXME

Document this function.


Method query_write_oob_callback

function(mixed:void) query_write_oob_callback()

FIXME

Document this function.


Method set_close_callback

void set_close_callback(function(mixed:void) close_cb)

Description

This function sets the close_callback for the file. The close callback is called when the remote end of a socket or pipe is closed.

The callback is called with the id of the file as argument.

Note

This function does not set the file nonblocking.

See also

Stdio.File.set_nonblocking, Stdio.File.close Stdio.File.query_close_callback, Stdio.File.set_read_callback, Stdio.File.set_write_callback, Stdio.File.set_id


Method query_close_callback

function(mixed:void) query_close_callback()

Description

This function returns the close_callback, which has been set with Stdio.File.set_nonblocking or Stdio.File.set_close_callback.

See also

Stdio.File.set_nonblocking, Stdio.File.set_close_callback


Method set_id

void set_id(mixed id)

Description

This function sets the id of this file. The id is mainly used as an identifier that is sent as the first argument to all callbacks. The default id is 0 (zero). Another possible use of the id is to hold all data related to this file in a mapping or array.

See also

Stdio.File.query_id


Method query_id

mixed query_id()

Description

This function returns the id that has been set with Stdio.File.set_id.

See also

Stdio.File.set_id


Method set_nonblocking

void set_nonblocking(function(mixed:void) read_callback, function(mixed:void) write_callback, function(mixed:void) close_callback)
void set_nonblocking(function(mixed:void) read_callback, function(mixed:void) write_callback, function(mixed:void) close_callback, function(mixed:void) read_oob_callback, function(mixed:void) write_oob_callback)
void set_nonblocking()

Description

This function sets a stream to nonblocking mode. When data arrives on the stream, read_callback will be called with some or all of this data. When the stream has buffer space over for writing, write_callback will be called so that you can write more data to it. If the stream is closed at the other end, close_callback will be called.

When out-of-band data arrives on the stream, read_oob_callback will be called with some or all of this data. When the stream allows out-of-band data to be sent, write_oob_callback will be called so that you can write out-of-band data to it.

All callbacks will have the id of file as first argument when called (see Stdio.File.set_id).

If no arguments are given, the callbacks will not be changed. The stream will just be set to nonblocking mode.

Note

Out-of-band data will note be supported if Pike was compiled with the option '--without-oob'.

See also

Stdio.File.set_blocking, Stdio.File.set_id, Stdio.File.set_read_callback, Stdio.File.set_write_callback, Stdio.File.set_close_callback


Method set_blocking

void set_blocking()

Description

This function sets a stream to blocking mode. i.e. all reads and writes will wait until data has been transferred before returning.

See also

Stdio.File.set_nonblocking


Method set_nonblocking_keep_callbacks
Method set_blocking_keep_callbacks

void set_nonblocking_keep_callbacks()
void set_blocking_keep_callbacks()

Description

toggle between blocking and nonblocking, without changing the callbacks

  CLASS Stdio.FILE

Inherits
  • File
Description

Stdio.FILE is a buffered version of Stdio.File, it inherits Stdio.File and has most of the functionality of Stdio.File. However, it has an input buffer that allows line-by-line input. Note that the output part of Stdio.FILE is not buffered at this moment.


Method set_charset

void set_charset(string charset)

Description

Sets the input and output charset of this file to the specified charset.


Method gets

string gets()

Description

Read one line of input.

Returns

This function returns the line read if successful, and 0 if no more lines are available.

See also

ngets, read, Stdio.FILE.line_iterator


Method printf

int printf(string format, mixed ... data)

Description

This function does approximately the same as:

write(sprintf(format,@data))
.

See also

write, sprintf


Method _get_iterator

object _get_iterator()

Description

Returns an iterator that will loop over the lines in this file.

See also

Stdio.FILE.line_iterator


Method line_iterator

object line_iterator(int|void trim)

Description

Returns an iterator that will loop over the lines in this file. If trim is true, all '\r' characters will be removed from the input.

See also

Stdio.FILE._get_iterator


Method ungets

void ungets(string s)

Description

This function puts a string back in the input buffer. The string can then be read with eg read, Stdio.FILE.gets or Stdio.FILE.getchar.

See also

read, Stdio.FILE.gets, Stdio.FILE.getchar


Method getchar

int getchar()

Description

This function returns one character from the input stream.

Returns

Returns the ASCII value of the character.

Note

Returns an int and not a string of length 1.

  CLASS Stdio.Port

Description

Handles listening to socket ports. Whenever you need a bound socket that is open and listens for connections you should use this program.


Method set_id

mixed set_id(mixed id)

Description

This function sets the id used for accept_callback by this port. The default id is this_object.

See also

Stdio.Port.query_id


Method query_id

mixed query_id()

Description

This function returns the id for this port. The id is normally the first argument to accept_callback.

See also

Stdio.Port.set_id


Method errno

int errno()

Description

If the last call done on this port failed, errno will return an integer describing what went wrong. Refer to your unix manual for further information.


Method listen_fd

int listen_fd(int fd, void|function accept_callback)

Description

This function does the same as port->bind, except that instead of creating a new socket and bind it to a port, it expects that the filedescriptor 'fd' is an already open port.

Note

This function is only for the advanced user, and is generally used when sockets are passed to Pike at exec time.

See also

Stdio.Port.bind, Stdio.Port.accept


Method bind

int bind(int port, void|function accept_callback, void|string ip)

Description

Bind opens a sockets and binds it to port number on the local machine. If the second argument is present, the socket is set to nonblocking and the callback funcition is called whenever something connects to the socket. The callback will receive the id for this port as argument. Bind returns 1 on success, and zero on failiure.

If the optional argument 'ip' is given, bind will try to bind to this ip name or number.

See also

Stdio.Port.accept


Method query_address

string query_address(string arg1)

FIXME

Document this function.


Method create

void Stdio.Port()
void Stdio.Port(int port)
void Stdio.Port(int port, function accept_callback)
void Stdio.Port(int port, function accept_callback, string ip)
void Stdio.Port("stdin")
void Stdio.Port("stdin", function accept_callback)

Description

If the first argument is other than "stdin" the arguments will be passed to Stdio.Port.bind.

When create is called with "stdin" as the first argument, a socket is created out of the file descriptor 0. This is only useful if that actually is a socket to begin with.

See also

Stdio.Port.bind


Method accept

File accept()

Description

This function completes a connection made from a remote machine to this port. It returns a two-way stream in the form of a clone of Stdio.File. The new file is by default set to blocking mode.

See also

Stdio.File

  CLASS Stdio.UDP

Description

UDP (User Datagram Protocol) handling.


Method bind

object bind(int port)
object bind(int port, string address)

Description

Binds a port for recieving or transmitting UDP.


Method enable_broadcast

int(0..1) enable_broadcast()

Description

Set the broadcast flag. If enabled then sockets receive packets sent to a broadcast address and they are allowed to send packets to a broadcast address.

Returns

Returns 1 on success, 0 (zero) otherwise.

Note

This is normally only avalable to root users.


Method wait

int(0..1) wait(int|float timeout)

Description

Check for data and wait max. timeout seconds.

Returns

Returns 1 if data are ready, 0 (zero) otherwise.


Method read

mapping(string:int|string) read()
mapping(string:int|string) read(int flag)

Description

Read from the UDP socket.

Flag flag is a bitfield, 1 for out of band data and 2 for peek

Returns

mapping(string:int|string) in the form ([ "data" : string recieved data "ip" : string recieved from this ip "port" : int ...and this port ])

See also

Stdio.UDP.set_read_callback


Method send

int send(string to, int port, string message)
int send(string to, int port, string message, int flags)

Description

Send data to a UDP socket. The recepient address will be to and port will be port.

Flag flag is a bitfield, 1 for out of band data and 2 for don't route flag.

Returns

The number of bytes that were actually written.


Method set_blocking

object set_blocking()

Description

Sets this object to be blocking.


Method connect

int(0..1) connect(string address, int port)

Description

Connect a socket to something.

This function connects a socket previously created with open_socket to a remote socket. The argument is the IP name or number for the remote machine.

Returns

Returns 1 on success, 0 (zero) otherwise.

Note

If the socket is in nonblocking mode, you have to wait for a write or close callback before you know if the connection failed or not.

See also

Stdio.UDP.query_address


Method query_address

string query_address()

Description

Returns the local address of a socket on the form "x.x.x.x port". If this file is not a socket, not connected or some other error occurs, zero is returned.


Method errno

int errno()

Description

Returns the error code for the last command on this object. Error code is normally cleared when a command is successful.


Method set_type

object set_type(int sock_type)
object set_type(int sock_type, int family)

Description

Sets socket type and protocol family.


Method get_type

array(int) get_type()

Description

Returns socket type and protocol family.


Constant MSG_OOB

constant MSG_OOB

FIXME

Document this constant.


Constant MSG_PEEK

constant MSG_PEEK

FIXME

Document this constant.


Method set_nonblocking

UDP set_nonblocking()
UDP set_nonblocking(function(mapping(string:int|string):void) read_cb, mixed ... extra_args)

Description

Set this object to nonblocking mode.

If read_cb and extra_args are specified, they will be passed on to Stdio.UDP.set_read_callback.

Returns

The called object.


Method set_read_callback

UDP set_read_callback(function(mapping(string:int|string):) read_cb, mixed ... extra_args)

Description

The read_cb function will receive a mapping similar to the mapping returned by Stdio.UDP.read:

"data" : string

Received data.

"ip" : string

Data was sent from this IP.

"port" : int

Data was sent from this port.


Returns

The called object.

See also

Stdio.UDP.read

  Module Stdio.Terminfo


Method getTermcap

Termcap Stdio.Terminfo.getTermcap(string term)

Description

Returns the terminal description object for term from the systems termcap database. Returns 0 if not found.

See also

Stdio.Terminfo.getTerm, Stdio.Terminfo.getTerminfo


Method getTerminfo

Terminfo Stdio.Terminfo.getTerminfo(string term)

Description

Returns the terminal description object for term from the systems terminfo database. Returns 0 if not found.

See also

Stdio.Terminfo.getTerm, Stdio.Terminfo.getTermcap


Method getTerm

Termcap Stdio.Terminfo.getTerm(string|void term)

Description

Returns an object describing the terminal term. If term is not specified, it will default to getenv or if that fails to "dumb".

Lookup of terminal information will first be done in the systems terminfo database, and if that fails in the termcap database. If neither database exists, a hardcoded entry for "dumb" will be used.

See also

Stdio.Terminfo.getTerminfo, Stdio.Terminfo.getTermcap, Stdio.getFallbackTerm


Method getFallbackTerm

Termcap Stdio.Terminfo.getFallbackTerm(string term)

Description

Returns an object describing the fallback terminal for the terminal term. This is usually equvivalent to Stdio.Terminfo.getTerm.

See also

Stdio.Terminfo.getTerm

  CLASS Stdio.Terminfo.Termcap

Inherits
  • TermMachine
Description

Termcap terminal description object.


Variable aliases

array(string) aliases


Method tputs

string tputs(string s)

Description

Put termcap string


Method create

void Stdio.Terminfo.Termcap(string cap, TermcapDB|void tcdb, int|void maxrecurse)

  CLASS Stdio.Terminfo.Terminfo

Inherits
  • TermMachine
Description

Terminfo terminal description object


Variable aliases

array(string) aliases


Method tputs

string tputs(string s)

FIXME

Document this function


Method create

void Stdio.Terminfo.Terminfo(string filename)

  CLASS Stdio.Readline


Method get_output_controller

OutputController get_output_controller()

FIXME

Document this function


Method get_input_controller

InputController get_input_controller()

FIXME

Document this function


Method get_prompt

string get_prompt()

FIXME

Document this function


Method set_prompt

string set_prompt(string newp, array(string)|void newattrs)

FIXME

Document this function


Method set_echo

void set_echo(int onoff)

FIXME

Document this function


Method gettext

string gettext()

FIXME

Document this function


Method getcursorpos

int getcursorpos()

FIXME

Document this function


Method setcursorpos

int setcursorpos(int p)

FIXME

Document this function


Method setmark

int setmark(int p)

FIXME

Document this function


Method getmark

int getmark()

FIXME

Document this function


Method insert

void insert(string s, int p)

FIXME

Document this function


Method delete

void delete(int p1, int p2)

FIXME

Document this function


Method pointmark

array(int) pointmark()

FIXME

Document this function


Method region

string region(int ... args)

FIXME

Document this function


Method kill

void kill(int p1, int p2)

FIXME

Document this function


Method add_to_kill_ring

void add_to_kill_ring(string s)

FIXME

Document this function


Method kill_ring_yank

string kill_ring_yank()

FIXME

Document this function


Method history

void history(int n)

FIXME

Document this function


Method delta_history

void delta_history(int d)

FIXME

Document this function


Method redisplay

void redisplay(int clear, int|void nobackup)

FIXME

Document this function


Method newline

string newline()

FIXME

Document this function


Method eof

void eof()

FIXME

Document this function


Method message

void message(string msg)

FIXME

Document this function


Method write

void write(string msg, void|int word_wrap)

FIXME

Document this function


Method list_completions

void list_completions(array(string) c)

FIXME

Document this function


Method set_nonblocking

void set_nonblocking(function f)

FIXME

Document this function


Method set_blocking

void set_blocking()

FIXME

Document this function


Method edit

string edit(string data, string|void local_prompt, array(string)|void attrs)

FIXME

Document this function


Method read

string read(string|void prompt, array(string)|void attrs)

FIXME

Document this function


Method enable_history

void enable_history(array(string)|History|int hist)

FIXME

Document this function


Method get_history

History get_history()

FIXME

Document this function


Method destroy

void destroy()

FIXME

Document this function


Method create

void Stdio.Readline(object|void infd, object|string|void interm, object|void outfd, object|string|void outterm)

Description

Creates a Readline object, that takes input from infd and has output on outfd.

Parameter infd

Defaults to Stdio.stdout.

Parameter interm

Defaults to Stdio.Terminfo.getTerm.

Parameter outfd

Defaults to infd, unless infd is 0, in which case outfd defaults to Stdio.stdout.

Parameter outterm

Defaults to interm.

  Module Stdio

Description

An instance of FILE("stdin"), the standard input stream. Use this when you want to read anything from the standard input. This example will read lines from standard input for as long as there are more lines to read. Each line will then be written to stdout together with the line number. We could use Stdio.stdout.write instead of just write, since they are the same function.

Example

int main() { int line; while(string s=Stdio.stdin.gets()) write(sprintf("%5d: %s\n",line++,s)); }


Method get_all_active_fds

array(int) Stdio.get_all_active_fds()

Description

Returns the id of all the active file descriptors.


Constant PROP_BIDIRECTIONAL

constant Stdio.PROP_BIDIRECTIONAL

FIXME

Document this constant.


Constant PROP_BUFFERED

constant Stdio.PROP_BUFFERED

FIXME

Document this constant.


Constant PROP_SHUTDOWN

constant Stdio.PROP_SHUTDOWN

FIXME

Document this constant.


Constant PROP_NONBLOCK

constant Stdio.PROP_NONBLOCK

FIXME

Document this constant.


Constant PROP_IPC

constant Stdio.PROP_IPC

FIXME

Document this constant.


Constant IPPROTO

constant Stdio.IPPROTO

FIXME

Document this constant.


Constant __OOB__

constant Stdio.__OOB__

Description

Implementation level of nonblocking I/O OOB support.

0

Nonblocking OOB support is not supported.

1

Nonblocking OOB works a little.

2

Nonblocking OOB almost works.

3

Nonblocking OOB works as intended.

-1

Unknown level of nonblocking OOB support.


This constant only exists when OOB operations are available, i.e. when Stdio.__HAVE_OOB__ is 1.


Constant __HAVE_OOB__

constant Stdio.__HAVE_OOB__

Description

Exists and has the value 1 if OOB operations are available.


Variable stderr

File Stdio.stderr

Description

An instance of FILE("stderr"), the standard error stream. Use this when you want to output error messages.


Variable stdout

File Stdio.stdout

Description

An instance of FILE("stdout"), the standatd output stream. Use this when you want to write anything to the standard output.


Method read_file

string Stdio.read_file(string filename)
string Stdio.read_file(string filename, int start, int len)

Description

Read len lines from a file filename after skipping start lines and return those lines as a string. If both start and len are omitted the whole file is read.

See also

Stdio.read_bytes, Stdio.write_file


Method read_bytes

string Stdio.read_bytes(string filename, int start, int len)
string Stdio.read_bytes(string filename, int start)
string Stdio.read_bytes(string filename)

Description

Read len number of bytes from the file filename starting at byte start, and return it as a string.

If len is omitted, the rest of the file will be returned.

If start is also omitted, the entire file will be returned.

Throws

Throws an error if filename isn't a regular file.

Returns

Returns 0 (zero) on failure to open filename.

Returns a string with the requested data otherwise.

See also

Stdio.read_file, Stdio.write_file, Stdio.append_file


Method write_file

int Stdio.write_file(string filename, string str, int|void access)

Description

Write the string str onto the file filename. Any existing data in the file is overwritten.

For a description of access, see Stdio.File.

Throws

Throws an error if filename couldn't be opened for writing.

Returns

Returns the number of bytes written.

See also

Stdio.append_file, Stdio.read_bytes, Stdio.File


Method append_file

int Stdio.append_file(string filename, string str, int|void access)

Description

Append the string str onto the file filename.

For a description of access, see Stdio.File.open.

Throws

Throws an error if filename couldn't be opened for writing.

Returns

Returns the number of bytes written.

See also

Stdio.write_file, Stdio.read_bytes, Stdio.File


Method file_size

int Stdio.file_size(string filename)

Description

Give the size of a file. Size -1 indicates that the file either does not exist, or that it is not readable by you. Size -2 indicates that it is a directory.

See also

file_stat, Stdio.write_file, Stdio.read_bytes


Method append_path

string Stdio.append_path(string absolute, string ... relative)

Description

Append relative paths to an absolute path and remove any "//", "../" or "/." to produce a straightforward absolute path as a result.

"../" is ignorded in the relative paths if it makes the created path begin with something else than the absolute path (or so far created path).

Note

Warning: This does not work on NT. (Consider paths like: k:/fnord)

See also

combine_path


Method simplify_path

string Stdio.simplify_path(string path)

Description

Returns a canonic representation of path (without /./, /../, // and similar path segments).


Method expand_symlinks

string|int(0..0) Stdio.expand_symlinks(string path)

Description

Unwinds all symlinks along the directory trail path, returning a path with no symlink components or 0, in case path does not exist, for instance because one of its links pointed to a nonexistent file or if there was a symlink loop. The returned path is also canonicized/simplified, removing "//", "/./" and the like.


Method perror

void Stdio.perror(string s)

Description

This function prints a message to stderr along with a description of what went wrong if available. It uses the system errno to find out what went wrong, so it is only applicable to IO errors.

See also

Stdio.werror


Method is_file

int Stdio.is_file(string path)

Description

Check if a path is a file.

Returns

Returns true if the given path is a file, otherwise false.

See also

Stdio.exist, Stdio.is_dir, Stdio.is_link, file_stat


Method is_dir

int Stdio.is_dir(string path)

Description

Check if a path is a directory.

Returns

Returns true if the given path is a directory, otherwise false.

See also

Stdio.exist, Stdio.is_file, Stdio.is_link, file_stat


Method is_link

int Stdio.is_link(string path)

Description

Check if a path is a symbolic link.

Returns

Returns true if the given path is a symbolic link, otherwise false.

See also

Stdio.exist, Stdio.is_dir, Stdio.is_file, file_stat


Method exist

int Stdio.exist(string path)

Description

Check if a path exists.

Returns

Returns true if the given path exists (is a directory or file), otherwise false.

See also

Stdio.is_dir, Stdio.is_file, Stdio.is_link, file_stat


Method file_equal

int Stdio.file_equal(string file_1, string file_2)

Description

Returns nonzero if the given paths are files with identical content, returns zero otherwise. Zero is also returned for any sort of I/O error.


Method async_cp

void Stdio.async_cp(string from, string to, function(int:void) callback, mixed ... args)

Description

Copy a file asynchronously.

This function is similar to cp, but works asynchronously.

Parameter from

Name of file to copy.

Parameter to

Name of file to create or replace with a copy of from.

Parameter cb

Function to be called on completion. The first argument will be 1 on success, and 0 (zero) otherwise. The rest of the arguments tp callback are passed verbatim from args.

Parameter args

Extra arguments to pass to callback.

Note

For callback to be called, the backend must be active (ie main must have returned -1).

Bugs

Currently the file sizes are not compared, so the destination file (to) may be truncated.

See also

cp, Stdio.sendfile


Method mkdirhier

int Stdio.mkdirhier(string pathname, void|int mode)

Description

Creates zero or more directories to ensure that the given pathname is a directory.

If a mode is given, it's used for the new directories after being &'ed with the current umask (on OS'es that support this).

Returns

Returns zero if it fails and nonzero if it is successful.

See also

mkdir


Method recursive_rm

int Stdio.recursive_rm(string path)

Description

Remove a file or directory a directory tree.

Returns

Returns 0 on failure, nonzero otherwise.

See also

rm


Method sendfile

object Stdio.sendfile(array(string) headers, File from, int offset, int len, array(string) trailers, File to)
object Stdio.sendfile(array(string) headers, File from, int offset, int len, array(string) trailers, File to, function(int:void) callback, mixed ... args)

Description

Sends headers followed by len bytes starting at offset from the file from followed by trailers to the file to. When completed callback will be called with the total number of bytes sent as the first argument, followed by args.

Any of headers, from and trailers may be left out by setting them to 0.

Setting offset to -1 means send from the current position in from.

Setting len to -1 means send until from's end of file is reached.

Note

The sending is performed asynchronously, and may complete before the function returns.

For callback to be called, the backend must be active (ie main must have returned -1).

In some cases, the backend must also be active for any sending to be performed at all.

Bugs

FIXME: Support for timeouts?

See also

Stdio.File.set_nonblocking


Method werror

void Stdio.werror(string s)

Description

Write a message to stderr. Stderr is normally the console, even if the process output has been redirected to a file or pipe.

  CLASS Stdio.Fd_ref

Description

Proxy class that contains stub functions that call the corresponding functions in Stdio.Fd.

Used by Stdio.File.


Variable _fd

Fd _fd

Description

Object to which called functions are relayed.

  CLASS Stdio.Fd

Description

Low level I/O operations. Use Stdio.File instead.


Method read

string read()
string read(int len)
string read(int len, int(0..1) not_all)

Description

Read data from a file or a string.

Attempts to read len bytes from the file, and return it as a string. If something goes wrong, zero is returned.

If a one is given as the second argument to Stdio.Fd.read, it will not try its best to read as many bytes as you have asked for, but will merely try to read as many bytes as the system read function will return. This mainly useful with stream devices which can return exactly one row or packet at a time.

If no arguments are given, Stdio.Fd.read will read to the end of the file/stream.

See also

Stdio.Fd.read_oob, Stdio.Fd.write


Method peek

int(-1..1) peek()
int(-1..1) peek(int|float timeout)

Description

Check if there is data available to read, or wait some time for available data to read.

Returns 1 if there is data available to read, 0 (zero) if there is no data available, and -1 if something went wrong.

See also

Stdio.Fd.errno, Stdio.Fd.read

Note

The function may be interrupted prematurely of the timeout (due to signals); check the timing manually if this is imporant.


Method read_oob

string read_oob()
string read_oob(int len)
string read_oob(int len, int(0..1) not_all)

Description

Read out-of-band data from a stream.

Attempts to read len bytes of out-of-band data from the stream, and returns it as a string. If something goes wrong, zero is returned.

If a one is given as the second argument to Stdio.Fd.read_oob, only as many bytes of out-of-band data as are currently available will be returned.

If no arguments are given, Stdio.Fd.read_oob will read to the end of the stream.

Note

This function is only available if the option '--without-oob' was not specified when the Pike runtime was compiled.

It is not guaranteed that all out-of-band data sent from the other end will be received. Most streams only allow for a single byte of out-of-band data at a time.

See also

Stdio.Fd.read, Stdio.Fd.write_oob


Method write

int write(string data)
int write(string format, mixed ... extras)
int write(array(string) data)
int write(array(string) format, mixed ... extras)

Description

Write data to a file or a stream.

Writes data and returns the number of bytes that were actually written.

If more than one argument is given, sprintf will be used to format them.

If data is an array, it will be concatenated, and then written.

0 is returned in nonblocking mode if it was not possible to write anything without blocking.

-1 is returned if something went wrong and no bytes were written.

Note

Writing of wide strings is not supported.

See also

Stdio.Fd.read, Stdio.Fd.write_oob


Method write_oob

int write_oob(string data)
int write_oob(string format, mixed ... extras)

Description

Write out-of-band data to a stream.

Writes out-of-band data to a stream and returns how many bytes that were actually written.

If more than one argument is given, sprintf will be used to format them.

-1 is returned if something went wrong and no bytes were written.

Note

This function is only available if the option '--without-oob' was not specified when the Pike runtime was compiled.

It is not guaranteed that all out-of-band data sent from the other end will be received. Most streams only allow for a single byte of out-of-band data at a time. Some streams will send the rest of the data as ordinary data.

See also

Stdio.Fd.read_oob, Stdio.Fd.write


Method close

int close()
int close(string direction)

Description

Close a file or stream.

If direction is not specified, both the read and the write direction will be closed. Otherwise only the directions specified will be closed.

See also

Stdio.Fd.open, Stdio.Fd.open_socket


Method open

int open(string filename, string mode)
int open(string filename, string mode, int access)
int open(int fd, st