Author Topic: PUTC  (Read 71 times)

Robert

  • Hero Member
  • *****
  • Posts: 1245
    • View Profile
PUTC
« on: October 17, 2024, 11:45:44 PM »
Perhaps a PUTC to go with the  GETC new in BCX 8.1.7?

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2330
    • View Profile
Re: PUTC
« Reply #1 on: October 18, 2024, 12:41:31 PM »
Thanks for the suggestion Robert.  Below is the entry into Revisions.txt for the next version of BCX:

Robert Wishlaw : Added PUTC (byte_expression, FilePtr) to the BCX lexicon as a complement to GETC(FP#)
                 PUTC will accept QBASIC style file pointers.  Here is a compilable example:
               
                         OPEN "test.txt" FOR OUTPUT AS #1
                         FOR CHAR i = 65 TO 70      ' The letters ABCDEF
                             PUTC (#1, i)
                         NEXT
                         PUTC (#1, 10 + 1 + 1 + 1)  ' 13 = Carriage Return  ( I used this line to test the parser )
                         PUTC (#1, 10)              ' 10 = Line Feed
                         FLUSH(#1)                  ' optional but always a good idea to FLUSH when you're done :-)
                         CLOSE #1 


The StdC call is putc (byte, FP);

but I wanted to rearrange the arguments, so that a byte expression could be more easily parsed, when needed.

Hence this crazy looking test line:  PUTC (#1, 10 + 1 + 1 + 1)  ' 13 = Carriage Return

The internal BCX processor for the new PUTC command is:

Code: [Select]

                CASE "putc"                                  ' MrBcx 819 (New)
                Stk$[Tmp] = "putc"                           ' MrBcx 819 (New)
                REMOVE "#" FROM Stk$[Tmp+2]                  ' MrBcx 819 (New)
                IF DataType(Stk$[Tmp+2]) = vt_NUMBER THEN    ' MrBcx 819 (New)
                    Stk$[Tmp+2] = "FP" + Stk$[Tmp+2]         ' MrBcx 819 (New)
                END IF                                       ' MrBcx 819 (New)
                '                                            ' MrBcx 819 (New)  Rearrange: putc ( fp# ,  byte-expreession )
                '                                            ' MrBcx 819 (New)         to: putc ( byte-expression, fp# )
                DIM fp$                                      ' MrBcx 819 (New)
                fp$ = Stk$[Tmp+2]                            ' MrBcx 819 (New)
                LShiftStk(Tmp+2)                             ' MrBcx 819 (New)
                LShiftStk(Tmp+2)                             ' MrBcx 819 (New)
                Stk$[Ndx] = ","                              ' MrBcx 819 (New)
                INCR Ndx                                     ' MrBcx 819 (New)
                Stk$[Ndx] = fp$                              ' MrBcx 819 (New)
                INCR Ndx                                     ' MrBcx 819 (New)
                Stk$[Ndx] = ")"                              ' MrBcx 819 (New)


It is working as it should and will be included in 8.1.9

Thanks ...



Robert

  • Hero Member
  • *****
  • Posts: 1245
    • View Profile
Re: PUTC
« Reply #2 on: October 18, 2024, 05:07:35 PM »
Thank you MrBcx.

PUTC will be handy for popping in a 13 or a 10 or both when the compiler spits out " No new-line at end of file ! "

Oh, yes, one more thing ... PUTC will also do exotic 21,  30, 118 and 155 if you are editing a file for, respectively, an IBM MainFrame Z/OS, a Lamborghini QNX, the Z80 Sinclair, or an 8-bit Atari.

Details here: https://en.wikipedia.org/wiki/Newline


MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2330
    • View Profile
Re: PUTC
« Reply #3 on: October 18, 2024, 05:08:38 PM »
Small update:  PUTC enclosing parentheses are optional.  Feels more BASIC-like ...


OPEN "test.txt" FOR OUTPUT AS #1
FOR CHAR i = 65 TO 70
    PUTC #1, i
NEXT
PUTC #1, 13  ' 13 = Carriage Return
PUTC #1, 10  ' 10 = Line Feed
FLUSH #1     ' optional but always a good idea to FLUSH when you're done :-)
CLOSE #1