如何在Haskell中将资源编译为二进制文件? [英] How to compile a resource into a binary in Haskell?
问题描述
假设我有一个 dictionary.txt
文件,我需要将它读入映射并在我的程序中使用它,我如何才能使这个dictionary.txt文件包含在编译的exe文件中?
您可以将序列化的数据类型存储为字节字符串。这里有一个例子:
<但是,嵌入文件会自动执行此过程,简单的嵌入。
所有方法基本归结为将静态数据表示为字节字符串,
{ - #LANGUAGE OverloadedStrings# - }
导入Data.Binary
将合格的Data.Map导入为M
将合格的Data.ByteString.Char8导入为S
导入Data.ByteString.Lazy
导入Codec.Compression.GZip
-
- 这是一个gzip压缩文字字符串,存储二进制编码DAT a.Map
-
mytable =
\US\139\b\NUL\NUL\NUL\NUL\NUL\NUL\ETXEN \
\\219\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \ 224 \ 171 \\\\\\\\\\\\\\\\\\\\\' \\ 210 \\\\\\\\\\\\\\\\\\\\\\\\\\' \ ENQ \ 214 \& 9> \ 202 \ 162 \ 179a \
\\\\\\\\\\\\\\\\\\\' SYN\157\DC2D\226 * \
\\\146\174o\t\167\DLE\209\i_\240\193\129 \\ 199< W \
\\ 250nC \CAN\212\CAN\162J\160\141C\178\133\216; \
\\\\\\\\\\\\\\\\\\\\'... \ 143 \\\\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 197 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'\\
main = print =<< M.lookupghcm
其中
- 从字符串中构建表:
m :: M.Map字符串(可能是字符串)
m =解码。解压。从块。返回$ mytable
Say I have a dictionary.txt
file, I need to read it into a map and use it in my program, how can I make this dictionary.txt file contained in the compiled exe file?
You can store serialized data types as bytestring literals. Here's an example:
- http://code.haskell.org/~dons/code/compiled-constants/
- http://haskell.org/haskellwiki/Compiling_in_constants
However, the file-embed automates this process, making it easier to do non-trivial embeddings.
All approaches essentially boil down to representing the static data as a bytestring literal,
{-# LANGUAGE OverloadedStrings #-}
import Data.Binary
import qualified Data.Map as M
import qualified Data.ByteString.Char8 as S
import Data.ByteString.Lazy
import Codec.Compression.GZip
--
-- this is a gzip compressed literal bytestring, storing a binary-encoded Data.Map
--
mytable =
"\US\139\b\NUL\NUL\NUL\NUL\NUL\NUL\ETXEN\
\\219\SO\194 \f\197\224\188\196\CAN\227\US\
\\224\171~\NAKc\GS4ce\161`\178\191\215(\176\
\\190\180\167\231\210\n\241\171\203\191\ti\
\\157\217\149\249< \ENQ\214\&9>\202\162\179a\
\\132X\233\ESC=\231\215\164\SYN\157\DC2D\226*\
\\146\174o\t\167\DLE\209\"i_\240\193\129\199<W\
\\250nC\CAN\212\CAN\162J\160\141C\178\133\216;\
\\\@4\144-W\203\209x\205\140\166\RS\163\237]9f\
\\170\143\ACK\163g\223\STX\184\&7\rH\222\FSW\
\\130\&7D\197\NUL\164\&0U\193\186\t\186o\
\\228\180~\NUL\a6\249\137#\SOH\NUL\NUL"
main = print =<< M.lookup "ghc" m
where
-- build the table from the bytestring:
m :: M.Map String (Maybe String)
m = decode . decompress . fromChunks . return $ mytable
这篇关于如何在Haskell中将资源编译为二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!