Author Topic: Tool for converting Unix format LF to M$ CRLF  (Read 141 times)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8497
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Tool for converting Unix format LF to M$ CRLF
« on: September 15, 2021, 11:01:40 AM »
I did a test piece to convert Unix text format to DOS/Windows CRLF format. Just added command line support for source and target files so it could be used as a utility for doing this conversion. Tested on files up to 8 gigabyte, seems to be fast enough and it works correctly.

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
'   -------------------
'   BUILD with PBCC 6.?
'   -------------------
    #compile exe "cnvt.exe"

    MACRO BufSize = 1024*1024*1                         ' smaller is faster than big

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

FUNCTION PBmain as LONG

    LOCAL cloc as QUAD                                  ' current location
    LOCAL flen as QUAD                                  ' file length
    LOCAL lcnt as QUAD                                  ' loop counter

    cmd$ = command$                                     ' get command line

    infile$  = parse$(cmd$," ",1)                       ' source file name

    If exist(infile$) = 0 Then                          ' check is source file exists
      StdOut "Source file "+infile$+" not found"
      waitkey$
      Exit FUNCTION
    End if

    outfile$ = parse$(cmd$," ",2)                       ' target file name

    kill outfile$                                       ' don't append to an existing file

    Open infile$  for Binary as #1
    Open outfile$ for Append as #2

    StdOut $CRLF+"processing "+infile$+" to "+outfile$

    flen = lof(1)                                       ' get the file length
    lcnt = flen \ BufSize                               ' integer divide
    lcnt = lcnt + 1                                     ' plus 1

    cloc = 0                                            ' current location is 0

    Do
      Seek #1, cloc                                     ' seek from current location
      Get$ #1,BufSize,FileChunk$                        ' get user defined chunk
      Replace chr$(10) with chr$(13,10) in FileChunk$   ' replace ascii 10 with ascii 13 10
      Print #2, FileChunk$;                             ' append the chunk to the last one
      cloc = cloc + BufSize                             ' increment current location by buffer size
      lcnt = lcnt - 1
    Loop while lcnt > 0

    Stdout $CRLF+"Conversion complete"+$CRLF+$CRLF+_    ' display the result
                 outfile$ + " written to disk at" +_
                 str$(lof(2)) + " bytes"

    Close #2
    Close #1

End FUNCTION

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

FUNCTION exist(fname$) as DWORD
  FUNCTION = Len( Dir$(fname$, 17) ) > 0
END FUNCTION

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy: