charconv.pas 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. type
  2. bar = array [0..255] of byte;
  3. par = ^bar;
  4. const
  5. ParamChars :set of char = ['D','d','K','k','W','w'];
  6. {Constants have stolen from FAR Manager (C) Eugene Roshal tables}
  7. WIN1251 : bar = (
  8. $00,$01,$02,$03,$04,$05,$06,$07,$08,$09,$0a,$0b,$0c,$0d,$0e,$0f,$10,$11,$12,$13,$14,
  9. $15,$16,$17,$18,$19,$1a,$1b,$1c,$1d,$1e,$1f,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$2a,$2b,$2c,$2d,
  10. $2e,$2f,$30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$3a,$3b,$3c,$3d,$3e,$3f,$40,$41,$42,$43,$44,$45,$46,
  11. $47,$48,$49,$4a,$4b,$4c,$4d,$4e,$4f,$50,$51,$52,$53,$54,$55,$56,$57,$58,$59,$5a,$5b,$5c,$5d,$5e,$5f,
  12. $60,$61,$62,$63,$64,$65,$66,$67,$68,$69,$6a,$6b,$6c,$6d,$6e,$6f,$70,$71,$72,$73,$74,$75,$76,$77,$78,
  13. $79,$7a,$7b,$7c,$7d,$7e,$7f,$3f,$3f,$27,$3f,$22,$3a,$c5,$d8,$3f,$25,$3f,$3c,$3f,$3f,$3f,$3f,$3f,$27,
  14. $27,$22,$22,$07,$2d,$2d,$3f,$54,$3f,$3e,$3f,$3f,$3f,$3f,$ff,$f6,$f7,$3f,$fd,$3f,$b3,$15,$f0,$63,$f2,
  15. $3c,$bf,$2d,$52,$f4,$f8,$2b,$49,$69,$3f,$e7,$14,$fa,$f1,$fc,$f3,$3e,$3f,$3f,$3f,$f5,$80,$81,$82,$83,
  16. $84,$85,$86,$87,$88,$89,$8a,$8b,$8c,$8d,$8e,$8f,$90,$91,$92,$93,$94,$95,$96,$97,$98,$99,$9a,$9b,$9c,
  17. $9d,$9e,$9f,$a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7,$a8,$a9,$aa,$ab,$ac,$ad,$ae,$af,$e0,$e1,$e2,$e3,$e4,$e5,
  18. $e6,$e7,$e8,$e9,$ea,$eb,$ec,$ed,$ee,$ef);
  19. KOI8R : bar = (
  20. $00,$01,$02,$03,$04,$05,$06,$07,$08,$09,$0a,$0b,$0c,$0d,$0e,$0f,$10,$11,$12,$13,$14,
  21. $15,$16,$17,$18,$19,$1a,$1b,$1c,$1d,$1e,$1f,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$2a,$2b,$2c,$2d,
  22. $2e,$2f,$30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$3a,$3b,$3c,$3d,$3e,$3f,$40,$41,$42,$43,$44,$45,$46,
  23. $47,$48,$49,$4a,$4b,$4c,$4d,$4e,$4f,$50,$51,$52,$53,$54,$55,$56,$57,$58,$59,$5a,$5b,$5c,$5d,$5e,$5f,
  24. $60,$61,$62,$63,$64,$65,$66,$67,$68,$69,$6a,$6b,$6c,$6d,$6e,$6f,$70,$71,$72,$73,$74,$75,$76,$77,$78,
  25. $79,$7a,$7b,$7c,$7d,$7e,$7f,$c4,$b3,$da,$bf,$c0,$d9,$c3,$b4,$c2,$c1,$c5,$df,$dc,$db,$dd,$de,$b0,$b1,
  26. $b2,$f4,$fe,$f9,$fb,$f7,$f3,$f2,$ff,$f5,$f8,$fd,$fa,$f6,$cd,$ba,$d5,$f1,$d6,$c9,$b8,$b7,$bb,$d4,$d3,
  27. $c8,$be,$bd,$bc,$c6,$c7,$cc,$b5,$f0,$b6,$b9,$d1,$d2,$cb,$cf,$d0,$ca,$d8,$d7,$ce,$fc,$ee,$a0,$a1,$e6,
  28. $a4,$a5,$e4,$a3,$e5,$a8,$a9,$aa,$ab,$ac,$ad,$ae,$af,$ef,$e0,$e1,$e2,$e3,$a6,$a2,$ec,$eb,$a7,$e8,$ed,
  29. $e9,$e7,$ea,$9e,$80,$81,$96,$84,$85,$94,$83,$95,$88,$89,$8a,$8b,$8c,$8d,$8e,$8f,$9f,$90,$91,$92,$93,
  30. $86,$82,$9c,$9b,$87,$98,$9d,$99,$97,$9a);
  31. DOS866 : bar = (
  32. $00,$01,$02,$03,$04,$05,$06,$07,$08,$09,$0a,$0b,$0c,$0d,$0e,$0f,
  33. $10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$1a,$1b,$1c,$1d,$1e,$1f,
  34. $20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$2a,$2b,$2c,$2d,$2e,$2f,
  35. $30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$3a,$3b,$3c,$3d,$3e,$3f,
  36. $40,$41,$42,$43,$44,$45,$46,$47,$48,$49,$4a,$4b,$4c,$4d,$4e,$4f,
  37. $50,$51,$52,$53,$54,$55,$56,$57,$58,$59,$5a,$5b,$5c,$5d,$5e,$5f,
  38. $60,$61,$62,$63,$64,$65,$66,$67,$68,$69,$6a,$6b,$6c,$6d,$6e,$6f,
  39. $70,$71,$72,$73,$74,$75,$76,$77,$78,$79,$7a,$7b,$7c,$7d,$7e,$7f,
  40. $80,$81,$82,$83,$84,$85,$86,$87,$88,$89,$8a,$8b,$8c,$8d,$8e,$8f,
  41. $90,$91,$92,$93,$94,$95,$96,$97,$98,$99,$9a,$9b,$9c,$9d,$9e,$9f,
  42. $A0,$A1,$A2,$A3,$A4,$A5,$A6,$A7,$A8,$A9,$Aa,$Ab,$Ac,$Ad,$Ae,$Af,
  43. $B0,$B1,$B2,$B3,$B4,$B5,$B6,$B7,$B8,$B9,$Ba,$Bb,$Bc,$Bd,$Be,$Bf,
  44. $C0,$C1,$C2,$C3,$C4,$C5,$C6,$C7,$C8,$C9,$Ca,$Cb,$Cc,$Cd,$Ce,$Cf,
  45. $d0,$d1,$d2,$d3,$d4,$d5,$d6,$d7,$d8,$d9,$da,$db,$dc,$dd,$de,$df,
  46. $e0,$e1,$e2,$e3,$e4,$e5,$e6,$e7,$e8,$e9,$ea,$eb,$ec,$ed,$ee,$ef,
  47. $f0,$f1,$f2,$f3,$f4,$f5,$f6,$f7,$f8,$f9,$fa,$fb,$fc,$fd,$fe,$ff);
  48. Procedure Usage;
  49. begin
  50. WriteLn('Usage: charconv.exe <mode_from> <mode_to> <input_name> [out_name]');
  51. WriteLn('where mode: ');
  52. WriteLn(' D : DOS-866');
  53. WriteLn(' W : WIN-1251');
  54. WriteLn(' K : KOI-8-R');
  55. WriteLn('');
  56. WriteLn('Copyright RodSoft 2001 by Innocenty Enikeew 9:159/1');
  57. Halt(255);
  58. end;{Usage}
  59. var
  60. TOKOI8R : bar;
  61. TOWIN1251: bar;
  62. fIn,fOut,tmp : file of byte;
  63. mIn,mOut : char;
  64. tIn,tOut : par;
  65. ch : byte;
  66. nIn,nOut : string;
  67. begin
  68. if ParamCount < 3 then Usage;
  69. for ch := 0 to 255 do TOKOI8R[KOI8R[ch]]:=ch;
  70. for ch := 0 to 255 do TOWIN1251[WIN1251[ch]]:=ch;
  71. nIn:=ParamStr(1);mIn := nIn[1];
  72. nIn:=ParamStr(2);mOut := nIn[1];
  73. if not ((mIn In ParamChars) and (mOut In ParamChars)) then Usage;
  74. case mIn of
  75. 'D','d' : tIn:=@DOS866;
  76. 'W','w' : tin:=@WIN1251;
  77. 'K','k' : tin:=@KOI8R;
  78. end;
  79. case mOut of
  80. 'D','d' : tOut:=@DOS866;
  81. 'W','w' : tOut:=@TOWIN1251;
  82. 'K','k' : tOut:=@TOKOI8R;
  83. end;
  84. nIn := ParamStr(3);
  85. if ParamCount = 4 then nOut:=ParamStr(4) else nOut:=nIn;
  86. {$I-}
  87. Assign(fIn,nIn);
  88. Reset(fIn);
  89. If IOResult <> 0 then
  90. begin
  91. WriteLn('Error opening ',nIn);
  92. Usage;
  93. end;
  94. Assign(fOut,'chconv.!!!');
  95. Rewrite(fOut);
  96. If IOResult <> 0 then
  97. begin
  98. WriteLn('Error opening tempfile');
  99. Halt(254);
  100. end;
  101. {$I+}
  102. while not EOF(fIn) do
  103. begin
  104. Read(fIn,ch);
  105. Write(fOut,tOut^[tIn^[ch]]);
  106. end;
  107. close(fIn);
  108. Close(fOut);
  109. {$I-}
  110. Rename(fOut,nOut);
  111. if IOResult <> 0 then
  112. begin
  113. Assign(tmp,nOut);
  114. Erase(tmp);
  115. Rename(fOut,nOut);
  116. end;
  117. {$I+}
  118. WriteLn('Converted ',mIn,'->',mOut);
  119. WriteLn('Files: ',nIn,'->',nOut);
  120. WriteLn('Copyright RodSoft 2001 by Innocenty Enikeew 9:159/1');
  121. end.