15 Ports

▼ |  | Unstable

1 | Guidelines for developing unstable libraries
2 | Bytes
3 | Classes and Objects
4 | Contracts
5 | Definitions
6 | Dictionaries
7 | Directories
8 | Exceptions
9 | Filesystem
10 | Functions
11 | Lists
12 | Net
13 | Path
14 | PLane T Packages
15 | Ports
16 | Pretty-Printing
17 | Regular Expressions
18 | Requiring Modules
19 | Sandboxed Evaluation
20 | Scribble Documentation
21 | Sets
22 | S-Expression Diff
23 | Source Locations
24 | Strings
25 | Structs
26 | Syntax
27 | Planet Package Macros
28 | Text Representations
29 | Multiple Values
30 | XML and CSS
31 | Polymorphic Contracts
32 | Finding Mutated Variables
33 | Find
34 | Interface-Oriented Programming for Classes
35 | Sequences
36 | Hash Tables
37 | Match
38 | Generics
39 | Mark Parameters
40 | Debugging
41 | Contracts for macro subexpressions
42 | GUI libraries

On this page:

read-all
read-all-syntax
port->srcloc
read-available-bytes

Version: 5.0.1.5
top← prev up next →

### 15`` Ports

Carl Eastlund <cce@racket-lang.org>

( require unstable/port)

This module provides tools for port I/O.

This library is unstable; compatibility will not be maintained. See Unstable for more information.

( read-all [reader port]) → list?
reader : (-> any/c) = read
port : input-port? = ( current-input-port)

This function produces a list of all the values produced by calling (reader) while current-input-port is set to port, up until it produces eof.

Examples:
> ( read-all read ( open-input-string "1 2 3"))
'(1 2 3)

> ( parameterize ([ current-input-port ( open-input-string "a b c")])
( read-all))

'(a b c)

( read-all-syntax [reader port]) → ( syntax/c list?)
reader : (-> ( or/c syntax? eof-object?)) = read
port : input-port? = ( current-input-port)

This function produces a syntax object containing a list of all the syntax objects produced by calling (reader) while current-input-port is set to port, up until it produces eof. The source location of the result spans the entire portion of the port that was read.

Examples:

( define port1 ( open-input-string "1 2 3"))

> ( port-count-lines! port1)
> ( read-all-syntax read-syntax port1)
#<syntax:1:0 (1 2 3)>

( define port2 ( open-input-string "a b c"))

> ( port-count-lines! port2)

> ( parameterize ([ current-input-port port2])
( read-all-syntax))

#<syntax:1:0 (a b c)>

( port->srcloc port [source span]) → srcloc?
port : port?
source : any/c = ( object-name port)
span : exact-nonnegative-integer? = 0

Produces a srcloc structure representing the current position of a port, using the provided source and span values to fill in missing fields. This function relies on port-next-location, so line counting must be enabled for port to get meaningful results.

Examples:

( define port ( open-input-string "1 2 3"))

> ( port-count-lines! port)
> ( read port)
1
> ( port->srcloc port)
(srcloc 'string 1 1 2 0)
> ( port->srcloc port "1 2 3" 1)
(srcloc "1 2 3" 1 1 2 1)

( read-available-bytes [port]) → ( or/c bytes? eof-object?)
port : input-port? = ( current-input-port)

This function reads all immediately available bytes from a port and produces a byte string containing them. If there are no bytes available and the port is known to have no more input, it produces eof; if there are none available but the port may have more input, it produces an empty byte string. This procedure never blocks to wait for input from the port.

Examples:

( define-values [in out] ( make-pipe))

> ( parameterize ([ current-input-port in]) ( read-available-bytes))
#""
> ( write-byte ( char->integer #\c) out)
> ( read-available-bytes in)
#"c"
> ( read-available-bytes in)
#""
> ( close-output-port out)
> ( read-available-bytes in)
#<eof>

top← prev up next →