Need to translate from Delphi to C++

M

Mariusz Sakowski

Can someone translate this code to a C++?

repeat
file.Read(bufor, 1);
for p := 1 to KeyCount do
begin
buf := buf xor Keys[p];
end;
targetFile.Write(bufor, 1);
until file.Size = file.Position;

where file and targetFile are TFileStream's and Keys is array of byte,
with KeyCount elements. Oh, and p is loop control variable, and buf is
byte, that's all I think:). Thanks in advance for any help.
 
J

Jeff Schwab

Here's a first crack, but where does bufor get used?

do
{
file.Read( bufor, 1 );

for( int p = 1; p <= KeyCount; ++p )
{
buf ^= Keys[ p ];
}

targetFile.Write( bufor, 1 );
}
while( file.Size == file.Position );
 
J

Jeff Schwab

Second crack, using 0-indexed array and for_each, correcting loop test,
using conventional case for identifiers, and removing do-while (which I
personally detest :):

inline void process_byte( Key const& key, char& buf )
{
buf ^= key;
}

while( file.size <= file.position )
{
file.read( bufor, 1 );

std::for_each( &keys[ 0 ], &keys[ key_count ], process_byte );

target_file.write( bufor, 1 );
}

Jeff said:
Here's a first crack, but where does bufor get used?

do
{
file.Read( bufor, 1 );

for( int p = 1; p <= KeyCount; ++p )
{
buf ^= Keys[ p ];
}

targetFile.Write( bufor, 1 );
}
while( file.Size == file.Position );

Mariusz said:
Can someone translate this code to a C++?

repeat
file.Read(bufor, 1);
for p := 1 to KeyCount do
begin
buf := buf xor Keys[p];
end;
targetFile.Write(bufor, 1);
until file.Size = file.Position;

where file and targetFile are TFileStream's and Keys is array of byte,
with KeyCount elements. Oh, and p is loop control variable, and buf is
byte, that's all I think:). Thanks in advance for any help.
 
J

Jeff Schwab

Third crack, using standard stream and actually correcting loop test,
not including EOF in output buffer, assuming "buf" and "bufor" were
meant to be the same variable (thus bringing sanity to the buffer reads
and writes):

namespace
{
typedef char Key;

char buf;
int const key_count = 100; // Or however many keys you need.
Key keys[ key_count ];

inline void process_byte( Key const& key, char& buf )
{
buf ^= key;
}
}

int main( )
{
// Assign keys, do whatever other set-up you need.

std::fstream file( "file_name", std::ios::in | std::ios::eek:ut );

while( file >> buf )
{
std::for_each( &keys[ 0 ], &keys[ key_count ], process_byte )
}
}


Jeff said:
Second crack, using 0-indexed array and for_each, correcting loop test,
using conventional case for identifiers, and removing do-while (which I
personally detest :):

inline void process_byte( Key const& key, char& buf )
{
buf ^= key;
}

while( file.size <= file.position )
{
file.read( bufor, 1 );

std::for_each( &keys[ 0 ], &keys[ key_count ], process_byte );

target_file.write( bufor, 1 );
}

Jeff said:
Here's a first crack, but where does bufor get used?

do
{
file.Read( bufor, 1 );

for( int p = 1; p <= KeyCount; ++p )
{
buf ^= Keys[ p ];
}

targetFile.Write( bufor, 1 );
}
while( file.Size == file.Position );

Mariusz said:
Can someone translate this code to a C++?

repeat
file.Read(bufor, 1);
for p := 1 to KeyCount do
begin
buf := buf xor Keys[p];
end;
targetFile.Write(bufor, 1);
until file.Size = file.Position;

where file and targetFile are TFileStream's and Keys is array of byte,
with KeyCount elements. Oh, and p is loop control variable, and buf is
byte, that's all I think:). Thanks in advance for any help.
 
J

Jeff Schwab

Last one, including write to output buffer.

Sorry for so many premature posts!

namespace
{
typedef char Key;

char buf;
int const key_count = 100; // Or however many keys you need.
Key keys[ key_count ];

inline void process_byte( Key const& key, char& buf )
{
buf ^= key;
}
}

int main( )
{
// Assign keys, do whatever other set-up you need.

std::fstream file( "file_name", std::ios::in | std::ios::eek:ut );

while( file >> buf )
{
std::for_each( &keys[ 0 ], &keys[ key_count ], process_byte );

file.putback( buf );
file.get( ); // Skip the char that was just "put back."
}
}


Jeff said:
Third crack, using standard stream and actually correcting loop test,
not including EOF in output buffer, assuming "buf" and "bufor" were
meant to be the same variable (thus bringing sanity to the buffer reads
and writes):

namespace
{
typedef char Key;

char buf;
int const key_count = 100; // Or however many keys you need.
Key keys[ key_count ];

inline void process_byte( Key const& key, char& buf )
{
buf ^= key;
}
}

int main( )
{
// Assign keys, do whatever other set-up you need.

std::fstream file( "file_name", std::ios::in | std::ios::eek:ut );

while( file >> buf )
{
std::for_each( &keys[ 0 ], &keys[ key_count ], process_byte )
}
}


Jeff said:
Second crack, using 0-indexed array and for_each, correcting loop
test, using conventional case for identifiers, and removing do-while
(which I personally detest :):

inline void process_byte( Key const& key, char& buf )
{
buf ^= key;
}

while( file.size <= file.position )
{
file.read( bufor, 1 );

std::for_each( &keys[ 0 ], &keys[ key_count ], process_byte );

target_file.write( bufor, 1 );
}

Jeff said:
Here's a first crack, but where does bufor get used?

do
{
file.Read( bufor, 1 );

for( int p = 1; p <= KeyCount; ++p )
{
buf ^= Keys[ p ];
}

targetFile.Write( bufor, 1 );
}
while( file.Size == file.Position );

Mariusz Sakowski wrote:

Can someone translate this code to a C++?

repeat
file.Read(bufor, 1);
for p := 1 to KeyCount do
begin
buf := buf xor Keys[p];
end;
targetFile.Write(bufor, 1);
until file.Size = file.Position;

where file and targetFile are TFileStream's and Keys is array of byte,
with KeyCount elements. Oh, and p is loop control variable, and buf is
byte, that's all I think:). Thanks in advance for any help.
 
J

Jerry Coffin

Can someone translate this code to a C++?

repeat
file.Read(bufor, 1);
for p := 1 to KeyCount do
begin
buf := buf xor Keys[p];

As an aside that has nothing to do with C++ per se: XOR forms a group,
so this repeated XORing with different keys accomplishes nothing useful.
Any number of XORs with different keys can be collapsed down to one XOR
with one key (which, in this case, would be the result of XORing the
bytes of your current keys together).
end;
targetFile.Write(bufor, 1);
until file.Size = file.Position;

I suspect this code is not what you intended -- you're reading from the
file into 'bufor', then doing XORing with 'buf' and finally writing out
'bufor' again. As such, your code is simply copying input to output in
a grossly inefficient fashion.

I'm going to assume that 'buf' and 'bufor' were really intended to be
the same variable so the XORs accomplish something. In that case, you
could use code something like this:

int ch;

while (EOF != (ch=fgetc(file))) {
for (int i=0;i<key_count;i++)
ch ^= keys;
fputc(c, target_file);
}

Note that the code above is basically C, using virtually nothing unique
to C++. Another approach that's much more C++ and less C is like this:

char encode_byte(char byte) {
for (int i=0; i<key_count; i++)
byte ^= keys;
return byte;
}

file.unsetf(std::ios_base::skipws);

std::istream_iterator<char> input(file), end;
std::eek:stream_iterator<char> output(target_file);

std::transform(input, end, output, encode_byte);

It's probably open to some argument whether this is really an
improvement -- it increases the code size a bit, but also separates the
I/O from the algorithm, which would probably have made the original
buf/bufor mixup considerably more obvious.

It's also worth noting that most of the increase in size of the source
code is really in the declarations for the iterators; if you're doing
more with the data than we are above, that size increase stays roughly
constant, while the savings in other areas tend to be roughly linear on
the amount of work being done. The result is that this is more or less
the worst-case scenario for the style of coding above, and when put to
more serious use, the result is often more positive.
 

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
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top