add file headers
[hs-pgms.git] / src / Util.hs
blobae271f847b8f23828b460d9e66b998b2682e998e
1 -- |
2 -- Module : Util
3 -- Copyright : (c) 2008 Bertram Felgenhauer
4 -- License : BSD3
5 --
6 -- Maintainer : Bertram Felgenhauer <int-e@gmx.de>
7 -- Stability : experimental
8 -- Portability : portable
9 --
10 -- This module is part of Haskell PGMS.
13 module Util (
14 findFile,
15 formatString
16 ) where
18 import Paths_mine
19 import System.Directory
20 import Data.Char
21 import Data.List
23 findFile :: FilePath -> IO FilePath
24 findFile name = do
25 let scan :: [IO FilePath] -> IO FilePath
26 scan [] = error $ "Couldn't find file '" ++ name ++ "'"
27 scan (c:cs) = do
28 f <- c
29 b <- doesFileExist f
30 if b then return f else scan cs
31 scan [getDataFileName name,
32 return $ "data/" ++ name,
33 return $ "../data/" ++ name,
34 return $ name]
36 formatString :: String -> String
37 formatString = unlines . concatMap (block . (++" ")) . intersperse "" . lines
38 where
39 block "" = []
40 block text = let
41 (chunk, rest) = splitAt 78 text
42 (end', start') = span (not . isSpace) (reverse chunk)
44 if null start' then chunk : block (dropWhile isSpace rest)
45 else reverse (dropWhile isSpace start')
46 : block (reverse end' ++ rest)