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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
module Config.ReadConfig
( autoCorr
, checkList
, corrList
, maxLength
, shallCheck
, shallCorrect
) where
import Directory (doesFileExist, getHomeDirectory)
import FilePath ((</>))
import Global
import List (isInfixOf, isPrefixOf, isSuffixOf, last)
import Read (readInt)
import ReadShowTerm (readQTerm)
import Unsafe (unsafePerformIO)
import Utils (fst4, snd4, thrd4, frth4)
import Config.Types (Check (..), cvCheck, help, configName, defaultLgth
,defaultCfg, defaultAutoC)
shallCheck :: Check -> Bool
shallCheck = flip elem checkList
shallCorrect :: Check -> Bool
shallCorrect = flip elem corrList
config :: [(Check, Int)]
config = unsafePerformIO $ liftIO fst4 getConfig
checkList :: [Check]
checkList = map fst $ filter (\p -> snd p `elem` [1,2]) config
corrList :: [Check]
corrList = map fst $ filter (\p -> snd p == 2) config
maxLength :: Int
maxLength = unsafePerformIO $ liftIO snd4 getConfig
autoCorr :: String
autoCorr = unsafePerformIO $ liftIO frth4 getConfig
currConfig :: Global (Maybe ([(Check, Int)], Int, String, String))
currConfig = global Nothing Temporary
getConfig :: IO ([(Check, Int)], Int, String, String)
getConfig = do
readGlobal currConfig >>= maybe readConfigFile return
readConfigFile :: IO ([(Check, Int)], Int, String, String)
readConfigFile = do
hasLocalConfigFile <- doesFileExist configName
configFile <- if hasLocalConfigFile
then return configName
else getHomeDirectory >>= return . (</> configName)
cfgExists <- doesFileExist configFile
currconf <- if cfgExists
then do srcL <- readFile configFile >>= return . lines
return (readCfg srcL, readMaxLength srcL,
"", readAutoCorrect srcL)
else return (defaultCfg, defaultLgth, "", defaultAutoC)
writeGlobal currConfig (Just currconf)
return currconf
readCfg :: [String] -> [(Check, Int)]
readCfg [] = []
readCfg (c:cs)
| null c = readCfg cs
| isPrefixOf "--" c = readCfg cs
| isPrefixOf "maxLength" c = readCfg cs
| isPrefixOf "autoCorrect" c = readCfg cs
| isSuffixOf "0" c = (cvCheck c, 0) : readCfg cs
| isSuffixOf "1" c = (cvCheck c, 1) : readCfg cs
| isSuffixOf "2" c = (cvCheck c, 2) : readCfg cs
| otherwise = error help
readMaxLength :: [String] -> Int
readMaxLength [] = defaultLgth
readMaxLength (c:cs)
| isPrefixOf "maxLength" c = readInt $ last $ words c
| otherwise = readMaxLength cs
readAutoCorrect :: [String] -> String
readAutoCorrect [] = defaultAutoC
readAutoCorrect (c:cs)
| isPrefixOf "autoCorrect" c = last $ words c
| otherwise = readAutoCorrect cs
|