Dario de Judicibus wrote:
"Thomas Matthews" <Th**********************@sbcglobal.net> wrote in message
news:vj*******************@newssvr33.news.prodigy. com...
Probably the most portable method is to read data into an input area,
apply endianism, apply encoding, throw into an output area.
The most efficient method is to read a large chunk into an input area,
process the data then throw into an output area. When reading large
chunks, just make them multiples of 16-bits. This method is more
efficient since you are keeping the stream flowing. Many streams
have a "ramp-up" time, which is minimalized for larger chunks of
data. This method allows the process to be split into three
asychronous tasks: input, process, output.
Do you have a sample skeleton for building a pipe? Just READ and WRITE. I'll
add encoding myself, of course ;-)
DdJ
#include <stdio.h>
#include <stdlib.h>
#define INPUT_BUFFER_SIZE 2048 /* 1024 * 16-bits */
#define OUTPUT_BUFFER_SIZE 4096
unsigned char input_buffer[INPUT_BUFFER_SIZE];
unsigned char output_buffer[OUTPUT_BUFFER_SIZE];
int main(void)
{
unsigned char * p_input;
unsigned char * p_input_end;
unsigned char * p_output;
size_t bytes_read;
size_t bytes_written;
size_t bytes_to_write;
bytes_read = fread(input_buffer, 1, INPUT_BUFFER_SIZE, stdin);
p_input_end = input_buffer + bytes_read;
p_output = output_buffer;
p_input = input_buffer;
while (bytes_read)
{
while (p_input != p_input_end)
{
/* The Process_Data function will increment the */
/* p_output pointer. */
Process_Data(p_input, &p_output);
bytes_to_write = &output_buffer[OUTPUT_BUFFER_SIZE]
- p_output;
if (bytes_to_write < 3 /* 24 bits */)
{
bytes_written = fwrite(stdout,
output_buffer,
1,
bytes_to_write);
if (bytes_written != bytes_to_write)
{
return EXIT_FAILURE;
}
p_output = output_buffer;
}
p_input += 2 * sizeof(char); /* 16-bit increment */
}
p_input = input_buffer;
bytes_read = fread(input_buffer, 1,
INPUT_BUFFER_SIZE, stdin);
}
return EXIT_SUCCESS;
}
In the above skeleton, I have not checked to see if the
freads were successful.
The program fills up an input buffer using data from the
standard input stream (i.e. pipe). The data in the
buffer is processed until the end of the buffer is
reached or the number of bytes read. The processing
function updates the output buffer pointer (after writing
the data to the buffer). If the output buffer is near
full, it is written to the standard output stream (possible
pipe).
You should tweak the size of the buffers to find the most
efficient size for your platform.
The above program has not been compiled nor tested. Any
errors are for the reader correct. The program is just
an illustration of a concept.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book