users-prolog
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: handling mixed I/O.


From: Sylvain Soliman
Subject: Re: handling mixed I/O.
Date: Tue, 22 Apr 2008 12:20:18 +0200
User-agent: Mutt/1.5.17 (2007-11-01)

* Ferreira Maurizio <address@hidden>:
> I need to do mixed (binary/text) output to a socket stream.
> (I'm trying to develop a micro http server).
...
> The problem arises when I need to output binary data (images).
> The trouble is that I dont' know how to set the Output stream type.
> if it has the default value (type=Text), I cannot copy on it the binary
> data,
> and if I set it in binary mode, I cannot use 'write' to write to it.
> 
> Any suggetion ?

What I did when facing the same problem (for the same purpose!) was to always
write in binary mode (AFAIK it is not possible to dynamically change the type),
so I just wrote a few helpers:

=====
% same as 'format/3' and 'write/2' but with bytes instead of chars
% for binary streams

formatb(S,F,L):-
   format_to_atom(A,F,L),
   writeb(S,A).

writeb(S,A):-
   atom_codes(A,C),
   put_bytes(S,C).

put_bytes(_,[]).
put_bytes(S,[B|L]):-
   put_byte(S,B),
   put_bytes(S,L).
=====

Best,

        Sylvain

-- 
Sylvain Soliman <address@hidden>         Tel: (+33) 1 39635761
INRIA Paris-Rocquencourt - Equipe CONTRAINTES      Fax: (+33) 1 39635469
Domaine de Voluceau, Rocquencourt, BP 105   GnuPG Public Key: 0x0F53AF99
78153 LE CHESNAY CEDEX - FRANCE    http://contraintes.inria.fr/~soliman/

Attachment: pgphLBVBJXBnL.pgp
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]