Last active
January 3, 2016 10:39
-
-
Save abhiranjankumar00/8450700 to your computer and use it in GitHub Desktop.
Trim all input/output files or specific files.
This file contains 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
{- | |
This will trim spaces at end of file and end of line for all | |
input/output files in current, input and output directory. | |
After compiling, put executable in /usr/bin/. | |
ghc -O2 -rtsopts -with-rtsopts="-K512m -A8m" -o trimSpaces trimSpaces.hs | |
sudo cp trimSpaces /usr/bin/ | |
Run `trimSpaces` at the problem/input/output directory to trim | |
all input/output files. | |
OR | |
Run `trimSpaces file1 file1` to trim specific files. | |
-} | |
module Main where | |
import Data.List | |
import Data.Char | |
import System.IO | |
import Text.Printf | |
import Control.Monad | |
import qualified Data.ByteString.Char8 as BS | |
import System.Directory | |
import System.FilePath | |
import Text.Regex.Posix | |
import System.Environment | |
main :: IO () | |
main = do | |
args <- getArgs | |
if not (null args) then | |
forM_ args $ \file -> do | |
fileExists <- doesFileExist file | |
if fileExists then | |
trimFile file | |
else return () | |
else do | |
curDirContent' <- getDirectoryContents curDir | |
inputDirExists <- doesDirectoryExist inputDirectory | |
outputDirExists <- doesDirectoryExist outputDirectory | |
inputDirContent' <- if inputDirExists then getDirectoryContents inputDirectory else return [] | |
outputDirContent' <- if outputDirExists then getDirectoryContents outputDirectory else return [] | |
let | |
curDirContent = sort. filter (\name -> (name =~ "^.*(input|output)[0-9]*.txt$")::Bool). map (curDir </>)$ curDirContent' | |
inputDirContent = sort. filter (\name -> (name =~ "^.*input[0-9]*.txt$")::Bool). map (inputDirectory </>)$ inputDirContent' | |
outputDirContent = sort. filter (\name -> (name =~ "^.*output[0-9]*.txt$")::Bool). map (outputDirectory </>)$ outputDirContent' | |
mapM_ trimFile curDirContent | |
mapM_ trimFile inputDirContent | |
mapM_ trimFile outputDirContent | |
where | |
curDir = "./" | |
inputDirectory = curDir </> "input" | |
outputDirectory = curDir </> "output" | |
trimFile :: FilePath -> IO() | |
trimFile path = do | |
hIn <- openFile path ReadMode | |
inContent <- BS.hGetContents hIn | |
printf "Read %d characters from %s \t -> \t" (BS.length inContent) path | |
hClose hIn | |
let | |
newLines = BS.pack "\n" | |
outContent = BS.intercalate newLines. map trimSpaces. BS.lines.trimSpaces $ inContent | |
hOut <- openFile path WriteMode | |
BS.hPutStr hOut outContent | |
printf "Written %d characters in %s\n" (BS.length outContent) path | |
hClose hOut | |
trimSpaces :: BS.ByteString -> BS.ByteString | |
trimSpaces = BS.reverse. BS.dropWhile isSpace. BS.reverse | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment