Implement a simple filter

  • Thread starter Dario de Judicibus
  • Start date
D

Dario de Judicibus

I wish to implement a simple C++ filter that reads from a file byte by byte
and writes to an output files two bytes for each byte read according to some
hardcoded table. I am looking for a very straightforward sample. It is
pratically a binary filter.

Thank you in advance.

DdJ
 
V

Victor Bazarov

Dario de Judicibus said:
I wish to implement a simple C++ filter that reads from a file byte by byte
and writes to an output files two bytes for each byte read according to some
hardcoded table. I am looking for a very straightforward sample. It is
pratically a binary filter.

What book on C++ do you have that doesn't describe file I/O?
 
J

Jacques Labuschagne

Dario said:
I wish to implement a simple C++ filter that reads from a file byte by byte
and writes to an output files two bytes for each byte read according to some
hardcoded table. I am looking for a very straightforward sample. It is
pratically a binary filter.

Thank you in advance.

DdJ

How about this:

#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <algorithm>
#include <iterator>
#include <fstream>
using namespace std;

class filter{
map<char, pair<char, char> > table;

public:
filter(){
// Add your own initialization here.
table['a'] = make_pair('b', 'c');
table['d'] = make_pair('e', 'f');
}
void operator()(char c){
static ofstream out("my_output.txt");
out << table[c].first << table[c].second;
}
};

int main()
{
ifstream input("my_input.txt");
istream_iterator<char> current(input), end;
for_each(current, end, filter());
}
 
M

Mike Wahler

Dario de Judicibus said:
I wish to implement a simple C++ filter that reads from a file byte by byte
and writes to an output files two bytes for each byte read according to some
hardcoded table. I am looking for a very straightforward sample. It is
pratically a binary filter.

Thank you in advance.

#include <fstream>
#include <ios>
#include <iostream>
#include <limits>
#include <map>
#include <string>

void show_table(const std::map<unsigned char,
std::basic_string<unsigned char> >&t)
{
std::map<unsigned char,
std::basic_string<unsigned char> >
::const_iterator it(t.begin());

while(it != t.end())
{
std::cout << unsigned int(it->first) << ": ";

for(std::string::size_type i = 0; i != it->second.size(); ++i)
std::cout << unsigned int(it->second) << ", ";

std::cout << '\n';
++it;
}
}

int main()
{
std::map<unsigned char, std::basic_string<unsigned char> > table;
unsigned char hi(std::numeric_limits<unsigned char>::max());
unsigned char c(hi);

while(c)
{
table[c] = std::basic_string<unsigned char>(2, hi - c);
--c;
}

table[c] = std::basic_string<unsigned char>(2, hi - c);

show_table(table);

std::basic_ifstream<unsigned char> input("input", std::ios::binary);
std::basic_ofstream<unsigned char> output("output", std::ios::binary);

if(input && output)
{
unsigned char c(0);

while(input.get(c))
{
std::basic_string<unsigned char> s(table[c]);

if(!output.write(s.c_str(), s.size()))
break;
}
}

return 0;
}

:)

-Mike
 
J

Jack Klein

Dario de Judicibus said:
I wish to implement a simple C++ filter that reads from a file byte by byte
and writes to an output files two bytes for each byte read according to some
hardcoded table. I am looking for a very straightforward sample. It is
pratically a binary filter.

Thank you in advance.

#include <fstream>
#include <ios>
#include <iostream>
#include <limits>
#include <map>
#include <string>

void show_table(const std::map<unsigned char,
std::basic_string<unsigned char> >&t)
{
std::map<unsigned char,
std::basic_string<unsigned char> >
::const_iterator it(t.begin());

while(it != t.end())
{
std::cout << unsigned int(it->first) << ": ";

for(std::string::size_type i = 0; i != it->second.size(); ++i)
std::cout << unsigned int(it->second) << ", ";

std::cout << '\n';
++it;
}
}

int main()
{
std::map<unsigned char, std::basic_string<unsigned char> > table;
unsigned char hi(std::numeric_limits<unsigned char>::max());
unsigned char c(hi);

while(c)
{
table[c] = std::basic_string<unsigned char>(2, hi - c);
--c;
}

table[c] = std::basic_string<unsigned char>(2, hi - c);

show_table(table);

std::basic_ifstream<unsigned char> input("input", std::ios::binary);
std::basic_ofstream<unsigned char> output("output", std::ios::binary);

if(input && output)
{
unsigned char c(0);

while(input.get(c))
{
std::basic_string<unsigned char> s(table[c]);

if(!output.write(s.c_str(), s.size()))
break;
}
}

return 0;
}

:)

-Mike


Since the table needs to be initialized at program start-up, can you
seriously suggest that in this particular case a map, or even a
vector, is better than:

const unsigned char lut[UCHAR_MAX][2] =
{
{ 0x00, 0x00 },
{ 0x01, 0x01 },
/* ... */
{ UCHAR_MAX, UCHAR_MAX}
};

??

That is, assuming the constants are pre-defined.
 
M

Mike Wahler

Jack Klein said:
On Sat, 03 Jan 2004 03:39:09 GMT, "Mike Wahler"
[snip]
int main()
{
std::map<unsigned char, std::basic_string<unsigned char> > table;
unsigned char hi(std::numeric_limits<unsigned char>::max());
unsigned char c(hi);

while(c)
{
table[c] = std::basic_string<unsigned char>(2, hi - c);
--c;
}

table[c] = std::basic_string<unsigned char>(2, hi - c);
[snip]


Since the table needs to be initialized at program start-up, can you
seriously suggest that in this particular case a map, or even a
vector, is better than:

const unsigned char lut[UCHAR_MAX][2] =
{
{ 0x00, 0x00 },
{ 0x01, 0x01 },
/* ... */
{ UCHAR_MAX, UCHAR_MAX}
};

Yes, that would be the 'hard-coded table' OP referred to.
But I'm not gonna type (at least) 256 initializers (which
I'd need for a full test). So I programmatically created
the data. I leave it to OP to adjust the code to his needs
(or, of course, ignore it, and use somebody else's suggestions).
??

That is, assuming the constants are pre-defined.

Right.

-Mike
 
D

Dario de Judicibus

Jacques Labuschagne said:
How about this:
....

Great code Jacques. It was exactly what I was looking for. Small, advanced.
I wouldn't be able to write it. Thank you again.

DdJ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,883
Members
47,414
Latest member
djangoframe

Latest Threads

Top