PDA

View Full Version : Pascal Code conversion to C



joan400
13-04-2005, 01:23 AM
Hi,

:?: :?: :?: Help :?: :!: :!: :!:
I have this task to convert a pascal code (from AS400) to C (ILE C). I haven't used Pascal for my programming so this would be the first hands on coding on Pascal that I would ever do. How can I convert the following declarations?

Code Set #1:

type
ddsrec = packed array(.1..92.) of char;
dp1 = @ddr;
ddr = packed record
case integer of
1 : (
p11 : array(.1..3.) of dp1;
srcseq : packed array(.1..6.) of char;
srcdat : packed array(.1..6.) of char;
seqno : packed array(.1..5.) of char;
ftype : char;
aoc : char;
cinds : packed array(.1..9.) of char;
ntype : char;
dummy : char;
name : packed array(.1..10.) of char;
reference : char;
dataln : packed array(.1..5.) of char;
data_type : char;
dec_pos : packed array(.1..2.) of char;
usage : char;
line : packed array(.1..3.) of char;
col : packed array(.1..3.) of char;
funct : packed array(.1..36.) of char
)
2 : (
p12 : array(.1..3.) of dp2;
dds : ddsrec;
)
end;


I have tried doing the following:


typedef struct {
union {
struct {
/*dp1 p11[3];*/
char srcseq[6];
char srcdat[6];
char seqno[5];
char ftype;
char aoc;
char cinds[9];
char ntype;
char dummy;
char name[10];
char reference;
char dataln[5];
char data_type;
char dec_pos[2];
char usage;
char line[3];
char col[3];
char funct[36];
} ddrua;
struct {
/*dp2 p12[3];*/
ddsrec dds;
} ddrub;
} ddru;
} ddr;
typedef ddr *dp1;


Is this correct? And as you can see, I have placed /*dp1 p11[3];*/ as a comment because I was not able to convert the line.


Code Set #2:

1100 type
1500 ddsrec = packed array(.1..92.) of char;
14600 sfdp = @sfd;
14700 sfd = packed record
14800 next : sfdp;
14900 name : string(10)
15000 first : ddscp;
15100 last : ddscp;
15200 win_row : integer;
15300 win_col : integer;
15400 win_width : integer;
15500 win_depth : integer;
15600 end;

16300 var
17300 sfl1 : boolean;
16600 tempsfdp:sfdp;
26900 {Save subfile help details for later}
27000
27100 procedure save_sfhelp(d:ddsrec)
27200 begin
27300 if sfl1 then
27400 begin
27500 new(tempsfdp)
27600 with tempsfdp@ do
27700 begin
27800 next := sfchain;
27900 sfchain := tempsfdp;
28000 first := nil;
28100 last := nil;
28200 name := format;
28300 end;
28400 sfl1 := false;
28500 end;
30100 end;

Kindly ignore the line numbers. I have used them as personal reference.
How will i be able to convert tempsfdp@ and the statement using with? C has no with syntax. I apoligize for so many queries. Hope you can help me.[/b]

--Joan400

Paulius
13-04-2005, 06:21 AM
I'm not familiar with this dialect of pascal, so i'm just gessing that @ simbol here stand for a pointer.

in the first snippet
p11 : array(.1..3.) of dp1;
should be
ddsrec *p11[3];

with is just a shortcut for members of records or classes, so
with tempsfdp@ do
begin
next := sfchain;
end;
should be
tempsfdp->next= sfchain;

joan400
13-04-2005, 07:22 AM
Hi Paulius,

The @ sign here denotes the address of a variable. In pascal, @y (e.g. where y may be declared as an integer) gives us the address of y. I have been the searching the web for similar code or variable-pointer use like the case of tempsfd@ but obviously, I am unlucky. Tempsfd@ is coded differently from the usual usage of @.

Anyway, thanks for a very quick reply. I am really having a headache with the codes I am converting now.

God bless!

Regards,
joan400

Sly
13-04-2005, 10:04 AM
This should do the trick. Yes, C has no 'with' statement, so you have to prefix every use of members with the pointer dereference. Mind you, I also dislike use of the 'with' statement in normal Pascal code, as do many professional Pascal programmers.



typedef struct _sfd
{
sfdp next;
char name[10];
ddscp first;
ddscp last;
int win_row;
int win_col;
int win_width;
int win_depth;
} sfd, *sfdp;

bool sfl1;
sfdp tempsfdp;

/* Save subfile help details for later */

void save_sfhelp(ddsrec d)
{
if (sfl1)
{
tempsfdp = (sfdp)malloc(sizeof(sfd));
tempsfdp->next = sfchain;
sfchain = tempsfdp;
tempsfdp->first = NULL;
tempsfdp->last = NULL;
strcpy(tempsfdp->name, format);
sfl1 = false;
}
}


If you do not mind me saying, that is badly written Pascal code that you have to convert.

joan400
14-04-2005, 01:32 AM
Hi Sly,

Thank you for your reply. For info, I have coded the following yesterday:
I have tried this:



typedef char ddsrec[92];
typedef struct {
/*sfdp next;*/
char name[10];
ddscp first;
ddscp last;
int win_row;
int win_col;
int win_width;
int win_depth;
} sfd;
typedef sfd *sfdp;
typedef int boolean;

sfdp sfchain, tempsfdp;
boolean sfl1;
char format[20];

/*Save subfile help details for later*/
void save_sfhelp(ddsrec d)
{
if ( sfl1 )
{
tempsfdp = new sfdp;
with tempsfdp@ )
{
next = sfchain;
sfchain = tempsfdp;
first = NULL;
last = NULL;
name = format;
}
sfl1 = 0;
}
}

I was not able to convert tempsfdp. Your code perhaps will certainly help me. I will try doing it on my compiler and update you with the results. Thanks!


If you do not mind me saying, that is badly written Pascal code that you have to convert.

I do not mind at all. Yeah, such a code trash! :D

God bless!

Regards,
joan400

joan400
14-04-2005, 03:50 AM
Hi Sly,

I have followed your advice but found some errors.


/*Save subfile help details for later*/
#include <stdio.h>
#define FALSE 0
#define TRUE 1

typedef int boolean;
typedef char ddsrec&#91;92&#93;;
typedef struct &#123;
ddscp next; /*Declaration missing ;*/
int sr;
int sc;
int er;
int ec;
ddsrec drec;
&#125; ddsc, *ddscp;
typedef struct &#123;
sfdp next; /*Declaration missing ;*/
char name&#91;10&#93;;
ddscp first;
ddscp last;
int win_row;
int win_col;
int win_width;
int win_depth;
&#125; sfd, *sfdp;

sfdp sfchain, tempsfdp;
ddscp tempddscp;
boolean sfl1;
char format&#91;20&#93;;
int start_row, start_col, end_row, end_col;

void save_sfhelp&#40;ddsrec d&#41;
&#123;
if &#40; sfl1 &#41;
&#123;
tempsfdp = &#40;sfdp&#41;malloc&#40;sizeof&#40;sfd&#41;&#41;;
tempsfdp->next = sfchain; /*Undefined symbol next in function...*/
sfchain = tempsfdp;
tempsfdp->first = NULL;
tempsfdp->last = NULL;
strcpy&#40;tempsfdp->name, format&#41;;
sfl1 = FALSE;
&#125;
tempddscp = &#40;ddscp&#41;malloc&#40;sizeof&#40;ddsc&#41;&#41;;
strcpy&#40;tempddscp->drec, d&#41;;
tempddscp->next = NULL; /*Undefined symbol next in function...*/
tempddscp->sr = start_row;
tempddscp->sc = start_col;
tempddscp->er = end_row;
tempddscp->ec = end_col;
if &#40; sfchain->first == NULL &#41;
sfchain->first = tempddscp;
else
sfchain->last->next = tempddscp; /*Undefined symbol next in function...*/

sfchain->last = tempddscp;
&#125;

I have placed comments on lines with errors. C does not accept the declaration ddscp next; within the structure ddsc. Same goes with sfdp next; in structure sfd. It says "Declaration missing ;"

Thus, variable next in the program is considered undefined. I can't make it work because of the declaration.

Hope you could help me. Thank you so much.

Regards,
joan400

Sly
14-04-2005, 04:01 AM
Hi Sly,

I have followed your advice but found some errors.

(code snipped)

I have placed comments on lines with errors. C does not accept the declaration ddscp next; within the structure ddsc. Same goes with sfdp next; in structure sfd. It says "Declaration missing ;"

Thus, variable next in the program is considered undefined. I can't make it work because of the declaration.
Generally in C, you do not define a specific type for a pointer. So in this case, change the code to the following:


/*Save subfile help details for later*/
#include <stdio.h>
#define FALSE 0
#define TRUE 1

typedef int boolean;
typedef char ddsrec&#91;92&#93;;
typedef struct &#123;
ddsc *next; /* Pointer type */
int sr;
int sc;
int er;
int ec;
ddsrec drec;
&#125; ddsc;
typedef struct &#123;
sfd *next; /* Pointer type */
char name&#91;10&#93;;
ddsc *first; /* Pointer type */
ddsc *last; /* Pointer type */
int win_row;
int win_col;
int win_width;
int win_depth;
&#125; sfd;

sfd *sfchain, *tempsfdp; /* Pointer type */
ddsc *tempddscp; /* Pointer type */
boolean sfl1;
char format&#91;20&#93;;
int start_row, start_col, end_row, end_col;

void save_sfhelp&#40;ddsrec d&#41;
&#123;
if &#40; sfl1 &#41;
&#123;
tempsfdp = &#40;sfd *&#41;malloc&#40;sizeof&#40;sfd&#41;&#41;; /* Pointer type */
tempsfdp->next = sfchain; /*Undefined symbol next in function...*/
sfchain = tempsfdp;
tempsfdp->first = NULL;
tempsfdp->last = NULL;
strcpy&#40;tempsfdp->name, format&#41;;
sfl1 = FALSE;
&#125;
tempddscp = &#40;ddsc *&#41;malloc&#40;sizeof&#40;ddsc&#41;&#41;; /* Pointer type */
strcpy&#40;tempddscp->drec, d&#41;;
tempddscp->next = NULL;
tempddscp->sr = start_row;
tempddscp->sc = start_col;
tempddscp->er = end_row;
tempddscp->ec = end_col;
if &#40; sfchain->first == NULL &#41;
sfchain->first = tempddscp;
else
sfchain->last->next = tempddscp;
sfchain->last = tempddscp;
&#125;
Notice how pointer types use the same name as the record type but with the * operator?

joan400
14-04-2005, 04:49 AM
Hi Sly,

I will study the information you have given me. I will study it that perhaps, it will take a while before I send another reply to you. Thank you so much. I will keep in touch as soon as I figure out something. I will try on the converting the codes now using your suggestion.

Deeply appreciated! 8)

Regards,
joan400

joan400
14-04-2005, 06:30 AM
Hi Sly,

I have tried your code but still get the message "Declaration missing" on lines having ddsc *next and sfd *next since they are within the ddsc and sfd structure, respectively.

Hoping there would still be an answer...Thanks.

Greatly appreciated!

Regards,
joan400

Sly
14-04-2005, 06:39 AM
Ah, yes. I see it now. I've been trying to use strict C syntax, where I normally use slightly relaxed C++ syntax.

In modern C compilers, you can use

struct ddsc
&#123;
ddsc *next;
&#125;;
In older C compilers, you had to do something like

typedef struct _ddsc
&#123;
struct _ddsc *next;
&#125; ddsc;
Hopefully one of those will work.

joan400
15-04-2005, 02:42 AM
Hi Sly,

It still produces "Undefined structure _ddsc" message on the line where we declare next. But I adopted the syntax of code you shared with me and was able to produce the following:
typedef struct _ddsc *ddscp;
typedef struct _ddsc
&#123;
ddscp next;
&#125; ddsc;It worked, Sly! Thank you, thank you, thank you!

Still, may I request that you give your comments on this if you see something wrong with the code and if it loses the connection between the structure ddsc and its pointer ddscp? A friend of mine (hi liquid!) suggested to use a forward declaration like the one you used.

Does this mean that declaring
typedef struct _ddsc *ddscp;automatically makes ddscp a pointer to structure ddsc? And does this make next a pointer to structure ddsc too?

Thanks again. 8)

Regards,
joan400

Sly
15-04-2005, 04:02 AM
Does this mean that declaring
typedef struct _ddsc *ddscp;automatically makes ddscp a pointer to structure ddsc? And does this make next a pointer to structure ddsc too?
Yes and yes. :)