cosmetics
[hs-pgms.git] / src / Util.hs
bloba3e4988e9407ee0e77f8f239b6c97b1245143fe5
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.
12 -- Miscellaneous utility functions
15 module Util (
16 findFile,
17 formatString
18 ) where
20 import Paths_mine
22 import System.Directory
23 import Data.Char
24 import Data.List
26 -- find a data file
27 -- this uses Paths_mine.getDataFileName, but also looks
28 -- in a few other places for convenient in-place running.
29 findFile :: FilePath -> IO FilePath
30 findFile name = do
31 let scan :: [IO FilePath] -> IO FilePath
32 scan [] = error $ "Couldn't find file '" ++ name ++ "'"
33 scan (c:cs) = do
34 f <- c
35 b <- doesFileExist f
36 if b then return f else scan cs
37 scan [getDataFileName name,
38 return $ "data/" ++ name,
39 return $ "../data/" ++ name,
40 return $ name]
42 -- format a text string to fit nicely into 78 columns of text.
43 formatString :: String -> String
44 formatString = unlines . concatMap (block . (++" ")) . intersperse "" . lines
45 where
46 block "" = []
47 block text = let
48 (chunk, rest) = splitAt 78 text
49 (end', start') = span (not . isSpace) (reverse chunk)
51 if null start' then chunk : block (dropWhile isSpace rest)
52 else reverse (dropWhile isSpace start')
53 : block (reverse end' ++ rest)