It depends on your exact use case but I'll suggest how I would solve it:
- I believe ExtLib comes with a Base64 module.
- You'll primarily want to operate on chunks of strings rather than a
character at a time. So whichever of the following suggestions you go
with you'll want to consume as much string as makes sense, then send
it through processing, getting out the Base64 chunk.
- If you are doing asynchronous things, you probably want to read from
some place, then publish it to a Pipe.t and have processing pull off
the Pipe.t and do something with it and pass it on wherever.
- If you're not, then you want to read a chunk, send it through
processing, and push out a string on the other side.
IME it is rare to structure Ocaml code like Haskell where you are
stringing together a bunch of lazy data structures and hiding where
exactly that list is being produced from. That means you wouldn't try
to feed a string into an in_channel (although it can be done) but your
lowest case would be handling a string and then layers above pull the
string (or are given it) from the appropriate place.
That being said, I think the Base64 module provides some mechanism to
hide that the input is coming from a string vs an in_channel. I don't
know how to use the latter.
So the simplest form of what you want looks something like just a
recursive function that asks an In_channel.t for its next work, does
Base64.str_encode or str_decode on it, and does something with the
result, and loops.
Although someone who writes more elegant Ocaml code can correct me.