Created
April 2, 2013 09:40
-
-
Save propella/5291085 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- コンパイル方法 | |
-- $ ghc -o dos2unix dos2unix.hs | |
import System.IO (openTempFile, hClose, hPutStr) | |
import System.Directory (renameFile) | |
import System.Environment (getArgs) | |
-- DOS -> UNIX 改行変換。変換後元のファイルを書き換えます。 | |
dos2unix :: String -> IO () | |
dos2unix input = do src <- readFile input -- ファイルの中身をごそっと読み込むと Haskell がちょっとずつ処理してくれる。 | |
let dst = filter (/= '\r') src -- src の中から CR 削除して dst へ | |
(tempfile, temph) <- openTempFile "." "dos2unix" -- 一時ファイルの作成 | |
hPutStr temph dst -- 一時ファイルに書き込む | |
hClose temph -- 一時ファイルを閉じる | |
renameFile tempfile input -- 一時ファイルを元のファイル名にリネーム | |
-- 引数があれば dos2unix を実行。無ければ usage を表示 | |
main = do args <- getArgs | |
let run (x:_) = dos2unix x | |
run _ = putStrLn ("usage: dos2unix filename") | |
run args |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment