Skip to content

Instantly share code, notes, and snippets.

@JustinChristensen
Last active December 2, 2023 19:31
Show Gist options
  • Save JustinChristensen/9f6bba03c9d8a40ff0b520471661db0e to your computer and use it in GitHub Desktop.
Save JustinChristensen/9f6bba03c9d8a40ff0b520471661db0e to your computer and use it in GitHub Desktop.
module Main where
import Control.Monad (forM_)
import System.FilePath (isExtensionOf)
import qualified Data.ByteString as B
import qualified Data.ByteString.Builder as U
import System.Directory (listDirectory)
import DataSize
data FixState
= Start
| Backslash
deriving (Show, Eq)
fixFile :: FilePath -> IO ()
fixFile file = do
putStrLn $ "fixing " <> file
contents <- B.readFile file
let (fixedContents, _) = B.foldl' op (mempty, Start) contents
U.writeFile file fixedContents
where
bs = 0x5c
op (contents, Start) !c = (contents <> U.word8 c, if c == bs then Backslash else Start)
op (contents, Backslash) !c
| c == bs = (contents <> U.word8 bs, Start)
| otherwise = (contents <> U.word8 bs <> U.word8 c, Start)
fixSlashes :: FilePath -> IO ()
fixSlashes directory = do
files <- listDirectory directory
let textFiles = filter (isExtensionOf "txt") files
forM_ (inData <$> textFiles) fixFile
where
inData file = directory <> "/" <> file
main :: IO ()
main = do
fixFile "example.txt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment