Bitwise IO: Difference between revisions

m
Fixed lang tags.
(C# code)
m (Fixed lang tags.)
Line 26:
 
=={{header|Ada}}==
<lang ada>with Ada.Streams; use Ada.Streams;
with Ada.Streams; use Ada.Streams;
with Ada.Finalization;
 
Line 46 ⟶ 45:
end record;
overriding procedure Finalize (Stream : in out Bit_Stream);
end Bit_Streams;</lang>
</lang>
The package provides a bit stream interface to a conventional stream. The object of Bit_Stream has a discriminant of any stream type. This stream will be used for physical I/O. Bit_Stream reads and writes arrays of bits. There is no need to have flush procedure, because this is done upon object destruction. The implementation is straightforward, big endian encoding of bits into Stream_Element units is used as required by the task:
<lang ada>package body Bit_Streams is
package body Bit_Streams is
procedure Finalize (Stream : in out Bit_Stream) is
begin
Line 82 ⟶ 79:
end loop;
end Write;
end Bit_Streams;</lang>
</lang>
 
Example of use:
<lang ada>with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;
<lang ada>
with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;
with Bit_Streams; use Bit_Streams;
 
Line 119 ⟶ 114:
raise Data_Error;
end if;
end Test_Bit_Streams;</lang>
</lang>
 
=={{header|C}}==
Line 128 ⟶ 122:
File: bitio.h
 
<lang c>#ifndef BIT_IO_H
#ifndef BIT_IO_H
#define BIT_IO_H
 
Line 146 ⟶ 139:
extern int biterr;
 
#endif</lang>
</lang>
 
File: bitio.c
 
<lang c>#include "bitio.h"
#include "bitio.h"
 
int biterr;
Line 248 ⟶ 239:
}
return wbit;
}</lang>
}
</lang>
 
===Usage example===
Line 255 ⟶ 245:
"Compression" of the ASCII byte standard input stream to the standard output:
 
<lang c>#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include "bitio.h"
Line 269 ⟶ 258:
bits_flush(stdout);
return 0;
}</lang>
}
</lang>
 
"Decompression" of a 7-bit encoded ASCII stream to a "regular" ASCII byte stream:
 
<lang c>#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include "bitio.h"
Line 287 ⟶ 274:
}
return 0;
}</lang ada>
}
</lang>
 
In some circumstances, the previous code could give an extra spurious byte; it happens when the original uncompressed input stream length (in byte) is 7 (mod 8); in this case, the last byte of the compressed stream contains only one "real" bit, the other 7 bits are just for padding. But the decompressor has no way to know this, and so it outputs the last 7 bits as they were "real", "expanding" them into a (spurious) byte.
Line 465 ⟶ 451:
: read-bit ( b m -- b' m' f )
dup 0= if 2drop init-read then
2dup and swap 2/ swap ;</lang>
</lang>
 
=={{header|OCaml}}==
The [http://code.google.com/p/ocaml-extlib/ extLib] provides [http://ocaml-extlib.googlecode.com/svn/doc/apiref/IO.html#6_BitsAPI bit oriented IO functions].
 
<lang ocaml>let write_7bit_string ~filename ~str =
let write_7bit_string ~filename ~str =
let oc = open_out filename in
let ob = IO.output_bits(IO.output_channel oc) in
Line 478 ⟶ 462:
IO.flush_bits ob;
close_out oc;
;;</lang>
</lang>
 
<lang ocaml>let read_7bit_string ~filename =
let read_7bit_string ~filename =
let ic = open_in filename in
let ib = IO.input_bits(IO.input_channel ic) in
Line 490 ⟶ 472:
Buffer.add_char buf c;
with End_of_file ->
(Buffer.contents buf)</lang>
</lang>
 
=={{header|Perl}}==
Anonymous user