[Lazarus] Translating a small C program to pascal (on lazarus)
Carlos E. R.
robin.listas at telefonica.net
Sun Feb 25 20:52:32 CET 2018
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
I want to translate a little Linux C program (600 lines) to pascal (so
that I can then add my parts in comfort). I failed to find a good
automated tool in Linux. I had a look here:
<http://wiki.freepascal.org/C_to_Pascal>
I used "OpenC2Pas" but there remains quite a lot to translate.
So I have to edit a lot, but my C is rusty.
I had this, in C (simplified to the sections I'm using so far):
#define SOCKET_COUNT 4
#define KEY_LEN 8
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
typedef enum
{
EG_PROTO_V20,
EG_PROTO_V21,
EG_PROTO_WLAN
} Protocol;
typedef enum
{
ACTION_ON,
ACTION_OFF,
ACTION_TOGGLE,
ACTION_LEFT,
ACTION_INVALID
} Action;
typedef struct
{
Action socket[SOCKET_COUNT];
} Actions;
The automatic translation did:
#define SOCKET_COUNT 4
#define KEY_LEN 8
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
typedef enum
begin
EG_PROTO_V20,
EG_PROTO_V21,
EG_PROTO_WLAN
end; Protocol;
typedef enum
begin
ACTION_ON,
ACTION_OFF,
ACTION_TOGGLE,
ACTION_LEFT,
ACTION_INVALID
end; Action;
typedef struct
begin
Action socket[SOCKET_COUNT];
end; Actions;
As you can see, it is quite incomplete.
The manual translation goes (I'm reading at
<http://rvelthuis.de/programs/convertpack.html> for info):
const
SOCKET_COUNT = 4;
KEY_LEN = 8;
type
Protocol = (EG_PROTO_V20, EG_PROTO_V21, EG_PROTO_WLAN);
Action = (ACTION_ON, ACTION_OFF, ACTION_TOGGLE, ACTION_LEFT, ACTION_INVALID);
_Actions = record
socket: array[0..SOCKET_COUNT] of Action;
end;
Actions = _Actions;
// typedef struct
// {
// Action socket[SOCKET_COUNT];
// } Actions;
Now, the part I have doubts (one of many) is the later. In C it is:
typedef struct
{
Action socket[SOCKET_COUNT];
} Actions;
The question is: is my translation correct?
_Actions = record
socket: array[1..SOCKET_COUNT] of Action;
end;
Actions = _Actions;
It confuses me that a single item, an array, is defined inside the struct
or record. I also have doubts about the translation of the array itself.
Is it an array of Action?
You see, it doesn't help at all that my C is rusty.
Based in the section "Structs and unions" of the "Rudy's Delphi Corner"
site I linked above. He says:
typedef struct _CERT_STORE_PROV_INFO {
DWORD cbSize;
DWORD cStoreProvFunc;
void **rgpvStoreProvFunc;
HCERTSTOREPROV hStoreProv;
DWORD dwStoreProvFlags;
HCRYPTOIDFUNCADDR hStoreProvFuncAddr2;
} CERT_STORE_PROV_INFO, *PCERT_STORE_PROV_INFO;
This results in:
PCertStoreProvInfo = ^TCertStoreProvInfo;
{$EXTERNALSYM _CERT_STORE_PROV_INFO}
_CERT_STORE_PROV_INFO = record
cbSize: DWORD;
cStoreProvFunc: DWORD;
rgpvStoreProvFunc: PPointer;
hStoreProv: HCERTSTOREPROV;
dwStoreProvFlags: DWORD;
hStoreProvFuncAddr2: HCRYPTOIDFUNCADDR;
end;
{$EXTERNALSYM CERT_STORE_PROV_INFO}
CERT_STORE_PROV_INFO = _CERT_STORE_PROV_INFO;
{$EXTERNALSYM PCERT_STORE_PROV_INFO}
PCERT_STORE_PROV_INFO = ^_CERT_STORE_PROV_INFO;
TCertStoreProvInfo = _CERT_STORE_PROV_INFO;
Then, the next part I'll have to translate is this:
typedef struct
{
uint8_t octets[KEY_LEN];
} Key;
typedef struct
{
/* since the protocol is little-endian, low word comes first */
uint16_t loword;
uint16_t hiword;
} __attribute__((__packed__)) Res;
First, I have to find the equivalence of uint8_t, otherwise it is very
similar to the first struct. Then comes uint16_t. And then, the last line:
__attribute__((__packed__)) Res;
Probably it is just a packed record.
Maybe it will all be too complicated, and I should instead call the C
program from my Pascal program in the command line so that it does its
thing. It would indeed be faster to code.
- --
Cheers,
Carlos E. R.
(from openSUSE 42.3 x86_64 "Malachite" at Telcontar)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2
iEYEARECAAYFAlqTFAgACgkQtTMYHG2NR9VuYACdHUodKSCy5maqrpRk1VaGwI1m
EkwAnjGHLfMsOfEB4v38adCY7grvEmij
=4pSm
-----END PGP SIGNATURE-----
More information about the Lazarus
mailing list