Fixed length records: Difference between revisions
Content added Content deleted
(Moved processing into a procedure.) |
(Added processing of Forth blocks.) |
||
Line 970: | Line 970: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
===Task: fixed length records=== |
|||
<lang Nim>import algorithm |
<lang Nim>import algorithm |
||
Line 1,002: | Line 1,004: | ||
............................................................8 enil detnednI |
............................................................8 enil detnednI |
||
NIGRAM TR 9 eniL</pre> |
NIGRAM TR 9 eniL</pre> |
||
===Bonus: Forth blocks=== |
|||
<lang Nim>import strutils |
|||
const EmptyRecord = repeat(' ', 64) |
|||
#--------------------------------------------------------------------------------------------------- |
|||
proc textToBlock(infile, outfile: string) = |
|||
## Read lines of a text file and write them as 64 bytes records. |
|||
let input = infile.open(fmRead) |
|||
defer: input.close() |
|||
let output = outfile.open(fmWrite) |
|||
defer: output.close() |
|||
var count = 0 |
|||
while not input.endOfFile: |
|||
var record = input.readLine() |
|||
if record.len > 64: |
|||
record.setLen(64) # Truncate to 64 bytes. |
|||
elif record.len < 64: |
|||
record &= repeat(' ', 64 - record.len) # Pad to 64 bytes. |
|||
if output.writeChars(record, 0, 64) != 64: |
|||
raise newException(IOError, "error while writing block file") |
|||
inc count |
|||
# Complete block with empty records. |
|||
let rem = count mod 16 |
|||
if rem != 0: |
|||
for _ in 1..(16 - rem): |
|||
if output.writeChars(EmptyRecord, 0, 64) != 64: |
|||
raise newException(IOError, "error while writing block file") |
|||
#--------------------------------------------------------------------------------------------------- |
|||
proc blockToText(infile, outfile: string) = |
|||
## Read 64 bytes records and write them as lines trimming spaces. |
|||
let input = infile.open(fmRead) |
|||
defer: input.close() |
|||
let output = outfile.open(fmWrite) |
|||
defer: output.close() |
|||
var line: string |
|||
while not input.endOfFile: |
|||
line.setLen(64) # Allocate space for the 64 bytes to read. |
|||
if input.readChars(line, 0, 64) != 64: |
|||
raise newException(IOError, "error while reading block file") |
|||
line = line.strip(leading = false, trailing = true) & '\n' |
|||
output.write(line) |
|||
#——————————————————————————————————————————————————————————————————————————————————————————————————— |
|||
when isMainModule: |
|||
textToBlock("block1.txt", "block.dat") |
|||
blockToText("block.dat", "block2.txt")</lang> |
|||
=={{header|Pascal}}== |
=={{header|Pascal}}== |