{The two procedures which are included are intended to allow you to place
 the cursor on a field, specify the field's size, and obtain its input.

 This is written for MS-DOS and PC-DOS systems, but may also work in
 CP/M environments.

 Variables to be declared are:
    Field : String[n] Where n = maximum field size to be input;
    Place,FieldSize : Integer;  (Place points to where you are in the field)
    Result : Integer;           (Indicates error in numeric conversion)
    Ch : Char;
    Cursor : Char;        (an underscore (_) for example)

 Note that you can include error conditions in I/O checking by passing
 the value of Result to your Error Trapping routines.

 You will need to set FieldSize prior to calling InputField, and you should
 reset Field to '' after assigning it to your input variable.

 StripBlanks is used if you are getting Numeric input.

 These procedures should be handy if you are using formatted screen input,
 such as invoice forms, time sheets, etc.

 written by Ken McClure
 75156,2641
 }


Program Input;

var
  Field         : String[10];
  FieldSize     : Integer;
  Ch            : Char;
  TestFieldOne  : String[8];
  TestFieldTwo  : Integer;
  Result        : Integer;
  Place         : Integer;
  Cursor        : Char;

procedure InputField;

label Bypass;

begin
    Field := '';
    Place := 0;
    Repeat
      Read(Kbd,Ch);
      If Length(Field)<=FieldSize then begin
        Case Ch of
          #8       : begin             {BackSpace key}
                       If Place=0 then begin  {avoid backspacing beyond}
                         Write(Trm,Chr(8));   {beginning of field      }
                         Write(' ');
                       end
                       else begin
                         Delete(Field,Place,1); {destructive backspace}
                         Place := Place-1;
                         Write(Trm,Chr(8));
                         Write(Cursor);
                         Write(Trm,Chr(8));
                       end;
                     end;
           #127    : begin;             {Delete Key}
                       If Place=0 then begin  {avoid backspacing beyond}
                         Write(Trm,Chr(8));   {beginning of field again}
                         Write(' ');
                       end
                       else begin
                         Delete(Field,Place,1); {destructive backspace}
                         Place := Place-1;
                         Write(Trm,Chr(8));
                         Write(Cursor);
                         Write(Trm,Chr(8));
                       end;
                     end;
           #13     : begin              {Carraige Return--End of input}
                       GoTo Bypass;     {for this field               }
                     end;
        else
          If Length(Field)<FieldSize then begin
            Place := Place+1;
            Write(Ch);
            Field := Field + Ch;
          end
          else begin
            If Ch <> Chr(13) then
              Write(Chr(7));            {Ring bell at terminal if at end of}
          end;                          {field and no RETURN key pressed   }
ByPass:
        end;
      end;
    Until Ch=Chr(13);
end;

procedure StripBlanks;            {don't use on Alpha-numeric fields...}
                                  {blanks there may be valid           }
begin
  Repeat
    Place := Pos(' ',Field);
    Delete(Field,Place,1);
    Place := Pos(' ',Field);
  Until Place=0;
end;

begin                               {little sample program}
  ClrScr;
  Write('What do you use to delineate fields? (e.g., "_") :');
  Read(Cursor);
  GoToXY(30,4);
  Write('Test Input');
  GoToXY(10,6);
  Write('Alpha Data: ');
  GoToXY(30,6);
  FieldSize := 8;
  For Place := 1 to FieldSize do begin
    Write(Cursor);
  end;
  GoToXY(30,6);
  InputField;
  TestFieldOne := Field;
  Field := '';
  GoToXY(10,8);
  Write('Numeric Data: ');
  GoToXY(30,8);
  FieldSize := 4;
  For Place := 1 to FieldSize do begin
    Write(Cursor);
  end;
  GoToXY(30,8);
  InputField;
  StripBlanks;
  Val(Field,TestFieldTwo,Result);
  Field := '';
end.