| home | contents | previous | next page | send comment | send link | add bookmark |

7. Some input and output

So far, you have seen examples of the put and get statements. This section explains all the features these statements provide. The T language has several standard subprograms for file input and output and for text and pixel graphics. These will be explained also.

put statements

The complete definition of the put statement is:

put [:stream,] put item{, put item}[...]
It is used for output of text data to files or the video display of your console. The value of stream must match an integer value obtained with the standard function open. If stream is omitted, the output is sent to the console for video display. A put item has the form:
expression [:width[:fraction width[:exponent width]]]
The expression can be of any standard type except boolean. The value of width is the total number of characters in the put item. Strings are left justified; numbers are right justified. The fraction width and exponent width options are for writing a number in a real number format. If a specified format is too small, the actual format width is increased to accommodate the item.

The optional ellipses ... symbol inhibits adding a new line after the last put item. Some examples:

put "hi":8
put 0.001:12:4:2
put 99:4

const pi : real := 3.1415926535

put pi
put pi:12
put pi:16:8:2

get statements

The complete definition of a get statement is:

get [:stream,] get item{, get item}
It is used for input of text data from files or the console. As above, the value of stream must match an integer value obtained with the standard function open. If stream is omitted, the input is obtained by keyboard entry. A get item is one of:
a.  name
b.  name : *
c.  name : width
Form (a) is used for token input; the root type of the get item's identifier can be integer, real, or string. This form skips white space until an initial character indicates the start of a token. Form (b) is used for line input and reads up to an end of line symbol. Form (c) reads width characters. The identifiers in forms (b) and (c) can only be string type. Some examples:
get :file, line : *             % inputs from a text file
get word                        % inputs from keyboard
get characters : 8              % inputs first 8 characters
This program is an example of file access:
%
%  "lister.t" lists T source programs
%

type file_name : string(32)

var Main_file_name : file_name
var Main_file : int
var Line_buffer: string


program

    open_main
    process_file

end program


procedure open_main

    put "Enter main file name: "
    get Main_file_name

    Main_file := open(Main_file_name, "r")
    if Main_file = 0 then
        put "ERROR -- File not found: ", Main_file_name
        assert false
    end if

end open_main


procedure process_file

    loop

        exit when eof(Main_file) 

        get :Main_file, Line_buffer : *
      
        if index(Line_buffer, "include ") >= 0 then
            process_include_file(Line_buffer)
        else
            put Line_buffer 
        end if

        Line_buffer := ""
    
    end loop

    if not close(Main_file) then
        put "file close error"
    end if

end process_file


procedure process_include_file(var inc_str : string)

    var i, j : int
    var include_file : int
    var include_file_name : file_name

    i := index(inc_str, "include")
    i := i + 8
    loop
        exit when inc_str(i) ~= ' ' 
        i := i + 1
    end loop

    j := 0
    loop
        include_file_name( j ) := inc_str( i )
        j := j + 1
        i := i + 1
        exit when inc_str( i ) = ' '
        exit when inc_str( i ) = chr( 0 )
    end loop
    include_file_name(j) := chr(0)
    
    include_file := open(include_file_name, "r")
    if include_file = 0 then
        put "ERROR -- Include file not found:  ", include_file_name
        return
    end if

    loop
        exit when eof(include_file)
        get :include_file, Line_buffer : *
        put Line_buffer 
        Line_buffer := ""
    end loop
    
    if not close(include_file) then
        put "file close error"
    end if

end process_include_file

text and pixel graphics

Several standard functions and procedures support text and pixel graphics. The procedure "setvideo" sets your console to a selected text or graphics mode that must be supported by your system. The function "videotype" returns a code for your system's equipment type. See section 10 for code tables.

The text graphic display procedures in T are:

cursor
locate
putch
putstr
scroll
The pixel graphics display procedures are:
putpixel
putline
Try the two programs below to see how the graphics functions work. The include statement is used to incorporate the "colors.t" listing into both programs.
%
% colors.t defines color constants for text and graphics
%
const black         : int := 0
const blue          : int := 1
const green         : int := 2
const cyan          : int := 3
const red           : int := 4
const magenta       : int := 5
const brown         : int := 6
const light_gray    : int := 7
const dark_gray     : int := 8
const light_blue    : int := 9
const light_green   : int := 10
const light_cyan    : int := 11
const light_red     : int := 12
const light_magenta : int := 13
const yellow        : int := 14
const white         : int := 15
const blink         : int := 128
The following program is an example of text graphics.
%
% "hello" says hello
%

include colors.t

program

    var key : int

    scroll(0, 24,  0, 79, light_gray, black, 0)
    cursor(0)

    window(9, 14, 31, 48, yellow, blue)

    locate(11, 37)
    putstr("Hello", yellow, blue)

    locate(12, 37)
    putstr("world!", yellow, blue)

    key := getkey
    locate(0, 0)
    cursor(1)
    scroll(0, 24,  0, 79, light_gray, black, 0)

end program


procedure window(tp, bt, lt, rt, fg, bg : int)

    var i : int

    % test for valid arguments
    assert tp >= 0
    assert bt <= 24
    assert lt >= 0
    assert rt <= 79

    scroll(tp, bt, lt, rt, yellow, blue, 0)
    locate(tp, lt+1)
    putch(chr(201), yellow, blue)
    locate(tp, rt-1)
    putch(chr(187), yellow, blue) 
    locate(bt, lt+1)
    putch(chr(200), yellow, blue)
    locate(bt, rt-1)
    putch(chr(188), yellow, blue) 

    for i := lt+2...rt-2 do
        locate(tp, i)
        putch(chr(205), yellow, blue)
        locate(bt, i)
        putch(chr(205), yellow, blue) 
    end for

    for i := tp+1...bt-1 do
        locate(i, lt+1)
        putch(chr(186), yellow, blue)
        locate(i, rt-1)
        putch(chr(186), yellow, blue) 
    end for

end window
The next program is an example of pixel graphics:
%
% "graph" draws a graph
%
include colors.t

program

    label program_exit :
    var vm, vt, key : int

    vm := videomode
    watch(vm)
    vt := videotype
    watch(vt)

    if vt <= 0 then
        goto program_exit
    elsif vt = 8 then                  % VGA 
        setvideo(18)
    elsif vt = 4 then                  % EGA
        setvideo(13)
    elsif vt = 2 then                  % CGA
        setvideo(4)
    else
        goto program_exit
    end if

    draw_sincostan

program_exit:
    key := getkey
    setvideo(vm)

end program


procedure draw_sincostan

    var i, j : int
    var x, y : real

    putline(0,   0,   0, 479, light_blue)
    putline(0, 239, 639, 239, light_blue)

    for i := 0...639 do

        x := i
        y := -50*sin(x / 100.0) + 240
        j := round(y)
        putpixel(i, j, light_green)

    end for

    for i := 0...639 do

        x := i
        y := -50*cos(x / 100.0) + 240
        j := round(y)
        putpixel(i, j, light_red)

    end for

    for i := 0...639 do

        x := i
        y := -50*tan(x / 100.0) + 240
        j := round(y)
        if 0 < j and j < 480 then
            putpixel(i, j, white)
        end if

    end for

end draw_sincostan

summary

This chapter has shown you the input and output capabilities of the T language. Keyboard input, text and pixel graphic output, and disk file input and output are supported. Section 10 contains several tables of related information.


| home | contents | previous | next page | send comment | send link | add bookmark |

Copyright © 2004, Stephen R. Schmitt