summaryrefslogtreecommitdiff
path: root/src/CSV.hs
blob: b4850b4a2c8f3e91c0f975f3027647e4e3ae5a2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
{-# LANGUAGE FlexibleContexts #-}

module CSV(
    Settings(..),

    specialChars,
    defaultSettings,
    unParseRecord,
    parseRecord
    ) where




--  This source is licensed under Creative Commons CC0 v1.0.

--  To read the full text, see license.txt in the main directory of this repository
--  or go to https://creativecommons.org/publicdomain/zero/1.0/legalcode.txt

--  For a human readable summary, go to https://creativecommons.org/publicdomain/zero/1.0/




import Text.ParserCombinators.Parsec ( (<|>), (<?>) )
import qualified Text.ParserCombinators.Parsec as Parsec
import qualified Data.Char as Char
import qualified Data.List as List




data Settings = Settings
    { separator :: Char
    , quote     :: Char
    , escape    :: Char }




defaultSettings = Settings
    { separator = ','
    , quote     = '\"'
    , escape    = '\\' }




specialChars :: Settings -> String
specialChars s = (separator s):(quote s):(escape s):[]




unParseRecord :: Settings -> [String] -> String
unParseRecord settings record =
    let escFunc c = if (c == escape settings || c == quote settings) then (escape settings):c:[] else c:[]
        escapeField s =
            if ((escape settings) `elem` s || (quote settings) `elem` s || (separator settings) `elem` s)
                then ((quote settings) : (concatMap escFunc s)) ++ ((quote settings):[])
                else s
    in List.intercalate [separator settings] (map escapeField record)




parseRecord :: Settings -> String -> Either Parsec.ParseError [String]
parseRecord settings input =
    Parsec.parse (record settings) "error" input


record s = do
    f <- (field s) `Parsec.sepBy` (Parsec.char (separator s))
    Parsec.optional eol
    Parsec.eof
    return f


field s =
    Parsec.many (Parsec.try (quoted s) <|> Parsec.many1 (fieldChar s)) >>=
    return . foldl1 (++)


quoted s =
    Parsec.between
        (Parsec.char (quote s))
        (Parsec.char (quote s))
        (Parsec.many (quotedChar s))


fieldChar s = allExcept s (specialChars s)


quotedChar s = allExcept s [quote s]


allExcept s c =
    Parsec.try (escapeChar s) <|>
    Parsec.satisfy (\x -> (not (Char.isControl x)) && (x `notElem` c))


escapeChar s = do
    Parsec.char (escape s)
    Parsec.oneOf (specialChars s)


eol  =  Parsec.try (Parsec.string "\r\n")
    <|> Parsec.try (Parsec.string "\r")
    <|> Parsec.try (Parsec.string "\n")
    <?> "end of line"