Is it possible to build in a file in a haskell program??
Alexandre Weffort Thenorio
alethenorio@home.se
Tue, 29 Apr 2003 18:58:55 +0200
Thanks but I am not sure what exactly you mean. I've done this:
Created a file called MyBinFile.hs and added such code:
module MyBinFile ( mybinfile ) where
mybinfile :: String
mybinfile = "Hello, world\x0A"
Created file Main.hs and added such code
module Main where
import MyBinFile ( mybinfile )
import IO
main = do h <- readFile file.bin
hPutStr h mybinfile
hClose h
Tried compiling main.hs file but it says "Failed to load interface for
MyBinFile".
Sorry but I am not what you can call a experienced programmer but if I
understood right I can read the bin file with haskell getting some kind of
binary string. The I can just add this binary string into my program and
tell it to write a file (In binary mode Using IoExts module) using this
String. Otherwise could you be a bit more specific??
Best Regards
Alex
----- Original Message -----
From: "Keith Wansbrough" <Keith.Wansbrough@cl.cam.ac.uk>
To: "Alexandre Weffort Thenorio" <alethenorio@home.se>
Cc: <haskell@haskell.org>
Sent: Tuesday, April 29, 2003 6:17 PM
Subject: Re: Is it possible to build in a file in a haskell program??
> > Hello again guys. I have made a small program that basically all it does
is
> > read a text file and write another text file with the info from the
first
> > one. Now let's say I have a bin file and I want the program to still
write
> > the text file and also create this bin file. Is it possible to build in
this
> > bin file inside the program so everything is inside the executable file
> > (exe) and then when the user runs the program it does it job (write the
text
> > file) and also creates this bin file exactly like it was on the same
> > directory the program is being run.
>
> Yes, of course.
>
> What you want to do is first write a Haskell (or Perl) program that reads
in a file and translates it into a little Haskell program, like this:
>
> module MyBinFile ( mybinfile ) where
> mybinfile :: String
> mybinfile = "Hello, world\x0A\x1A\x00\x01\x02\
> \\x03\x04\x05.....\
> \......\x00\x00"
>
> (the string continuation syntax is defined in the Haskell report, as are
the standard string escapes for characters outside \x20..\x7E)
>
> Then your main program looks like this:
>
> module Main where
> import MyBinFile ( mybinfile )
> import IO
>
> main = do h <- ... open the file you want ...
> hPutStr h mybinfile
> hClose h
>
> So you run your Haskell/Perl script on the original file to generate the
Haskell file MyBinFile.hs, then you compile MyBinFile.hs and Main.hs, and
distribute the resulting binary.
>
> Warning: compilation may be a bit slow with such a large string.
>
> Alternatively, you could get even trickier, by using the linker to stick
your original bin file onto the end of the executable. Then you could just
open the executable file itself in Haskell, find the bin file, and read it
out. You coudl either interpret the ELF/EXE binary format, or just look for
a magic string (careful you don't find the pattern in your search code
rather than the marker you want to find!).
>
> Let us know how you get on.
>
> HTH.
>
> --KW 8-)
>
> --
> Keith Wansbrough <kw217@cl.cam.ac.uk>
> http://www.cl.cam.ac.uk/users/kw217/
> University of Cambridge Computer Laboratory.
>
>