summaryrefslogtreecommitdiff
path: root/src/compiler/GF/Index.hs
diff options
context:
space:
mode:
authorgdetrez <gdetrez@crans.org>2010-12-11 16:36:30 +0000
committergdetrez <gdetrez@crans.org>2010-12-11 16:36:30 +0000
commitd7ae73f1c7c5b95f1f08dadd314fa7143602b523 (patch)
treee76f59a491f36859ae8636affcb6e969ada65af6 /src/compiler/GF/Index.hs
parenta59df6b49575f54756615c6e2b860ea0972a81eb (diff)
Adding an option to the gf compiler to add an index to pgf files
This is gonna be used by the android library to skip unused part of a pgf file
Diffstat (limited to 'src/compiler/GF/Index.hs')
-rw-r--r--src/compiler/GF/Index.hs36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/compiler/GF/Index.hs b/src/compiler/GF/Index.hs
new file mode 100644
index 000000000..a685f09c2
--- /dev/null
+++ b/src/compiler/GF/Index.hs
@@ -0,0 +1,36 @@
+{--
+This module provide a function for indexing a pgf.
+
+It reads the pgf and add a global flag, called "index", containing a string
+with concrete names and size in bytes separated by a column.
+ex : "DisambPhrasebookEng:18778 PhrasebookBul:49971 PhrasebookCat:32738..."
+--}
+module GF.Index (addIndex) where
+
+import PGF
+import PGF.Data
+import PGF.Binary
+import Data.Binary
+import Data.ByteString.Lazy (readFile,length)
+import qualified Data.Map as Map
+import Data.Map (toAscList)
+import Data.List (intercalate)
+import qualified Data.ByteString.Lazy as BS
+
+addIndex :: PGF -> PGF
+addIndex pgf = pgf {gflags = flags}
+ where flags = Map.insert (mkCId "index") (LStr $ showIndex index) (gflags pgf)
+ index = getIndex pgf
+
+
+showIndex :: [(String,Int)] -> String
+showIndex = intercalate " " . map f
+ where f (name,size) = name ++ ":" ++ show size
+
+getsize :: Binary a => a -> Int
+getsize x = let bs = encode x in fromIntegral $ Data.ByteString.Lazy.length bs
+
+getIndex :: PGF -> [(String,Int)]
+getIndex pgf = cncindex
+ where cncindex = map f $ Data.Map.toAscList $ concretes pgf
+ f (cncname,cnc) = (show cncname, getsize cnc)