All the advice and help I got on my difficulties till now have been
very useful. My current problem is a little weird and can't
figure out what is happening.
I have been able to get the serialization working with DirTree based
on the suggestions I have received till now. I have a function calcMD5
which given a FilePath will traverse the entire tree calculating the
checksum of each file it encounters. The resultant structure is
serializable by encode. But when I do a encodeFile to store the result
to a file I get nothing.
,----
| *Main> calcMD5 "/tmp/tmp"
| AncTree "/tmp" (DirW {name = "tmp", contents = [FileW {name = "passwd", file = Prop {md5sum = f54e7cef69973cecdce3c923da2f9222, modTime = Tue Jul 6 07:18:16 IST 2010, filenam = "/tmp/tmp/passwd"}}]})
|
| *Main> liftM encode $ calcMD5 "/tmp/tmp"
| Chunk "\NUL\NUL\NUL\NUL\NUL\NUL\NUL\EOT/tmp\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\ETXtmp\NUL\NUL\NUL\NUL\NUL\NUL\NUL\SOH\SOH\NUL\NUL\NUL\NUL\NUL\NUL\NUL\ACKpasswd\NUL\245N|\239i\151<\236\220\227\201#\218/\146\"\NULL2\139`\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\SI/tmp/tmp/passwd" Empty
`----
clearly the encoding is working.
But if I try to use encodeFile to write it to a file the file is not
created.
,----
| *Main> liftM (encodeFile "/tmp/tmp-list") $ calcMD5 "/tmp/tmp"
|
| $ ls /tmp/tmp-list
| ls: cannot access /tmp/tmp-list: No such file or directory
`----
I tried a few other things like converting the Bytestring from encode
into a string using show and then doing a writeFile on
it. Unfortunately none of them worked.
regards
--
Anand Mitra
Hi Anand
MD5Digest is an abstract type (the constructor is not exported from
its module) but it is an instance of Binary.
ClockTime (from System.Time) is not an instance of Binary but it does
export its constructor.
Neither are instances of Data.Data.
So I would hand-craft an instance of Binary for the Props datatype
rather than try to first make them instances of Data.
The code will be something like this, as I don't have MD5 installed it
is unchecked:
class Binary Props where
put (Prop md5 tim name) =
do { putWord8 0 -- number each constructor
; put md5 -- MD5Digest has a Binary instance
; putTOD tim
; put name
}
put Blank =
do { putWord8 1 } -- number each constructor
get = do { typ <- getWord8 -- get the constructor tag...
; case typ of
0 -> getProp
1 -> return Blank
}
getProp :: Get Props
getProp = do
{ md5 <- get
; tim <- getTOD
; name <- get
; return (Prop md5 tim name)
}
-- ClockTime doesn't have a binary instance
-- but a it has a single constructor
--
-- > TOD Integer Integer -
--
-- and Integer has a Binary instance, so I
-- would make auxillaris for put and get:
putTOD :: ClockTime -> Put ()
putTOD (TOD a b) = do { put a ; put b }
getTOD :: Get ClockTime
getTOD = do { a <- get; b <- get; return (TOD a b) }