[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here

HP Pascal for OpenVMS
Language Reference Manual


Previous Contents Index

8.110 XOR Function

The XOR function returns a value (of the same type as the parameters) of a binary logical exclusive-OR operation on two specified parameters.


XOR( p1, p2 )

The p1 and p2 parameters must be of the same type and must be of either the BOOLEAN or SET types.



Result := XOR( ['A','B','C'],['B','C','D'] );  {Returns ['A','D']}

For More Information:

8.111 ZERO Function

The ZERO function returns data, whose type depends on the context of the function call, that sets any variable (except a file variable) to its binary zero.


ZERO

If you attempt to use the ZERO function to initialize a file variable, an error occurs. Do not specify a parameter list when you call the ZERO function.

Table 8-3 shows the value that ZERO assigns for each data type.

Table 8-3 Value of ZERO
Data Type Value
All INTEGER types 0
All UNSIGNED types 0
CHAR The character NUL
BOOLEAN FALSE
Enumerated The enumerated element
with ORD(element) = 0
Subrange 0 1
REAL 0.0
DOUBLE 0.0
QUADRUPLE 0.0
ARRAY ZERO applied to each component
RECORD ZERO applied to each field
VARYING OF CHAR, STRING The null string
SET The empty set
Pointer NIL

1Note that an ordinal target with a subrange type can be initialized outside of the subrange. The compiler treats this as an error if used in a compile-time expression.

The ZERO function is used in two ways. You can use it as a compile-time expression to initialize a variable in the TYPE, VAR, CONST, or VALUE sections. You can also use it on the right side of an assignment statement that appears in the executable section where the target is either a variable, or, within the body of a function, the function identifier.

This example shows both uses:


TYPE
    Pre_Zeroed_Record = RECORD
                         A: INTEGER;
                         B: Array [1..3] of Real
                        END VALUE ZERO;

VAR
    An_Array   : Array [1..10] of Real;
    Pre_Zeroed : Pre_Zeroed_Record;

BEGIN
    An_Array :=ZERO; {Initializes all of An_Array to zeroes}
    .
    .
    .

For More Information:


Chapter 9
Input and Output Processing

The HP Pascal I/O model provides an extensive set of predeclared routines. When programming with I/O, remember that the routines and their effects depend on the capabilities available in your environment; not all routines or organizations are available across environments.

This chapter discusses the following topics:

For More Information:

  • On environment-specific I/O details (HP Pascal for OpenVMS User Manual and Appendix C).

9.1 Files and File Organizations

A file is an organized collection of logically related data items. Data items within files are called file components. The file organization defines the physical arrangement of the components within the physical file, what types of access information are present in each component, and how components may be accessed by a program.

The HP Pascal I/O model includes three file organizations: sequential, relative, and indexed.

To open a file, you can call the OPEN procedure. Also, you usually call one of the EXTEND, RESET, and REWRITE procedures to establish a starting position for reading from or writing to a file. (For relative and indexed file organizations, you can use procedures to locate a specific component to access.)

HP Pascal makes distinctions between permanent external files and temporary internal files. An external file has a name in a directory and exists outside the context of an HP Pascal program. An internal file has no name and is deleted after the program finishes execution.

A file declared in the program heading is external by default. A file declared in a nested block is internal by default. To change the default for internal files, call the OPEN procedure or specify a file name in the EXTEND, RESET, or REWRITE procedures.

The file is then considered external and is retained with the specified name after the program has finished execution. If you open an internal file with the EXTEND, RESET, or REWRITE procedure without a file name, the file remains an internal file.

Default Information:

If you do not specify a file organization at the time of file creation (using the OPEN procedure), HP Pascal creates a file with sequential organization.

The following sections describe file organizations.

9.1.1 Sequential File Organization

Sequential file organization specifies that file components are stored one after the other, in the order in which they were entered into the file. HP Pascal supports this organization for files on disk. This is the only organization supported for files on magnetic tape, on terminals, on card readers, and on line printers. Figure 9-1 shows this file organization.

Figure 9-1 Sequential File Organization


You cannot insert components between any two existing components, because no physical space separates them. You can only add records to the end of the file (the most recently added component), truncate the file from a specified component to the end of the file, or rewrite the file.

For More Information:

9.1.2 Relative File Organization

Relative file organization consists of a series of fixed-length component positions (called cells) numbered consecutively from 1 to n. The numbered, fixed-length cells enable HP Pascal to calculate the component's physical position in the file. The cell numbers are called relative component numbers. HP Pascal supports this organization on disk files only. Figure 9-2 shows this file organization.

Figure 9-2 Relative File Organization


Each component in the file may be randomly assigned to a specific cell. You can place components in unused cells and in cells from which components have been deleted. You cannot replace a component in a cell, but you can modify an existing component.

The length of the actual component may vary even though the cell containing the component is of a fixed length. If the component is smaller than the cell, the remaining space in the cell is unused.

For More Information:

9.1.3 Indexed File Organization

Indexed file organization specifies that, in addition to the stored components, there exists at least a primary key and possibly alternate keys (first alternate key, second alternate key, and so forth). HP Pascal uses the primary key to store components and uses a program-specified key or keys to retrieve data. HP Pascal supports this organization on disk files only.

To define a key and certain characteristics of keys, use the KEY attribute. You can define up to 254 alternate keys.

An index is a structure that provides a component collating sequence for the file, that is, a mechanism for accessing components in different orders depending on the specified index (name, address, telephone number, and so forth).

Figure 9-3 shows an indexed file organization that uses only a primary key.

Figure 9-3 Indexed File Organization


In Figure 9-3 the components are logically stored in an order that is determined by the primary index. (The actual physical location of components is transparent to your program.) Figure 9-4 shows the presence of a first alternate index (determined by the presence of first alternate keys in the components) that points to components stored in order by the primary key.

Figure 9-4 A First Alternate Key


For More Information:

In an indexed file, each component includes one or more key fields (or simply keys) that HP Pascal uses to build the specified indexes. Each key is identified by its location within the component, its length, and its data type.

A key may be one of the following data types:

  • A single, contiguous character string
  • A 2- or 4-byte unsigned binary number
  • A 1-, 2-, or 4-byte signed integer

You can use the KEY attribute to specify certain characteristics about the index and about the keys themselves. Table 9-1 describes these characteristics.

Table 9-1 Characteristics of the KEY Attribute
Keys Description
Sort order This characteristic determines how HP Pascal creates an index, and determines the order in which HP Pascal accesses components. The order can either be ascending or descending. If you specify ascending order, HP Pascal considers the component with an equal or greater key value to be the next component for access. If you specify descending order, HP Pascal considers the component with an equal or lesser key value to be the next component for access. Using different indexes and both ascending or descending order, you can use different collating sequences for a file's components according to the needs of your application.
Duplicate keys This characteristic permits you to use the key value in more than one component. However, only the first component having the key value can be accessed randomly; other components having the same key value can only be accessed sequentially.
Changeable keys This characteristic applies to alternate keys only. When you specify changeable keys, you can change the alternate keys in a component when you update the component. When an alternate key value changes, HP Pascal automatically adjusts the appropriate index to reflect the new key value.

If you do not allow duplicate keys, HP Pascal rejects any attempt to place a component into a file if it contains a key value that is a duplicate of an existing component. If you do not explicitly create the file to accept alternate key values, then attempts to change key values generate an error.

For More Information:

  • On the KEY attribute ( Section 10.2.23)
  • On additional key characteristics (HP Pascal for OpenVMS User Manual)

9.2 Component Formats

When you declare a file variable in your program, HP Pascal automatically creates a file buffer variable of the component type. This variable takes on the value of one file component at a time. You can access only one file component, called the current component, at a given time.

You cannot perform operations on a file while a reference to the file buffer variable exists. To dereference the file buffer variable, write the name of the file buffer variable followed by a circumflex (^), the dereferencing character.

Predeclared I/O procedures move the file position. As the file position changes, the variable in the file buffer changes. Figure 9-5 shows how this change occurs.

Figure 9-5 File Buffer Contents


Suppose you declare a file variable Math_Scores of type FILE OF INTEGER. You might call a procedure to move the file position to the first component of this file. At this point, the file buffer variable Math_Scores^ equals the value of the first component (here, 90). If you then called a procedure to advance the file position by two components, Math_Scores^ would equal the value of the third component (here, 70).

Consider the following example:


VAR
f : FILE OF INTEGER;
{In the executable section:}


OPEN( File_Variable := f,
      File_Name     := 'sample.dat',
      History       := OLD,
      Organization  := Sequential,
      Access_Method := Direct; );

EXTEND( f );
F^ := 20;
PUT( f );

The OPEN procedure opens the existing file, sample.dat. The EXTEND procedure positions the file after its last component. The assignment statement places the value 20 into the file buffer variable (F^). The PUT procedure writes the value of the file buffer variable at the end of the file, f.

When you declare a variable of type FILE, you indicate the type of all of its components. For example:


TYPE DataRecord = RECORD {A DataRecord is a component of this}
       Field1: INTEGER;  {file; each component contains an   }
       Field2: REAL;     {integer and a real value.          }
       END {RECORD};
VAR DataFile = FILE OF DataRecord; {VAR gains access to the file}

After calling RESET, REWRITE, or EXTEND, you use file variables to refer to the selected component. The following code continues the previous example:


RESET(DataFile);            {Read component into the file buffer}
MyInt  := DataFile^.Field1; {Copy the integer field into MyInt}
MyReal := DataFile^.Field2; {Copy the real field into MyReal}

The assignments use the FILE variable to refer to components of the file buffer.

In each file, all components are of the same file component format. Component format defines the size (or maximum size) of each component and any processing information needed in addition to the data portion of the component. The HP Pascal I/O model supports the following formats for file components:

Default Information:

For new TEXT and VARYING OF CHAR files, HP Pascal creates variable-length components by default. For other types of new files, HP Pascal creates fixed-length components. If you access an existing file, your specified component type must match the component type specified at the creation of the file; if it does not, you generate an error.

Table 9-2 shows which of the file organizations support which of the component formats.

Table 9-2 File Organization Support for Component Formats
Organization Supported Component Format
Sequential All component formats
Relative 1 Fixed length
  Variable length
Indexed Fixed length
  Variable length

1Although the relative file organization allows variable-length components, those variable-length components are contained in a fixed-length cell that must be large enough to contain the largest stored component.

For More Information:

9.2.1 Fixed-Length Component Format

Fixed-length components are all the same length. The fixed-length component format is supported in all file organizations. HP Pascal determines the length of a component at the time of file creation. You cannot change the length of the components after you create the file.

9.2.2 Variable-Length Component Format

The variable-length component format enables components to be only as long as the data requires. The variable-length format is supported in all file organizations.

When you use OPEN to create a file of variable-length components, you can specify the value (in bytes) of the largest component permitted in the file. Any attempt to store a component containing more bytes than the specified value results in an error.

9.2.3 Stream Component Format

The stream component format is a continuous stream of bytes that contains special delimiting characters (called terminators) that separate components. In addition to being recognized as delimiters, HP Pascal considers the terminators to be a valid part of the component data. The stream format is supported only in sequential files on disk.

Table 9-3 contains the acceptable types of stream component formats:

Table 9-3 Acceptable Types of Stream Component Formats
Type Description
STREAM_CR This type recognizes a carriage-return character as the component terminator.
STREAM_LF This type recognizes a line feed as the component terminator.
STREAM This type uses a terminator from a limited set of special characters: the carriage return (CR); the carriage-return/line-feed combination (CR/LF); or the form feed (FF).


Previous Next Contents Index