By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,947 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

Need to translate from Delphi to C++

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.


Jul 22 '05 #2

P: n/a
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 Schwab wrote:
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.



Jul 22 '05 #3

P: n/a
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::out );

while( file >> buf )
{
std::for_each( &keys[ 0 ], &keys[ key_count ], process_byte )
}
}
Jeff Schwab wrote:
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 Schwab wrote:
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.



Jul 22 '05 #4

P: n/a
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::out );

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 Schwab wrote:
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::out );

while( file >> buf )
{
std::for_each( &keys[ 0 ], &keys[ key_count ], process_byte )
}
}
Jeff Schwab wrote:
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 Schwab wrote:
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.


Jul 22 '05 #5

P: n/a
In article <1b**************************@posting.google.com >,
sa***@o2.pl says...
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[i];
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[i];
return byte;
}

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

std::istream_iterator<char> input(file), end;
std::ostream_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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.