473,562 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need to translate from Delphi to C++

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.Writ e(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
5 1935
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.Writ e( 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.Writ e(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
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.wri te( 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.Writ e( 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.Writ e(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
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.wri te( 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.Writ e( 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.Writ e(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
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.wri te( 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.Writ e( 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.Writ e(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
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.Writ e(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(cha r byte) {
for (int i=0; i<key_count; i++)
byte ^= keys[i];
return byte;
}

file.unsetf(std ::ios_base::ski pws);

std::istream_it erator<char> input(file), end;
std::ostream_it erator<char> output(target_f ile);

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2239
by: Graeme Matthew | last post by:
Hi all, I just wanted to tell someone :-) I was previously a perl programmer and have been a visual basic frustrated developer for a number of years, only for the reason that corporates are so obsessed with using proprietary marketing focused computer languages and not using true computer science oriented solutions written by computer...
10
2630
by: Beach Potato | last post by:
Dear Y'all: I'm about to start porting a big old project written in anscient version of Delphi to something more stable, robust, supportable and maybe even portable. Since I haven't seriously touched C for large implementations, I'm seeking advice on what to use for development. My ultimate goal is to spend as less time on it as possible....
9
293
by: Aaron Smith | last post by:
Our company is trying to decide to go .Net or not and which language to use if we do. I have been doing a lot of research on .Net and VB.Net. I used to be a VB 6 developer till I started to work here, so all along I have been promoting VB. I'm also a VC++ developer, but have a little less experiance in that than in VB. I would like to know...
6
3157
by: Erva | last post by:
Hi, Is there someone who has moved from Delphi to VS.NET? I'am using Delphi currently but seriously considering to moving VS.NET. I would like to hear if someone has already done that, is it worth of it or should i continue to ude Delphi for new projects. I'am developing mostly desktop apps but in th future also ASP.NET apps. -erva
0
1021
by: Mariusz Sakowski | last post by:
Can someone translate this code to a C++ langauge? repeat plik.Read(bufor, 1); for p := 1 to iloscLiczb do begin bufor := bufor xor Klucze; end; plikDocelowy.Write(bufor, 1); pb.Position := pb.Position + 1;
48
3203
by: Chad Z. Hower aka Kudzu | last post by:
A few of you may recognize me from the recent posts I have made about Indy <http://www.indyproject.org/indy.html> Those of you coming to .net from the Delphi world know truly how unique and "huge" Indy is both as a project, in support, development, and use. But Indy is new to the .net world. Indy is a HUGE library implementing over 120...
9
3091
bvdet
by: bvdet | last post by:
I have done some more work on a simple class I wrote to calculate a global coordinate in 3D given a local coordinate: ## Basis3D.py Version 1.02 (module macrolib.Basis3D) ## Copyright (c) 2006 Bruce Vaughan, BV Detailing & Design, Inc. ## All rights reserved. ## NOT FOR SALE. The software is provided "as is" without any warranty....
0
1148
by: alaa123 | last post by:
please i really need help to make a start I need to write an XSLT script that takes a file as its input and produces bargraph as its output the file (input) svg:svg xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:svg="http://www.w3.org/2000/svg" width="555" height="320"> <svg:text x="277.5" y="20" text-anchor="middle"...
11
2890
by: gnuist006 | last post by:
Is there a Delphi equivalent in the C world or Scheme/LISP world ? Recently, Delphi is in resurgence. In Russia people are using like crazy. For example, Bolega has written a free image processing program scankromsator in delphi because its easy to write a gui. In arabia people are using it also. Also delphi, I heard delphi allows visual...
0
7655
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7869
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7935
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6221
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5477
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5193
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2073
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1191
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
903
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.