Free Pascal has been choosen as a project of the month (April)

Free pascal has been choose as project of the month in sourceforge, and they interviewed it’s inventor Florian Klaempfl

 

http://sourceforge.net/blog/april-2014-project-of-the-month-free-pascal/

Auto Completion in Lazarus

I have found these days a new auto-completion feature that already exist in Lazarus, it is variables auto-completion. Previously I was only using procedure header / method auto-completion, by typing method header inside class definition part then clicking Ctrl-Shift-C to get implementation part of the procedure/method.

Now someone in Lazarus forum told us that auto completion is available for variables, for example, you can write for loop without defining loop variable first:

 


begin
  for i:= 1 to 10 do
    Writeln(i);
end.

Then stop in i variable and click Ctrl-Shift-C and you will get an automatic definition with a proper type:

 


var
  i: Integer;
begin
  for i:= 1 to 10 do
    Writeln(i);
end.

 

Also you can use it with parameter values:


procedure getRandomNumber(var x: string);
begin
  x:= Random(100);
end;

begin
  GetRandomNumber(MyNumber);
  Writeln(MyNumber);
end.

 

Then stop at MyNumber and click Ctrl-Shift-C to get:


procedure getRandomNumber(var x: Integer);
begin
  x:= Random(100);
end;

var
  MyNumber: Integer;
begin

  GetRandomNumber(MyNumber);
  Writeln(MyNumber);

end.

I found it very useful, so that I liked to share.

Now you can stop defining variables before using it, just use the variable and auto-define it later.

How to write Linux service application using FreePascal/Lazarus

Linux service application is a type of applications that has no text output in screen, and it runs in background when Linux is working, and it can run in log off mode, also it starts when operating system starts.

To write these type of applications using Lazarus do the following:

1. Create new project, select Program, give it a name, for example TimeLogger

2. In an infinite loop (repeat until false) write current date and time in log.txt text file:

program TimeLogger;

{$mode objfpc}{$H+}

uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes  , sysutils
{ you can add units after this };

var
TextF: TextFile;
begin
repeat
AssignFile(TextF, 'log.txt');
if FileExists('log.txt') then
Append(TextF)
else
Rewrite(TextF);

Writeln(TextF, DateTimeToStr(Now));

CloseFile(TextF);
Sleep(10000);

until false;
end.

3. Run the application from terminal, after a while stop it using Ctrl + C, to make sure that it is working. You should get log.txt file in the same directory

4. Write script file in /etc/init.d directory and name it timelogger. Put this code in the script:


#!/bin/sh
if [ "$1" = start ] ; then
cd /home/motaz/projects/TimeLogger
/TimeLogger &
fi

change the path of directory in which TimeLogger exists, and give execute permission to this script (sudo chmod +x /etc/init.d/timelogger)

5. Now you can start it using service command:


service timelogger start

And you can check it using ps -ef command:


motaz@t400laptop:~$ ps -ef | grep TimeLogger

you will get a result like this which means that it is working now :

motaz    24561     1  0 17:09 pts/1    00:00:00 ./TimeLogger
motaz    24600 24218  0 17:10 pts/1    00:00:00 grep --color=auto TimeLogger

Don’t forget to check the log file contents (log.txt)

 

6. If you want to check the status and to stop the service, then you need to modify the script source to add these new functionalities:


#!/bin/sh
if [ "$1" = start ] ; then
cd /home/motaz/projects/TimeLogger
./TimeLogger &
fi

#!/bin/sh
if [ "$1" = start ] ; then

count=$(ps -ef | grep TimeLogger | wc -lc | awk '{print $1}')
if [ $count -lt 2 ] ; then
cd /home/motaz/projects/TimeLogger
./TimeLogger &
echo "started"
else
echo "Already running"
fi
fi

if [ "$1" = status ] ; then
count=$(ps -ef | grep TimeLogger | wc -lc | awk '{print $1}')
if [ $count -lt 2 ] ; then
echo "Stopped"
else
echo "Running..."
fi
fi

if [ "$1" = stop ] ; then
count=$(ps -ef | grep TimeLogger | wc -lc | awk '{print $1}')
if [ $count -gt 1 ] ; then
psid=$(ps -ef | grep TimeLogger | head -n1 | awk '{print $2}')
kill -9 $psid
echo "Stopped"
else
echo "Not Running"
fi
fi

Now can you check it’s status, stop and start it:


service timelogger status

service timelogger stop

service timelogger start

7. Finally to make it work automatically when system is booted, execute this command:


sudo update-rc.d timelogger defaults

and you will get a result like this:


update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
Adding system startup for /etc/init.d/timelogger ...
/etc/rc0.d/K20timelogger -> ../init.d/timelogger
/etc/rc1.d/K20timelogger -> ../init.d/timelogger
/etc/rc6.d/K20timelogger -> ../init.d/timelogger
/etc/rc2.d/S20timelogger -> ../init.d/timelogger
/etc/rc3.d/S20timelogger -> ../init.d/timelogger
/etc/rc4.d/S20timelogger -> ../init.d/timelogger
/etc/rc5.d/S20timelogger -> ../init.d/timelogger

This has been tested on Ubuntu desktop and server version 12.04

My Story with Object Pascal language. Part one: Turbo Pascal

Before the university I have studied BASIC language using my MSX 64 K Bytes Ram, 2.8 Z80 Computer. On that time cassette tapes were the only available storage media that I have. On this computer I have learned the basic of programming, dealing with data, input and output, and finally I was able to write some graphical games, using spirit in MSX BASIC as I do remember.

After I have joined Sudan university in 1995 , I found that Turbo Pascal 6 is used as an introductory language for computer programming. At first I hated it, I found it more complex than Basic. Then I started using QBasic in University labs. After writing a game in QBasic I decided to try Pascal language again, and that happens before first official course of programming language. The course of Pascal language has started in the second semester. In the second semester I write three graphical computer games using Turbo Pascal 7. I remember that in lab it was not allowed to play games, but my friends copy these games and start to play with them. When lab administrator ask them to stop playing games, they say, this is a program of Turbo Pascal not a game and they open the source to prove it. Here are some snapshots of that games:

carCarFinGuess

At some point lab administrators upgrade Turbo Pascal 6 to Turbo Pascal 7, and when we asked our seniors about the difference, the say TP7 makes different color for reserved words.

I have resumed developing a lot of applications using Turbo Pascal during the first year in university. One of my important projects was typing tutorial, a text interface application to practice typing.

typing

typingrep

At the middle of second year at university I started to move into graphical interface instead of text mode. I spent enough time to write graphical library which includes menus, buttons, and different dialog boxes which supports mouse and is similar to Windows, at that time it was Windows version 3.11. Then I used these graphical libraries to write some applications like Arabic font designer, and diskettes library organizer.

arabicfont

I want to comment that Turbo Pascal 7 has a wonderful help system, strong compiler, bug free editor, fast executables and a debugger from the future.

New language additions in Free Pascal

These are many new improvements of Object Pascal programming language in Free Pascal compiler. I don’t know how much new are they, but at least they are new since Delphi 7 language. I haven’t used any of these features before (until now) because I wasn’t know about them.

1. For .. in loop:

This can be used to loop in string characters:


procedure ForInLoop(aStr: string);
var
  ch: Char;
begin
  for ch in aStr do
    WriteLn(ch);
end;

or through set items:


procedure ForInSetLoop;
var
  s: set of 1 .. 100;
  i: Integer;
begin
  s:= [1, 3, 7];
  for i in s do
    Writeln(i);
end;

2. += operator

This new operator can be used to concatenate strings and add values to numbers:


var
  aName: string;
begin
  aName:= 'Free';
  aName += ' Pascal';
  Writeln(aName); // Free Pascal
end;

For numbers:


x:= 10;
x+= 15;
Writeln(x); // 25

3. Properties without OOP

Now you can define a property in a structured code:


var
  x: Integer;

procedure SetX(aX: Integer);
begin
  x:= ax;
end;

function GetX: Integer;
begin
  Result:= x;
end;

property MyX: Integer read GetX write SetX;

// Main program
begin
  MyX:= 170;
  Writeln(MyX);

end.

4. Bit packed record

You can define a record of bits, and display it as byte:

type
  tbit = 0..1;

  tBitsByte = bitpacked record
    bit0   : tbit;
    bit1   : tbit;
    bit2   : tbit;
    bit3   : tbit;
    bit4   : tbit;
    bit5   : tbit;
    bit6   : tbit;
    bit7   : tbit;
  end;

var
  aByte: tBitsByte;
begin
  aByte.bit0:= 1;
  aByte.bit1:= 0;
  aByte.bit2:= 1;
  Writeln(Byte(aByte)); // 5

5. Sealed class

You can prevent inheriting from a class by adding the keyword sealed:

TMyClass = class sealed
  private
    fValue: Integer;
  public
    constructor Create(aValue: Integer);
    destructor destroy; override;
    function GetValue: Integer;
end;

I couldn’t find a good example right now to explain why we need to do this, but in the future I may get one.

6. Class methods and variables

You can declare methods and variables that can be used by class name before object instantiation the same like Java static methods as in this example:


TMyClass = class sealed
  private
    class var fValue: Integer;
  public
    constructor Create(aValue: Integer);
    class function GetValue: Integer;
    class procedure SetValue(aValue: Integer);
end;

constructor TMyClass.Create(aValue: Integer);
begin
  inherited Create;
  fValue:= aValue;
end;

class function TMyClass.GetValue: Integer;
begin
  Result:= fValue;
end;

class procedure TMyClass.SetValue(aValue: Integer);
begin
  fValue:= aValue;
end;

// Main code

begin
  TMyClass.SetValue(900);
  Writeln(TMyClass.GetValue);
end.

7. case of String


case aName of
'Free Pascal': Writeln('Lazarus IDE');
'C++': Writeln('CodeBlocks IDE');
end;

 

If you know any new useful additions please let me now to write it here.

References:

http://lazarus.freepascal.org/index.php/topic,19107.0.html

http://www.freepascal.org/docs-html/ref/refse24.html#x56-630004.6

http://edn.embarcadero.com/article/34324

Freepascal Answers blog 2012 in review

FreePascal answers blog review for 2012

Here’s an excerpt:

4,329 films were submitted to the 2012 Cannes Film Festival. This blog had 13,000 views in 2012. If each view were a film, this blog would power 3 Film Festivals

Click here to see the complete report.

Brook: a new web framework for FreePascal/Lazarus

A new and promising web development framework has been born for Free Pascal/ Lazarus developers. It supports REST, JSON, and you can download it from here:

http://brookframework.org/

TurboBird, a FireBird database administration tool that is written using FreePascal/Lazarus

TurboBird is a free and open source FireBird database administration tool that is written using FreePascal/Lazarus

Download page (Available for Linux, Windows, and Mac)

Development Source code in GitHub

Ohloh summary report about TurboBird

Can not load default Firebird clients (“libfbembed.so” or “libgds.so”). Check your installation

If you are using Free Pascal / Lazarus to develop applications that needs to access FireBird database, or you need to run applications that has been written using FreePascal/Lazarus and Firebird database, make sure you have installed Firebird client library.

If you have installed FireBird database engine (server) in the same machine, that means you already have this library installed, but for thee client machines you need to do the followings:

In windows you can install the client library from the same server packages that you can get from this link:

http://www.firebirdsql.org/en/server-packages/

This time choose Minimal client only installation.

In Linux you can install libfbclient2 package. For instance in Debian family you can execute this command:


sudo apt-get install libfbclient2

After installing Firebird client package you will get libraries like this in default Linux library directory:


/usr/lib/i386-linux-gnu/libfbclient.so.2
/usr/lib/i386-linux-gnu/libfbclient.so.2.5.1

Lazarus applications are still can not access them. You need to make a symbolic link by the default name to make FreePascal/Lazarus applications be able to load Firebird library.

In older versions of Linux you can do it like this:


sudo ln /usr/lib/libfbclient.so.2 /usr/lib/libfbclient.so

In newer versions of Linux use one of these two commands:

For 32 bit Linux:


sudo ln /usr/lib/i386-linux-gnu/libfbclient.so.2 /usr/lib/i386-linux-gnu/libfbclient.so

For 64 bit Linux:


sudo ln /usr/lib/x86_64-linux-gnu/libfbclient.so.2 /usr/lib/x86_64-linux-gnu/libfbclient.so

Also you can check Firebird library existence from your FreePascal/Lazarus code by doing the followings:

1. Add dynlibs in main project source code

2. Write this code before creating forms at main project code:


{$IFDEF Unix}
{$DEFINE extdecl:=cdecl}
  fbclib = 'libfbclient.' + sharedsuffix;
{$ENDIF}
{$IFDEF Windows}
  {$DEFINE extdecl:=stdcall}
  fbclib = 'fbclient.dll';
  seclib = 'gds32.dll';
{$ENDIF}

{$R *.res}

var
  IBaseLibraryHandle : TLibHandle;
begin
  IBaseLibraryHandle:= LoadLibrary(fbclib);
  {$IFDEF Windows}
    if IBaseLibraryHandle = NilHandle then
      IBaseLibraryHandle:= LoadLibrary(seclib);
  {$ENDIF}
  // Check Firebird library existance
  if (IBaseLibraryHandle = nilhandle) then
    Application.MessageBox('Unable to load Firebird library: ' + #10 + fbclib,
      'Warning', 0);

  Application.Initialize;
  Application.CreateForm(TfmMain, fmMain);

How to create new empty FireBird database using FreePascal/Lazarus

This is a very easy task in FreePascal/Lazarus.

Drop TIBConnection component from SQLdb page in a form or data module.

Drop a button, and write this code in button’s OnClick event:


procedure TForm1.Button1Click(Sender: TObject);
begin
  IBConnection1.DatabaseName:= '/home/motaz/firebird/newdb.fdb';
  IBConnection1.CharSet:= 'UTF8';
  IBConnection1.UserName:= 'sysdba';
  IBConnection1.Password:= 'masterkey';
  IBConnection1.CreateDB;
end;

This will create an empty FireBird database in the location you have specified in DatabaseName property, with UTF8 character set.

Later you can create tables, generators, indexes,  and constraints using SQLQuery component example:


create table BOOKS (
  BOOKID INTEGER not null ,
  BOOKNAME VARCHAR(52) ,
  CONTENTS BLOB,
  LOCATION VARCHAR(55)
  , constraint PK_BOOKS_1 primary key (BOOKID)
);

This will create a new table called Books. You need to put this DDL script in SQL property of SQLQuery component that linked to your IBConnection.

Follow

Get every new post delivered to your Inbox.