home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / PASCAL / DSUTIL11 / BIN2BIT / BIN2BIT.PAS < prev   
Pascal/Delphi Source File  |  1993-10-28  |  24KB  |  658 lines

  1. {-----------------------------------------------------------------------}
  2. { PROJECT        NON-PROFIT HIGH QUALITY PROFESSIONAL SOFTWARE,  }
  3. {            AVAILABLE FOR ALL WORLD                }
  4. { LIBRARY        SYSTEM UTILITIES                                }
  5. { MODULE        BIN_TO_BIT_CONVERTER                            }
  6. { FILE NAME        BIN2BIT.PAS                    }
  7. { PURPOSE        Convert the binary file to a bit-asm formatting }
  8. {                       text file.                                      }
  9. { VERSION        1.30                        }
  10. { DATE            28-Oct-93                    }
  11. { DESIGN        Dmitry Stefankov                }
  12. { IMPLEMENTATION    Dmitry Stefankov                 }
  13. { COMPANY        Freelance Software Engineer            }
  14. { ADDRESS        Isakowskogo str, 4-2-30                }
  15. {            Moscow, 123181                    }
  16. {            USSR                        }
  17. {            Tel. 007 (095) 944-6304                }
  18. { COPYRIGHT NOTICE    Copyright (C) 1987-1993, Dmitry Stefankov    }
  19. { RESTRICTED RIGHTS    AVAILABLE ONLY FOR FREE DISTRIBUTION,           }
  20. {            NOT FOR COMMERCIAL PURPOSE            }
  21. { COMPUTER        IBM PC or compatible                }
  22. { OPERATING SYSTEM    MS/PC-DOS Version 3.30 or higher        }
  23. { COMPILER        Turbo Pascal Version 6.0            }
  24. {                       (Borland International Inc.)  or compatible     }
  25. { ASSEMBLY LANGUAGE    Microsoft MASM 5.10 or compatible               }
  26. { LINKER        Turbo Pascal internal                           }
  27. { ARGUMENTS        <infile>     -  input  stream                   }
  28. {                       <outfile>    -  output stream                   }
  29. {                       <bytecount>  -  # of bytes for symbol           }
  30. {                       <initchar>   -  initial number for Char         }
  31. {                       <ascii>      -  switch for select ASCII char set}
  32. { RETURN        None                        }
  33. { REQUIRES        None                                            }
  34. { NATURAL LANGUAGE      English Language                                 }
  35. { SPECIAL        None                        }
  36. { DESCRIPTION        1.Read from input stream                        }
  37. {                       2.Write Byte-bit ASM formatting output stream   }
  38. {                       3.If switch present then                        }
  39. {                            add ASCII representation for each line     }
  40. { REVISION HISTORY    Dima Stefankov (DS)                }
  41. {               1.00   08-Jan-92  DS  initilal release        }
  42. {                       1.01   09-Jan-92  DS  some syntax corrections   }
  43. {                       1.02   10-Jan-92  DS  new read/write algorithms }
  44. {                       1.03   24-Jan-92  DS  some corrections          }
  45. {            1.10   25-Aug-92  DS  updated documentation    }
  46. {                       1.20   08-Oct-92  DS  some style corrections    }
  47. {                       1.21   27-Oct-92  DS  some corrections          }
  48. {                       1.22   04-Nov-92  DS  some updates              }
  49. {                       1.23   07-Apr-93  DS  fixed a bug with bit str  }
  50. {                       1.24   20-May-93  DS  some style updates        }
  51. {            1.25   04-Jul-93  DS  updated documentation    }
  52. {            1.30   28-Oct-93  DS  some style updates    }
  53. {-----------------------------------------------------------------------}
  54.  
  55.  
  56. {*======================= PROGRAM HEADER PART ==========================*}
  57.  
  58. PROGRAM   BinaryFileToBitByteAssemblerFormatFile;
  59.  
  60.  
  61. {*** other modules ***}
  62. {*USES;*}
  63.  
  64. {** switches for compilation **}
  65. {$S-}                {*  stack checking     *}
  66. {$R-}                   {*  range checking     *}
  67. {$M 16384,65536,65536}  {*  memory allocation  *}
  68.  
  69.  
  70. {*========================== CONSTANTS PART ============================*}
  71.  
  72. CONST
  73.      asPurpose                  =       'BitAsmFile Converter';
  74.      asVersion                  =       '1.30';
  75.      asAuthor                   =       'Dima Stefankov';
  76.      asCopyright                =       'Copyright (c) 1987, 1993';
  77.      asProgram                  =       'Bin2bit';
  78.      asProgramPrompt            =       asProgram+': ';
  79.      asProgramU                 =       'BIN2BIT';
  80.  
  81.      { exit codes }
  82.      errTerminateOK             =     0;
  83.      errBadParmsNumber          =     1;
  84.      errSourceNotFound          =     2;
  85.      errDestDontWrite           =     3;
  86.      errSameNames               =     4;
  87.      errBadFmtBytesValue        =     5;
  88.      errSrcOpenFailed           =     6;
  89.      errDestCreateFailed        =     7;
  90.      errBadBytesValue           =     8;
  91.      errBadCharValue            =     9;
  92.  
  93.      aPercent100                =     100;
  94.  
  95.      achHexPrefix               =     '$';
  96.      achDosExtMark              =     '.';
  97.      asBlankStr                 =     '';
  98.      asSpace2                   =     '  ';
  99.      asSpaces5                  =     '     ';
  100.      asInDefExt                 =     'bin';
  101.      asOutDefExt                =     'asm';
  102.  
  103.      aAsciiNone                 =     0;
  104.      aAscii7                    =     1;
  105.      aAscii8                    =     2;
  106.  
  107.      achAsciiBit7               =     '7';
  108.      achAsciiBit8               =     '8';
  109.  
  110.      aDefaultBytesPerLine       =     8;
  111.      aMaxBytesPerLine           =     128;
  112.      aHexRadix                  =     16;
  113.  
  114.      aMaxOnHeap                 =     32768;
  115.      aMaxOutBufSize             =     32768;
  116.  
  117.      achNULL                    =     #0;
  118.      achHTAB                    =     #9;
  119.      achCR                      =     #13;
  120.      achSkip                    =     '.';
  121.      achZero                    =     '0';
  122.      achOne                     =     '1';
  123.      achDelimiter               =     ',';
  124.      achSemicolon               =     ';';
  125.  
  126.      achYes                     =     'Y';
  127.      achNo                      =     'N';
  128.  
  129.      asAsmData                  =     'DB';
  130.      asHexSuffix                =     'h';
  131.      asAsmBitData               =     'b';
  132.      asAsmNoList                =     '.XLIST';
  133.      asAsmList                  =     '.LIST';
  134.  
  135.  
  136.  
  137. {*==================== TYPE DECLARATIONS PART ==========================*}
  138.  
  139. TYPE
  140.     STR3        =       STRING[3];
  141.     STR4        =       STRING[4];
  142.     STR8        =       STRING[8];
  143.  
  144.  
  145. {*====================== TYPED CONSTANTS PART ==========================*}
  146.  
  147. CONST
  148.      setHexChars  :    SET OF System.Char  =  ['0'..'9','A'..'F','a'..'f'];
  149.      setAscii7    :    SET OF System.Char  =  [#32..#126];
  150.      setAscii8    :    SET OF System.Char  =  [#32..#126,#128..#255];
  151.      setIbmAscii  :    SET OF System.Char  =  [#32..#126,#128..#254];
  152.  
  153.      gadbAsciiFormat      :       System.Byte         =  aAsciiNone;
  154.      gdbInitCharCount     :       System.Byte         =  0;
  155.      gadbSymbolsInString  :       System.Byte         =  aDefaultBytesPerLine;
  156.      gddBytesCountDone    :       System.Longint      =  0;
  157.  
  158.  
  159. {*=========================== VARIABLES PART ===========================*}
  160.  
  161. VAR
  162.    gfInputStream        :       FILE;
  163.    gsInFileName         :       STRING[80];
  164.  
  165.    gfOutputFormatText   :       System.Text;
  166.    gfOutputStream       :       FILE  ABSOLUTE  gfOutputFormatText;
  167.    gsOutFileName        :       STRING[80];
  168.  
  169.    gddOffsetInFile      :       System.Longint;
  170.    gddByteCount         :       System.Longint;
  171.    gddInFileSize        :       System.Longint;
  172.  
  173.    gpMemoryBlock        :       System.Pointer;
  174.    gdwMemBlockSize      :       System.Word;
  175.    gdwBytesRead         :       System.Word;
  176.  
  177.    giErrorCode          :       System.Integer;
  178.  
  179.    gsTempInput          :       STRING;
  180.    gchInUser            :       System.Char;
  181.  
  182.    gdbTextOutBuf        :       ARRAY[1..aMaxOutBufSize]  OF  System.Byte;
  183.  
  184.  
  185. {*=========================== FUNCTIONAL PART ==========================*}
  186.  
  187. FUNCTION  _fnbFileExist(VAR fStruc : FILE; sFileName : STRING) : System.Boolean;
  188. {* Check that file exits. *}
  189. VAR
  190.   bResult  :  System.Boolean;
  191.  
  192. BEGIN
  193.   {** attempt to open the file **}
  194.     System.Assign(fStruc,sFileName);
  195.     {$I-}
  196.     System.Reset(fStruc);
  197.     {$I+}
  198.  
  199.   {** copy the result of last I/O operation **}
  200.     bResult := (System.IOResult = 0);
  201.  
  202.   IF (bResult)
  203.     THEN  System.Close(fStruc);
  204.   {if-then}
  205.  
  206.   _fnbFileExist := bResult;
  207. END; { _fnbFileExist }
  208.  
  209.  
  210. FUNCTION  _fnsForceFileNameExt(sFileName, sDefExt : STRING) : STRING;
  211. {* Add extension for filename if not present. *}
  212. BEGIN
  213.    IF (System.Pos(achDosExtMark,sFileName) = 0)
  214.      THEN sFileName := sFileName + achDosExtMark + sDefExt;
  215.    {if-then}
  216.   _fnsForceFileNameExt := sFileName;
  217. END;
  218. { _fnsForceFileNameExt }
  219.  
  220.  
  221. FUNCTION   _fnsByteToAsmFormat(dbInput : System.Byte) : STR4;
  222. {* Converts a byte to the ASM format number representation. *}
  223. CONST
  224.     chHexDigitsTable : ARRAY[0..15] OF System.Char = '0123456789ABCDEF';
  225.  
  226. BEGIN
  227.   _fnsByteToAsmFormat := achZero +
  228.                          chHexDigitsTable[dbInput SHR 4] +
  229.                          chHexDigitsTable[dbInput AND $0F] +
  230.                          asHexSuffix;
  231. END;  { _fnsByteToAsmFormat }
  232.  
  233.  
  234. FUNCTION  _fnsByteToBitString(dbInput : System.Byte; chZero,chOne : System.Char) : STR8;
  235. {* Return the bit representation of the byte. *}
  236.       inline($5A                 {    pop   dx          ; DL = One           }
  237.             /$5B                 {    pop   bx          ; BL = Zero          }
  238.             /$58                 {    pop   ax          ; AL = Number        }
  239.             /$88/$C7             {    mov   bh,al    ; BH = Number        }
  240.             /$89/$E7             {    mov   di,sp       ; make stack frame   }
  241.             /$36/$C4/$3D         {    les   di,ss:[di]  ; ES:DI -> string    }
  242.             /$B9/$08/$00         {    mov   cx,8        ; repeat count       }
  243.         /$FC         {    cld        ; forward direction  }
  244.             /$88/$C8             {    mov   al,cl       ; AL = string length }
  245.         /$AA         {    stosb        ; store it           }
  246.                                  { NextBit:                                  }
  247.             /$88/$D8             {    mov   al,bl       ; AL = Zero          }
  248.             /$D0/$E7             {    shl   bh,1        ; transfer bit in CF }
  249.             /$73/$02             {    jnc   BitDone     ; if not (CY), zero  }
  250.             /$88/$D0             {    mov   al,dl        ; AL = One          }
  251.                                  { BitDone:                                  }
  252.             /$AA             {    stosb        ; put a Char         }
  253.             /$E2/$F5);           {    loop  NextBit     ; repeat till done   }
  254. { _fnsByteToBitString }
  255.  
  256.  
  257. FUNCTION  _fnsUpcaseStr(sInput : STRING) : STRING;
  258. {* Make all uppercase. *}
  259. VAR
  260.   dbIndex  :  System.BYTE;
  261.   dbCount  :  System.BYTE;
  262.  
  263. BEGIN
  264.   dbCount := System.Length(sInput);
  265.  
  266.   IF (dbCount <> 0)  THEN
  267.     FOR  dbIndex :=  1  TO  dbCount  DO
  268.         sInput[dbIndex] := System.Upcase(sInput[dbIndex]);
  269.     {for-to-do}
  270.   {if-then}
  271.  
  272.    _fnsUpcaseStr := sInput;
  273. END; { _fnsUpcaseStr }
  274.  
  275.  
  276. FUNCTION   _fnchGetFirstChar(sInput : STRING) : System.Char;
  277. {* Returns a first char from string. *}
  278. VAR
  279.   chTemp  :  System.Char;
  280.  
  281. BEGIN
  282.    IF (System.Length(sInput) <> 0)
  283.      THEN  chTemp := sInput[1]
  284.      ELSE  chTemp := achNULL;
  285.    {if-then-else}
  286.   _fnchGetFirstChar := chTemp;
  287. END;
  288. { _fnchGetFirstChar }
  289.  
  290.  
  291. FUNCTION  _fnsNumToStr3(dwNum : System.Word) : STR3;
  292. {* Convert a numeric value to its string representation. *}
  293. VAR
  294.   sTemp : STR3;
  295.  
  296. BEGIN
  297.    System.Str(dwNum:3,sTemp);
  298.    _fnsNumToStr3 := sTemp;
  299. END;
  300. { _fnsNumToStr3 }
  301.  
  302.  
  303.  
  304. {*=========================== PROCEDURAL PART ==========================*}
  305.  
  306. PROCEDURE    _CopyrightDisplay;
  307. {* Outputs the copyright notice. *}
  308. BEGIN
  309.      System.WriteLn(asPurpose+'  Version '+asVersion+',  '+asCopyright+'  '+asAuthor);
  310. END;  { _CopyrightDisplay }
  311.  
  312.  
  313. PROCEDURE  _WriteBufferToDisk(VAR fOutTextFile : System.Text;
  314.                                   pMemBuf : System.Pointer;
  315.                                   dwByteCount : System.Word;
  316.                                   dbAsciiSwitch : System.Byte);
  317. {* Writes the contents of buffer in hexadecimal format to file. *}
  318. VAR
  319.   sOutStr            :  STRING;
  320.   sAsciiStr          :  STRING;
  321.   gdbByteArray       :  ARRAY[1..aMaxBytesPerLine] OF System.Byte;
  322.   dwOffsetInBuffer   :  System.Word;
  323.   dbInByte, dbIndex  :  System.Byte;
  324.   dbCountInLine      :  System.Byte;
  325.   dbCutLineIndex     :  System.Byte;
  326.   chAddChar          :  System.Char;
  327.  
  328. BEGIN
  329.   {** initial offset in file buffer **}
  330.     dwOffsetInBuffer := 0;
  331.  
  332.  
  333.   {* try to optimal value for # bytes per one text string *}
  334.   IF  (gadbSymbolsInString > 10)
  335.      THEN  BEGIN
  336.               IF (gadbSymbolsInString > 20)
  337.                    THEN  dbCutLineIndex := (gadbSymbolsInString DIV 8)
  338.                    ELSE  dbCutLineIndex := (gadbSymbolsInString DIV 2);
  339.              {if-then-else}
  340.            END
  341.      ELSE
  342.         dbCutLineIndex := gadbSymbolsInString + 1;
  343.   {if-then-else}
  344.  
  345.  
  346.   {** main process **}
  347.   WHILE  (dwByteCount <> 0) DO
  348.   BEGIN
  349.     {** init'd strings **}
  350.     sOutStr   := ';**     '+asAsmData+achHTAB;
  351.     sAsciiStr := asBlankStr;
  352.  
  353.     {* test for full/partial line *}
  354.     IF ((dwByteCount DIV gadbSymbolsInString) <> 0)
  355.       THEN  dbCountInLine := gadbSymbolsInString
  356.       ELSE  dbCountInLine := dwByteCount;
  357.     {if-then-else}
  358.  
  359.           BEGIN
  360.              FOR  dbIndex := 1  TO  dbCountInLine  DO
  361.              BEGIN
  362.                    IF ((dbIndex = ((dbIndex DIV dbCutLineIndex)*dbCutLineIndex+1))
  363.                           AND (dbIndex <> 1))  THEN
  364.                    BEGIN
  365.                        {** erase a delimiter **}
  366.                        System.Delete(sOutStr,System.Length(sOutStr),1);
  367.  
  368.                        IF  (dbAsciiSwitch <> aAsciiNone)
  369.                           THEN  sOutStr := sOutStr+achHTAB+achHTAB+
  370.                                            achSemicolon+asSpace2+sAsciiStr;
  371.                        {if-then}
  372.  
  373.                        System.WriteLn(fOutTextFile,sOutStr);
  374.                        sOutStr   := ';**     '+asAsmData+achHTAB;
  375.                        sAsciiStr := asBlankStr;
  376.                    END;
  377.                    {if-then}
  378.  
  379.                dbInByte := System.Mem[System.Seg(pMemBuf^):(System.Ofs(pMemBuf^)+dwOffsetInBuffer)];
  380.                gdbByteArray[dbIndex] := dbInByte;
  381.                sOutStr := sOutStr + _fnsByteToAsmFormat(dbInByte);
  382.  
  383.                IF  (dbIndex < dbCountInLine)
  384.                  THEN  sOutStr := sOutStr + achDelimiter;
  385.                {if-then}
  386.  
  387.                chAddChar := System.Char(dbInByte);
  388.                CASE  dbAsciiSwitch  OF
  389.                         aAscii7   : IF  (chAddChar IN setAscii7)
  390.                                        THEN  sAsciiStr := sAsciiStr + chAddChar
  391.                                        ELSE  sAsciiStr := sAsciiStr + achSkip;
  392.                                     {if-then-else}
  393.                         aAscii8   : IF  (chAddChar IN  setAscii8)
  394.                                        THEN  sAsciiStr := sAsciiStr + chAddChar
  395.                                        ELSE  sAsciiStr := sAsciiStr + achSkip;
  396.                                     {if-then-else}
  397.                END;
  398.                {case-of}
  399.  
  400.                System.Inc(dwOffsetInBuffer);
  401.              END;
  402.              {for-to-do}
  403.  
  404.              IF  (dbAsciiSwitch <> aAsciiNone)
  405.                 THEN  BEGIN
  406.                         FOR dbIndex := 1  TO  (gadbSymbolsInString-dbCountInLine)
  407.                            DO  sOutStr := sOutStr + asSpaces5;
  408.                         {for-to-do}
  409.                         sOutStr := sOutStr + achHTAB + achHTAB +
  410.                                    achSemicolon + asSpace2 + sAsciiStr;
  411.                       END;
  412.              {if-then}
  413.  
  414.              System.WriteLn(fOutTextFile,sOutStr);
  415.              System.Dec(dwByteCount,dbCountInLine);
  416.  
  417.              sOutStr   := ';*       '+achHTAB + achHTAB +' Char #';
  418.              System.Write(fOutTextFile,sOutStr,gdbInitCharCount);
  419.              chAddChar := System.Char(gdbInitCharCount);
  420.              IF  NOT(chAddChar IN setIbmAscii)
  421.                  THEN  System.WriteLn(fOutTextFile,'  '''+'''')
  422.                  ELSE  System.WriteLn(fOutTextFile,'  '''+ chAddChar+'''');
  423.              {if-then-else}
  424.              System.Inc(gdbInitCharCount);
  425.  
  426.              FOR  dbIndex := 1  TO  dbCountInLine  DO
  427.              BEGIN
  428.                  sOutStr   := achHTAB + achHTAB + asAsmData + achHTAB+
  429.                       _fnsByteToBitString(gdbByteArray[dbIndex],achZero,achOne)+asAsmBitData;
  430.                  System.WriteLn(fOutTextFile,sOutStr);
  431.              END;
  432.              {for-to-do}
  433.  
  434.              System.WriteLn(fOutTextFile);
  435.  
  436.           END;
  437.   END;
  438.   {while-do}
  439.  
  440. END;  { _WriteBufferToDisk }
  441.  
  442.  
  443. {*============================== MAIN PART =============================*}
  444.  
  445. BEGIN
  446.   _CopyrightDisplay;
  447.  
  448.      IF (System.ParamCount < 2) THEN
  449.      BEGIN
  450.           System.WriteLn(asProgramPrompt+'  help screen for you.');
  451.           System.WriteLn('Usage: infile outfile [bytes[initchar[ascii]]]');
  452.           System.WriteLn('  infile   -  source filename       (def. ext. = '+asInDefExt+')');
  453.           System.WriteLn('  outfile  -  destination filename  (def. ext. = '+asOutDefExt+')');
  454.           System.WriteLn('  bytecnt  -  number of bytes per char for representation (def.=',aDefaultBytesPerLine,
  455.                          ', max=',aMaxBytesPerLine,')');
  456.           System.WriteLn('  initchar -  initial char number   (def. num. = ',gdbInitCharCount,')');
  457.           System.WriteLn('  ascii    -  optional, enable to add ASCII-char string');
  458.           System.WriteLn('                '+achAsciiBit7+' -> 7-bit ASCII format,');
  459.           System.WriteLn('                '+achAsciiBit8+' -> 8-bit ASCII format.');
  460.           System.WriteLn('                       (default=none)');
  461.           System.Halt(errBadParmsNumber);
  462.      END;
  463.      {if-then}
  464.  
  465.  
  466.   {** copy the parameters from command line **}
  467.   gsInFileName  := _fnsUpcaseStr(System.ParamStr(1));
  468.   gsInFileName := _fnsForceFileNameExt(gsInFileName,asInDefExt);
  469.  
  470.   gsOutFileName := _fnsUpcaseStr(System.ParamStr(2));
  471.   gsOutFileName := _fnsForceFileNameExt(gsOutFileName,asOutDefExt);
  472.  
  473.  
  474.   {* may be same names? *}
  475.   IF (gsInFileName = gsOutFileName)  THEN
  476.   BEGIN
  477.     System.WriteLn(asProgramPrompt+'  Unable to use same file as input and as output');
  478.     System.Halt(errSameNames);
  479.   END;
  480.   {if-then}
  481.  
  482.  
  483.   {** source file exists? **}
  484.   IF  NOT(_fnbFileExist(gfInputStream,gsInFileName)) THEN
  485.   BEGIN
  486.     System.WriteLn(asProgramPrompt+'  Unable to open file '+gsInFileName);
  487.     System.Halt(errSourceNotFound);
  488.   END;
  489.   {if-then}
  490.  
  491.  
  492.   {** may be destination file present? **}
  493.   IF (_fnbFileExist(gfOutputStream,gsOutFileName)) THEN
  494.   BEGIN
  495.     System.Write(asProgramPrompt+' Output file '+gsOutFileName+
  496.                  ' already exists. Overwrite? (n/y): ');
  497.     System.ReadLn(gsTempInput);
  498.     IF (System.UpCase(_fnchGetFirstChar(gsTempInput)) <> achYes)
  499.       THEN  System.Halt(errDestDontWrite);
  500.     {if-then}
  501.   END;
  502.   {if-then}
  503.  
  504.  
  505.  
  506.   {** read the following parameter = bytes switch **}
  507.   IF  (System.ParamCount >= 3) THEN
  508.   BEGIN
  509.        gsTempInput := System.ParamStr(3);
  510.        System.Val(gsTempInput,gadbSymbolsInString,giErrorCode);
  511.        IF  (gadbSymbolsInString = 0) OR (gadbSymbolsInString > aMaxBytesPerLine)
  512.           THEN   BEGIN
  513.              System.WriteLn(asProgramPrompt+' Invalid value for BYTES switch.');
  514.              System.Halt(errBadBytesValue);
  515.                  END;
  516.        {if-then}
  517.        IF  (giErrorCode <> 0)  THEN
  518.        BEGIN
  519.              System.WriteLn(asProgramPrompt+' Invalid format for BYTES switch.');
  520.              System.Halt(errBadFmtBytesValue);
  521.        END;
  522.        {if-then}
  523.   END;
  524.   {if-then}
  525.  
  526.  
  527.  
  528.   {** read the following parameter = initchar switch **}
  529.   IF  (System.ParamCount >= 4) THEN
  530.   BEGIN
  531.        gsTempInput := System.ParamStr(4);
  532.        System.Val(gsTempInput,gdbInitCharCount,giErrorCode);
  533.        IF  (giErrorCode <> 0)  THEN
  534.        BEGIN
  535.              System.WriteLn(asProgramPrompt+' Invalid value for INITCHAR switch.');
  536.              System.Halt(errBadCharValue);
  537.        END;
  538.        {if-then}
  539.   END;
  540.   {if-then}
  541.  
  542.  
  543.  
  544.   {** read the following parameter = ascii switch **}
  545.   IF  (System.ParamCount >= 5) THEN
  546.   BEGIN
  547.        gsTempInput := System.ParamStr(5);
  548.        CASE  gsTempInput[1] OF
  549.             achAsciiBit7  :  gadbAsciiFormat := aAscii7;
  550.             achAsciiBit8  :  gadbAsciiFormat := aAscii8;
  551.        ELSE
  552.           {** reserved **};
  553.        END;
  554.        {case-of}
  555.   END;
  556.   {if-then}
  557.  
  558.  
  559.  
  560.   {** open the source file **}
  561.   System.Assign(gfInputStream,gsInFileName);
  562.   {$I-}
  563.   System.Reset(gfInputStream,1);
  564.   {$I+}
  565.  
  566.   IF  (System.IoResult <> 0) THEN
  567.   BEGIN
  568.     System.WriteLn(asProgramPrompt+'  Unable to open '+gsInFileName);
  569.     System.Halt(errSrcOpenFailed);
  570.   END;
  571.   {if-then}
  572.  
  573.  
  574.   {** create the destination file **}
  575.   System.Assign(gfOutputFormatText,gsOutFileName);
  576.   System.SetTextBuf(gfOutputFormatText,gdbTextOutBuf);
  577.   {$I-}
  578.   System.Rewrite(gfOutputFormatText);
  579.   {$I+}
  580.  
  581.   IF  (System.IoResult <> 0) THEN
  582.   BEGIN
  583.     System.WriteLn(asProgramPrompt+'  Unable to create '+gsOutFileName);
  584.     System.Halt(errDestCreateFailed);
  585.   END;
  586.   {if-then}
  587.  
  588.  
  589.   {** get a count of bytes to read. **}
  590.     gddByteCount := System.FileSize(gfInputStream);
  591.     gddInFileSize := gddByteCount;
  592.  
  593.  
  594.   {** get memory on heap **}
  595.   IF  (System.MaxAvail < aMaxOnHeap)
  596.     THEN  gdwMemBlockSize := System.MaxAvail
  597.     ELSE  gdwMemBlockSize := aMaxOnHeap;
  598.   {if-then-else}
  599.   System.GetMem(gpMemoryBlock,gdwMemBlockSize);
  600.  
  601.  
  602.  
  603.   {** write first lines to output stream **}
  604.   System.WriteLn(gfOutputFormatText);
  605.   System.WriteLn(gfOutputFormatText,';  SOURCE FILE:  '+gsInFileName);
  606.   System.WriteLn(gfOutputFormatText,';  Created by '+asProgram+' utility, '+asCopyright+'  '+asAuthor);
  607.   System.WriteLn(gfOutputFormatText);
  608.   System.WriteLn(gfOutputFormatText,asAsmNoList);
  609.   System.WriteLn(gfOutputFormatText);
  610.  
  611.  
  612.   {** main loop: read_buffer/write_to_text_file **}
  613.   WHILE (gddByteCount <> 0) DO
  614.   BEGIN
  615.       IF  ((gddByteCount DIV gdwMemBlockSize) <> 0)
  616.         THEN  gdwBytesRead :=  gdwMemBlockSize
  617.         ELSE  gdwBytesRead :=  gddByteCount;
  618.       {if-then-else}
  619.       System.Inc(gddBytesCountDone,gdwBytesRead);
  620.  
  621.           BEGIN
  622.                System.WriteLn(asProgramPrompt+' Reading...');
  623.                System.BlockRead(gfInputStream,
  624.                                 System.Mem[System.Seg(gpMemoryBlock^):System.Ofs(gpMemoryBlock^)],
  625.                                 gdwBytesRead,
  626.                                 gdwBytesRead);
  627.                System.WriteLn(asProgramPrompt+' Writing...');
  628.                _WriteBufferToDisk(gfOutputFormatText,
  629.                                   gpMemoryBlock,
  630.                                   gdwBytesRead,
  631.                                   gadbAsciiFormat);
  632.                System.Dec(gddByteCount,gdwBytesRead);
  633.                System.Write(achCR+asProgramPrompt+' Completed ('+
  634.                _fnsNumToStr3((gddBytesCountDone*aPercent100) DIV gddInFileSize)+'%)');
  635.                System.WriteLn;
  636.           END;
  637.   END;
  638.   {while-do}
  639.  
  640.   {** write last lines to output stream **}
  641.   System.WriteLn(gfOutputFormatText);
  642.   System.WriteLn(gfOutputFormatText,asAsmList);
  643.  
  644.   {** free memory on heap **}
  645.   System.FreeMem(gpMemoryBlock,gdwMemBlockSize);
  646.  
  647.  
  648.   {** close all files **}
  649.   System.Close(gfInputStream);
  650.   System.Close(gfOutputFormatText);
  651.  
  652.  
  653.   {** report all done **}
  654.   System.WriteLn(asProgramPrompt+' Done.');
  655.  
  656.   {* System.Halt(errTerminateOk); *}
  657. END.
  658.