Bayesean Blog - Desktop, Mobile and IOT Developer Blog


C to Delphi Open Source Convertors Shootout

Posted on 8th Jun 2018 in Delphi FMX


C2D38.png

Background

Recently I was challenged to convert a rather large C/ C++ library with all its multiple examples and as I am NOT a seasoned C/C++ coder like many out there, to manually convert required far too many invested man hours to justify starting the process.  

Googling the Open- Source community for binaries or compliable source code that was designed to convert C / C++ including H and C files, presented me with many options that required further investigation. I tried and tested all, and discarded the many that were obsolete.  As always, one conversion app proved to be better than the rest for various reasons.

In early testing it became apparent that there is no such thing as a 100% convertor. Not even paid versions offer that privilege!

When testing, I found that after conversion, the convertor that kept to the C, C++ or H files original flow including the comments definitely made it easier to cross-check and to continue with the manual translation.

A convertor that translated to a full Delphi layout is impressive but WILL add extra man hours when you cross-compare results.

Why is C/C++ so different to Delphi?

The actual language structure is vastly different and this in itself makes the translation rather difficult. C/C++  is class –orientated and uses class libraries and has some specific unique processes that Delphi just does not have which can makes translation sometimes of a trial and error process to ensure that the results are the same.

If you are looking for more information on this subject then here are a few reading options:

Criteria - Open Source Shootout Contenders?

The objective was to be as fair as possible to all contenders and to keep it broad by including:-

  • All open-source applications 32bit or 64bit binary (exe) that are available as convertors.
  • All Unicode compliable source apps that do not rely on third- party commercial products.

 Shootout Measurement procedures

The test file is a + 2000 line .h file with external references and small C files.

  • Compile / Setup on Windows 10 and be able to convert C, C++ and H files and especially translate H libraries that makes external calls to a DLL.
    • Be functional.
    • Identify sections of code that could not be converted.
    • Retain the original format including comments.
  • Produce Quality Delphi code as close to 100% translated as possible.

Which Convertors are excluded?

  • Any Commercial product whether offered as a trial period or not and Lazarus code.
  • Those that could not compile on D10 up Compilers such as: - c2pas, Dr Bob’s header conversion and the Conversion Helper Package.
  • Various pre-Unicode versions that would not compile.

 

The Shootout !

Three convertors were used to translate a large .H file and thereafter smaller files were used to test the versatility of each Convertor.

 

aston21.png

C2PAS32 Convertor

C2PAS32 is a command-line tool created in 2001 by AstonShell and is offered as freeware. It translates C files (*.h; *.c) to a pascal unit.

 Package Installation

The App installs easily on a Windows 10 platform. It has a limited HTM Help styled file that gives limited information on how to translate a file. Some output settings can be adjusted by the ini file such as changing the DLL Name setting if you wish to call an external DLL.

 

Before converting, the C, C++ or H file needs to be in the same folder as the

C2PAS32.exe before it can be called through the CMD command-line App. If you are converting many files, this can be a little laborious and tedious to run.

 

Conversion

C2PAS3236.png

Conversion is quick and automatically saves the results as a similar namespace to the C file but as a ‘.pas’ file. Such as  libusb_h.pas.

Converted File Format

On conversion, the file retains 95% of the original format and retains the original flow of the C, C++, H file.

It processes routines and arguments, for and while loops, case statements, structs, enums, arrays and many other common routines.

It retains all the C,  C++ , H files comments and includes optional bracketed comments if it is not sure if it’s conversion i.e.  NULL to 0 {nil}. If no comparable Delphi process found, then the original is left intact. It includes its own alternate comments where translation is unknown. This really makes it easier for further manual conversion.

Whilst it reads the H file well and recognises the external calls and creates most functions and procedures accurately, it does not open the H file from a corresponding C, C++ file and consolidate the C and H files data. The C and H file will have to be manually merged.

It translated approximately 80% of the file of with approximately 40% of the conversion was totally converted.

The balance of the 35% required small changes such as fixing if statements and Delphi syntax.

Further translation required Enums and Structs converted to the Delphi Type and Const namespaces.  C++ chars converted to char instead of byte(s) or byte arrays.

 It converted external and standard functions, procedures, variables, constants and created the pointer variables mostly correct.

The slowest manual process was to convert the last 20% of the code and to shift the final code into the interface and implementation sections to test compile.

Trying the convertor on smaller C, C++ files it was found that C functions such as malloc, arg, argv, and macro’s  and Sprintf were not translated which required manual translation.

 C2PASconverted41.png

The final translation of the libusb.h  was processed using this convertor.

 Summary

  • Command-Line is a bit tedious to process.
  • Creates the translated file effortlessly.
  • 95% of translated code retained.
  • 80% converted of which only 40% correctly.
  • 35% required further minor converting.
  • 25% full final manual conversion required.

  CtoPascal34.png

 C to pascal 2.0 Convertor

Background

This application can be downloaded from A.L Gun.  Developed by Ural Gunaydin. The latest version is C to Pascal Converter 2.0, Update 21.7.2015.

It converts GUID Macros, updated help issues. Comes with installation program  "Setup.exe".

Package Installation

The application loads with an installer and places an icon in the menu in windows 10.

This is a fully fledged traditional styled text editor application which also allows for the merging of files (untested).

The Application easily opens a C, C++, H file and loads it in a text-memo format. A group project can be set up for multiple file processes.

To translate the file, a run button is initiated and the application in the background loops through a set of script algorithms to parse the file. This results with the original overwritten within the text memo.  After translation, the file can be directly edited and / or saved.

 On Translation a total of 98 script passes were made on 2040 lines of code.

All the identifiers (definitions) used for parsing can be viewed. Only the basic identifiers have been added to the parser. Further basic parsing identifiers can be added manually to the application.

 The application retained 100% of the converted file of which approximately 20% of the file was correctly translated. Approximately 30% of the file required minor changes.  Approximately 50% of the file required large changes.

Due to the way in which the translator works, the application will not lose any data and comments are retained.  This is great for comparing the original to the translated file.

It strangely converted some comments to code which made for manual correction.

It did not convert type information correctly and battled to parse functions and procedures correctly. It does not parse pointers at all. It is not set up to parse the external functions and procedures to a DLL.  All other tested file gave similar results.

 

C2PAS2conv50.png

Summary

  • Retained 100% of the converted file.
  • 20% of the file fully translated.
  • 50% of the file required minor manual translation.
  • 30% of the file required final manual translation.
  • A great editor and a lot of thought gone into the package.

 

C2Delphi56.png C to Delphi Convertor

Background

C-to-Delphi Convertor is developed by Wouter Van Nifterick which resides in GitHub.

The tool contains a split view where you can write standard C code on the left side of and automatically translated code in Object Pascal (Delphi) on the right side of the split view. The Delphiast library is used to perform syntax checking on the Delphi code side. Some of the features of the converter tool include the following options:

If function main exists, a program will be generated, otherwise a pascal unit with interface/implementation sections.

It converts routines and arguments, for loops, case statements, structs, enums, 1 and 2 dimensional arrays. It features many other common routines to Delphi equivalents (strcpy, strcat, strlen, printf, argv, argc, etc) and classes.

It uses drag/drop multiple C files to the application. It'll find the .h files that belong to it, and convert all to .pas files in the same folder.

Conversion

As it makes use of third party Components (Delphiast, Synedit) that I do not use, I made use of the supplied binary from the GITHUB site. The FMX App starts up quickly and is presented with a processed example.  It provides word search edit-box and button to search for words.  

The app is modern in look of feel and as an editor, it shows text highlighting. Loading a C, C++ or H file makes use of modern drag n drop functionality. This is great for ease of use. Once the file has been dropped onto the Application, the original file is highlighted in the second Column and is automatically translated to appear in the third Column and the first Column shows a tree-view of all the translated functions procedures.

C2Delphiconv9.png

There is no visible method to save the file. Windows key functions have to be used.

Ctrl-A highlights all text. Ctrl-C copies the text.  Ctrl-V allows you to paste the text into an external text editor such as Windows memo. Then the text can be saved as a pas file.

 Converted File Format

The Conversion is fast and is fully translated into the Delphi format on dropping a file in the application.

It immediately sets up a Delphi file with the Class routines converted into the users.

For what it translated, the code is cleanly translated into types, const and variables and class functions and procedures.

The conversion retained 0% of the original file and all comments were deleted.

Surprisingly approximately 18% of the code remained of which approximately 80% was accurately translated.

It mostly converts char to bytes but does not recognise pointers in the variables.

I does not convert #define macros, C specific functions and any external calling procedures and functions to a DLL. What it can’t translate, it deletes from the result.  This makes it difficult to translate the rest of the file.

Testing other C files. It generally converted small snippets of code and that is where this convertor works best.  It converts structs and enums close to 100% but struggled to convert pointers.  This is the only app tested that will find the H file for a C file to convert.

Summary

  • Is the best for C, C++ limited code translations but not suited for large header file translations.
  • It retained 18% of original H file converted with 14% fully translated. This statistic improves for small code translations.
  • It converts directly to a Delphi full format.
  • Difficult to save to a Pas file.
  • This Application is work in progress on GitHub and should evolve to be a top contender.

 

Happy Coding


Lauro      Commented   2 months ago Reply

Congratulations on the Code! This example can be used in DBGrid to make the Tree feature. | Customers A | -Sales $ 100.00

Add a Comment

9+5

Recent News

Delphi VCL Buttons in DBGrid
Two Helper Apps for Delphi LibUSB
Delphi Libusb Library Introduction
Delphi Object directly to a Json string in a REST Client
Delphi using Environment Variables in your App
Delphi FMX Leaflet Plotter using OSM Maps
C2PAS32 Convertor Application
C to Delphi Open Source Convertors Shootout

Categories

Bootstrap 4
Delphi VCL
Delphi FMX
Ajax
Bootstrap 3
CSS
XE4>Delphi > XE4
Delphi < XE4

Archives

October 2018

Delphi VCL Buttons in DBGrid
Two Helper Apps for Delphi LibUSB

September 2018

Delphi Libusb Library Introduction

August 2018

Delphi Object directly to a Json string in a REST Client
Delphi using Environment Variables in your App

July 2018

Delphi FMX Leaflet Plotter using OSM Maps

June 2018

C2PAS32 Convertor Application
C to Delphi Open Source Convertors Shootout
Delphi command-line programs with DOSCommand

May 2018

Delphi PDF Embedded viewer with PDF.js

March 2018

Delphi FMX - Changing TCharacter to TCharHelper
Make Your Delphi App POP using Javascript!

January 2018

Delphi FMX Dashboard using Chart.JS
Delphi FMX Form Docking

December 2017

PHP Slim REST Server & Delphi Auth Part 5

November 2017

Delphi FMX REST Client App Part 4

October 2017

Delphi VCL REST Pricing Client App Part 3

September 2017

Delphi REST VCL Client Basic Auth Part 2B

August 2017

Delphi REST Client Part 2A
PHP PDO REST Server Part 1

July 2017

PHP REST Server and Delphi Client Intro

June 2017

Delphi SQLite Encryptor-Decryptor Tool
Updating Applications Manifest using Delphi

May 2017

Create a Visual IP Address Geolocation with PHP

March 2017

PHP Downloader using Countdown timer
PHP File Downloader from a Inbox Selection

February 2017

Javascript Image-File Uploader with ThumbViewer

January 2017

Morris Charts and PHP-PDO

December 2016

CSS to create a functional Toggle Button