{"id":1080,"date":"2017-01-29T00:15:51","date_gmt":"2017-01-28T15:15:51","guid":{"rendered":"http:\/\/randt.jp\/?p=1080"},"modified":"2017-01-29T00:18:46","modified_gmt":"2017-01-28T15:18:46","slug":"haskellini%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%81%a8exception","status":"publish","type":"post","link":"https:\/\/randt.jp\/?p=1080","title":{"rendered":"[haskell]ini\u30d5\u30a1\u30a4\u30eb\u3068exception"},"content":{"rendered":"<p>ini\u5f62\u5f0f\u306e\u30c7\u30fc\u30bf\u3092\u30d1\u30fc\u30b9\u3057\u3066\u5024\u3092\u53d6\u5f97\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002\u307e\u305f\u3001\u30d5\u30a1\u30a4\u30eb\u304c\u7121\u3044\u3068\u304d\u306e\u4f8b\u5916\u30ad\u30e3\u30c3\u30c1\u3092\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<pre>\r\n{-# LANGUAGE ScopedTypeVariables #-}\r\nmodule Main where\r\nimport qualified Data.HashMap as HM\r\nimport qualified Data.Ini as Ini\r\nimport qualified Data.Text as Tx\r\nimport Control.Exception.Safe\r\n\r\nmain::IO ()\r\nmain = do\r\n    fi < - Ini.readIniFile \"test.ini\"\r\n     `catch` (\\(e::SomeException) -> do\r\n       putStrLn $ displayException e\r\n       return $ Ini.parseIni (Tx.pack \"\")\r\n     )\r\n    case fi of\r\n     Left fil -> putStrLn \"\"\r\n     Right fir -> do\r\n      putStrLn $ get (Tx.pack \"\u3042\") (Tx.pack \"sessionId\") fir\r\n      putStrLn $ get (Tx.pack \"\u3044\") (Tx.pack \"sessionId\") fir\r\n    putStrLn $ getFromStr (Tx.pack \"\u3042\")\r\n    return ()\r\n\r\n    where\r\n     get::Tx.Text -> Tx.Text -> Ini.Ini -> String\r\n     get section key ini =\r\n      case Ini.lookupValue section key ini of\r\n       Left l -> l\r\n       Right r -> Tx.unpack r\r\n\r\n     getFromStr::Tx.Text -> String\r\n     getFromStr section =\r\n      case Ini.parseIni (Tx.pack \"[\u3042]\\nhost=localhost\\nport=6666\\nsessionId=\u306a\u3044\") of\r\n       Left il -> il\r\n       Right ir -> get section (Tx.pack \"sessionId\") ir\r\n<\/pre>\n<pre>\r\n~$ cat test.ini\r\n[\u3042]\r\nhost=localhost\r\nport=6666\r\nsessionId=0x0001\r\n~$ .\/Main \r\n0x0001\r\nCouldn't find section: \u3044 < -- Left String\r\n\u306a\u3044\r\n~$ \r\n<\/pre>\n<p>lookupValue\u3084parseIni\u306f\u4f8b\u5916\u9001\u51fa\u305b\u305a\u3001\u5024\u304c\u898b\u3064\u304b\u3089\u306a\u304d\u6642\u306fLeft String\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>\r\n~$ rm test.ini\r\n~$ .\/Main \r\ntest.ini: openFile: does not exist (No such file or directory)\r\nCouldn't find section: \u3042\r\nCouldn't find section: \u3044\r\n\u306a\u3044\r\n~$ \r\n<\/pre>\n<p>catch\u306fMonadCatch\u306e\u578b\u30af\u30e9\u30b9\u3092\u6271\u3046\u305f\u3081IO\u304b\u3089\u629c\u3051\u3060\u305b\u307e\u305b\u3093\u3002\u305d\u306e\u3088\u3046\u306a\u3082\u306e\u306a\u306e\u3067\u3057\u3087\u3046\u304b\u3002\u7406\u89e3\u3067\u304d\u3066\u3044\u307e\u305b\u3093\u3002<\/p>\n<pre>\r\n~$ cat test.ini\r\nsss\r\n~$ .\/Main \r\nCouldn't find section: \u3042\r\nCouldn't find section: \u3044\r\n\u306a\u3044\r\n~$\r\n<\/pre>\n<p>ini\u30d5\u30a1\u30a4\u30eb\u306e\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u306b\u6cbf\u3063\u3066\u306a\u3044\u30d5\u30a1\u30a4\u30eb\u3001\u5358\u306b\u30bb\u30af\u30b7\u30e7\u30f3\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u3060\u3051\u3067\u3059\u3002\u30d1\u30fc\u30b9\u306f\u3067\u304d\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u5b9f\u884c\u30d5\u30a1\u30a4\u30eb\u30b5\u30a4\u30ba\u306f4598736\u30d0\u30a4\u30c8\u3067\u3057\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ini\u5f62\u5f0f\u306e\u30c7\u30fc\u30bf\u3092\u30d1\u30fc\u30b9\u3057\u3066\u5024\u3092\u53d6\u5f97\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002\u307e\u305f\u3001\u30d5\u30a1\u30a4\u30eb\u304c\u7121\u3044\u3068\u304d\u306e\u4f8b\u5916\u30ad\u30e3\u30c3\u30c1\u3092\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002 {-# LANGUAGE ScopedTypeVariables #-} module Main w &#8230;<\/p>\n<p> <a class=\"continue-reading-link\" href=\"https:\/\/randt.jp\/?p=1080\"><span>Continue reading<\/span><i class=\"crycon-right-dir\"><\/i><\/a> <\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[137,146,19],"tags":[],"class_list":["post-1080","post","type-post","status-publish","format-standard","hentry","category-haskell","category-ini","category-19"],"_links":{"self":[{"href":"https:\/\/randt.jp\/index.php?rest_route=\/wp\/v2\/posts\/1080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randt.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randt.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randt.jp\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/randt.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1080"}],"version-history":[{"count":2,"href":"https:\/\/randt.jp\/index.php?rest_route=\/wp\/v2\/posts\/1080\/revisions"}],"predecessor-version":[{"id":1082,"href":"https:\/\/randt.jp\/index.php?rest_route=\/wp\/v2\/posts\/1080\/revisions\/1082"}],"wp:attachment":[{"href":"https:\/\/randt.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randt.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randt.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}