Translating Free Pascal to GNU C

T

The Doctor

One word: HELP!

I got a programme written in Pascal which I am in the process of
translating into c. ptoc did a good job, still there are some
fixes that need to be done.

1)

/*try
getmem(molbuf,sizeof(molbuftype));
except
on e:Eoutofmemory do
begin
writeln('Not enough memory');
halt(4);
end;
end;*/

and

/*try
getmem(atom,n_atoms*sizeof(atom_rec));
getmem(bond,n_bonds*sizeof(bond_rec));
getmem(ring,sizeof(ringlist));
getmem(ringprop,sizeof(ringprop_type));
except
on e:Eoutofmemory do
begin
writeln('Not enough memory');
halt(4);
end;
end;*/

How isthis translated into C?

2) exit , what is the C equivalent?

3) beep, how do you say that in C?

4) /* str(n_atoms:1,tmpstr); lblank(3,tmpstr); */

How is this translated in C?

I am more likely available at (e-mail address removed) .
 
J

Jack Klein

One word: HELP!

I got a programme written in Pascal which I am in the process of
translating into c. ptoc did a good job, still there are some
fixes that need to be done.

1)

/*try
getmem(molbuf,sizeof(molbuftype));
except
on e:Eoutofmemory do
begin
writeln('Not enough memory');
halt(4);
end;
end;*/

and

/*try
getmem(atom,n_atoms*sizeof(atom_rec));
getmem(bond,n_bonds*sizeof(bond_rec));
getmem(ring,sizeof(ringlist));
getmem(ringprop,sizeof(ringprop_type));
except
on e:Eoutofmemory do
begin
writeln('Not enough memory');
halt(4);
end;
end;*/

How isthis translated into C?

2) exit , what is the C equivalent?

3) beep, how do you say that in C?

4) /* str(n_atoms:1,tmpstr); lblank(3,tmpstr); */

How is this translated in C?

I am more likely available at (e-mail address removed) .

So what makes you think that anyone who reads comp.lang.c or
comp.lang.c++ knows, or cares, anything at all about Pascal? What
makes you think that anyone reading comp.lang.pascal knows, or cares,
anything about C?

Perhaps you should hire a competent programmer.

What you posted doesn't look all that much like Pascal, at least not
as I remember it. Have they added a 'sizeof' keyword to Pascal
lately? Or 'try' and 'except'? It's been some time since I did
anything with Pascal, but I am pretty sure none of these are part of
the language.
 
C

Chris McDonald

Jack Klein said:
So what makes you think that anyone who reads comp.lang.c or
comp.lang.c++ knows, or cares, anything at all about Pascal?
...
...
What you posted doesn't look all that much like Pascal, at least not
as I remember it.


So, by your own admission, the OP was correct in their thinking that
someone who reads comp.lang.c knows something about Pascal.
 
P

Paul Mesken

What you posted doesn't look all that much like Pascal, at least not
as I remember it. Have they added a 'sizeof' keyword to Pascal
lately? Or 'try' and 'except'? It's been some time since I did
anything with Pascal, but I am pretty sure none of these are part of
the language.

Perhaps it's Delphi, Pascal's OO offspring. But I'm not at all ashamed
that I don't know for sure ;-)
 
A

Alf P. Steinbach

[Note: The OP crossposted to comp.lang.c, comp.lang.c++ and com.lang.pascal]

* The Doctor:
I got a programme written in Pascal which I am in the process of
translating into c. ptoc did a good job, still there are some
fixes that need to be done.

1)

/*try
getmem(molbuf,sizeof(molbuftype));
except
on e:Eoutofmemory do
begin
writeln('Not enough memory');
halt(4);
end;
end;*/


Whatever language the above is, in C++ it would be like

try
{
getmem( molbuf, sizeof( molbuftype ) );
}
catch( Eoutofmemory const& )
{
std::cout << "Not enough memory" << std::endl;
std::exit( 4 );
}

Now it's natural to think that 'getmem' has something to do with
dynamic memory allocation, but you give no information about that.

The output should really be to the standard error stream, not to
standard output, but I've chosen to follow the original code
faithfully, warts and all -- it's Bad Code in several respects.

...
How isthis translated into C?

2) exit , what is the C equivalent?

'exit'.

But in the code you've presented you've used 'halt'.

Which leads me to think you knew the answer to that one, and
accidentally let Freud slip your tongue.

3) beep, how do you say that in C?

'beep'. Note that this is not a built-in or even standard library
name. You'll need to define its meaning.

4) /* str(n_atoms:1,tmpstr); lblank(3,tmpstr); */

How is this translated in C?

Assuming it is a comment in whatever "Pascal" you're using (it's not
a standard Pascal comment), it's exactly the same in C and C++.
 
T

The Doctor

So what makes you think that anyone who reads comp.lang.c or
comp.lang.c++ knows, or cares, anything at all about Pascal? What
makes you think that anyone reading comp.lang.pascal knows, or cares,
anything about C?

Perhaps you should hire a competent programmer.

What you posted doesn't look all that much like Pascal, at least not
as I remember it. Have they added a 'sizeof' keyword to Pascal
lately? Or 'try' and 'except'? It's been some time since I did
anything with Pascal, but I am pretty sure none of these are part of
the language.

Try Free Pascal.
 
T

The Doctor

So, by your own admission, the OP was correct in their thinking that
someone who reads comp.lang.c knows something about Pascal.

Enough comedy, just answer the question.
 
T

The Doctor

Perhaps it's Delphi, Pascal's OO offspring. But I'm not at all ashamed
that I don't know for sure ;-)

Nice to see Pascal to C translators, but the end-game require a manual
redo.
 
T

The Doctor

[Note: The OP crossposted to comp.lang.c, comp.lang.c++ and com.lang.pascal]

* The Doctor:
I got a programme written in Pascal which I am in the process of
translating into c. ptoc did a good job, still there are some
fixes that need to be done.

1)

/*try
getmem(molbuf,sizeof(molbuftype));
except
on e:Eoutofmemory do
begin
writeln('Not enough memory');
halt(4);
end;
end;*/


Whatever language the above is, in C++ it would be like

try
{
getmem( molbuf, sizeof( molbuftype ) );
}
catch( Eoutofmemory const& )
{
std::cout << "Not enough memory" << std::endl;
std::exit( 4 );
}

What about C?

I do not recall a try call in c.

E.EoutofMemory is Pascalish IIRC.
Now it's natural to think that 'getmem' has something to do with
dynamic memory allocation, but you give no information about that.

The output should really be to the standard error stream, not to
standard output, but I've chosen to follow the original code
faithfully, warts and all -- it's Bad Code in several respects.



'exit'.

But in the code you've presented you've used 'halt'.

Which leads me to think you knew the answer to that one, and
accidentally let Freud slip your tongue.

There was exit and halt, so halt I will replace with exit.
'beep'. Note that this is not a built-in or even standard library
name. You'll need to define its meaning.

I was looking at curses, still.
Assuming it is a comment in whatever "Pascal" you're using (it's not
a standard Pascal comment), it's exactly the same in C and C++.

str?? I guess it was a string.
 
T

The Doctor

However, in that case, the OP didn't seem to be correct in thinking that the
same "someone" also cares about Pascal.

*SCNR*

Chemistry Profs writing in Pascal??
 
R

Rolf Magnus

Chris said:
So, by your own admission, the OP was correct in their thinking that
someone who reads comp.lang.c knows something about Pascal.

However, in that case, the OP didn't seem to be correct in thinking that the
same "someone" also cares about Pascal.

*SCNR*
 
C

CBFalconer

The said:
One word: HELP!

I got a programme written in Pascal which I am in the process of
translating into c. ptoc did a good job, still there are some
fixes that need to be done.

1)

/*try
getmem(molbuf,sizeof(molbuftype));
except
on e:Eoutofmemory do
begin
writeln('Not enough memory');
halt(4);
end;
end;*/

To start with, this isn't Pascal. However, just looking at the
sense of what it seems to be trying to do, I suggest:

if (!(molbuf = malloc(sizeof *molbuf))) {
fputs("Not enough memory\n", stderr);
exit(EXIT_FAILURE);
}
/* success, continue */
and

/*try
getmem(atom,n_atoms*sizeof(atom_rec));
getmem(bond,n_bonds*sizeof(bond_rec));
getmem(ring,sizeof(ringlist));
getmem(ringprop,sizeof(ringprop_type));
except
on e:Eoutofmemory do
begin
writeln('Not enough memory');
halt(4);
end;
end;*/

How isthis translated into C?

Suggestion:

/* this statement allows for freeing if any one
of the allocation group fails */
atom = bond = ring = ringprop = NULL;
if (!(atom = malloc(n_atoms * sizeof *atom)) ||
!(bond = malloc(n_bonds * sizeof *bond)) ||
!(ring = malloc(sizeof *ring)) ||
!(ringprop = malloc(sizeof *ringprop)) ) {
fputs("Not enough memory\n");
if (atom) free(atom);
if (bond) free(bond);
if (ring) free(ring);
exit(EXIT_FAILURE);
}
/* allocations successful, onward */

(all of which assumes that what is wanted is space for one item of
the type to which the pointers point, or an array of such for atom
and bond).
2) exit , what is the C equivalent?

In C, exit exits the complete program. If you are thinking of the
abortion of an extension in some quasi Pascal systems where exit
exits the procedure/function, just use 'return'. For a function
use "return value". In future write the Pascal correctly, such as:

PROCEDURE foo;
LABEL 10;
BEGIN
....
IF bar THEN GOTO 10;
....
10: END;
3) beep, how do you say that in C?

System dependent.
4) /* str(n_atoms:1,tmpstr); lblank(3,tmpstr); */

How is this translated in C?

No idea. What is it supposed to do? Again, it isn't Pascal.

Followups set, because this has nothing to do with C++, and
comp.lang.pascal has been split into various subgroups for many
years now, and should not be used. I chose comp.lang.pascal.misc
because the original has no resemblance to real Pascal.

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
J

Jonas Maebe

Just a couple of small remarks.
fputs(stderr, "Not enough memory\n");

No. You need more coffee.[/QUOTE]

Actually, I normally use fprintf instead of fputs and I (ignorantly)
figured they'd use the same order of arguments. That's not the case, so
I was wrong indeed.


Jonas
 
K

Keith Thompson

Jonas Maebe said:
Just a couple of small remarks.


fputs(stderr, "Not enough memory\n");

No, the fputs() function is declared as:

int fputs(const char *s, FILE *stream);

so CBFalconer's code is correct.
Same here.

That should also be

fputs("Not enough memory\n", stderr);
 
C

CBFalconer

Keith said:
No, the fputs() function is declared as:

int fputs(const char *s, FILE *stream);

so CBFalconer's code is correct.

I am usually fairly confident that the FILE* argument goes at one
end or the other of various i/o calls. So this gives me a 50-50
chance of being right at all times. It also means I can easily be
confused. Luckily an alias on my machine allows me to look the
calling sequence up in N869.txt quite rapidly. Compilations also
tend to point out any errors, so I don't worry too much about it.
 
R

Richard Bos

CBFalconer said:
I am usually fairly confident that the FILE* argument goes at one
end or the other of various i/o calls. So this gives me a 50-50
chance of being right at all times.

AFAIIA, the FILE * goes at the end in all functions that take one,
except for variadic functions, where it would be unwieldy to have it at
the end so it goes at the front. ICBW, but I don't think so.

Richard
 

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

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top