Difference between revisions of "Chelper"

From Free Pascal wiki
Jump to navigationJump to search
Line 28: Line 28:
 
* cconvert - util has memory leaks. However since it's used as an external process, it shouldn't physically leak the memory.
 
* cconvert - util has memory leaks. However since it's used as an external process, it shouldn't physically leak the memory.
 
* No complete expression parser!
 
* No complete expression parser!
* todo: Objective-C to Objective-P
 
  
 
=== Installation ===
 
=== Installation ===

Revision as of 20:36, 15 August 2010

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 an external process, it shouldn't physically leak the memory.
  • No complete expression parser!

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

Shortkeys

  • ctrl+b - convert a single C declaration to Pascal
  • ctrl+shift+b - convert all C declarations to Pascal from the current cursor position to the end of the file.

General

  • 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.

  • If you're sure your defines (explained below) are configured well enough. You can try to convert the rest of the file in one call by pressing ctrl+shift+b

i.e. you have 2 declarations in the file:

int fn(int, float);
extern int v;

placing the cursor before them and pressing ctrl+shift+b, would generate.

function fn(par0: Integer; par1: Single): Integer; cdecl; external;

var
  v : Integer; external;

You must be careful, because it's common to use macroses in C headers. Leaving any of macroses undefined might produce wrong pascal declaration. The next section explains how to define a macros for the chelper.

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;