How to resolve the algorithm Fixed length records step by step in the M2000 Interpreter programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Fixed length records step by step in the M2000 Interpreter programming language

Table of Contents

Problem Statement

Fixed length read/write Before terminals, computers commonly used punch card readers or paper tape input. A common format before these devices were superseded by terminal technology was based on the Hollerith code, Hollerith code. These input devices handled 80 columns per card and had a limited character set, encoded by punching holes in one or more rows of the card for each column. These devices assumed/demanded a fixed line width of 80 characters, newlines were not required (and could not even be encoded in some systems). Write a program to read 80 column fixed length records (no newline terminators (but newline characters allowed in the data)) and then write out the reverse of each line as fixed length 80 column records. Samples here use printable characters, but that is not a given with fixed length data. Filenames used are sample.txt, infile.dat, outfile.dat. Note: There are no newlines, inputs and outputs are fixed at 80 columns, no more, no less, space padded. Fixed length data is 8 bit complete. NUL bytes of zero are allowed. These fixed length formats are still in wide use on mainframes, with JCL and with COBOL (which commonly use EBCDIC encoding and not ASCII). Most of the large players in day to day financial transactions know all about fixed length records and the expression logical record length. To create the sample input file, use an editor that supports fixed length records or use a conversion utility. For instance, most GNU/Linux versions of dd support blocking and unblocking records with a conversion byte size.

Forth systems often include BLOCK words. A block is 1024 bytes. Source code is stored as 16 lines of 64 characters each (again, no newline character or sequence to mark the end of a line). Write a program to convert a block file to text (using newlines). Trailing spaces should be excluded from the output. Also demonstrate how to convert from a normal text file to block form. All lines either truncated or padded to 64 characters with no newline terminators. The last block filled to be exactly 1024 characters by adding blanks if needed. Assume a full range of 8 bit byte values for each character. The COBOL example uses forth.txt and forth.blk filenames.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Fixed length records step by step in the M2000 Interpreter programming language

Source code in the m2000 programming language

Module FixedFile {
      Read fixed$
      OldLocale=Locale
      \\ chr$(string_argument$)
      \\ use Locale to convert from Ansi to Utf-16LE
      \\ Read Ansi form files also use Locale
      Locale 1032
      Try ok {
            \\ Make the file first
            Const Center=2
            Font "Courier New"
            Bold 0
            Italic 0
            Def long m, z=1, f
            Def text2read$,test3write$
            Form 100, 50  ' 100 by 60 characters
            Document txt$={Line 1...1.........2.........3.........4.........5.........6.........7.........8
                  Line 2
                  Line 3
                  Line 4
                  
                  Line 6
                  Line 7
                       Indented line 8............................................................
                  Line 9                                                                 RT MARGIN
                  }
            \\ use Help Open in M2000 console for details
            \\ Method one
            Report Center,  "Make file"
            \\ for WIDE Random \\  for Utf-16
            Open fixed$ for Random Exclusive as #f len=80
            m=Paragraph(txt$, 0)
            z=1
            If forward(txt$, m) then
                  while m, z<10
                        text2write$=Paragraph$(txt$,(m))
                        Print format$("Len:{0}, Data: {1}",Len(text2write$),text2write$)
                        Put #f, text2write$ , z
                        \\ record number from 1
                        \\ if number is total records plus one
                        \\ we append a record
                        z++     
                  End while
            End If
            Print "Press any key"
            Push Key$ : Drop
            Form 80, 40
            Report Center,  "Method1"
            For z=1 to 9
                  Get #f, text2read$, z
                  text2read$=StrRev$(text2read$)
                  Put #f, text2read$, z
                  Print text2read$
            Next z
            Close #f
            Report Center,  "Method2"
            \\ Method2
            \\ Buffer Clear Line80 ... \\ to clear memory
            \\ here we write all bytes so not needed
            Buffer Line80 as byte*80
            m=filelen(fixed$)
            If m mod 80=0 Then
                  m=1
                  \\ now Get/Put read write at byte position
                  \\ we have to use seek to move to byte position
                  \\ This way used for Binary files
                  Open fixed$ for Input as #f1
                  Open fixed$ for Append as #f2
                  while not eof(#f1)
                        seek #f1, m
                        Rem Print seek(#f)
                        Get #f1, Line80
                        Return line80,0:=Str$(StrRev$(Chr$(Eval$(line80,0,80))))
                        seek #f2, m
                        Put #f2, Line80
                        seek #f1, m
                        Get #f1, Line80
                        Print Chr$(Eval$(line80,0,80))
                        m+=80
                  End While
                  Close #f1
                  Close #f2
            End if
      }
      \\ use Close with no parameters for close all files if something happen
      If error then Close: Print Error$
      Locale OldLocale    
}
FixedFile "fixed.random"

Form 80,50
Print "Forth's Blocks"
\\ Forth Blocks
Structure Line16 {
      a`Line as byte*64
}
NewBlock=lambda Line16 -> {
      Buffer a`Block as Line16*16
      \\ fill spaces
      Return a`Block, 0:=str$(string$(" ",1024))
      =a`Block
}
\\ Events are value types, but for closures and groups are reference types
Event Doit {
      Read something$
}
Header=Doit
DisplayBlock= Lambda NewBlock, Line16, Doit, Header (Blocks`File$,Block`Number, UseLocale=1033)->{
      Page1=NewBlock()
      Open Blocks`File$ for input as #f
            Seek #f, 1024*(Block`Number-1)+1
            Get #f,Page1
      Close #f
      Document NewDoc$
      \\ need to convert from Ansi
      Call Event Header, "Block:"+Str$(Block`Number)
      oldlocale=locale
      locale UseLocale
      For i=0 to 15
            lineAny$=chr$(Eval$(Page1,i, Len(Line16)))
            Call Event Doit, format$("{0::-2} {1}",i,chr$(Eval$(Page1,i, Len(Line16))))
      Next i
      locale oldlocale
}
Document ForthCode$={( Large letter F) 
      : STAR    [CHAR] * EMIT ;
      : STARS   0 DO  STAR  LOOP ;
      : MARGIN  CR 30 SPACES ;
      : BLIP    MARGIN STAR ;
      : BAR     MARGIN 5 STARS ;
      : F       BAR BLIP BAR BLIP BLIP CR ;
      }
\\ Make Document bigger than 16 lines
\\ doc.par(ForthCode$)  return paragraphs (here we have no wrap)
\\ actuall lines per layer can be found from Report (the renderer)
\\ using Reportlines. layer can be the printer page.
ForthCode$=string$(ForthCode$,5)
Print "Make Block"
Page1=NewBlock()
Locale 1033
Blocks`File$="Forth Blocks"
Block`Number=1
\\ Apppend three times same blocks
For Pass=1 to 3
      If Doc.Len(ForthCode$)>0 then
            For i=1 to Doc.par(ForthCode$)-1
            \\ we give order number but Paragraph$ use unique number for paragraphs
            \\ if we didn't delete or insert lines, then these two are the same
                  Print Paragraph$(ForthCode$, Paragraph(ForthCode$,i))
                  \\ convert to Ansi using Locale
                  \\ offset from 0, so minus 1
                  \\ offset 1 is at len(Line16)
                  \\ Page1(0) is the real address, but here doesn't matter
                  Return Page1, (i-1) mod 16:=Str$(Paragraph$(ForthCode$, Paragraph(ForthCode$,i)))
                  if i mod 16=0 then Gosub SaveBlock
            Next i
            i--
            if Not i mod 16=0 then  Gosub SaveBlock
      End if
Next Pass
\\ now we read from disk
Class DocumentKeeper {
      Document Text$
      Function AppendLine(aline$) {
            \\ right trim$
            .Text$<=Mid$(Trim$("*"+aline$),2)+{
            }
      }
}
Function Disp(aline$) {
      Print aline$
}
DocumentKeeper=DocumentKeeper()
Event Doit New Disp()
Event Header New Disp()
For i=1 to Block`Number-1
      Call DisplayBlock(Blocks`File$, i)
      Print "Press any key"
      Push key$ : Drop
Next i
Event Doit Drop Disp()
Event Doit New DocumentKeeper.AppendLine()
Event Header Hold
For i=1 to Block`Number-1
      Call DisplayBlock(Blocks`File$, i)
Next i
Report DocumentKeeper.Text$

End 
SaveBlock:
      Print "Save as Number ";Block`Number
      If Exist(Blocks`File$) then 
            \\ check if there is space for this block
            If Not filelen(Blocks`File$) div 1024>=Block`Number-1 Then
                  Error "Wrong Block Number"
            End if
      Else
            Print "not exist"
            Open Blocks`File$ for output as #f
            Close #f
            Wait 100
            \\ or Error "Empty File" if we wish only read
            If Block`Number<>1 then Error "Wrong Block Number"
      End if
      Open Blocks`File$ for append as #f
      Seek #f, Block`Number*1024-1023 ' so we seek to first byte
      Put #f, Page1
      Close #f
      Block`Number++
      Page1=NewBlock()
Return

  

You may also check:How to resolve the algorithm Monty Hall problem step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm One-dimensional cellular automata step by step in the Scala programming language
You may also check:How to resolve the algorithm 15 puzzle game step by step in the Simula programming language
You may also check:How to resolve the algorithm Chaocipher step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Multisplit step by step in the AutoHotkey programming language