I have a lot of code which I am wanting to port from Sun's Sparcworks. The main algorithms are in Pascal, but the user interface was written in XView which was linked in via C.
In Sparcworks, linking from Pascal to C involves statements of the following form:
PROCEDURE X_SelAlgorithmn(VAR select_no : integer; VAR smess: string); external c;
This is calling a C function which is returning values for select_no and smess.
Some of my calls included a lot of variables which were set by a complicated form e.g. procedure x_rscinput_form(mc_code: string; var mc_name: string; var efficiency, d_rule_a, avail_ops, shifts, children, dist_rule_b: integer; var x_pos, y_pos, x_size, y_size: real; var tr_dev, audit_p, update_p: integer; var opt_mc_code: string; var hour_rate, ohead_rate: real; var minsu, minmc, mintr, batchsize, transfer, optmcrule: integer; var dist: boolean; var co_name: string); external c;
Thus returning lots of variables with different types from C to Pascal.
With GNU Pascal (thanks to the African Cheif and Kevan) I can pass variables from Pascal to C and I can get single variables back again. Is there a way that I can pass multiple variables into C and back into Pascal without resorting to global variable or using files?
Any help would be very gratefully received.
Many thanks, Chris
Dr Christian Hicks Senior Lecturer, School of Mechanical & Systems Engineering, Stephenson Building, University of Newcastle upon Tyne, NE1 7RU. Phone: +44 191 222 6238 Mobile 0795 8317804 Fax: + 44 191 222 8600 Homepage: http://www.staff.ncl.ac.uk/chris.hicks
On 15 Nov 2003 at 20:55, Dr Christian Hicks wrote:
I have a lot of code which I am wanting to port from Sun's Sparcworks. The main algorithms are in Pascal, but the user interface was written in XView which was linked in via C.
In Sparcworks, linking from Pascal to C involves statements of the following form:
PROCEDURE X_SelAlgorithmn(VAR select_no : integer; VAR smess: string); external c;
This is calling a C function which is returning values for select_no and smess.
Some of my calls included a lot of variables which were set by a complicated form e.g. procedure x_rscinput_form(mc_code: string; var mc_name: string; var efficiency, d_rule_a, avail_ops, shifts, children, dist_rule_b: integer; var x_pos, y_pos, x_size, y_size: real; var tr_dev, audit_p, update_p: integer; var opt_mc_code: string; var hour_rate, ohead_rate: real; var minsu, minmc, mintr, batchsize, transfer, optmcrule: integer; var dist: boolean; var co_name: string); external c;
Thus returning lots of variables with different types from C to Pascal.
With GNU Pascal (thanks to the African Cheif and Kevan) I can pass variables from Pascal to C and I can get single variables back again. Is there a way that I can pass multiple variables into C and back into Pascal without resorting to global variable or using files?
I don't think that it is any different in gpc. You simply use parameter lists, as above. The main things to watch for are data types that might mean different things or that don't exist, and the size of data types. For example, I am not sure that the "Real" type means anything in C. But perhaps "Single" and "Double" mean the same thing in gcc as they mean in gpc - if so, then you could use either of them instead of "Real".
"Integer" in gpc probably means the same thing as "int" in gcc, as long as it is the same platform.
"Var foo:String" means nothing to a gcc compiler, AFAICS. To pass string variables to and fro, you will need to use the "PChar" or "CString" type in gpc. This is roughly equivalent to "char *foo" in gcc, or perhaps also "char foo[size]" (or at least they should all be type-compatible if passed as parameters). People from a Windows background are accustomed to using PChar types for interfacing with the Windows API, which expects C-style parameters. If you are not, then you have to get used to the gpc functions (in the "gpc" and "strings" unita) for dealing with null terminated strings (e.g., strcopy, strlen, strpas, strpcopy, strcat). Most of these are used freely in C and so would pose no problem to a C programmer.
For passing Pascal records, I think you will need to use pointers to structs or something like that.
Can't think of anything else ... except perhaps file parameters - and I can't help you there. I wouldn't know what to do with them in this context.
Best regards, The Chief -------- Prof. Abimbola A. Olowofoyeku (The African Chief) web: http://www.bigfoot.com/~african_chief/
Prof A Olowofoyeku (The African Chief) wrote:
On 15 Nov 2003 at 20:55, Dr Christian Hicks wrote:
I have a lot of code which I am wanting to port from Sun's Sparcworks. The main algorithms are in Pascal, but the user interface was written in XView which was linked in via C.
In Sparcworks, linking from Pascal to C involves statements of the following form:
PROCEDURE X_SelAlgorithmn(VAR select_no : integer; VAR smess: string); external c;
This is calling a C function which is returning values for select_no and smess.
Some of my calls included a lot of variables which were set by a complicated form e.g. procedure x_rscinput_form(mc_code: string; var mc_name: string; var efficiency, d_rule_a, avail_ops, shifts, children, dist_rule_b: integer; var x_pos, y_pos, x_size, y_size: real; var tr_dev, audit_p, update_p: integer; var opt_mc_code: string; var hour_rate, ohead_rate: real; var minsu, minmc, mintr, batchsize, transfer, optmcrule: integer; var dist: boolean; var co_name: string); external c;
Thus returning lots of variables with different types from C to Pascal.
With GNU Pascal (thanks to the African Cheif and Kevan) I can pass variables from Pascal to C and I can get single variables back again. Is there a way that I can pass multiple variables into C and back into Pascal without resorting to global variable or using files?
I don't think that it is any different in gpc. You simply use parameter lists, as above. The main things to watch for are data types that might mean different things or that don't exist, and the size of data types. For example, I am not sure that the "Real" type means anything in C. But perhaps "Single" and "Double" mean the same thing in gcc as they mean in gpc - if so, then you could use either of them instead of "Real".
In GPC `Real' is the same as `Double' and `double' in C. `Single' and `ShortReal' are `float' in C.
"Integer" in gpc probably means the same thing as "int" in gcc, as long as it is the same platform.
Not probably, but definitely. :-)
"Var foo:String" means nothing to a gcc compiler, AFAICS. To pass string variables to and fro, you will need to use the "PChar" or "CString" type in gpc. This is roughly equivalent to "char *foo" in gcc,
It it equivalent. Never use the Pascal `String' type in C compatible parameter lists (which is the main problem in your example).
or perhaps also "char foo[size]" (or at least they should all be type-compatible if passed as parameters).
Yes, because in C `foo bar[baz]' and `foo *bar' are equivalent in parameter lists (and pointer targets), anyway.
People from a Windows background are accustomed to using PChar types for interfacing with the Windows API, which expects C-style parameters. If you are not, then you have to get used to the gpc functions (in the "gpc" and "strings" unita) for dealing with null terminated strings (e.g., strcopy, strlen, strpas, strpcopy, strcat).
I don't recommend this, but rather to convert to and from Pascal strings immediately. GPC automatically converts Pascal to C strings, as long as the C strings are used read-only. If they are changed or returned as function results (like here), you can convert back using CString2String.
For passing Pascal records, I think you will need to use pointers to structs or something like that.
Pascal `record' = C `struct' (as long as you don't use `packed', and don't used different alignment options and the field types match exactly). There are not necessarily pointers involved, unless the C side uses pointers (`struct foo *bar') which it often does.
Can't think of anything else ... except perhaps file parameters - and I can't help you there. I wouldn't know what to do with them in this context.
Pascal files are not compatible with C, and C `FILE' isn't compatible with Pascal. The best you can do it to share the file descriptor (see `FileHandle', `AssignHandle' in Pascal, `fileno', `fdopen' in C), but because of buffering etc. this can get a little tricky.
Other things:
Pascal `var' parameters correspond to C pointers (`double *x_pos' etc. in your example).
Never use `const' in C compatible parameter lists. If a parameter is meant to be passed by reference, but read-only (`const foo *' in C), use `protected var'. However, for `const char *', do not use `protected var Foo: CString' (because `CString' is already a pointer so it would pass a pointer to a pointer), but simply `CString'.
Frank