Translating Free Pascal to GNU C

S

SM Ryan

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

# So what do I basically do?

Long jump.


#include <setjmp.h>

enum {START,NOMEM,...};

jmp_buf trying;
int ec = setjmp(trying);
switch (ec) {
case START:
molbuf = malloc(sizeof(molbuftype));
if (!molbuf) longjmp(trying,1);
break;
case NOMEM:
printf("Not enough memory\n");
exit(4);
break
case ...
}
 
T

those who know me have no need of my name

in comp.lang.c i read:
So what do I basically do?

the usual allocation idiom seems appropriate:

molbuftype *molbuf = malloc(sizeof *molbuf);
if (0 == molbuf)
{ /* death upon failure seems what your example does */
fputs("Not enough memory\n", stderr);
exit(4);
}

if this is a library function reconsider having it output and exit, instead
only return an indication to the caller so it may decide what to do. this
may be more in keeping with the exception semantics of your other language.

the exit value (4) is a non-standard, and may or may not do what you hope.
even if it does work on one platform it may mis-function on another.
 
T

The Doctor

Thhank you so far forthe hints.

I did compile this in C++ and chike city :-s

Next question, looks like
/*str(atom^.X:1:4,tmpstr); */

Is suppose to initialize a character in Pascal.

In the translated C so far we have:

tmpstr = " ";

And atom is:

atomlist *atom;

and

typedef atom_rec atomlist[max_atoms];

and finally:

typedef char str2[2];
typedef char str3[3];
typedef char str4[4];
typedef char str5[5];
typedef char str8[8];
typedef struct atom_rec {
str2 element;
str3 atype;
u_int32_t x;
u_int32_t y;
u_int32_t z;
int formal_charge;
u_int32_t real_charge;
int hexp; // explicit H count
int htot; // total H count
int neighbor_count;
int ring_count;
boolean arom;
} atom_rec;

SO how do I translate the initialization from PAscal to C?
 
C

CBFalconer

those said:
the usual allocation idiom seems appropriate:

molbuftype *molbuf = malloc(sizeof *molbuf);
if (0 == molbuf)
{ /* death upon failure seems what your example does */
fputs("Not enough memory\n", stderr);
exit(4);
}

I gave him the virtually identical recommendation last Monday, so I
suspect 'Doctor' doesn't bother to read the replies.
 
T

The Doctor

I gave him the virtually identical recommendation last Monday, so I
suspect 'Doctor' doesn't bother to read the replies.

Sorry, I do and I did implement this.

NOt to get that weird matrix initialized.
 
T

The Doctor

More tranlsation.

Now I need to turn the Pascal fillchar into
a C subroutine.

Add to the list:

extractfilename

fileexists

assign

I wonder if there is a way to turn Pascal into C is one go!
 
T

The Doctor

More tranlsation.

Now I need to turn the Pascal fillchar into
a C subroutine.

Add to the list:

extractfilename

fileexists

assign

I wonder if there is a way to turn Pascal into C in one go!

Translated, compile but now we are geting segmentation faults.


Did I do a correct translation of the mentioned file?
 

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,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top