Chelper

From Lazarus wiki
Jump to navigationJump to search

About

Chelper is Lazarus extension tool, that helps converting C headers to Pascal. Yes, it's yet another C-to-Pascal converting tool like lot of similar (H2PAS, ToPas), but one might find it more useful.

The extension doesn't try to convert a header for the user, instead it's tool to help with manual translation, doing almost all of "dirty work" for the user.

Author

Dmitry 'skalogryz' Boyarintsev

License

modified LGPL (same as the FPC RTL and the Lazarus LCL). You can contact the author if the modified LGPL doesn't work with your project licensing.

Download

There's no "downloadable" package yet, you can access the tool from SVN:

https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/components/chelper

svn co https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/components/chelper

Change Log

  • Version 0.8, 8 Aug 2010

Dependencies / System Requirements

  • Lazarus 0.9.29 or Higher

Status: Beta

Issues

  • cconvert - util has memory leaks. However since it's used as the external process, it shouldn't physically leak the memory.
  • No expression parser!
  • parser unable to handle "const" keyword correctly in some declarations, i.e.
extern const apr_uint32_t *const svn_ctype_table;

produces incorrect

var
 const_ : Papr_uint32_t;

if you remove all const keyword from the expression, to get

extern apr_uint32_t * svn_ctype_table;

you'll get correct var

 svn_ctype_table : Papr_uint32_t; external;

Installation

  • Get the package from SVN
  • Open and build cconvert.lpi project. You should get "cconvert" executable file.
  • Install Chelper.lpk package to the Lazarus.
  • Open Tools->C to Pascal Options->Converter, press Select and choose cconvert executable file.

How to use

  • Open a file with C declarations in Lazarus IDE or start a new file copying declarations there
  • Place the cursor at the line where C declaration and press Ctrl+B (the shortcut would be modifiable in future).

for example:

int c;
int b; 

if the cursor placed at 'inc c;' line and ctrl+b pressed the result would be:

var
  c : Integer;
int b;

exactly one definition is converted. The cursor would be place right after the converted definition. You can press ctrl+b again, and the result would be:

var
  c : Integer;
var 
  b : Integer;

Parsing definitions one-by-one will get your C headers ready with a very short period of time, as well as keeping it clean and correct from Pascal point of view.


Defines

In most cases parameterless macro definitions, like

#define VALUE 1 << 2;

are used like constant values. Chelper translates them as constants trying to convert the define expression.

const
  VALUE = 1 shr 2;

In other cases defines are used for some "special" markings or additional information for external tools.

#define LIB_DEPRECATED 
LIB_DEPRECATED int somefunc(int p);

without the knowledge of LIB_DEPRECATED being the macros (of nothing), it's impossible to parse function declaration correctly. You can specify macroses used by Chelper in the special defines file.

Select Tools->C to Pascal options->Main select the defines file and press Edit.

This would open the custom defines file in Lazarus IDE. All macroses declared at this file are used by Chelper. After adding/editing macros defines save the file and you can return to the C header converting.


Chelper handles parameter macroses as well.

int somefunc SAFE_PARAMS( (int i))

adding the the following declaration into defines file

#define SAFE_PARAMS(x) x

will allow to parse the declaration correctly

function somefunc(i: Integer): Integer;