comp.lang.ada
 help / color / mirror / Atom feed
* How do I "Get" just 4 characters?
@ 1993-03-04 19:22 Kenneth Anderson
  1993-03-04 22:18 ` Dat Trieu Le
  1993-03-05  5:56 ` Craig Manahan
  0 siblings, 2 replies; 6+ messages in thread
From: Kenneth Anderson @ 1993-03-04 19:22 UTC (permalink / raw)


I had a question posed to me by one of my students, that asked : How
do I get just 4 characters of input?

Using the following program as an example:

with Text_IO;

procedure test is

   subtype pin_type is string(1..4);

   A   : pin_type;
   Num : INTEGER;

begin
   Text_IO.Get(A);
   Text_IO.PUT_LINE(A);
end test;

Now then, if I run the program and type "1234567890",

what I see is

1234567890
1234

What I would like to see is this:

1234
1234

I.E. I don't want to see 567890, I want the program to kick in right away
after the fourth character is typed, not wait for the user to type Return.

I tried changing the program to this:

with Text_IO;

procedure test is

   subtype pin_type is string(1..4);

   A : pin_type;
   B : CHARACTER;

begin
   Get(B);
   A(1) := B;
   Get(B);
   A(2) := B;
   Get(B);
   A(3) := B;
   Get(B);
   A(4) := B;
   Text_IO.PUT_LINE(A);
end test;

But the same behavior results...

Any suggestions?

Thanks in advance,

Ken



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How do I "Get" just 4 characters?
  1993-03-04 19:22 How do I "Get" just 4 characters? Kenneth Anderson
@ 1993-03-04 22:18 ` Dat Trieu Le
  1993-03-04 23:20   ` Ron Sercely
  1993-03-05  5:56 ` Craig Manahan
  1 sibling, 1 reply; 6+ messages in thread
From: Dat Trieu Le @ 1993-03-04 22:18 UTC (permalink / raw)


kanderso@mabillon.ICS.UCI.EDU (Kenneth Anderson) writes:

>I had a question posed to me by one of my students, that asked : How
>do I get just 4 characters of input?

>Using the following program as an example:

>with Text_IO;

>procedure test is

>   subtype pin_type is string(1..4);

>   A   : pin_type;
>   Num : INTEGER;

>begin
>   Text_IO.Get(A);
>   Text_IO.PUT_LINE(A);
>end test;

>Now then, if I run the program and type "1234567890",

>what I see is

>1234567890
>1234

>What I would like to see is this:

>1234
>1234

>I.E. I don't want to see 567890, I want the program to kick in right away
>after the fourth character is typed, not wait for the user to type Return.

>I tried changing the program to this:

>with Text_IO;

>procedure test is

>   subtype pin_type is string(1..4);

>   A : pin_type;
>   B : CHARACTER;

>begin
>   Get(B);
>   A(1) := B;
>   Get(B);
>   A(2) := B;
>   Get(B);
>   A(3) := B;
>   Get(B);
>   A(4) := B;
>   Text_IO.PUT_LINE(A);
>end test;

>But the same behavior results...

>Any suggestions?

>Thanks in advance,

Hi Ken,

I believe TEXT_IO works much like buffered input io. That is,
it will read from the input buffer. the Get message reads from
that buffer. If there is not enough characters in the buffer,
then the program will wait until the user has completed its
input to the buffer. Text_IO doesn't consider inputs to be complete
until it is followed by a carriage-return. If you want Ada to
interpret characters one-at-a-time, then a routine to interface
to the hardware may have to be written.

hope this helps.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How do I "Get" just 4 characters?
  1993-03-04 22:18 ` Dat Trieu Le
@ 1993-03-04 23:20   ` Ron Sercely
  1993-03-05  5:45     ` Dat Trieu Le
  0 siblings, 1 reply; 6+ messages in thread
From: Ron Sercely @ 1993-03-04 23:20 UTC (permalink / raw)


In article <C3Dxz1.J81@news.cso.uiuc.edu> dl10696@ehsn4.cen.uiuc.edu (Dat Trieu Le) writes:
>kanderso@mabillon.ICS.UCI.EDU (Kenneth Anderson) writes:
>
>>I had a question posed to me by one of my students, that asked : How
>>do I get just 4 characters of input?
>
>>Using the following program as an example:
>
>>with Text_IO;
>
>>procedure test is
>
>>   subtype pin_type is string(1..4);
>
>>   A   : pin_type;
>>   Num : INTEGER;
>
>>begin
>>   Text_IO.Get(A);
>>   Text_IO.PUT_LINE(A);
>>end test;
>
>>Now then, if I run the program and type "1234567890",
>
>>what I see is
>
>>1234567890
>>1234
>
>>What I would like to see is this:
>
>>1234
>>1234
>
>>I.E. I don't want to see 567890, I want the program to kick in right away
>>after the fourth character is typed, not wait for the user to type Return.
>
>>I tried changing the program to this:
>
>>with Text_IO;
>
>>procedure test is
>
>>   subtype pin_type is string(1..4);
>
>>   A : pin_type;
>>   B : CHARACTER;
>
>>begin
>>   Get(B);
>>   A(1) := B;
>>   Get(B);
>>   A(2) := B;
>>   Get(B);
>>   A(3) := B;
>>   Get(B);
>>   A(4) := B;
>>   Text_IO.PUT_LINE(A);
>>end test;
>
>>But the same behavior results...
>
>>Any suggestions?
>
>>Thanks in advance,
>
>Hi Ken,
>
>I believe TEXT_IO works much like buffered input io. That is,
>it will read from the input buffer. the Get message reads from
>that buffer. If there is not enough characters in the buffer,
>then the program will wait until the user has completed its
>input to the buffer. Text_IO doesn't consider inputs to be complete
>until it is followed by a carriage-return. If you want Ada to
>interpret characters one-at-a-time, then a routine to interface
>to the hardware may have to be written.
>
>hope this helps.

package SET_TTY_MODES is
 
   procedure SET_CBREAK;
   procedure UNSET_CBREAK;
 
end SET_TTY_MODES;
 
with TTY;
with OS_FILES;
with V_I_BITS;
with IOCTL;
 
package body SET_TTY_MODES is
 
   STTY_STATE : TTY.TOTAL_STATE;
 
   procedure SET_CBREAK is
   begin
      TTY.GET_TTY_STATE(FD => OS_FILES.STDIN_FD, STATE => STTY_STATE);
      STTY_STATE.U_TTY_STATE.SG_FLAGS := SHORT_INTEGER(
      	V_I_BITS.BIT_OR(
	    INTEGER(STTY_STATE.U_TTY_STATE.SG_FLAGS), 
	    IOCTL.CBREAK));
      TTY.SET_TTY_STATE(FD => OS_FILES.STDIN_FD, STATE => STTY_STATE);
   end SET_CBREAK;
 

   procedure UNSET_CBREAK is
   begin
      TTY.GET_TTY_STATE(FD => OS_FILES.STDIN_FD, STATE => STTY_STATE);
      STTY_STATE.U_TTY_STATE.SG_FLAGS := SHORT_INTEGER(
      	V_I_BITS.BIT_AND(
	    INTEGER(STTY_STATE.U_TTY_STATE.SG_FLAGS),
	    V_I_BITS.BIT_NEG(IOCTL.CBREAK)));
       TTY.SET_TTY_STATE(FD => OS_FILES.STDIN_FD, STATE => STTY_STATE);
   end UNSET_CBREAK;
 
end SET_TTY_MODES;



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How do I "Get" just 4 characters?
  1993-03-04 23:20   ` Ron Sercely
@ 1993-03-05  5:45     ` Dat Trieu Le
  1993-03-05 15:55       ` Ron Sercely
  0 siblings, 1 reply; 6+ messages in thread
From: Dat Trieu Le @ 1993-03-05  5:45 UTC (permalink / raw)


>>I believe TEXT_IO works much like buffered input io. That is,
>>it will read from the input buffer. the Get message reads from
>>that buffer. If there is not enough characters in the buffer,
>>then the program will wait until the user has completed its
>>input to the buffer. Text_IO doesn't consider inputs to be complete
>>until it is followed by a carriage-return. If you want Ada to
>>interpret characters one-at-a-time, then a routine to interface
>>to the hardware may have to be written.
>>
>>hope this helps.

>package SET_TTY_MODES is
> 
>   procedure SET_CBREAK;
>   procedure UNSET_CBREAK;
> 
>end SET_TTY_MODES;
> 
>with TTY;
>with OS_FILES;
>with V_I_BITS;
>with IOCTL;
> 
>package body SET_TTY_MODES is
> 
>   STTY_STATE : TTY.TOTAL_STATE;
> 
>   procedure SET_CBREAK is
>   begin
>      TTY.GET_TTY_STATE(FD => OS_FILES.STDIN_FD, STATE => STTY_STATE);
>      STTY_STATE.U_TTY_STATE.SG_FLAGS := SHORT_INTEGER(
>      	V_I_BITS.BIT_OR(
>	    INTEGER(STTY_STATE.U_TTY_STATE.SG_FLAGS), 
>	    IOCTL.CBREAK));
>      TTY.SET_TTY_STATE(FD => OS_FILES.STDIN_FD, STATE => STTY_STATE);
>   end SET_CBREAK;
> 

>   procedure UNSET_CBREAK is
>   begin
>      TTY.GET_TTY_STATE(FD => OS_FILES.STDIN_FD, STATE => STTY_STATE);
>      STTY_STATE.U_TTY_STATE.SG_FLAGS := SHORT_INTEGER(
>      	V_I_BITS.BIT_AND(
>	    INTEGER(STTY_STATE.U_TTY_STATE.SG_FLAGS),
>	    V_I_BITS.BIT_NEG(IOCTL.CBREAK)));
>       TTY.SET_TTY_STATE(FD => OS_FILES.STDIN_FD, STATE => STTY_STATE);
>   end UNSET_CBREAK;
> 
>end SET_TTY_MODES;

Hi,

what does this package do?



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How do I "Get" just 4 characters?
  1993-03-04 19:22 How do I "Get" just 4 characters? Kenneth Anderson
  1993-03-04 22:18 ` Dat Trieu Le
@ 1993-03-05  5:56 ` Craig Manahan
  1 sibling, 0 replies; 6+ messages in thread
From: Craig Manahan @ 1993-03-05  5:56 UTC (permalink / raw)


This may be no help at all but here it goes...
When you get A as a String of length 4 you really do only have "1234"
but, when you do a PUT_LINE it is putting the entire input buffer.
If you just want to have the output be "1234" either insert a Text_IO.Skip_Line
before you do a Put_Line or just use Text_IO.Put instead of Put_Line

Just an assumption since I've never used Put_Line before.


Craig



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How do I "Get" just 4 characters?
  1993-03-05  5:45     ` Dat Trieu Le
@ 1993-03-05 15:55       ` Ron Sercely
  0 siblings, 0 replies; 6+ messages in thread
From: Ron Sercely @ 1993-03-05 15:55 UTC (permalink / raw)


Sorry for the cryptic post.  Most of my message was (due to my error) ommited.
I do my development on Convex Ada, which is derived from Verdix Ada.  On most
Verdix systems, I/O requests eventually map to C library calls.  The default
model used for reads on must Unix system uses line buffering, (so that the
delete key works, for example).  The way that line buffering is disable is to
set the stty mode to cbreak.  This package uses commonly supplied Verdix
packages to allow a program to set STANDAND_INPUT to cbreak mode, which
solves the problem of the orignial post. 

with Text_IO;
with set_tty_modes;

procedure test is

   subtype pin_type is string(1..4);
   A   : pin_type;
   Num : INTEGER;

begin
   set_tty_modes.set_cbreak;  -- inserted line
   Text_IO.Get(A);
   Text_Io.New_Line;  -- necessary to duplicate the output desired
                      -- by the original poster
   Text_IO.PUT_LINE(A);
   set_tty_modes.unset_cbreak;  -- inserted line
end test;


Hope the post makes sense this time :-)

Ron Sercely




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~1993-03-05 15:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1993-03-04 19:22 How do I "Get" just 4 characters? Kenneth Anderson
1993-03-04 22:18 ` Dat Trieu Le
1993-03-04 23:20   ` Ron Sercely
1993-03-05  5:45     ` Dat Trieu Le
1993-03-05 15:55       ` Ron Sercely
1993-03-05  5:56 ` Craig Manahan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox