Is it possible to build in a file in a haskell program??

Keith Wansbrough Keith.Wansbrough@cl.cam.ac.uk
Tue, 29 Apr 2003 17:17:48 +0100


> 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.