[project @ 1999-01-23 18:07:42 by sof]
[ghc-hetmet.git] / ghc / tests / programs / dmgob_native1 / Main.lhs
1
2 Test program for reading a binary file containing a sequence of
3 vectors of possibly different dimension.  The format of the file is 
4
5         Block size                              Data
6         --------------------------------        ---------------------
7
8         sizeof(int) bytes                       dimension of vector 1
9         sizeof(float) x dimension1 bytes        vector 1
10         sizeof(int) bytes                       dimension of vector 2
11         sizeof(float) x dimension2 bytes        vector 2
12         sizeof(int) bytes                       dimension of vector 3
13         sizeof(float) x dimension3 bytes        vector 3
14
15                 :                                  :
16
17 This program will print the dimension, then the vector, then a blank
18 line, then the dimension of the next vector, the next vector, then a
19 blank line, etc.
20
21 ----------------------------------------------------------------------
22
23 > module Main where
24
25 > import Native
26 > import MaybeStateT
27 > import System
28
29 > main = getArgs           >>= \ args ->
30 >       case args of
31 >
32 >       [file]  -> readFile file   >>= \ bs ->
33 >                  let
34 >                    vs = readVectors bs
35 >                  in
36 >                  putStrLn (display vs)
37 >
38 >       _       -> error " need a binary file name"
39
40
41
42 > type Vector           = [Float]
43
44
45 > readVectors           :: Bytes -> [Vector]
46 > readVectors bs =
47 >       case readVector bs of
48 >       Nothing         -> []   -- assume there are no more vectors to read
49 >       Just (v, bs')   -> v : readVectors bs'
50
51
52 > readVector            :: MST Bytes Vector
53 > readVector =
54 >       readBytes                               `bindMST` \dimension ->
55 >       listReadBytes dimension                 `bindMST` \v ->
56 >       returnMST v
57
58
59
60 > display :: [Vector] -> String
61 > display (v:vs) = displayVector v ++ display vs
62 > display []     = "\n"
63
64 > displayVector :: Vector -> String
65 > displayVector v = shows (length v) "\n" ++
66 >                   shows v "\n\n"
67