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

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Unix2Dos, Dos2Unix file conversion alias
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Unix2Dos, Dos2Unix file conversion alias

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page


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

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.


779 views.

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

[Home]

Written by Nick Gammon - 5K

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

[Best viewed with any browser - 2K]    [Internet Contents Rating Association (ICRA) - 2K]    [Web site powered by FutureQuest.Net]