|
@@ -0,0 +1,62 @@
|
|
|
+{-# LANGUAGE NamedFieldPuns #-}
|
|
|
+
|
|
|
+module Main where
|
|
|
+
|
|
|
+import qualified Control.Exception as Exn
|
|
|
+import qualified Foreign.Lua as Lua
|
|
|
+
|
|
|
+data Config = Config
|
|
|
+ { cfgHost :: String,
|
|
|
+ cfgPort :: Int,
|
|
|
+ cfgFiles :: [String]
|
|
|
+ }
|
|
|
+ deriving (Show)
|
|
|
+
|
|
|
+readConfig :: FilePath -> IO Config
|
|
|
+readConfig configPath =
|
|
|
+
|
|
|
+ Exn.bracket Lua.newstate Lua.close $ \st ->
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Lua.unsafeRunWith st $ do
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Lua.openlibs
|
|
|
+
|
|
|
+
|
|
|
+ _ <- Lua.loadfile configPath
|
|
|
+
|
|
|
+
|
|
|
+ Lua.call 0 0
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ cfgHost <- readGlobal "host"
|
|
|
+ cfgPort <- readGlobal "port"
|
|
|
+ cfgFiles <- readGlobal "files"
|
|
|
+
|
|
|
+
|
|
|
+ return Config {cfgHost, cfgPort, cfgFiles}
|
|
|
+
|
|
|
+
|
|
|
+readGlobal :: Lua.Peekable t => String -> Lua.Lua t
|
|
|
+readGlobal name = do
|
|
|
+
|
|
|
+ Lua.getglobal name
|
|
|
+
|
|
|
+ idx <- Lua.gettop
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ value <- Lua.peek idx
|
|
|
+
|
|
|
+ Lua.pop 1
|
|
|
+
|
|
|
+ return value
|
|
|
+
|
|
|
+main :: IO ()
|
|
|
+main = do
|
|
|
+ cfg <- readConfig "sample.lua"
|
|
|
+ print cfg
|