Skip to content

Instantly share code, notes, and snippets.

@jarnaldich
Created March 31, 2017 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarnaldich/517b77d755da186a4f9091d62838d4d2 to your computer and use it in GitHub Desktop.
Save jarnaldich/517b77d755da186a4f9091d62838d4d2 to your computer and use it in GitHub Desktop.
Add fields from a directory tree with XML files using lenses, Directory Tree
#!/usr/bin/env stack
{-
stack
--resolver lts-4.2
--install-ghc runghc
--package xml-lens
--package directory-tree
-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import qualified System.Directory.Tree as DT
import System.FilePath ( takeExtension )
import System.Environment (getArgs)
import Text.XML
import Text.XML.Lens
import Text.Printf (printf)
import Data.Text as T
import Data.Text.Read (decimal)
main = do
[diskRoot] <- getArgs -- Gross...
tree <- DT.readDirectoryWith readTiles diskRoot
printf "Total tiles: %d\n" (sum $ DT.dirTree tree)
where
readTiles :: FilePath -> IO Int
readTiles f
| takeExtension f `elem` [".xml", ".XML"] = do
doc <- Text.XML.readFile def f
let lens = root . el "isd" ./ el "TIL" ./ el "NUMTILES" . text
return $ either (const 0) fst $ decimal (doc ^. lens)
| otherwise = return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment