[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Unix2Dos, Dos2Unix file conversion alias

Unix2Dos, Dos2Unix file conversion alias

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Fri 19 Nov 2010 03:28 AM (UTC)

Amended on Fri 19 Nov 2010 04:57 AM (UTC) by Nick Gammon

Message
This alias uses LPEG to handle the general case of converting files from Unix to DOS format, or vice-versa.

As described somewhere in the LPEG documentation, you can make a pattern that matches any of the standard line endings, namely:


  • \r
  • \n
  • \r\n
  • \n\r


Since this pattern matches any of the above, it will successfully find the line endings on any format file, without having to know in advance what format it is in.

The alias asks for an input file, and reads it in.

It then asks for the new line endings that you want (Dos, Mac, Unix) and then converts the line endings, reports the before and after sizes, and offers to save the converted data (press Esc to not save).

A modification of the general technique could be used to convert an entire directory.

You could also add additional line endings (for example, "<br>\n" to end each line).


<aliases>
  <alias
   match="file_convert"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>

-- get input file
local inputname = utils.filepicker ("Choose file to convert")

if not inputname then return end

-- read input file
local f = assert (io.open (inputname, "rb"))
local s = f:read "*a"
f:close ()

-- four forms of delimiter
local delim = lpeg.P (lpeg.P("\\n") * "\\r") + 
        lpeg.P (lpeg.P("\\r") * "\\n") + 
        lpeg.P ("\\n") + 
        lpeg.P ("\\r")

-- conversion function
local function gsub (s, patt, repl)
  patt = lpeg.P(patt)
  patt = lpeg.Cs((patt / repl + 1)^0)
  return lpeg.match(patt, s)
end

local choices    = { 
  ["\\n"]     = "Unix (LF only)", 
  ["\\r\\n"]  = "Dos (CR/LF)",   
  ["\\r"]     = "Mac (CR only)",
   }  -- end choices

-- choose new line-ending type
local delimiter = utils.listbox ("Choose new line endings ...", 
                                 "Format of output file", choices, "\\r\\n")

if not delimiter then return end

-- convert line endings to new format
local converted = gsub (s, delim, delimiter)

-- show stats
print (string.format ("Input  size (%%s) = %%i bytes (%%0.1f Kb).", inputname, #s, #s / 1024))
print ("Output size =", #converted, "bytes.")

-- ask to save
local outputname = utils.filepicker ("File name to save as", inputname, nil, nil, true)

if not outputname  then return end

-- write it
local f = assert (io.open (outputname , "wb"))
assert (f:write (converted))
f:close ()

-- confirmation
print ("Done. Saved as:", outputname)

</send>
  </alias>
</aliases>



- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


4,057 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]