100 REMark  Some of definitions
110 REMark 
120 dev$ = "dsk1_"
130 nt_name$ = dev$ & "_qdos.nme"
140 nt_xsize = 16
150 REMark 
160 REMark  Open a nice window
170 REMark 
180 OPEN #4, con_512x100a0x100
190 PAPER #4,0
200 CLS #4
210 REMark  ============
220 REMark  Main program
230 REMark  ============
240 PAPER #4, 2 : INK #4, 7
250 PRINT #4, "Please, insert disk with files to fix."
260 PAPER #4, 0
270 PRINT #4, \"When you're done,"
280 PRINT #4, "press 'm' for manual file selection or"
290 PRINT #4, "any other key for automatic, _QDOS.NME"
300 PRINT #4, "based, file fixing."
310 key$ = INKEY$(#4, -1)
320 CLS #4
330 IF key$ == "m"
340    manual_fix #4
350 ELSE 
360    auto_fix #4
370 END IF 
380 CLOSE #4
390 REMark  ------------------------
400 REMark  Procedures and functions
410 REMark  ------------------------
420 DEFine PROCedure manual_fix(ch)
430 LOCal name$,length
440    CLS #ch
450    INK #ch, 5 : PAPER #ch, 2
460    PRINT #ch, "Manual file fixing"
470    PAPER #ch, 0
480    PRINT #ch, "End by giving an empty name."
490    INK #ch, 7
500    REPeat loop
510       INPUT #ch, \"File: "; name$
520       IF name$ = "" THEN EXIT loop
530       INPUT #ch, "Length of data: "; length
540       fix_file dev$ & name$, length
550    END REPeat loop
560 END DEFine 
570 REMark 
580 DEFine PROCedure auto_fix(ch)
590 LOCal length, nt, t, n, nt_pos
600 LOCal qlname$, tosname$
610    length = file_length(nt_name$)
620    nt = MALLOC(length)
630    LBYTES nt_name$, nt
640    IF (get_string$(nt, 8) <> "NTT v1.0")
650       FREE nt
660       PRINT #ch, "The disk didn't have a valid _QDOS.NME"
670       RETurn 
680    END IF 
690    CLS #ch
700    INK #ch, 5 : PAPER #ch, 2
710    PRINT #ch, "Automatic file fixing"
720    PRINT #ch
730    INK #ch, 7 : PAPER #ch, 0
740    n = (length - 8 - 2) / (64 + 16)
750    FOR t = 0 TO n - 1
760       nt_pos = nt + 8 + t * (64 + nt_xsize)
770       qlname$ = get_string$(nt_pos + 16, PEEK_W(nt_pos + 14))
780       tosname$ = get_string$(nt_pos + 66, PEEK_W(nt_pos + 64))
790       PRINT #ch, qlname$ & "/" & tosname$ & ": ";
800       IF (PEEK(nt_pos + 5) = 1)
810          datalen = PEEK_L(nt_pos + 6)
820          PRINT #ch, "DATA = " & datalen
830 REMark   dummy$ = INKEY$(#ch, -1)
840          fix_file dev$ & tosname$, datalen
850       ELSE 
860          PRINT #ch, "Nothing to do"
870       END IF 
880    END FOR t
890    FREE nt
900 END DEFine 
910 REMark 
920 DEFine FuNction get_string$(addr, length)
930 LOCal n, res$
940    res$ = ""
950    FOR n = addr TO addr + length - 1
960       res$ = res$ & CHR$(PEEK(n))
970    END FOR n
980    RETurn res$
990 END DEFine 
1000 REMark 
1010 DEFine PROCedure fix_file(name$, datalen)
1020 LOCal info_buf
1030    info_buf = MALLOC(64)
1040    OPEN #9, name$
1050    HEADREAD #9, info_buf
1060    POKE info_buf + 5,1
1070    POKE_L info_buf + 6, datalen
1080    HEADSET #9, info_buf
1090    CLOSE #9
1100    FREE info_buf
1110 END DEFine 
1120 REMark 
1130 DEFine FuNction file_length(name$)
1140 LOCal info_buf, length
1150    info_buf = MALLOC(64)
1160    OPEN #9, name$
1170    HEADREAD #9, info_buf
1180    CLOSE #9
1190    length = PEEK_L(info_buf)
1200    FREE info_buf
1210    RETurn length
1220 END DEFine 
