Anda di halaman 1dari 48

BLAISE

BLAISE PASCAL MAGAZINE 41


PASCAL MAGAZINE
D E L P H I, L A Z A R U S, O X Y G E N E, S M A R T M O B I L E,
A N D P A S C AL R E L A T E D L A N G U A G E S
A N D R O I D, I O S, M A C, W I N D O W S & L I N U X

Alan Turing June 23, 1912


AlanTuring 23 6 1912
23 Alan 6 Turing 1912
@23Alan6TuringMCMXII
@23Alan6TuringMcmX2!
@23A6TMcmX2!

SECURITY AND SAFETY IN APPLICATIONS - BY ANDREA RAIMONDI


B E G I N N I N G O F T I M E . . . T H E W AT E R C L O C K - B Y E D I T O R
THE NEW LAZARUS 1.4 - BY EDITOR
SECURITY IN APPLICATIONS: PASSWORD HANDLING
- BY ANDREA RAIMONDI

IN REMEMBERANCE OF OF ALAN TURING - BY EDITOR

LAZARUS NOW CAN USE GOOGLE APIS


REST CLIENTS: USING THE GOOGLE APIS IN FREE PASCAL
- BY MICHAEL VAN CANNEYT

ARDUINO: THE VISUINO PROJECT - PART 1


BY BOIAN MITOV
WORKING WITH GOOGLE MERCHANTS RATING DATA USING KBMMW
BY KIM MADSEN

PRINTED ISSUE PRICE 15,00


DOWNLOAD ISSUE PRICE 5,00
BLAISE
BLAISE PASCAL MAGAZINE 41
PASCAL MAGAZINE
D E L P H I, L A Z A R U S, S M A R T M O B I L E S T U D I O,
A N D P A S C A L R E L A T E D L A N G U A G E S
F O R A N D R O I D, I O S, M A C, W I N D O W S & L I N U X

CONTENTS

Articles
SECURITY AND SAFETY IN APPLICATIONS
- BY ANDREA RAIMONDI - PAGE 5
BEGINNING OF TIME...THE WATER CLOCK
- BY EDITOR - PAGE 9
THE NEW LAZARUS 1.4
- BY EDITOR - PAGE 14
SECURITY IN APPLICATIONS: PASSWORD HANDLING
- BY ANDREA RAIMONDI - PAGE 18
IN REMEMBRANCE OF OF ALAN TURING
- BY EDITOR - PAGE 23
LAZARUS NOW CAN USE GOOGLE APIS
REST CLIENTS: USING THE GOOGLE APIS IN FREE PASCAL
- BY MICHAEL VAN CANNEYT - PAGE 25
ARDUINO: THE VISUINO PROJECT - PART 1
- BY BOIAN MITOV - PAGE 37
WORKING WITH GOOGLE MERCHANTS RATINGDATA USING KBMMW
- BY KIM MADSEN - PAGE 43

MOTION

BLAISE
BLAISE PASCAL
PASCAL MAGAZINE
MAGAZINE

Advertisers

Barnsten 4
BetterOffice 21
Components 4 Developers 48
Computer Math & Games 4
Daniel Teti 47
Raize Software 8 Publisher: Foundation for Supporting the Pascal Programming Language
Visuino MITOV 22 in collaboration with the Dutch Pascal User Group (Pascal Gebruikers Groep)
Stichting Ondersteuning Programmeertaal Pascal

2 Issue Nr 3 2015 BLAISE PASCAL MAGAZINE


Stephen Ball Peter Bijlsma Michal Van Canneyt,
http://delphiaball.co.uk -Editor peter @ blaisepascal.eu michael @ freepascal.org
@DelphiABall

Marco Cant David Dirkse Benno Evers


www.marcocantu.com www.davdata.nl b.evers
marco.cantu @ gmail.com E-mail: David @ davdata.nl @everscustomtechnology.nl

Bruno Fierens Primo Gabrijeli Cary Jensen


www.tmssoftware.com www. www.jensendatasystems.com
bruno.fierens @ tmssoftware.com primoz @ gabrijelcic.org http://caryjensen.blogspot.nl

Max Kleiner John Kuiper Wagner R. Landgraf


www.softwareschule.ch wagner @ tmssoftware.com
max@kleiner.com

Kim Madsen Peter van der Sman Jeremy North


www.component4developers jeremy.north @ gmail.com

Detlef Overbeek - Editor in Chief Howard Page Clark Andrea Raimondi


www.blaisepascal.eu E-mail: hdpc @ talktalk.net andrea.raimondi @ gmail.com
editor @ blaisepascal.eu

Wim Van Ingen Schenau Rik Smit Bob Swart


-Editor rik @ blaisepascal.eu www.eBob42.com
wisone @ xs4all.nl Bob @ eBob42.com

Daniele Teti
www.danieleteti.it
d.teti@bittime.it

Please note: extra space characters have been deliberately added around the @ symbol in
these email addresses, which need to be removed if you use them.
editor @ blaisepascal.eu
Authors - Christian name in alphabethical order
A Andrea Raimondi , L Wagner R. Landgraf, Sergey Lyubeznyy
B Stephen Ball, Peter Bijlsma, Dmitry Boyarintsev K Max Kleiner
C Michal Van Canneyt, Marco Cant, M Kim Madsen, Felipe Monteiro de Cavalho
D David Dirkse, Daniele Teti N Jeremy North,
F Bruno Fierens O Inoussa Ouedraogo
G Primo Gabrijeli, Mattias Gaertner P Howard Page-Clark,
H Fikret Hasovic S Rik Smit, Bob Swart,
J Cary Jensen Z Siegfried Zuhr

Editor - in - chief
Detlef D. Overbeek, Netherlands Tel.: +31 (0)30 890.66.44 / Mobile: +31 (0)6 21.23.62.68
News and Press Releases email only to editor@blaisepascal.eu

Editors
Peter Bijlsma, W. (Wim) van Ingen Schenau, Rik Smit,
Correctors
Howard Page-Clark, James D. Duff
Trademarks
All trademarks used are acknowledged as the property of their respective owners.
Caveat Whilst we endeavour to ensure that what is published in the magazine is correct, we cannot accept responsibility for any errors or omissions.
If you notice something which may be incorrect, please contact the Editor and we will publish a correction where relevant.
Subscriptions ( 2013 prices )
1: Printed version: subscription 65.-- Incl. VAT 6 % (including code, programs and printed magazine,
10 issues per year excluding postage).
2: Electronic - non printed subscription 45.-- Incl. VAT 21% (including code, programs and download magazine)

Subscriptions can be taken out online at www.blaisepascal.eu or by written order, or by sending an email to office@blaisepascal.eu
Subscriptions can start at any date. All issues published in the calendar year of the subscription will be sent as well.
Subscriptions run 365 days. Subscriptions will not be prolonged without notice. Receipt of payment will be sent by email.
Subscriptions can be paid by sending the payment to:
ABN AMRO Bank Account no. 44 19 60 863 or by credit card: Paypal
Name: Pro Pascal Foundation-Foundation for Supporting the Pascal Programming Language (Stichting Ondersteuning Programeertaal Pascal)
IBAN: NL82 ABNA 0441960863 BIC ABNANL2A VAT no.: 81 42 54 147 (Stichting Programmeertaal Pascal)
Subscription department Edelstenenbaan 21 / 3402 XA IJsselstein, The Netherlands / Tel.: + 31 (0) 30 890.66.44 / Mobile: + 31 (0) 6 21.23.62.68
office@blaisepascal.eu

Copyright notice
All material published in Blaise Pascal is copyright SOPP Stichting Ondersteuning Programeertaal Pascal unless otherwise noted and may
not be copied, distributed or republished without written permission. Authors agree that code associated with their articles will be made
available to subscribers after publication by placing it on the website of the PGG for download, and that articles and code will be placed on
distributable data storage media. Use of program listings by subscribers for research and study purposes is allowed, but not for commercial
purposes. Commercial use of program listings and code is prohibited without the written permission of the author.

Issue Nr 3 2015 BLAISE PASCAL MAGAZINE 3


CALLING ALL VCL DEVELOPERS!
Whether you are building new software, or updating existing applications, RAD Studio XE8 is a must! Quickly update
and extend your existing VCL based Windows applications to create modern, connected app solutions for Windows and
beyond! Try it with your complimentary 30-day license now!

Must-Have Features For VCL Developers in NEW RAD Studio,


Delphi and C++Builder Xe8

NEW! MORE THAN 20 NEW IDE FEATURES FOR CODING PRODUCTIVITY:


Code faster and more efficiently with new refactorings, multi-paste support, clipboard history, parenthesis matching,
Smart Keys, Code Navigation Toolbar, project statistics and more!
ENHANCED! THE VCL AND FMX RTL GETS BETTER AND YOU BENEFIT BIG TIME:
The RTL has better performance, parallel support, native HTTP/S and Bluetooth, optimized Generics collections and
much more
NEW! GETIT PACKAGE MANAGER: ACCESS AND INSTALL FROM THE CLOUD,
popular VCL and FMX source code libraries, components and tools like AsyncPro and Power PDF right from within the
IDE. Get the updated TurboPack for free!
ENHANCED! APP TETHERING:
Now it's easier than ever to extend your VCL and FMX desktop application UI, data and control to mobile and wearables
via WiFi, ethernet or Bluetooth
NEW! APPANALYTICS:
Finally understand exactly how your customers use your VCL or FMX app. Simply add the TAppAnalytics component
and get instant access to application usage statistics through your cloud dashboard
RECENTLY ADDED! Easily Add Parallel Processing
to Your New and Existing VCL Apps: Delivering 2x to 8x performance gains on Multi-Core systems
NEW! NATIVE PLATFORM HTTP/S VCL/FMX SUPPORT FOR WINDOWS, MAC, IOS AND
ANDROID: Makes must-have secure connections a snap
ENHANCED! MBAAS CLOUD SERVICES:
Easily power your VCL and FMX desktop and mobile application backend infrastructure in the cloud with user
management, push notifications, data storage and more
NEW! IOT, BEACONS AND MORE:
Easily add IoT gadgets and sensors, like proximity beacons, into your existing Windows VCL and FMX appsNEW! Unit
Testing: It's has never been easier or more complete with the new integrated DUnitX testing framework
ENHANCED! FireDAC:
The best DAC gets better with Updates Management, dbExpress migration, SQLite encryption, InterBase XE7 Change
Views and more
ENHANCED! EMS (Enterprise Mobility Services):
Easily integrate your applications to your enterprise and to the world with Push Notifications, REST API publishing,
external credentials, database connection pooling, client API component, new administrative app and more
ENHANCED! Docs, Samples and Technical Content:
Learn what you can do and how to do it with more tech docs and videos than ever before
NEW! Update Subscription:
Take advantage of Update Subscription to stay up-to-date with latest versions, updates for older product versions and
much more

or call: +31 (0)235422227


http://www.barnsten.com/default/newxe8
SECURITY AND SAFETY IN APPLICATIONS BY ANDREA RAIMONDI (PAGE 1)
Introduction Once you do that, you can browse the site looking
There is a gigantic misconception based off the
for clues and finding an admin password to get
fact many developers think that to enable
security you need to use a library. While that inside the hosting panel. Once there, you are King:
surely is part of what you need to do, it's by far you can download the database and crack the
not the only thing, because security is so much passwords, plus getting C/C numbers in the
more than that. process. Here we really have two datasets a
malicious user is interested in: the password list
Security and Safety and the credit card numbers. I don't need to
When we talk about security and safety we really
explain why the second is attractive, but what
refer to two different things: security is about
the environment around you, while safety is about the password list? Why is it so attractive?
about you. They are often used (wrongly) Well, the thing is that humans stubbornly demand
interchangeably because of this lack of to be humans, so they want an easy life. Because of
understanding. So, when we say that data must this, they are probably using for that site the same
be secure, we are saying that there has to be no password they are using elsewhere, for instance in
way to arrive at a situation where it can be their home banking. Get it now? It is not always
grabbed hold of from people who should not
about the single site they are attacking, more often
have access to it.
If that happens, then the data must also be safe,
is about the knowledge they can acquire.
i.e. it must be difficult (or impossible) to use by
said people or their associates. Hard and Low Potential
The original website has been hacked and the
black market has been flooded with credit card
numbers. A group of investors, however, thinks
The Internet there is potential in the site's concept, if only
Let's get this straight: in the Internet age, data is things could be made in a way that allowed them
neither secure nor safe ever. Just accept this and to avoid the mistakes of the past. So they bring in a
realise all you can do is to make it really difficult to security expert who makes the following changes:
actually break in and/or use the data. Passwords cannot be retrieved anymore,
What do we mean, then, when we say that the data they have to be reset.
is secure and/or safe in an Internet setting? What SQL errors aren't showing anymore.
we are really saying (or better, what we should be The only acceptable C/C numbers are those
saying, alas some companies still try to sell snake of virtual cards, which are generated by the
oil) is that the data is not attractive enough to home banking or those which also input you
malignant parties to be worth their time. for a password (which is not stored in the DB)

Data Attractiveness These changes alone make all the difference: the
What makes data attractive to malicious users? fact you have to reset the password means that it
There are two main traits which play a role in how has been hashed, so it's not easy to recover. Also,
attractive a data set is: ease of grab and potential; if the fact you only accept virtual c/c numbers or
the data is easy to grab and with a lot of potential real ones that need a confirmation password
then malicious parties have a really strong interest means that they are essentially useless. For virtual
in getting it. On the other hand, if the data is cards, if a break-in happens, they can easily be
extremely difficult to grab and/or has little disabled and made useless as well.
potential, then they do not have as much interest in
getting hold of it, making it more secure and more As you can see, a few changes make the data
safe in the process.So let's just make an example of harder to get and with very little value in case of a
each and see how your data fits in the picture: break in. The thing here is that now, they only get
one potentially promising set of data instead of
Easy and High Potential two, because even if the admins do not realise
A website called We store your credit cards has a soon of the break in, there is just so much money
login form which shows SQL errors. When you go that criminals can sell a lot less than on real
into your profile, you can see your password in credit cards. Also, if the virtual and real numbers
clear text. It reminds you of your credit card are not clearly identified in the DB, this makes all
number and security code. Why is that an attractive of them useless, because there is no way criminals
target? The fact it shows SQL errors means there is can distinguish them without using them,
no protection from SQL Injection, which means that therefore exposing the fact that a break-in has
you can try to log in with credentials you do not occurred.
own.

Issue Nr 3 2015 BLAISE PASCAL MAGAZINE 5


SECURITY AND SAFETY IN APPLICATIONS (PAGE 2)
Think Holistically* There are so many things that can potentially go
When you are designing an application or website, wrong and these are just a start.
security and safety should be among the design
considerations. If you are accepting an extranet Users and Roles
login, for instance, you need to hash your Let me get back to this for a minute please. One
passwords with an algorithm that isn't easy to thing is the database users and another is
break in, i.e. that is secure. Hence, you can't use application users.
MD5 or SHA1 for example. That's a really bad
idea. You really should be using SHA2 or SHA256. There are two schools of thought in this: one says
Or, if you are using an insecure algorithm for Every application user should be associated to a
some reason, then you ensure you can use it with database user and the other says: No way,
an HMAC. HMACs have this beautiful property of application users are one thing and database users
being secure even if you use an insecure hash are another. Of the two, under a security point of
function. view, the latter is correct. Why should an
(* Holism Chiefly Philosophy:The theory that parts of a application user have an account on the database?
whole are in intimate interconnection, such that they Using social engineering, a malicious user could
cannot exist independently of the whole, or cannot be approach an application user who is elevated
understood without reference to the whole, which is enough to cause havoc. This is a very bad idea.
thus regarded as greater than the sum of its parts. ) This is why we have roles see. Your identity and
your role are (and should always be) two different
But logins are just the tip of the iceberg. things. It makes sense in everyday life, where we
How about the JavaScript? Are you passing experience it all the time, why should it be any
JavaScript functions in your JSON and calling different in applications?
eval?
That's another no-no because it's dangerous. Application Screens
What happens if the data is valuable and a There is this really bad tendency to make
malicious user breaks in on the server and adds a applications that operate in light mode until a
PHP file and injects a malicious JavaScript serial number is typed in where they become fully
function? operational. This is a very dangerous way of
How about the structure of your website? making software, because there is a respected and
If you can upload files, are you keeping them in a long tradition among criminals of checking serial
safe place and using an internal script which is numbers and deriving key generators. I do not like
allowed to fetch them or are you keeping them in this at all because I think it's way too easy to break
a place where they can be grabbed? the mould and be a leecher.
So, what should you be doing? Well, for a start,
And your webpages, are you enforcing security you should have two versions: one for the light and
for really important pages? Or are you letting the other for the fully functioning one. It is really
anyone browse to userlist.php? easy to split forms so that there's one you can use in
Desktop applications aren't exempt either from the light version and an inherited one which you
this sort of problems. Where are you storing your can use in the full. It is not difficult to do and yields
files? Are you taking measures to make them great results. That is not all, however. Each screen
difficult to use? Are you storing them in a binary should only have an associated menu item visible if
or text format? Binary formats are inherently more your role allows it. And you should check user and
difficult to handle, it requires skill. role when loading the form, so that if they do not
What database are you using? Are you using match the expected ones a message can be
Firebird, which allows an attacker to download displayed saying the user is not authorized and
the db and use his/her own security accounts to then unloaded gracefully. This is because there are
read it or are you using Oracle, where you can ways to make a menu item appear on Windows, it
only do that AFTER you have exported the has been done before.
database (hence requiring a correct account in the first
place)? Licensing
Another issue is licensing. I have seen, again, a
And are your database accounts safe? Are you disturbing trend of having online activations, user
using SQL Server with Windows Authentication, harassment etc., all in the name of revenue. This
which means that if a malicious user gets hold of means we punish the good guys while the bad ones
your Windows account he/she can roam free in get mostly away with it, because you know
the DB as well? these things can be removed most of the time.

6 Issue Nr 3 2015 BLAISE PASCAL MAGAZINE


SECURITY AND SAFETY IN APPLICATIONS (PAGE 3)
There are ways to do it, because I still see pirated So should we do something like being able to
copies of software floating around so it's not like associate forms to roles so that users can get what
they serve a purpose. They only make life difficult they want while not increasing the risk too much?
for honest people who purchase their stuff. And There are cases, however, where I feel that
they come at an incredibly high cost because this developers should put their feet down and just flat
stuff doesn't cost peanuts. I have also seen out refuse to do something on security grounds.
madness such as, for example, encrypted Better yet, if you can, make the user sign a
executables and things like this. document where they recognize you were against
something on security grounds and they wanted it
That's completely bonkers because it makes anyway; If possible, make them sign it in blood
memory management in Windows a mess. So, (yes, I am serious, DNA always works in court).
how do we solve this? It's pretty simple actually: Before getting to that, however, always try to
just use decent serial numbers that work locally explain clearly why you are refusing something on
and that are detached from the working machine. security grounds. When I was living in Naples,
When you need to check that the serial Italy, I was in an apartment block. On the floor just
number is ok, then pop up a box asking for the below ours, a judge lives who has two internet
email addressed used in registration and match it connections: his home one and the other one
against the available serial numbers. connected to the tribunal.
If the number is available and it's the first time a He was whining at me of this, saying he
check occurs, then it's very likely a legit user who couldn't download or install anything on this
wasn't yet registered. If it's not available or if the special machine. He was suffering it because he
email is different from what it should be, then it's didn't understand the problem. So I stopped him
very likely a pirate. You can also offer the option and said How would you like if someone
to register straight away, so that the following
connected to the tribunal using your account and
checks are performed directly against the taken
wrote the motivation to a judgement instead of
serial numbers.
you? He went pale and said Don't even joke
When you make the serial number and
about that. This is when I told him the Internet is
activation code independent from the machine,
a dangerous place. This is why that machine is
you make computer upgrades possible easily. You
also make the user less frustrated and less limited, to avoid even the chance something like
worrisome. You can provide a way to enter a that happens. He hasn't whined about it since.
username and password and download the
registration number, so that they don't even have Is it worth it?
to back it up to re-enter it. Make it easy on them. Now, we have discussed a few principles that
All you want is to have legitimate users. That should really be considered when doing
is all which counts. Ideally, users should not even applications or websites. These things, however,
have to see their registration number. They are absolutely not free, so what you do or not do
shouldn't have to care. also has to depend on your financials. Is it worth,
for example, to add role checking to all forms?
Convenience trumps security Maybe yes, if you are doing an accounting
every time package, or maybe not If you are simply doing a
Then there's the problem of convenience. Should personal ledger.
we allow a user to log in automatically? What Also, can you afford for example to only
happens if an XSS attack steals the login cookie? check serial numbers when the internet is
Should we track the IPs? Should we restrict the connected? Again, maybe yes or maybe not,
functionality only to the same IP and if it changes depends on your circumstances. There are cases,
for any reason they need to log in again? for example, where you really need to check that a
Users love convenience and some of the user is licensed to use a certain application, but
above advice goes just in this direction: it is very those cases are only about situations where
convenient for users not to have to back-up a computers do not change often and where
serial number and just you know log in from connectivity is not an issue. So, each application
the application to grab it. Other things, however, should weigh in the advantages against the
go against convenience: the roles, checking and disadvantages of a certain approach.
making sure that forms work with the correct Striking a balance is very difficult and
users, etc.
sometimes you make the wrong decisions. It
What may happen in some cases is that
happens all the time, so you should be able to
someone is given a role that he/she should not
change your mind when your situation changes:
have just to access a certain form, while in the
an open mind is always an asset, ever so much so
process putting everyone at risk because of the
in security.
increased privilege.

Issue Nr 3 2015 BLAISE PASCAL MAGAZINE 7


BLAISE 2015
THE WATER CLOCK PAGE 1
In this article series I will try to inform you about Korea and Japan. Some water clock designs were
very important , yet not very well known subjects developed independently and some knowledge was
of the time line we showed in the last item (NR. 40). transferred through the spread of trade. These early
This time the water clock is the subject: water clocks were calibrated with a sundial.
I suppose you never realized that very ancient people
already new about time and measuring it.
It goes as far back as to 4500 BC (before Christ) as far
as the documentation reaches. Most of the
documentation that follows is gathered trough the
internet and sometimes through books. We thank te
authors for that and making it available:
http://en.wikipedia.org/wiki/Water_clock
http://fourriverscharter.org/projects/Inventions/pages/
china_waterclock.htm.
Surely there is more to be found , but this illustrates
much of the knowledge we have about this subject.
One last thing is there to be said: the not being
scalable of the water clock is the reason that it took so Figure 1: Ancient Persian clock
long before time was in use for all people:
the invention of the mainspring in the early 15th Persia
century allowed portable clocks to be built, evolving According to Callisthenes, the Persians were using water
into the first pocketwatches by the 17th century, then clocks in 328 BC to ensure a just and exact distribution of
it became possible to create a portable clock and from
water from qanats to their shareholders for agricultural
there on it was an invention that became available
even on your espresso machine. irrigation. The use of water clocks in Iran, especially in
Zeebad, dates back to 500BC. Later they were also used to
determine the exact holy days of pre-Islamic religions,
such as the Nowruz, Chelah, or Yald - the shortest,
longest, and equal-length days and nights of the years.
A water clock or clepsydra (Greek kleptein) is any
timepiece in which time is measured by the regulated The water clocks used in Iran were one of the most
flow of liquid into or out from a vessel where the amount practical ancient tools for timing the yearly calendar. The
is then measured. water clock, or fenjaan, was the most accurate and
Water clocks, along with sundials, are likely to be the commonly used timekeeping device for calculating the
oldest time-measuring instruments. Where and when amount or the time that a farmer must take water from a
they were first invented is not exactly known, and given qanat or well for irrigation, until it was replaced by more
their great antiquity it may never be. accurate current clocks.
The bowl-shaped outflow is the simplest form of a Persian water clocks were a practical and useful tool
water clock and is known to have existed in Babylon and for the qanat's shareholders (A qant is one of a series of
in Egypt around the 16th century BC. Other regions of the well-like vertical shafts, connected by gently sloping tunnels.
world, including India and China, also have early Qants create a reliable supply of water for human settlements
evidence of water clocks, but the earliest dates are less and irrigation in hot, arid, and semi-arid climates) to calculate
certain. Some authors, however, claim that water clocks the length of time they could divert water to their farm.
appeared in China as early as 4000 BC. -(Cowan, Harrison Therefore a very fair and clever old person was elected to
J. (1958). "Time and Its Measurement: From the stone age to be the manager of the water clock(MirAab), and at least
the nuclear age". Ohio: The World Publishing Company.) two full-time managers were needed to control and
Some modern timepieces are called "water clocks" observe the number of fenjaans and announce the exact
but work differently from the ancient ones. Their time during the days and nights.
timekeeping is governed by a pendulum, but they use
water for other purposes, such as providing the power The fenjaan consisted of a large pot full of water and a
needed to drive the clock by using a water wheel or bowl with a small hole in the center. When the bowl
something similar, or by having water in their displays. became full of water, it would sink into the pot, and the
manager would empty the bowl and again put it on the
The Greeks and Romans advanced water clock design to top of the water in the pot. He would record the number
include the inflow clepsydra with an early feedback of times the bowl sank by putting small stones into a jar.
system, gearing, and escapement mechanism, which were
connected to fanciful automata and resulted in improved The place where the clock was situated, and its managers,
accuracy. were collectively known as khaneh fenjaan. Usually this
Further advances were made in Byzantium, Syria would be the top floor of a public-house, with west- and
and Mesopotamia, where increasingly accurate water east-facing windows to show the time of sunset and
clocks incorporated complex segmental and epicyclic sunrise. There was also another time-keeping tool named
gearing, water wheels, and programmability, advances a staryab or astrolabe, but it was mostly used for
which eventually made their way to Europe. superstitious beliefs and was not practical for use as a
Independently, the Chinese developed their own farmers' calendar. The Zeebad Gonabad water clock was
advanced water clocks, incorporating gears, escapement in use until 1965 when it was substituted by modern
mechanisms, and water wheels, passing their ideas on to clocks.

Issue Nr 3 2015 BLAISE PASCAL MAGAZINE 9


THE WATER CLOCK PAGE 2

Egypt India
The oldest water clock of which there is physical evidence N. Kameswara Rao suggests that pots excavated from
dates to c. 1417-1379 BC, during the reign of Amenhotep Mohenjo daro might have been used as water clocks;
III where it was used in the Temple of Amen-Re at they are tapered at the bottom, have a hole on the side,
Karnak. The oldest documentation of the water clock is and are similar to the utensil used to perform
the tomb inscription of the 16th century BCE Egyptian abhishekam (pour holy water) on shivalingam.
court official Amenemhet, which identifies him as its It is suggested that the use of the water clock in ancient
inventor. These simple water clocks, which were of the India is mentioned in the Atharvaveda from the 2nd
outflow type, were stone vessels with sloping sides that millennium BC. Ghati or Kapala (clepsydra or water clock)
allowed water to drip at a nearly constant rate from a is referred to in Jyotisha Vedanga, where the amount of
small hole near the bottom. There were twelve separate water that measures a nadika (24 minutes) is mentioned.
columns with consistently spaced markings on the inside
to measure the passage of "hours" as the water level A more developed form of the clepsydra is described in
reached them. The columns were for each of the twelve the Suryasiddhanta. At Nalanda, a Buddhist university,
months to allow for the variations of the seasonal hours. four hours a day and four hours at night were measured
These clocks were used by priests to determine the time at by a water clock, which consisted of a copper bowl
night so that the temple rites and sacrifices could be holding two large floats in a larger bowl filled with
performed at the correct hour.[8] These clocks may have water. The bowl was filled with water from a small hole
been used in daylight as well. at its bottom; it sank when completely filled and was
marked by the beating of a drum at daytime.
The amount of water added varied with the seasons and
this clock was operated by the students of the university.
The description of a water clock in astrologer
Varahimira's Pancasiddhantika (505) adds further detail
to the account given in the Suryasiddhanta.

Figure 2: Water clock calculations by Nab-apla-iddina


Babylon
In Babylon, water clocks were of the outflow type and
were cylindrical in shape. Use of the water clock as an aid
to astronomical calculations dates back to the Old
Babylonian period (c. 2000 BC- 1600 BC).

While there are no surviving water clocks from the


Mesopotamian region, most evidence of their existence
comes from writings on clay tablets. In these tablets,
water clocks are used in reference to payment of the night
and day watches (guards).

These clocks were unique, as they did not have an


indicator such as hands (as are typically used today) or
grooved notches (as were used in Egypt). Instead, these
clocks measured time "by the weight of water flowing
from" it. The volume was measured in capacity units
Figure 3: Chinese Water clock model
called qa. The weight, mana (the Greek unit for about one
pound), is the weight of water in a water clock. China
The water-powered mechanism of Su Song's
It is important to note that during Babylonian times, astronomical clock tower, featuring a clepsydra tank,
time was measured with temporal hours. So, as seasons waterwheel, escapement mechanism, and chain drive to
changed, so did the length of a day. "To define the length power an armillary sphere and 113 striking clock jacks to
of a 'night watch' at the summer solstice, one had to pour sound the hours and to display informative plaques
two mana of water into a cylindrical clepsydra; its In China, as well as throughout eastern Asia, water
emptying indicated the end of the watch. One-sixth of a clocks were very important in the study of astronomy
mana had to be added each succeeding half-month. and astrology. The oldest reference dates the use of the
At equinox, three mana had to be emptied in order to water-clock in China to the 6th century BC. From about
correspond to one watch, and four mana were emptied 200 BC onwards, the outflow clepsydra was replaced
for each watch of the winter solstitial night." almost everywhere in China by the inflow type with an
indicator-rod borne on a float.

10 Issue Nr 3 2015 BLAISE PASCAL MAGAZINE


THE WATER CLOCK PAGE 3

Huan Tan (40 BCE 30 CE), a Secretary at the Court in The water clock created by Su Song in 1088 was one of
charge of clepsydrae, wrote that he had to compare the most important and desired inventions of its time.It
clepsydrae with sundials because of how temperature took Su Song approximately 12 years to build an
and humidity affected their accuracy, demonstrating amazingly detailed water clock. Those 12 years that Su
that the effects of evaporation, as well as of temperature Song was building his water clock he also took the time
on the speed at which water flows, were known at this to draw out plans to build his magnificent clock. His
time. In 976, Zhang Sixun addressed the problem of the clock was a very complicated thing. It included 117
water in clepsydrae freezing in cold weather by using manikins that came out of the tower every hour on the
liquid mercury instead. Again, instead of using water, hour and banged gongs and rang bells or carried a tablet
the early Ming Dynasty engineer Zhan Xiyuan (c. 1360- that said the hour. It was powered by an 11 foot water
1380) created a sand-driven wheel clock, improved wheel with 36 buckets of water mounted on its
upon by Zhou Shuxue (c. 1530-1558). perimeter.

The use of clepsydrae to drive mechanisms illustrating The clock's water wheel only turned 100 times a day and
astronomical phenomena began with Zhang Heng (78- was able to keep time relatively accurately. Su Song's
139) in 117, who also employed a waterwheel. clock not only kept time but allowed people to observe
Zhang Heng was the first in China to add an extra constellations that were important to Chinese astrology.
compensating tank between the reservoir and the inflow
vessel, which solved the problem of the falling pressure Su Song built his astronomical water clock in 1088, and
head in the reservoir tank. Zhang's ingenuity led to the for 79 years the amazingly complicated clock stood in
creation by Yi Xing (683727) and Liang Lingzan in 725 the capital. One day the Jin army came and
of a clock driven by a waterwheel linkwork escapement disassembled the clock and brought the pieces to their
mechanism. capital which is modern day Beijing .

The same mechanism would be used by Su Song They weren't able to rebuild it because of the complexity
(10201101) in 1088 to power his astronomical clock of the clock. They might have been able to manage if
tower, as well as a chain drive. Su Song's clock tower, they had taken the plans with them. The fact that it was
over 30 feet (9.1 m) tall, possessed a bronze power- stolen suggests that it was a very important invention
driven armillary sphere for observations, an and since it was impossibleto be rebuilt it must have
automatically rotating celestial globe, and five front been extremely complicated.
panels with doors that permitted the viewing of
changing mannequins which rang bells or gongs, and People today have tried to rebuild the water clock but
held tablets indicating the hour or other special times of the best replica we currently have is about five feet tall
the day. and doesn't actually keep time

Today, in Beijing's Drum Tower an outflow clepsydra is


operational and displayed for tourists. It is connected to
automata so that every quarter-hour a small brass statue
of a man claps his cymbals.

Figure 4: The water-powered mechanism plans Figure 5: The Chinese Hydraulic Water clock
of Su Song's astronomical clock tower, created by Su Song

Issue Nr 3 2015 BLAISE PASCAL MAGAZINE 11


THE WATER CLOCK PAGE 4
Greco-Roman world
An early 19th-century illustration of Ctesibius's (285222
BC) clepsydra from the 3rd century BC. The hour
indicator ascends as water flows in. Also, a series of gears
rotate a cylinder to correspond to the temporal hours.
In Greece, a water clock was known as a clepsydra (water
thief). The Greeks considerably advanced the water clock
by tackling the problem of the diminishing flow.
They introduced several types of the inflow
clepsydra, one of which included the earliest feedback
control system. Ctesibius invented an indicator system
typical for later clocks such as the dial and pointer.
The Roman engineer Vitruvius described early alarm
clocks, working with gongs or trumpets. A commonly
used water clock was the simple outflow clepsydra.
This small earthenware vessel had a hole in its side near
the base. In both Greek and Roman times, this type of
clepsydra was used in courts for allocating periods of
time to speakers. In important cases, when a person's life
was at stake for example, it was filled. But, for more
minor cases, it was only partially filled. If proceedings
were interrupted for any reason, such as to examine
documents, the hole in the clepsydra was stopped with
wax until the speaker was able to resume his pleading.
Figure 6: An early 19th-century illustration
In the 4th century BC, the clepsydra is known to have of Ctesibius's (285222 BC) clepsydra from the
been used as a stop-watch for imposing a time limit on 3rd century BCE. The hour indicator ascends as water
clients' visits in Athenian brothels. flows in. Also, a series of gears rotate a cylinder to
Slightly later, in the early 3rd century BCE, the correspond to the temporal hours
Hellenistic physician Herophilos employed a portable
clepsydra on his house visits in Alexandria for
measuring his patients' pulse-beats. By comparing the
rate by age group with empirically obtained data sets, he
was able to determine the intensity of the disorder.

Between 270 BCE and 500 CE, Hellenistic (Ctesibius, Hero


of Alexandria, Archimedes) and Roman horologists and
astronomers were developing more elaborate
mechanized water clocks. The added complexity was
aimed at regulating the flow and at providing fancier
displays of the passage of time.
For example, some water clocks rang bells and gongs,
while others opened doors and windows to show
figurines of people, or moved pointers, and dials.
Some even displayed astrological models of the universe.
The 3rd century BCE engineer Philo of Byzantium
referred in his works to water clocks already fitted with
an escapement mechanism, the earliest known
of its kind.
The biggest achievement of the invention of clepsydrae
during this time, however, was by Ctesibius with his
incorporation of gears and a dial indicator to
Figure 7: Al-Jazari's elephant water clock - 1206
automatically show the time as the lengths of the days
changed throughout the year, because of the temporal Medieval Islamic world
timekeeping used during his day. In the medieval Islamic world (632-1280), the use of
water clocks has its roots from Archimedes during the
Also, a Greek astronomer, Andronicus of Cyrrhus, rise of Alexandria in Egypt and continues on through
supervised the construction of his Horologion, known Byzantium. The water clocks by Persian engineer
today as the Tower of the Winds, in the Athens Al-Jazari, however, are credited for going "well beyond
marketplace (or agora) in the first half of the 1st century anything" that had preceded them.
BC. This octagonal clocktower showed scholars and In al-Jazari's 1206 treatise, he describes one of his water
shoppers both sundials and mechanical hour indicators. clocks, the elephant clock. The clock recorded the
It featured a 24-hour mechanized clepsydra and passage of temporal hours, which meant that the rate of
indicators for the eight winds from which the tower got flow had to be changed daily to match the uneven
its name, and it displayed the seasons of the year and length of days throughout the year.
astrological dates and periods.

12 Issue Nr 3 2015 BLAISE PASCAL MAGAZINE


THE WATER CLOCK PAGE 5 - END

To accomplish this, the clock had two tanks, the top tank The first water clocks to employ complex segmental and
was connected to the time indicating mechanisms and the epicyclic gearing was invented earlier by the Arab
bottom was connected to the flow control regulator. engineer Ibn Khalaf al-Muradi in Islamic Iberia c. 1000.
Basically, at daybreak the tap was opened and water
flowed from the top tank to the bottom tank via a float His water clocks were driven by water wheels, as was
regulator that maintained a constant pressure in the also the case for several Chinese water clocks in the 11th
receiving tank. century. Comparable water clocks were built in
Damascus and Fez. The latter (Dar al-Magana) remains
until today and its mechanism has been reconstructed.

The first European clock to employ these complex gears


was the astronomical clock created by Giovanni de
Dondi in c. 1365.
Like the Chinese, Arab engineers at the time also
developed an escapement mechanism which they
employed in some of their water clocks.
The escapement mechanism was in the form of a
constant-head system, while heavy floats were used as
weights.

Figure 8:Water-powered automatic castle clock


of Al-Jazari, 12th century.
The most sophisticated water-powered astronomical
clock was Al-Jazari's castle clock, considered by some to
be an early example of a programmable analog
computer, in 1206
Figure 9: An incomplete scaled-down model of Jang
It was a complex device that was about 11 feet (3.4 m) Yeong-sil's self-striking water clock
high, and had multiple functions alongside timekeeping. Korea
It included a display of the zodiac - In 1434 during the Choson (or Joseon) Dynasty, Chang
(In both astrology and historical astronomy, the zodiacis a circle Yongsil (or Jang Young Sil), Palace Guard
of twelve 30 divisions of celestial longitude that are centered and later Chief Court Engineer, constructed the
upon the ecliptic, the apparent path of the Sun across the Jagyeongnu (self-striking water clock or striking clepsydra)
celestial sphere over the course of the year.)- for King Sejong.
and the solar and lunar orbits, and a pointer in the shape What made the Jagyeongnu self-striking (or automatic)
of the crescent moon which traveled across the top of a was the use of jack-work mechanisms,
gateway, moved by a hidden cart and causing automatic by which three wooden figures (jacks) struck objects to
doors to open, each revealing a mannequin, every hour. signal the time.
This innovation no longer required the reliance of human
It was possible to re-program the length of day and night workers, known as "rooster men",
in order to account for the changing lengths of day and to constantly replenish it.
night throughout the year, and it also featured five By 1554, the water clock spread from Korea to Japan.
musician automata who automatically play music when Water clocks were used and improved upon throughout
moved by levers operated by a hidden camshaft attached Asia well into the 15th century.
to a water wheel.

Other components of the castle clock included a main


reservoir with a float, a float chamber and flow regulator,
plate and valve trough, two pulleys, crescent disc
displaying the zodiac, and two falcon automata dropping
balls into vases.

Issue Nr 3 2015 BLAISE PASCAL MAGAZINE 13


NEW LAZARUS VERSION 1.4 FPC 2.6.4 (PAGE 1)

starter expert

A new version of Lazarus has been


released. It's a major step in
programming with Lazarus...
The IDE has been updated very much so
here is a list of items that helps you
understand what changed

LCL Changes
Added methods and utilities to load objects from FPC resources
Changed all LCL resources from LRS to RES.
As a result they can be edited in executables using resource editors on Windows platform.
DBImage changes versus 1.2:
DbImage implements loading stream directly if it doesn't have a known header,
WriteHeader property. This makes writing image header optional improving compatibility
with Delphi controls
Translations unit: SetFuzzy (boolean with default value false) parameter was added
to TPOFile.Add method. It allows to mark PO entry as fuzzy (when true).
TDateTimePicker and TDBDateTimePicker components were added.
They are Delphi compatible and are installed by default, but have their own package
instead of being part of LCL.
TComboBoxEx and TCheckComboBox components were added.
They are Delphi compatible and are part of LCL.

IDE Interfaces Changes


Easier hint windows in IdeIntf
Unit IdeHelpIntf has a new THintWindowManager class.
In Lazarus IDE, the external renderer now is the TurboPowerIProDsgn package. This refactoring
was related to fixing a long time nasty crash bug which prevented including
TurboPowerIProDsgn by default in BigIde. Now it is included.
IDE Changes
Resources
All IDE resources are stored in RES files now.
Component images can be loaded from resources stored in RES files now.
LazRes and LrsToLfm have been improved to assist in resource migration process.
Project options has a 'Resource' section which allows to add any user resources which are
stored in the project .RES file together with project icon, manifest and version information.
Project version information was extended to allow saving build-related attribute information.
Editor
Auto-Indent now supports "tab only". If a new line is started, the indent of the previous line is
recreated using tabs. And then either spaces or cut off.
Keyword-pair/triplet highlight (matching begin,end) can be turned on/off for each pair/triplet
Keyword-pair/triplet supports if/then/else
Compiler message marks. Each compiler message shows an icon in the left gutter,
a wavy underline in the text and a mark on the right gutter.
You can right click on the left icon to get context actions, e.g. Quick Fixes.
Refactoring tool Show Abstract Methods now supports class interfaces.
Identifier completion box (Ctrl+Space) has now options to disable sorting for scope
~

=







K
If you disable both the list is sorted alphabetically.

14 Issue Nr 3 2015 BLAISE PASCAL MAGAZINE


NEW LAZARUS VERSION 1.4 FPC 2.6.4 (PAGE 2)
Debugger
Attach: List available processes (Windows/Linux/OSX only)
Debug-Inspector: Select member values (class/record/(dyn)array/pointer-deref) by double click on row
Allow single step from exception to except/finally block (includes stepping through implicit
except blocks, at then end of some methods).
Also fix single step now works, if it steps over an ignored exception.
BETA: Alternative debugger fpdebug [blog]
Designer
New Undo feature supports component moving, resizing and deleting.
Also property changed by Object Inspector can be undone.
Component palette
Palette is fully configurable. Pages can be added, pages and components can be reordered and
components can be moved between pages freely. The difference between default layout and user
layout is stored in environment options. Layouts can be exported/imported, too.
Help
The Help/Help menu starts the lhelp help viewer and shows all CHM files (if present and
configured, as done by e.g. the Windows installer)
lhelp supports diagnostic output to a log file (using e.g. --debug-log=)
Project
The target processor option (don't confuse this with the target CPU) now uses the -Cp option
instead of the -Op option. This is required by ARM processors.
The compiler in the project's compiler options is used to setup codetools. Formerly the IDE asked
the default compiler from the Tools / Options using the project's target OS and CPU.
That means you can now use build modes to switch between different compiler versions
and code navigation will use the right settings.
When target OS/CPU are not given (aka empty or default) the IDE/lazbuild now queries
the compiler for its default target platform. In other words: For cross compiling it is now enough
to select the compiler. Formerly the defaults were taken from the IDE's OS/CPU.
The IDE now parses the -Xp and -V options in the custom compiler options to find
the right compiler.
Project Option "Main unit has uses section containing all units of project":
Formerly the main source was only updated when it was enabled. Now: Only additions are
affected by this option. Renames and removes always updates the main source if it is Pascal.
Under Unix/Linux the IDE now opens always a project (lpi) in its physical path
(i.e. all symlinks are resolved). This is compatible with FPC,
which always uses the physical directory as working directory.
Project Inspector:
The Inspector now supports multi selection. For example delete multiple files or set properties.
You can drag files from other applications and drop them on Inspector to add files to the project.
You can drag files to other directories (drag the files onto a file or directory in the package editor).
New menu item: Move/copy selected files to a directory. This makes it easy to split a big
package into several sub directories.
Packages
Packages now use by default the project compiler. This comes from a change to the macro
$(CompPath), which now resolves to the project compiler.
The target processor option now uses the -Cp option instead of the -Op option.
this is required by ARM processors.
Under Unix the IDE now opens always a package (lpk) in its physical path
(i.e. all symlinks resolved). This is compatible with fpc, which always uses the
physical directory as working directory.
Package Editor:
The package editor now supports multi selection. For example delete multiple
files or set properties.
You can now copy or move files via drag and drop between package editors and
to/from project inspector.
You can drag files from other applications and drop them on package editors to add
files to the package.
You can drag files to other directories (drag the files onto a file or directory in the package editor).
New menu item: Move/copy selected files to a directory. This makes it easy to split a big
package into several sub directories.
Packages are now compiled in parallel. See:
http://wiki.freepascal.org/Lazarus_Packages#Parallel_Compilation

Issue Nr 3 2015 BLAISE PASCAL MAGAZINE 15


NEW LAZARUS VERSION 1.4 FPC 2.6.4 (PAGE 3)
Messages
This window was completely rewritten. Every tool output is now clearly separated
by a header line.
Each external tool (e.g. the compiler) now runs in a thread.
The same for the biggest part of the parser.
The IDE now passes -vbq (message ids and full file names) to the compiler.
The window can handle much bigger outputs, so you can compile with more verbosity
The IDE now knows where each message came from and what tool has created them.
For example you can see the reason why a package was compiled and the parameters via the
"About" menu item.
Switch between English and translation at any time
The fpc message parser uses the message ids, the errore.msg file and the translated error*.msg file.
The -Fr compiler option (pass custom fpc message file) on Project/Package Options / Compiler
Options / Messages was removed.
Search in Messages window
Filter for message types and urgency (hint, note, warn ...)
Choose file names styles (short, relative, full)
Change the colors in Tools / Options / Messages
when you insert/delete text in the source editor the messages are now updated in line *and*
column number.
The Compile Info window was removed because it had no maintainer.
This includes the options "Show compile dialog" and "Autoclose compile dialog".
Quick fix for fpc message "No implementation for interface method found",
shows abstract methods dialog
Miscellaneous
new IDE macros $(LazVer) and $(FPC_FULLVERSION), see
http://wiki.freepascal.org/IDE_Macros_in_paths_and_filenames
Compiling for x86-64 Linux, *BSD and Solaris now passes -Cg (Generate PIC) code to the compiler.
Use -Cg- if you don't want that.
Plugins
Leakview now supports GDB stack traces without source file names and only mangled identifiers.
TurboPowerIProDsgn is installed by default (is part of BigIde). It means all hints and info panels
have a nice HTML formatting with colors and links for sources. The package could be installed
also earlier for the same effect but there was a crash bug which prevented it from being
included by default.
EditorToolbar is installed by default (is part of BigIde). It can be hidden from View menu,
thus uninstall is not necessarily needed for anyone. Lots of bugs in EditorToolbar were fixed.
Components
TOpenGLControl: works now under Linux/QT/X

Changes affecting compatibility


LCL incompatibilities
TEditButton was rewritten.
TEditButton has been redesigned as a "grouped control".
It now inherits from a different ancestor (it is no longer derived from TCustomMaskEdit).
The new design now properly aligns and anchors.
Effects:
Derived components can no longer override (or use) all of the (protected) methods of
TCustomMaskEdit. Most of the commonly used methods have been propagated
with new names reflecting the new design e.g.:
Change -> EditChange
DblClick-> EditDblClick
When the component has focus, then checking if (ActiveControl is TButtonEdit) will be False,
since ActiveControl in this case is the internal edit control (of type TEbEdit).
(Code that checks for (ActiveControl is TCustom(Mask)Edit) will still evaluate to True.)

16
6 Issue Nr 3 2015 BLAISE PASCAL MAGAZINE
NEW LAZARUS VERSION 1.4 FPC 2.6.4 (PAGE 4 - end)
Remedy:
Use EditChange, EditDblClick etc. in derived controls.
Use (ActiveControl.Parent is TEditButton), or alternatively (ActiveControl is TEbEdit)
TControl.GetChildsRect renamed to TControl.GetChildrenRect
Effect: compile error: There is no method in an ancestor class to be overridden:
"TYourControl.GetChildsRect(Boolean): <record type>;"
Reason: Incorrect English
Remedy: use GetChildrenRect instead. TControl.GetChildsRect renamed to
TControlScrollBar.AutoCalcRange was removed
Effect: compiler error: There is no method in an ancestor class to be overridden:
"TYourControlScrollBar.AutoCalcRange;"
Reason: Code for calculating the AutoScroll ranges was moved from the two scrollbars
to the new proteced method TScrollingWinControl.CalculateAutoRanges.
This prevents an endless loop when the two scrollbars depend on each other, it simplifies
the code and reduces some overhead.
Remedy: Override TYourControl.CalculateAutoRanges instead.
TDateEdit.DialogTitle property was removed
Effect: Compile error if the property was set in code. LFM loader removes it from form files.
Reason: Unused and thus misleading and confusing.
Remedy: Remove it from your code.
TMemo, TTextStrings and TCustomStringGrid: changed behaviour of
LoadFromFile/SaveToFile
Effect: LoadFromFile/SaveToFile respectively LoadFromCSVFile/SaveToCSVFile now
take strings in UTF8-encoding (was: system encoding) as their parameter.
Reason: LCL uses UTF8 internally; consistency with e.g. TSynEdit.
Remedy: do not use Utf8ToSys() anymore in the calls to these procedures.

IDE incompatibilities
Changed parameters
LazarusIDE.DoJumpToCompilerMessage: changed Line integer to TMessageLine
CompilerOptions.ShowAllProcsOnError: was removed, option -vb is now always passed
CompilerOptions.ShowNothing: was removed, not needed
LazarusHelp.ShowHelpForMessage: removed parameter Line.
The IDE now always shows helpfor the currently selected line.
IDE Macro CompPath
Effect: IDE uses the project compiler instead of the default compiler set in the IDE options.
Reason: The macro $(CompPath) now resolves to the project compiler.
Packages are now compiled with the project compiler.
Remedy: Use $CompPath(IDE)
Old IDE does not reopen first file when opening a project
Effect: When a project saved with a new IDE (1.3+) is opened with an old IDE (e.g. 1.2),
the first file in the source editor is not reopened automatically.
Reason: The default value for Editor position is now "0", which is not stored in the lpi,
creating smaller lpi files.
Remedy: Open the file manually.
IDE does not show Compile Dialog
Effect: Compile Info window does not appear when compiling.
Options Show compile dialog and Autoclose compile dialog are missing.
Reason: The code that runs the compiler was completely rewritten. The dialog needed a
big rewrite too, but it had no maintainer.
Remedy: You can abort a compile via Run / Abort Build or its shortcut Ctrl+Alt+Shift+G.
You can change the shortcut in Tools / Options / Editor / Key Mappings.
The number of errors, warnings and hints are shown in the Messages window.
IDE/lazbuild cross compiles for different target OS/CPU
Effect: When the target OS/CPU are not set (i.e. empty or default) the 1.4 IDE/lazbuild
compile for a different target than Lazarus 1.2.
Reason: Formerly the IDE took as default its own OS/CPU. Now it queries the project's
compiler and uses its default target OS/CPU.
Remedy: Specify target OS/CPU in Project options / Compiler options or for compiling
the IDE set Tools / Configure build Lazarus.

Issue Nr 3 2015 BLAISE PASCAL MAGAZINE 17


BY ANDREA RAIMONDI
SECURITY IN APPLICATIONS: PASSWORD HANDLING (PAGE 1)
Introduction Clearly, as this preference emerged,
Have you ever had to create a registration guess who started having a closer look at it to
module with a login dialog? Of course you check whether there were pitfalls? That's right, the
have - every programmer I know of has had bad guys and also researchers. Someone found a
to. Especially if you are a beginner, it is not really bad hole in the function, so its use has been
obvious what is the best way to go about discouraged over time, just as the DES became
creating such a dialog. In times past you useless as more computing power was added to
might have got away with use of a plain text personal
password. The Data Encryption Standard (DES, was
computers.
Today, though, I see tutorials which start by once a predominant symmetric-key algorithm
explaining that plain text passwords are a for the encryption of electronic data
bad idea. There is a good reason for this
advice: users tend to re-use their passwords When researchers realised that Md5 totally
for multiple websites/applications. So if a insecure, a new family of hash functions emerged
username-password combination is exposed,
called SHA. There are several of these, some of
it could very well be useful across a number
which have already been broken. The one regarded
of sites.
most highly right now is SHA256. So that's the one
you should use in new projects.
Convenience trumps security, again The MD5 message-digest algorithm is a widely used
As the section header states, convenience trumps cryptographic hash function producing a 128-bit (16-byte)
security every time. Users reuse account names and hash value, typically expressed in text format as a 32 digit
passwords because it is convenient. Also, users are hexadecimal number. MD5 has been utilized in a wide
not generally very good at coming up with new variety of cryptographic applications, and is also commonly
passwords, let alone remembering them which used to verify data integrity.
makes our job much harder. Hence, we really have
to do two things: So, is that it? Is it enough to use that and feel you
Ensure that the password cannot have completed your job well? Well No, because
be guessed easily you are doing only one iteration using it. That's not
Help the user find a good password good. You should be doing at least a hundred
and stick with it iterations, using the previous output as input.
Since you are the developer, it is your job to ensure Better yet, get the previous output, encode it in
the password cannot easily be broken. Even if the some way (say, MIME or BASE64) and then hash it
user chooses goofy as their password, it is your again. I know the registration and login processes
job to ensure that this password takes a long time to will be slower this way, but does anybody really
be discovered. It's not the user's job but your job care? I mean, computers are very fast, even the
and yours alone. Please note that a password will ones providing cheap hosting. Another second is
eventually be discovered, the real issue here is how not going to be a noticeable problem.
long it takes before that happens.
So, after that, are we finished? Well No, because
Make it harder on the bad guys your job is also to suggest to users the kind of
If you have ever built anything exposed on the passwords they will need to use. To this end, I see
internet which needs user registration (whether a lots of people repeating the common clich: short
3-tier application or a website), you will be familiar passwords are easily broken, so you should have a
with the concept of hashing. You will also have minimum length limit. You have no idea how
been told that not all hashes are born equal and angry I get at this bad advice. First of all, bad guys
there's a thing called a cryptographic hash. This love a minimum limit. They really do, because it
kind of hash function has the property that by chops off an entire dataset of millions of potential
flipping one bit, you make the end result matches with lengths less than the minimum and
completely different. This is called an avalanche they can therefore optimise their search. By the
effect because it's like an avalanche, every small way, this is how the British broke Enigma, because
difference increases over successive iterations the Germans started to put limits to the frequency
ultimately producing an enormous change. of the starting and ending letters with the intent of
If you have been involved with websites in the making the detection harder.
past, your first port of call would've been MD5 for It backfired though, because after they put limits,
this. Of course it would, because it was fast and some codes started reappearing, which greatly
supposedly secure. helped the British to break it. We really never learn
anything from history, do we?

18 Issue Nr 3 2015 BLAISE PASCAL MAGAZINE


SECURITY IN APPLICATIONS: PASSWORD HANDLING (PAGE 2)

The second thing is that, luckily, a good majority of Or the password could be, more
passwords input by users are 6 characters or longer. viciously, 57tony19. It still contains exactly the
So, why is it so easy to break them? Well, part of same information and it's just as easy to remember,
this is that many website developers think they will but it is a lot harder to guess because it does not fit
be smarter than the bad guys and so do not hash the statistical expectation. Hence, if you give this
their passwords. They think they can get away kind of advice on your website, you can be sure
simply with encryption so that the password that your users will find ways to comply with your
cannot then be recovered. suggestions while still remembering the password
The reason this is bad is that a lot of the time easily. Plus, if this is the advice you keep giving,
they do not just commit the cardinal sin of the bad guys will have to assume that all bets are
encrypting them, they even use very poor off, and they do not like that.
encryption methods which are vulnerable to The password you hash does not have
frequency analysis or other statistical analysis. to be the same one as was entered
Again, this happens because of a desire not to delay If the previous section made you think, this will
login times, which really should not be an issue make you think even more. Why on Earth should
since login and registrations are done fairly you be using the same password they give you to
infrequently compared to other operations. hash? Just use another one, derived from that. This
Frequency analysis is called password mangling and it can be as
Frequency what? Consider this text: it is written simple as counting the characters of a string and
in English and is fairly long. If you count the letters, ordering them by frequency. So let's say the
you will notice that some appear more frequently password is tony1957: you will end up with
than others. Hence, if you know a text has been 1t1o1n1y11191517. Now, that's a password! See?
written in English and you use a bad encryption Even with a bad password, you still can get a
algorithm, some bytes will appear more frequently good password. And good luck with the statistics
than others. By matching the two pieces of on that! If you want to be particularly devious, you
information, you can reasonably assume that a can have a password strength evaluator which
certain byte corresponds to a certain letter. This is highlights the bad passwords leaving the good
how you break a Caesar cipher. The Vigenre ones alone. Also, along with this, you can also use
cipher is very similar, except that you have to do it some padding characters, which will be part of the
in a multi-step fashion. string being hashed and the malicious user will be
left wondering whether the user or the system
Statistics inputted them, making the password ultimately
Another tool used by malicious users is statistics. useless.
When they break a certain set of passwords, they
will update a count in their dictionaries, so they When the going gets tough, the tough
know which are more likely to appear than others. get going
This is another very useful tool allowing them to If you feel that the above is only a good start, you
optimise away another swathe of potential can also go overboard and be really mean. One of
passwords. They know, for example, that if the the techniques I devised (but never found a reason
username is Tony there are good chances the to use, because I don't deal with stuff requiring this
password could have 2 to 4 digits representing the level of security) is the multi-password: who says
year of birth. All of this information is fed to their that you always have to use the same password
tools to weed out bad candidates and speed up the field? Why can't you use one, say, from Monday to
recovery of passwords. Wednesday and another, with a different hash, from
Thursday to Sunday?
You can use statistics against them, When the cracker downloads the MySQL dump
though he'll find out there are two password fields hang
Oh yes! You can and you should. Say that we have on, is the password split in two fields or are they
a user Tony with password tony1957. That different encodings of the same password?
password will be found in less than 2 hours, But I would only use this if I wanted to make the
granted. Do not even doubt that. Can we make that poor malicious user cry really hard.
password more secure without radically changing
it? Oh yes, we can. What if the password was How nasty you are is only up to you
tony5719? Now, there are still four digits, but it's I am sure that I have now inspired you to think of
not a year anymore and the malicious code ideas that can work, provided that you make a plan
dealing with the year will fail, because it does not and discuss it with both your peers and your
account for that kind of thing. seniors, because it's easy to get these things wrong

Issue Nr 3 2015 BLAISE PASCAL MAGAZINE 19


SECURITY IN APPLICATIONS: PASSWORD HANDLING (PAGE 3)

and do something stupid instead of clever. What I Darn! Save them in encrypted files on
am suggesting here is not that you should not be another machine and only make them accessible
clever, I am suggesting that you should be clever in through a script which only accepts requests from
things that do not affect the cryptographic the machine where your website is located and
properties of the hash and that is where people possibly use a really long and nasty password in
usually get it wrong because they try to be cleverer the script to decrypt them.
than the researchers that do this for a living. News
flash: you are not. What if we didn't need passwords at all?
We have been discussing passwords, but really do
Not all passwords can be hashed we really need them? Have you ever heard of
If your password cannot be hashed, you are in alternatives to passwords? And I am not talking
trouble. What do you mean some password can't be about biometric stuff. That does not work unless it's
hashed? Well, for example, say that you have to done properly, which means with devices that cost
automatically log in to a site and need to save the thousands of dollars, specific usage protocols and
credentials: that account needs to be saved trained personnel. What I am talking about is the
somewhere because you need it for the function to use of images. Say that you are creating a website
work. This is one of those cases where, as a and that you have 1 or 2 million small images on
developer, you have to assert security over the server. Now, say that when you register a user,
convenience: you simply do not store them. You you show 10 of those images and save which ones
ask them every time. There is no other secure way, you were showing on the user profile. Now, let's
because malicious users can't reach what you do also say that you associate each image to a piece of
not store. If you have an E-Commerce website and text which defies statistics and then allow the user
you store credit card numbers, you should stop. to pick 4 out of 10 in a certain order. They have to
Just don't. You are not PayPal and will never be: use that order again when they log in. I argue that
stop being delusional. With an ever increasing in such a case, if the images shown are truly
number of bad attacks on really well known random, you might not even really need a
websites, it is dawning on users that convenience username because the chances that two users get
sometimes is a bad thing. We are not yet where we the same image set and pick the same 4 in the same
should be, but the time will come when they realise order are minimal. The system is also easy to
it and will start requesting it. If you were able to expand if needed by simply adding new images.
explain why you made this choice, they will They do not even need to be really big, I'd say that
certainly trust you more than the other guy who a 1000x1000 pixel image should be more than
says they can store their credit card number enough, therefore limiting the amount of hard disk
securely. space needed.

You have to give up features you cannot


implement
Obviously, the choice of not storing some
passwords comes at the expense of features that
would of course be possible if you did. It takes a lot
of guts and courage to stand up and say We can't Creating of a password:
do this because it is dangerous for our users and Alan Turing June 23, 1912
eventually our reputation when we get hacked but AlanTuring 23 6 1912
it has to be done sometimes. But what if you are 23 Alan 6 Turing 1912
told to go on and save the data anyway? @23Alan6TuringMCMXII
If you really, really, really have to, there are some @23Alan6TuringMcmX2!
things you can actually do @23A6TMcmX2!
These are suggestions that you should only use if
you are being ordered to do this and if you have no
other choice. For a start, do not save them in the
database. If the cracker gets in and gets a dump, he
or she will go straight to the accounts table and try
to get the passwords encrypted there, only to find
out they actually aren't there at all.

20 Issue Nr 3 2015 BLAISE PASCAL MAGAZINE


SECURITY IN APPLICATIONS: PASSWORD HANDLING (PAGE 4)
Conclusion On the other hand, if you are just doing a
To sum up: security is really about options and trade- to-do list, do you really need high security? Probably
offs. If you decide to do something, not, so maybe it is just worth using a good hash and
then you need to know the consequences of your go with that. It all boils down to what you, your
actions at all times. This is how you make considered employer or your customers stand to lose.
decisions. I know that some of the above suggestions
may sound extreme, or crazy but sometimes being That is where you should draw the line:
creative helps in making problems easier, or removes passwords will be broken, files will be decrypted. A
them altogether. determined individual will eventually get his or her
hands on what he or she should not.
For instance, take the suggestion made above that What matters, though, is the amount of time taken to
you move your encrypted profile information to another do so. If the servers are properly monitored,
machine. Imagine a cracker or black-hat who takes the if it takes the attacker a week to decrypt the data,
time to break into the machine, only to find out that the then that data will most likely be useless,
data he is looking for is not actually there, and he has because the breach will have been detected and
to do it all over again. And then maybe he or she does communicated to customers. That is what you can do
and finds that he has to copy a large swathe of files and there's not much else that matters really.
and, when done, he or she will have to decrypt them
one by one because, of course, a binary append just The Internet is a nasty place, there's nowhere to hide
doesn't work. How do you think he or she will feel? and you can simply go with the flow but be smart
about it.
Yeah, the malicious user may eventually break into
your data. He or she might actually decrypt all of them.
But the time wasted on it won't be recovered. Even if About the author:
using a bunch of machines in the cloud, the time spent Andrea Raimondi,
is not recouped. And then he or she must be lucky and born 1977.
the profiles must be worth it. You understand what I
mean there, Started programming at ripe old age of 14.
don't you? Professionally started in 2000.
Currently consulting with a multinational
company.
Has a great passion for security and how to
outsmart the scum that trawls through the
interwebs. andrea.raimondi@gmail.com

better office benelux | asterlaan 6 5582EH waalre | 040 222 26 43 gtan@better-office.com


What is Visuino?
Visuino is the latest innovative software from Mitov Software. A visual programming
environment allowing you to program your Arduino boards. Although it currently
supports the official Arduino boards, it is not restricted to their support alone and
requests to support new hardware are welcome.

INTRODUCTION What is Arduino?


The components found in the Visuino software represent their hardware components and you
will easily be able to create and design your programs using drag and drop. No equipment or
hardware is needed to run the software in design mode. Once you have completed the design,
you can connect Arduino board upload and run it.

For those people who are not strong on writing code then designing, compiling and creating
Arduino programs has never been easier! Why waste time on creating code when we have done
all the hard work for you already? You have your Arduino board, and great hardware design,
see it running in minutes, not hours!

Currently we are running a Beta program which you can be part of by joining our Google group.
Join the group now to download and test the software or send an email to mitov@mitov.com.

IN THE NEXT ISSUE:


ALL ABOUT VISUINO

www.visuino.com

VISUINO IS THE LATEST INNOVATIVE PRODUCT


FROM MITOV SOFTWARE.
IN REMEMBRANCE OF OF ALAN TURING
Alan Turing was a genius who might have saved
the world....
Founder of computer science, mathematician,
philosopher, codebreaker, visionary and a gay man
before his time:
Here is the statement of apology by the
Prime Minister, Gordon Brown,
10 September 2009: 60 years to late
... a brilliant mathematician... whose unique
contribution helped to turn the tide of war...
horrifying that he was treated so inhumanely...

1912 (23 June): Birth, Paddington, London


1926-31: Sherborne School
1930: Death of friend Christopher Morcom
1931-34: Undergraduate at King's College, Cambridge University
1932-35: Quantum mechanics, probability, logic. Fellow of
King's College, Cambridge
1936: The Turing machine, computability, universal machine
1936-38: Princeton University. Ph.D. Logic, algebra, number theory
1938-39: Return to Cambridge. Introduced to German Enigma
cipher machine
1939-40: The Bombe, machine for Enigma decryption
1939-42: Breaking of U-boat Enigma, saving battle of the Atlantic
1943-45: Chief Anglo-American crypto consultant. Electronic work.
1945: National Physical Laboratory, London
1946: Computer and software design leading the world.
1947-48: Programming, neural nets, and artificial intelligence
1948: Manchester University, first serious mathematical use
of a computer
1950: The Turing Test for machine intelligence
1951: Elected FRS. Non-linear theory of biological growth
1952: Arrested as a homosexual, loss of security clearance
1953-54: Unfinished work in biology and physics
1954 (7 June): Death (suicide) by cyanide poisoning,
Wilmslow, Cheshire.

Issue Nr 3 2015 BLAISE PASCAL MAGAZINE 23


INFORMATION ABOUT: ENIGMA STEALING SECRETS

Arthur Scherbius, a German engineer, An Enigma machine was any of


developed his 'Enigma' machine, capable of several electro-mechanical rotor
transcribing coded information, in the hope of cipher machines used in the
interesting commercial companies in secure twentieth century for
communications. In 1923 he set up his enciphering and deciphering
Chiffriermaschinen Aktiengesellschaft secret messages.
(Cipher Machines Corporation) in Berlin to Enigma was invented by
manufacture his product. Within three years the the German engineer
German navy was producing its own version, Arthur Scherbius
followed by the army in 1928 and the air force in at the end of World War
1933. I. Early models were
used commercially
Enigma allowed an operator to type in a from the early 1920s,
message, then scramble it by using three to five and adopted by military
notched wheels, or rotors, which displayed and government services of
different letters of the alphabet. The receiver several countries, most notably Nazi Germany
needed to know the exact settings of these rotors before and during World War II. Several different
in order to reconstitute the coded text. Over the Enigma models were produced, but the German
years the basic machine became more military models are the most commonly
complicated as German code experts added plugs recognised.
with electronic circuits.
Britain and her allies first understood the German military messages enciphered on the
problems posed by this machine in 1931, when Enigma machine were first broken by the Polish
Hans Thilo Schmidt, a German spy, allowed his Cipher Bureau, beginning in December 1932.
French spymasters to photograph stolen Enigma This success was a result of efforts by three
operating manuals. Initially, however, neither Polish cryptologists, Marian Rejewski, Jerzy
French nor British cryptanalysts could make Rycki and Henryk Zygalski, working for Polish
headway in breaking the Enigma cipher. military intelligence. Rejewski reverse-
It was only after they had handed over details to engineered the device, using theoretical
the Polish Cipher Bureau that progress was made. mathematics and material supplied by French
Helped by its closer links to the German military intelligence. Subsequently the three
engineering industry, the Poles managed to mathematicians designed mechanical devices for
reconstruct an Enigma machine, complete with breaking Enigma ciphers, including the
internal wiring, to read the German forces cryptologic bomb.
messages between 1933 and 1938. From 1938 onwards, additional complexity was
repeatedly added to the Enigma machines,
Ultra intelligence making decryption more difficult and requiring
With German invasion imminent in 1939, the further equipment and personnelmore than the
Poles opted to share their secrets with the British, Poles could readily produce.
and Britain's Government Code and Cipher School
(GC&CS) at Bletchley Park, Buckinghamshire, On 25 July 1939, in Warsaw, the Poles initiated
became the centre for Allied efforts to keep up French and British military intelligence
with dramatic war-induced changes in Enigma representatives into their Enigma-decryption
output. techniques and equipment, including Zygalski
sheets and the cryptologic bomb, and
Top mathematicians and general problem-solvers promised each delegation a Polish-reconstructed
were recruited and a bank of early computers, Enigma. The demonstration represented a vital
known as 'bombes', was built to work out the basis for the later British continuation and effort.
Enigmas vast number of settings. During the war, British cryptologists decrypted a
The Germans were convinced that Enigma output vast number of messages enciphered on Enigma.
could not be broken, so they used the machine The intelligence gleaned from this source,
for all sorts of communications on the battlefield, codenamed "Ultra" by the British, was a
at sea, in the sky and, significantly, within its substantial aid to the Allied war effort.
secret services. The British described any
intelligence gained from Enigma as 'Ultra', and Though Enigma had some cryptographic
considered it top secret. weaknesses, in practice it was German
procedural flaws, operator mistakes, failure to
Only a select few commanders were made aware systematically introduce changes in
of the full significance of Ultra, and used it encipherment procedures, and Allied capture of
sparingly to prevent the Germans realising their key tables and hardware that, during the war,
ciphers had been broken. enabled Allied cryptologists to succeed.

By Andrew Lycett

24 Issue Nr 3 2015 BLAISE PASCAL MAGAZINE


LAZARUS NOW CAN USE GOOGLE APIS PAGE 1

starter expert Version 1.4 smartphones is expected to interact


FP 2.6.4 and up
with online services. To be able to
interact with these web applications and
REST CLIENTS: USING THE this data, APIs are needed. This is increasingly
GOOGLE APIS IN FREE PASCAL done using REST technologies. (REST stands for
CREATED AND WRITTEN BY Representational State Transfer). REST is not a
MICHAL VAN CANNEYT. protocol, rather an architectural way to make data
available on the web. It rests on 2 pillars:
ABSTRACT
This article demonstrates how you can use 1. Everything is a resource, accessible through
the Google APIs to access Google's services. a URI (In computing, a uniform resource
Using the Google APIs presumes use of dentifier (URI) is a string of characters used to
OAuth 2, and the use of REST technologies. identify a name of a resource.) - which almost
The article also shows you how to create implies use of the HTTP protocol. In database
terms one could say that every record of a
your own Google API in Pascal using the
table is accessible through its own URI.
Google Discovery service.
2. Data manipulation follows mostly the CRUD
(Create Read Update Delete) pattern, much as
1 INTRODUCTION data in a database. These operations translate
More and more, applications are connected to the nicely to the HTTP verbs POST, GET, PUT
Web or implemented solely on the web (Facebook, and DELETE.
twitter). Data is fetched from and stored in the web,
even for traditionally desktop oriented software Since all this is very much the technology used in
such as spreadsheets or word-processing software. web browsers, it is easy to understand and use.
Google docs (or Apps) and more recently Microsoft The downside of being an architecture is that there
Office365 are prime examples of this. You could is no strict protocol, so each application developer
even say that these web APIs have become as can develop his own protocol Where XML was the
important as the traditional OS and installed format of choice for messages in SOAP-related
software APIs: Software running on tablets and APIs, for REST APIs this has been replaced
mostlywith data descriptions in JSON format
Figure 1: The Google APIs component palette

Figure 2: The google drive demo program

Issue Nr 3 2015 BLAISE PASCAL MAGAZINE 25


LAZARUS NOW CAN USE GOOGLE APIS PAGE 2

(JavaScript Object Notation), which has several 3. Serialization of objects is done using
advantages over XML: it is less verbose (that applies JSON. Therefore the basic REST object
to the specification as well: the JSON specification fits on contains a JSON serialization
an A4 page), it is less sensitive to whitespace, and it mechanism, based on RTTI.
has some native notion of data types (including
string, boolean, number, object, and array). Last but not The upshot of these architectural decisions is
least, it is a subset of Javascript, and as such can be that there are several classes involved in the
handled natively by any browser. REST implementation:
Obviously all the data on the web needs to be
protected from unauthorized access. This protection TFPWebclient
is increasingly done using OAuth (version 2). (OAuth handles HTTP(S) messages.
is an open standard for authorization. OAuth provides A descendent of this client is needed, which uses a
client applications a 'secure delegated access' to particular TCP/IP suite to actually send the request
server resources on behalf of a resource owner. It specifies and read the response.
a process for resource owners to authorize third-party
A request is represented by a TWebRequest class,
access to their server resources without sharing their
the response will come in the form of a
credentials.
TWebResponse class.
Requests are executed using the ExecuteRequest
OAuth is also implemented using JSON. OAuth in
and ExecuteSignedRequest methods: To each
essence relies on the user giving consent to an
TFPWebclient instance, a TRequestSigner
application (mostly the browser) to use data on his or
her behalf. component can be attached. This component is
All the technologies needed to perform REST allowed to examine the request and response when
operations and OAuth authorization are available in they are sent or received, allowing a request to be
Free Pascal. It was therefore only a matter of time signed (for instance by adding an Authorization header
before a comprehensive set of components became with a Bearer token).
available to easily access web APIs. In this article
TFPOauth2Handler
we'll describe how to access the Google APIs using
REST in Free Pascal and Lazarus, and demonstrate is a class that handles OAuth 2 authentication.
how they can be used in sample applications such Technically, it is a descendent of a
as the Google Drive demo (figure 2) TRequestSigner that will add the OAuth2
header. This class may use the TFPWebclient
Access to Microsoft Office365 is also instance (or a second TFPWebclient instance) to
being worked on, but will be the subject execute token exchange requests as part of the
of a later article. OAuth2 flow.
The class can be used in offline mode (for desktop
2 Architecture apps) as well as in online mode (for web applications).
The Free Pascal implementation of the web APIs
makes several assumptions: TRestObject
1. Transport uses the HTTP(s) protocol. This is the basic object that represents a
Several TCP/IP socket implementations are REST resource. It has 2 important methods:
available (including Synapse, lnet, Indy, the FPC LoadFromJSON and SaveToJSON. These methods
native client). Each developer has his own use the RTTI to create a JSON representation of the
preferred implementation which he uses. object, or to read the object properties from a JSON
So, the REST APIs should work with each of representation. It also has a mechanism to record
those. That means that the HTTP request and which properties have been changed.
response mechanism has been abstracted into We need a mechanism to record property changes
a new class called TFPWebClient. Concrete is needed because many REST APIs allow both PUT
implementations of this abstract class have been and PATCH (or UPDATE) methods. A PUT method
made for Synapse and TFPHTTPClient. generally completely replaces a resource with the
new value specified in the request, whereas
2. Authentication of the HTTP requests happens PATCH modifies the resource by applying the
using OAuth2, but other mechanisms can be changes in the request to the existing resource.
implemented as well. Since the Oauth2
protocol involves exchanging tokens with a
webserver, it needs a HTTPS transport layer
as well.

26 Issue Nr 3 2015 BLAISE PASCAL MAGAZINE


LAZARUS NOW CAN USE GOOGLE APIS PAGE 3

To be able to support PATCH-like functionality, Unfortunately, the EDMX description


a mechanism is needed to record which properties is still in XML (somewhat awkward if
have changed, and to send only these properties in the JSON format is to be used) and not
a request. This mechanism is implemented using as complete as the Google-provided
the property Index mechanism. Each property has descriptions, implying that always a certain (very
a unique index, and a property setter which small) amount of manual interpretation is always
accepts this index (the property getter may or may not required.
use this index). When the property is set, the index
is used to record the change. Since the index is Converters for these two formats have been
unique, the RTTI can then be used to construct a implemented in FPC: The Google Service Discovery
list of property names that were modified. REST description document can be converted
Armed with these objects, a REST API client can be automatically to a complete Object Pascal
made: all that needs to be done is create implementation of a client for the REST APIs.
descendents of TRestObject, and load them There are two programs available to do this:
from a HTTP response.". To modify data on the
REST server, the object's properties are set, and the A command-line program which can be used to
object serialized to JSON. This JSON is then sent to convert a single API to an Object Pascal unit.
the server (with using the appropriate HTTP method) . The REST description can be a file with the
That is basically it. JSON description of the service, or the
program can download a service description
3 Service descriptions from the Google Service Discovery server.
Google has more than 100 APIs, each of which has A GUI program that allows you to browse and
more than 1 resource, containing many data search the Google APIs and convert a selected
structures which can be manipulated. To write API to an Object Pascal unit.
objects and serialization code for each of these The Google service discovery itself is a REST
resources would be a very tedious task indeed. API. So, an Object Pascal implementation can
Luckily, this is not necessary. Google offers a be generated for it, using the command-line
Google Service Discovery service, which returns a program. This has been done, and the Google
JSON document that completely describes each of discovery program was constructed using
its rest-based APIs. The Google Discovery Service this unit.
is described at
https://developers.Google.com The code to convert a Google REST API
/discovery/
description to an Object pascal unit is a component
The service consists of 2 parts: it lists the services (TDiscoveryJSONToPas), which can be descended
offered by Google, and it offers a description of the from and modified if need be. If the choices made
REST API for each of these services. For people
by the author for the generated code are not to your
acquainted with SOAP implementations: this
particular liking, the code can easily be modified to
description is equivalent to the WSDL description
generate different code. It has 3 important methods:
document. It is based on a JSON schema:
LoadFromStream This method can be used to
http://json-schema.org/
load a REST service description from a stream
The REST API description also contains containing valid JSON. There are 2 auxiliary
information on the authorizations needed to use methods LoadFromFile and LoadFromJSON to
the resources in the APIs (the so-called authorization make life easier.
scope). It is very important to be aware of these SaveToStream Calling this will generate
scopes: the user of the API will be asked for the Pascal code, and save the resulting code to a
consent based on these scopes. More on this below. stream. The SaveToFile method will do the same
The Microsoft REST APIs are based on their but save directly to file, and will set the unit name if
OData specification, and OData based services it was not yet set.
have a similar document (the service document),
based on EDMX, (An .edmx file is an XML file that
defines a model that can be used with Entity
Framework. The model is made up of a conceptual
model, a storage model, and the mapping between these
models. An .edmx file also contains information that is
used by the EF Designer to render a model graphically.)
described at: https://www.odata.org/

Issue Nr 3 2015 BLAISE PASCAL MAGAZINE 27


LAZARUS NOW CAN USE GOOGLE APIS PAGE 4

Execute for this service can be found etc.


can be used to generate the code. The This class contains a method called
code is then available in the Source property. ServiceCall which is used by all
resources in the API to execute service
There are some properties that can be used to requests. It will use the client to do the actual
control the code: the parent class name for the HTTP request.
generated resource classes, the unit name to
generate, the depth of code indentation can be set TGoogleResource
and so forth. The component can be found in the For each resource exposed by the service,
googlediscoverytopas unit. A similar a descendant of this class is generated that has
component was made to convert the Microsoft all the methods for that resource, as described
OData service discriptions to Pascal code. in the REST service description.
A small command-line program (googleapiconv) TGoogleResource uses an instance of the
is available that can be used to generate a Pascal TGoogleAPI class to handle all calls to the
source file from a service description file. It can also service. For the Discovery API, there is a single
fetch the description from the Google Discovery resource class TApisResource.
service, based on the name (and version) of the
service, or simply by providing a URL. TGoogleBaseObject
For each data type used in the API,
For example, the following command will generate a descendant of this class is used: it is a
a unit for the Google calendar API : descendant of TBaseObject and handles
googleapiconv -s calendar/v3 -o loading from and saving to JSON. For the
calendar.pp Discovery service, there are 2 main datatypes
The same unit can also be generated using this that descend from this base class:
command: TDirectoryList for the list of services, and
googleapiconv -u TRestDescription which describes a
http://www.googleapis.com/discovery/ REST API. The latter is used to create a
v1/apis/calendar/v3/rest -o Pascal unit.
calendar.pp
The JSON serialization mechanism works with an
4 A service description breakdown object factory. When it needs to create an object of a
Since the Google Discovery service is a service, certain type (the 'kind' in Google parlance), it looks
there is a description of itself in the Google in the factory to see which class must actually be
Discovery service. Therefore, the command-line created. The TGoogleAPI object will register all the
program was used to create a unit classes it uses in the factory. It is possible to
googlediscovery which implements the Google override the classes in the factory, so that when a
Discovery service API. A Google API breaks down class is requested a descendant of the class can be
into 4 parts, all of which have a base class in the returned.
googleservice unit:
TGoogleClient For instance, the TDirectoryList is registered
This is a simple component that handles the with name discovery#directoryList. To
transport and authorization. It uses a have the API return a TMyDirectoryList item
TFPWebClient and a TFPOauth2Handler whenever it requires a discovery#directoryList
to communicate with Google servers. instance, the TMyDirectoryList can simply be
TGoogleAPI registered with the same name:
There is a descendant of this component for TMyDirectoryList.RegisterObject;
each Google service API, which handles all
calls to the service. It uses a TGoogleClient The class must override the RestKindName class
component to handle actual communication. method for this to work correctly:
The code generator creates a descendant of this
class in the unit it creates for the service. Function TMyDirectoryList.RestKindName: string;
begin
For the Google Discovery service, the class is Result:='discovery#directoryList';
called TDiscoveryAPI. This class contains end;
some class methods that expose metadata
about the service. You can find out the base
URL for the service, what authorization scopes
are used, where documentation and icons are

28 Issue Nr 3 2015 BLAISE PASCAL MAGAZINE


LAZARUS NOW CAN USE GOOGLE APIS PAGE 5

The advantage is that this mechanism allows the Then an instance of the Discovery API
programmer to create descendants of the classes in is created, and connected to the client
an API which have customised behaviour and component. The last 2 lines handle
properties, rather than modify the service initialization of the code generator, and update
description unit. When data arrives from the the caption of the Google API. In general the above
server, all private classes will be instantiated mechanism will be the same for all applications that
instead of the declared stock classes. want to communicate with a Google service API.
When the service changes, the API converter The TApisResource class represents the resources
can then regenerate the service description unit, exposed by the discovery API, and was generated
and not all customizations are not all lost. If the by the code generator as follows:
object factory does not contain TApisResource = Class(TGoogleResource)
a definition for a certain class, Public
the serializer will always fall Class Function ResourceName : String; override;
Class Function DefaultAPI : TGoogleAPIClass; override;
back to instantiating an instance
Function GetRest(_api: string; version: string) : TRestDescription;
of the declared property type. Function List(AQuery : string = '') : TDirectoryList;
Function List(AQuery : TApislistOptions) : TDirectoryList;
end;

5 Using the generated APIs The first 2 methods are for the
Armed with these base classes, it is time to start API's internal bookkeeping for the
using them to create an actual program. factory methods. The interesting
The Google discovery demo program uses the methods are List and GetRest:
googlediscovery unit to create a small GUI The latter requires the name and
program. Using this GUI program the following the version of the API, and will
actions can be performed: return a description of the REST API in the
TRestDescription instance. These 2 parameters
View and search in available services. are required and are encoded in the path of the URI
Open the documentation of a service used to access the resource: this is a feature of the
in a browser. API and is reflected in the signature of the methods
View the JSON rest description of the service. generated by the API.
Generate a unit based on the REST description As seen in the declaration, the List method of
of a service. this resource comes in 2 forms: One accepts a string,
the other a structure of type TApislistOptions.
The main form of the application simply shows a This pattern can be seen in all resource classes
list of services, with a button to (re)fetch the list, generated by the code generator, and this is a
and a textbox to filter the list. design choice: for each call the Google REST
The OnCreate event is used to set up everything up: description document describes for each call what
optional parameters the call accepts, and usually
procedure TMainForm.FormCreate(Sender: TObject); these parameters serve to filter the returned
begin // set up communication. response. These parameters are passed to the API in
the query variables encoded in the URL.
FClient:=TGoogleClient.Create(Self);
This is always translated by the code generator
FClient.WebClient:=TSynapseWebClient.Create(Self);
// Register all classes so they can be streamed. into 2 calls: rather than creating a method that
TDiscoveryAPI.RegisterAPIResources; contains all the parameters in its signature, a
// create the API and hook it up to the Google client. record is declared that contains each parameter as a
FDiscoveryAPI:=TDiscoveryAPI.Create(Self); field. For the List method, these parameters are
FDiscoveryAPI.GoogleClient:=FClient;
described in TApisListOptions:
// The code generator uses its own objects.
TDiscoveryJSONToPas.RegisterAllObjects; TApisListOptions = Record
UpdateCaption; _name : string;
end; preferred : boolean;
end;

The first 2 lines set up the communication: in the


example code, the Synapse descendant of the
TFPWebClient class is used to handle transport.
To be able to stream all objects in the discovery
API, all resources needed for the API are registered.

Issue Nr 3 2015 BLAISE PASCAL MAGAZINE 29


LAZARUS NOW CAN USE GOOGLE APIS PAGE 6

Since the API can change and to allow for custom user. Note that the base classes
queries, whenever there are such optional used in the APIs will always free
parameters for a method, the method is generated properties of class or dynamic array type
twice. The second form just accepts a query stringwhen they are destroyed: the user does not
which is passed on as-is in the URL (which means need to do this, but you do need to be aware of it.
it must be URL-encoded). Internally, the method The ShowDiscovery method
using the record just constructs the query from shows all services in the list. It has two
non-empty fields in the record, and calls the latter
arguments that can be used to filter the list:
method. PreferredOnly and a filter on text (the title,
So how can we use the TApisResource and its name, description and labels are filtered on this text).
methods ? Google APIs come in different versions, and one of
Each API class has methods to create the resource these versions is the preferred version. Normally
instances used in the API. For the discovery API, this is the version that should be used in new
there is only 1 resource, so the number of methodsimplementations. To cater for this, the demo
is limited: program has a check menu
TDiscoveryAPI = Class(TGoogleAPI) which can be used to show
//Add create function for resources only the preferred versions
Function CreateApisResource(AOwner : TComponent) : TApisResource;
Function CreateApisResource : TApisResource; of an API.
//Add default on-demand instances for resources
Property ApisResource : TApisResource Read GetApisInstance;
end;

The CreateApisResource call will


create an instance of TApisResource,
The ShowDiscovery method just fills a listview
and hooks it up to itself, so the resource
with the result of the call:
can execute service calls. Optionally, an
owner for the resource can be specified (if procedure TMainForm.ShowDiscovery(PreferredOnly : Boolean;
none is specified, the API instance is the FilterOn : String);
Var DLI : TDirectoryListitems; LI : TListItem;
owner). When the ApisResource begin
property is read, it will create an instance FilterOn:=LowerCase(Filteron);
of TApisResource if need be, and keep it LVServices.Items.BeginUpdate;
try
in memory till the API is freed.
LVServices.Items.Clear;
The same pattern is reused in all the API LVServices.Column[1].Visible:=Not PreferredOnly;
classes generated by the API code For DLI in FDirectory.Items do
generator. if ShowItem(DLI) then
begin
In the demo application, when the Refresh
LI:=LVServices.Items.Add;
button or menu item is chosen, the list of LI.Caption:=DLI.name;
available API's is fetched and displayed. LI.Data:=DLI;
This happens using the List method of the With LI.SubItems,DLI do
TApisResource. begin
Add(BoolToStr(preferred,'True','False'));
procedure TMainForm.DoFetch;
begin Add(id);
// Free any previous list. Add(title);
FreeAndNil(FDirectory); Add(version);
// Get the new list using a default ApisResource. Add(description);
FDirectory:=FDiscoveryAPI.ApisResource.List(); Add(discoveryLink);
ShowDiscovery(MIPreferredOnly.Checked,EFilter.Text); Add(discoveryRestUrl);
end; Add(documentationLink);
Add(icons.x16);
Add(icons.x32);
The second line of code gets the list of Add(DoComma(labels));
end;
services. For this, it uses the default end;
ApisResource property of the UpdateCaption;
TAPIDiscovery instance. The first line finally
LVServices.Items.EndUpdate;
cleared any previous list. The result of any
end;
API methods needs to be freed by the end;

30 Issue Nr 3 2015 BLAISE PASCAL MAGAZINE


LAZARUS NOW CAN USE GOOGLE APIS PAGE 7

The call to ShowItem decides whether an 6 Setting up OAuth 2 on Google


item must be shown or not, depending The Google discovery service is publicly
on the options to ShowDiscovery. available. This means that anyone can call
The rest of the program uses the properties these services, no authentication or authorization
of the TDirectoryListitems: the instances is required. Most other services, how- ever, do
are stored in the Data property of the listitems in need authorization to be used. The Google API
the listview. For instance, to show the converter overrides 2 method of TGoogleAPI:
documentation of a particular API,
Class Function APIAuthScopes : TScopeInfoArray;virtual; abstract;
the following code is used: Class Function APINeedsAuth : Boolean ;virtual;

function TMainForm.CurrentAPI: TDirectoryListitems; The first method, APIAuthScopes lists the


begin scopes for which authentication can be re-
If Assigned(LVServices.Selected) quested when an API is used. The second
and Assigned(LVServices.Selected.Data)
then method, APINeedsAuth, returns True if
Result:=TDirectoryListitems(LVServices.Selected.Data) the API needs authorization.
else (this is the case when the APIauthscopes
Result:=Nil;
array is non- empty).
end;
The ServiceCall method of the
procedure TMainForm.AViewHelpExecute(Sender: TObject); TGoogleAPI class uses this function to
begin decide whether it must make a signed or
OpenURL(CurrentAPI.DocumentationLink);
end;
unsigned request. For the Google
Discovery service, the APINeedsAuth
returns False.
Things can hardly get more simple than this.
Setting up OAuth 2 for an application
To view the JSON description of a service, the
requires several steps:
following code is used:
1. The developer needs a Google account.
procedure TMainForm.APreViewRestExecute(Sender: TObject); 2. In the Google developer console,
Var DLI : TDirectoryListitems; the application must be registered.
begin
DLI:=CurrentAPI; The Google developer console is
ViewRestAPI(DLI.Name,DLI.DiscoveryRestUrl); available at:
end; https://
console.developers.google.com/project
procedure TMainForm.ViewRestAPI(const AName, AURL: String); The console currently looks like figure 4
Var S : TMemoryStream; on the next page 31, where one
begin application is defined.
S:=TMemoryStream.Create;
try 3. When the application is defined, access
if HttpGetBinary(AURL,S) then to Google APIs must be set up. This is
begin done under the APIs and Auth - APIs
ViewFile(S,sJSON,'REST discovery for '+AName);
section of the console. The programmer
S:=Nil;
end; needs to declare to Google which APIs
finally his application will use.
S.Free; https://console.developers.google.com
end; /project
end; If this is not done correctly, then any
calls to an API will fail, even if the
authorized user has given consent that
The HttpGetBinary call is a part the application may do so.
of the Synapse TCP/IP suite, and the ViewFile
call just shows a When authorizing, the Google
secondary form with a syntax highlighter. authentication service will present a consent
The other functions of the program are simple screen, which will list all actions that an
variations on these calls, the code for it will not be application is allowed to perform on behalf
presented here. of the user. The list of actions reflects what
The program can be seen in action in on page 31 APIs the programmer has selected here.
Figure 3: The Google Service Discovery demo The API selection is shown in figure 5 on
page 35.

Issue Nr 3 2015 BLAISE PASCAL MAGAZINE 31


LAZARUS NOW CAN USE GOOGLE APIS PAGE 8

4. As a last step, a pair of keys must be generated For desktop applications, the 'native
for the application. This is done under the client application' type must be chosen,
APIs and Auth - Credentials section of the and urn:ietf:wg:oauth:2.0:oob
console. One key is a unique identifier for the or http://localhost must be chosen
application (the client ID), the other is a secret as the Redirect URI.
key (a password). These are used when asking In the Google Developer Console a new Client
for user consent: they are sent to the Google ID and Client Secret can then be generated.
authorization server when the application
needs permission to fetch data from a user. The client ID and Secret must be used in the code
Through the OAuth 2 protocol flow, of your application - preferably scrambled
the application will then end up with a new somehow. Google offers a JSON download of this
token (the access token) that it will use to ask data, the contents of this file must be kept secret.
permissions to acces data on behalf If this data becomes public, then another
programmer can impersonate your application and
of the user.
start downloading or, worse, wreak havoc on the
The credentials configuration is shown in
user's data (and you will get the blame for it).
figure 6 on page 36. Depending on what kind The second method, APINeedsAuth, returns True
of application you are developing, different if the API needs authorization. (This is the case when
settings must be used. the APIauthscopes array is non-empty).

Figure 3: The Google Service Discovery demo

Figure 4: Defining an application in Google


LAZARUS NOW CAN USE GOOGLE APIS PAGE 9
Figure 5: Selecting APIs for the application in Google

procedure TMainForm.FormCreate(Sender: TObject);


begin
// Set up Google client.
FClient:=TGoogleClient.Create(Self);
FClient.WebClient:=TSynapseWebClient.Create(Self);
FClient.WebClient.RequestSigner:=FClient.AuthHandler;
FClient.AuthHandler.WebClient:=FClient.WebClient;
FClient.AuthHandler.Config.AccessType:=atOffLine;
// We want to enter a code.
7 Setting up OAuth 2 in the application FClient.OnUserConsent:=@DoUserConsent;
Once the server part was set up, the application // Create a calendar API and connect it to the client.
part can be configured. To demonstrate this, the FCalendarAPI:=TCalendarAPI.Create(Self);
Google Calendar demo program is used. It FCalendarAPI.GoogleClient:=FClient;
makes use of the googlecalendar unit, generated // Register calendar resources.
TCalendarAPI.RegisterAPIResources;
with the API code generator. The calendar demo LoadAuthConfig;
shows a list of the user's calendars of the user, end;
and allows the user to view the items in the
calendar. The list of calendars and events in the
calendar are simple listboxes, to make it simpler
to understand.The application starts in much the
same way as the discovery service demo:

Figure 6: Configuring application credentials


LAZARUS NOW CAN USE GOOGLE APIS PAGE 10

The code differs only in the setup of the Procedure TMainForm.DoUserConsent(Const AURL: String;
authentication handler: The webclient's Out AAuthCode: String);
begin
RequestSigner property is set to the // Make the code entry visible.
Google Client AuthHandler property. GBAccess.Visible:=True;
EAccessCode.Text:='<enter code here>';
When the webclient needs to sign a FAccessState:=acsWaiting;
request (basically, it adds an // Show the URL in the browser
OpenUrl(AURL);
authorization handler), it checks the
// Wait for the user to enter the code
AuthHandler. This will check if an While (FAccessState=acsWaiting) do
access token is available. For the Application.ProcessMessages;
// If the user has entered the code, return it
authentication handler to be able to do if FAccessState=acsOK then AAuthCode:=EAccessCode.Text;
its work, 2 properties must be set: GBAccess.Visible:=False;
end;

FClient.AuthHandler.Config.AccessType:=atOffLine;
FClient.OnUserConsent:=@DoUserConsent; The code for this event handler looks like the
example above:

The first line tells the authentication handler class The code starts by showing the button and edit
that the application is an offline application. control. It then repeatedly runs the application
The second line registers an event handler: for an message loop to wait for the user to enter the
offline application, this event handler is called if authorization code. The OK and Cancel buttons
user consent is needed. The last line of the simply set a state, which is picked up in the loop:
OnCreate event handler loads the configuration procedure TMainForm.BSetAccessClick(Sender: TObject);
from an ini file; we'll get back to this. begin
FAccessState:=acsOK;
Now, when the application needs to do a service end;
call to a Google service, the authentication
procedure TMainForm.BCancelClick(Sender: TObject);
handler will check if it has an access token. If it begin
does not, and it does not have a refresh token FAccessState:=acsCancel;
(with which it can ask an access token from end;
Google), it will call the DoUserConsent event
handler.
Obviously, it would be annoying for the user to
When the event handler is called, several things
have to login and enter this code each time the
must be done:
application is used.
1. The event handler gets an URL to a Fortunately, this is not necessary: the application
Google authentication server, saves the tokens it received after the first calls to
which must be displayed in a browser. the server.
2. The Google authentication server procedure TMainForm.SaveRefreshToken;
will then ask the user to log in Var ini:TIniFile;
(if he or she is not yet logged in) and begin // We save the refresh token for later use.
will ask permission for your With FClient.AuthHandler.Session do
if RefreshToken<>'' then
application to access the calendar.
begin ini:=TIniFile.Create('Google.ini');
3. The browser will then display an try
authorization code, which must be With ini do
begin
entered by the user in the program.
WriteString('Session','RefreshToken',RefreshToken);
4. Once the user has entered the WriteString('Session','AccessToken',AccessToken);
authorization code, the event WriteString('Session','TokenType',AuthTokenType);
handler may return, passing the WriteDateTime('Session','AuthExpires',AuthExpires);
code back to the authorization WriteInteger('Session','AuthPeriod',AuthExpiryPeriod);
end;
handling component. finally
In the calendar demo application, Ini.Free;
there is a groupbox with an edit end;
control and 2 buttons (OK and Cancel). end;
end;
In this edit control the user can enter
the authorization code returned by
Google. Initially, this groupbox is invisible.

34 Issue Nr 3 2015 BLAISE PASCAL MAGAZINE


LAZARUS NOW CAN USE GOOGLE APIS PAGE 11

The code below shows what properties of the user


session must be saved. For safety reasons, it is
better not to save the AccessToken in a desktop
application.

But for instance in a CGI web application, the


access token can better be saved, to avoid having
to exchange the refresh token for an access token
each time the Google service needs to be called.
In the OnCreate handler of the application, an
attempt is made to load these tokens from the ini
file, together with the client ID and secret:

procedure TMainForm.LoadAuthConfig;
Var ini:TIniFile;
begin
ini:=TIniFile.Create('Google.ini');
try
With FClient.AuthHandler.Config,Ini do begin
// Registered application needs calendar scope
ClientID :=ReadString('Credentials','ClientID','');
ClientSecret :=ReadString('Credentials','ClientSecret','');
AuthScope :=ReadString('Credentials','Scope', 'https://www.googleapis.com/auth/calendar');
// We are offline.
RedirectUri:='urn:ietf:wg:oauth:2.0:oob';
end;
With FClient.AuthHandler.Session,Ini do begin
// Session data
RefreshToken:=ReadString('Session','RefreshToken','');
AccessToken:=ReadString('Session','AccesToken','');
AuthTokenType:=ReadString('Session','TokenType','');
AuthExpires:=ReadDateTime('Session','AuthExpires',0);
AuthExpiryPeriod:=ReadInteger('Session','AuthPeriod',0);
end;
finally
Ini.Free;
end;
end;

If the application is run the first time, the session


tokens are empty, and the user consent event will
be called. Later runs of the application will load
the refresh and access token from the ini file, and
the user no longer needs to log in or enter an
access code. Unless he revokes the right of the
application to work on his/her behalf.
Needless to say, these tokens should be stored in a
safe way. The above method of loading the client
secret and ID was implemented done for
convenience, but is not recommended for use in
real life applications: the Client ID and Client

Secret must be secret, and an .ini file is not


suitable for this.
The code to fetch the events and calendar list is in
fact very similar to the code used in the discovery
demo, but we'll show it anyway:

Issue Nr 3 2015 BLAISE PASCAL MAGAZINE 35


LAZARUS NOW CAN USE GOOGLE APIS (FOR ANDROID) PAGE 12 - END

procedure TMainForm.BFetchCalendarsClick(Sender: TObject);


var Entry: TCalendarListEntry;
Resource : TCalendarListResource; EN: String; i:integer; About the Author
begin Michael Van Canneyt has been a
LBCalendars.Items.Clear; contributor to Free Pascal since
FreeAndNil(CalendarList);
almost 20 years. He works mostly
Resource:= Nil;
try on the documentation, Linux
Resource:=FCalendarAPI.CreateCalendarListResource; platform support and Database and
CalendarList:=Resource.list(''); Web programming classes.
SaveRefreshToken; Naturally, he uses all this code also
if assigned(calendarList) then
for i:= 0 to Length(calendarList.items)-1 do
professionally.
begin
Entry:=calendarList.items[i];
EN:=Entry.Summary;
if EN='' then
EN:=Entry.id+' ('+Entry.description+')';
LBCalendars.Items.AddObject(IntToStr(i)+': '+EN,Entry);
end;
BFetchEvents.Enabled:=LBCalendars.Items.Count>0;
finally
FreeAndNil(Resource);
end;
end;

Figure 7: The calendar demo program in action

The code differs on 2 accounts: Conclusion


REST is an important architecture in web APIs.
The code does not make use of the default When done right, it makes developing a client
calendarlist resource of the Calendar API. library very easy. Google offers a complete REST
Instead it uses the API for its services, and Lazarus and Free Pascal
CreateCalendarListResource to create now contain now a complete client-library
a private instance, which is freed at the end implementation for the Google APIs, as has been
of the routine. demonstrated here. This client library is somewhat
young and certainly subject to improvement, but is
After the call to list the calendars is made, certainly usable. Not all aspects have been treated
the SaveRefreshToken routine is called here: much more can be said about authentication,
explicitly to save the refresh token. This can getting information about the logged in user, and
also be handled in different ways: The saving sesion state information. A similar library
TOAuth2Handler has an event called for the Microsoft Office365 environment is
OnAuthSessionChange which is called currently being developed. All these topics will be
whenever the session information changes, treated in a future article.
The author is indebted to Ludo Brands and the late
and the Store property which can be set
Reinier Olislagers for their ideas on how to handle
to a component that handles storing and
serialization and implementation of the OAuth 2
retrieving of session and configuration
protocol.
variables.

The result of all this code can be seen in figure 7 in


the middle of this page.

36 Issue Nr 3 2015 BLAISE PASCAL MAGAZINE


ARDUINO: THE VISUINO PROJECT - PART 1 PAGE 1
BY BOIAN MITOV
starter expert It took me hours to put even fairly simple code
Delphi together - reading from one sensor, and controlling
a motor. Hardly impressive achievement.
To become the master of the world with my
The Arduino board: a guide to quickly
Arduino, I would need a better approach.
get you started using Delphi
or: How a new product comes into Since obviously, nobody else had solved the
being. problem I was facing, I decided to do it myself,
How to quickly and efficiently start and got down to work. Here is what my goal was:
using Arduino, and connect to it from
Delphi, and how a new product is born. 1. In order a development tool to be attractive to
inexperienced developers it must be very easy
to use. It had to be easy enough for a kid to
Today as Delphi and FPC developers, we feel learn it, and it should not require you to learn
masters of our Desktops, Servers, and Mobile programming language or programming
devices, but we lack that mastery once we are techniques. In short, it should be intuitive
outside this digital world. Whenever we need to visual design tool.
control some other equipment, or collect some
interesting data from sensors, we feel that lack of 2. In order for it to be attractive to experienced
power, that we have grown to enjoy. and expert developers, it must produce
compact, and highly efficient, scalable code.
This was the feeling that drove me at the end of the
last year, to buy myself an Arduino board with a 3. Since Arduino didn't have an operating
small starter sensors and peripherals kit. system, it must allow components to be
designed as a collaborative plug-in framework,
Arduino is an open source hardware platform, so they can work together, and not interfere
developed initially primarily by Massimo Banzi, with each other. In essence a small
David Cuartielles, Tom Igoe, Gianluca Martino and rudimentary, component oriented OS core
David Mellis. The Arduino team/teams also was needed.
manufacture and sell Arduino boards, however since
the platform is Open Source, there is a huge Fortunately for me, I was already experienced in
number of other manufacturers of Arduino developing high performance component
compatible boards ranging in prices from ~$1 to frameworks for Delphi, C++ Builder, and .NET, as
~$100, and with huge range of sizes, some smaller well as haaving a solid hardware background.
than a quarter, and a huge range of capabilities,
from few digital and analog pins, all the way to I also had a ready graphical development IDE for
WI/FI, and GSP enabled boards, with high number Windows called OpenWire Studio. OpenWire Studio is
of digital, and analog pins. a very flexible and open architecture Graphical
IDE, and can easily be adapted to program almost
This great diversity, in capabilities, sizes, and anything.
prices, makes Arduino a very attractive development At least in theory, now here was the chance for it
platform, for almost any project that requires to prove that it is up to the task solving my little
monitoring, interfacing or controlling the world Arduino problem.
outside our comfortable Delphi/FPC controlled I started with the most difficult of the problems.
boxes, or at least that is what I thought... Writing a component framework and very
rudimentary scheduling functionality. For this I
I received my Arduino KIT, anxiously unpacked it, used a very rudimentary lightweight flavor of the
and hooked it to my system. good old OpenWire, implemented in C++.
So far so good, it started to blink. Next I created few Delphi components and using
Then I started digging the web to learn how to the new Mitov.Runtime RTTI started to generate
program it, and my fantasy to control the world corresponding C++ code for Arduino from them.
with it came crashing down on me. The Arduino Finally I cloned the OpenWire Studio, and
boards are usually programmed using a very installed the newly created components in it.
simple to use but extremely rudimentary IDE called And the first version of Visuino was born. All that
Arduino IDE, in C/C++, with very low level code, was needed was to add buttons to generate the
that requires fairly deep hardware, and firmware Arduino code, and to start the Arduino IDE so I could
knowledge to do even relatively simple tasks. compile the code.

Issue Nr 3 2015 BLAISE PASCAL MAGAZINE 37


ARDUINO: THE VISUINO PROJECT - PART 1 PAGE 2

This happened exactly in the middle of the Delphi So using PlotLab, I naturally added a Scope
Week, celebrating 20 years of Delphi, and so as I component and hooked it to the serial port.
did a brief live interview about my experience with The Visuino was shaping very well. Not only could
Delphi over the years, David I and Jim McKeeth, I program my boards with it, but I could also
suggested to show the Visuino as example of what monitor and plot the data from one channel.
can be achieved with Delphi. But what if I need to monitor more channels?
So it was, that the first people to see the product I still was not satisfied. To send data from
live in action (bugs and all), were the Delphi fans multiple channels, over a single communication
watching the Delphi Live broadcast! channel, I needed to package the data in some
At this point I already had achieved all the goals form of structure, so I designed a package and un-
I had in mind, when I started the project. package components that allow the data to be
All I needed was to write more components, packaged and transmitted as a structured packet.
and play with it, but soon I started to discover Now I was able to plot multiple channels easily.
more shortcomings. I was able to program my I went even further, allowing Visuino to
Arduino with great ease, but my Arduino and my PC automatically configure the scope from the
were living in separated worlds. package format in my Arduino design.
I wanted to see on my screen, what my Arduino Scope piloting was nice, but sometimes we want to
was collecting as data, or processing. As a see the data in Gauges, and LEDs so I decided to
minimum, I needed a terminal window, so I add instrumentation view as well, using the
created an OpenWire serial port component, and InstrumentLab component package. My Visuino was
hooked a terminal window, with the necessary user feature complete, I was happy, and I already had
interface. I was able to see my data, but it was in Delphi components developed that allowed me to
text format. What if I need to see it in a plot? easily communicate with the board.

Figure 1

38 Issue Nr 3 2015 BLAISE PASCAL MAGAZINE


ARDUINO: THE VISUINO PROJECT - PART 1 PAGE 3

The next logical step was to package and release


them, so other developers can communicate with
Arduino from their Delphi applications.

Now that we are trough with the short Visuino


history, it is time to take a real look at it.
You can download the Visuino Beta by visiting
www.visuino.com .

Before installing make sure you have the latest


version of the Arduino IDE installed.
Currently this is 1.6.4 and is available for
download here :
http://www.arduino.cc/en/Main/Software
.
The Arduino IDE comes with all standard Arduino Figure 2
libraries that will be needed to compile the code
generated by Visuino.
After installing the Arduino IDE, you can go ahead
with the Visuino installation.
If you have done a default installation of your Figure 3
Arduino IDE, there will be no need to do any
Once you have familiarized
configuration of Visuino.
yourself with the demos, you
Once installed you can start Visuino.
can try to create a new project yourself. The design
area always contains an Arduino component.
In the center you will have the Visuino's design
You can select the Arduino board type by setting the
area. This is where you will visually design your
BoardType property or by clicking on the setup
project.
icon of the Arduino component.
On the right of figure 1 (page before) is the Figure 4
component toolbar where the components are The simplest project is a
organized in different categories and sub- blinking LED. Drop a
categories. Pulse Generator from
the toolbar.
In the top left corner of figure 1 (page before) is the
overview navigation area, and bellow it is the Connect it to
property editor. pin 13 of the Arduino
Below the Design Area is located the Serial component.(figure 5)

Terminal the Scope and - as we will see later - the


Instruments Panel.

The best place to start learning is to watch the


Visuino video tutorials:
https://www.youtube.com/watch?v=v- Figure 5
yMtIzgIeU,
Now your design is ready, and you can generate
and the Arduino code by clicking on the Send to
Arduino IDE for Compilation button:
https://www.youtube.com/watch?v=wKKlhg
KtDoI

The next step is opening the included demo


projects. You can easily access them from the menu
by selecting |File|Open Demo...|

Figure 6

Issue Nr 3 2015 BLAISE PASCAL MAGAZINE 39


ARDUINO: THE VISUINO PROJECT - PART 1 PAGE 4

This will generate the Arduino code, and launch


the Arduino IDE where you can compile and
upload the compiled code into your Arduino board:

Figure 8

In the Bytes editor type 55 55 . This will be used


to identify the starting point of a package.
You can use any number of bytes, with any values,
but 2 bytes are a good choice, and common values
such as 00 00 should be avoided as they often
appear in data. The component makes sure the
header is properly recognized even if 55 55 is
present in the data, by special encoding.
Figure 7

Next we can connect a few sensors to the Arduino


pins, and monitor them in Visuino.
Start a new Visuino project, by selecting |File|New|.
Add a Packet component to the Design:

Figure 9

You can close the editor by clicking on the OK


button.

Double-click on the Packet1 to open the elements


editor:

And connect its output to the Input of the Serial Port


(The software input for the serial receives data that
will be sent as serial output from the hardware. )

In the property editor expand the HeadMarker


and click on the ...ellipsis button: Figure 10

40 Issue Nr 3 2015 BLAISE PASCAL MAGAZINE


ARDUINO: THE VISUINO PROJECT - PART 1 PAGE 5

With this editor you can add data channels to the


packet. For this example I will add 2 Analog and 2
digital channels.
Now we can connect the packet elements to the
pins where our sensors are connected:

Figure 11
Your design is ready. You can generate, compile,
and upload the code to your Arduino. Now you
can use the Visuino Scope and Instrumentation
Panel to view the data. Select the com port to which
the Arduino is connected, and from the Format
drop-down select Packet1: Then click Connect.

Figure 12

Issue Nr 3 2015 BLAISE PASCAL MAGAZINE 41


ARDUINO: THE VISUINO PROJECT - PART 1 PAGE 6

Figure 13

In the scope you can see the data arriving from the
sensors: see figure above
And the same data can be seen
in the Instrument Panel:

Figure 14

You have seen how you can receive


and visualize data from sensors in the
Visuino. In the next issue you will
learn how to receive data from
Arduino into your Delphi applications.

42 Issue Nr 3 2015 BLAISE PASCAL MAGAZINE


GENERATE DELPHI SUPPORT FOR READING BY KIM MADSEN
AND WRITING XML FILES PAGE 1
- THAT ADHERE TO THE GOOGLE MERCHANTS RATING DATA SPECIFICATIONS
starter expert First download the XSD from Google here:
Delphi https://developers.google.com/
merchant-review-feeds/schema
This article is actually less about how Google Next one have to compile (if not already done) and
Merchants ratings work, and more about the
run the kbmMW ConvertXSD.exe application.
principle of utilizing kbmMW to convert XSD (XML
schema documents) to easily streamable Delphi Now click Convert XSD file, and select the
objects. downloaded merchant_reviews.xsd file.
kbmMW Enterprise Edition includes a complete A split second later, the file has been read, validated
XSD schema converter, which takes an xsd file as and a merchant_reviews.pas file has been generated
input and outputs readily compilable Pascal in the same directory.
objects, that are very easy to read, alter and
stream to and from XML and JSON.

First a little bit about the XSD


schema converter.
Its delivered as a demo application
(that is full featured in what it does),
but which can be tailored if
required to any developers need.
Its main internal components, are
a TkbmMWXSDParser class (with
a number of assistant classes),
a TkbmMWXSDPascalCodeGen
class (descending from
TkbmMWXSDCustomCodeGen)
and of course the TkbmMWDOMXML
class for speedy and complete
handling of the XML, in which
XSD files are written.
As can be seen, its actually
possible to utilize the parse tree
generated by the XSD parser to
output other types documents by
inheriting from the
TkbmMWXSDCustomCodeGen.
That's however out of scope for
this article.
Ive done some comparisons with
the built in XSD importer tool in
Delphi, and find that the kbmMW
ConvertXSD tool is better and more
accurate in converting XSD
documents, and easily converts
XSD documents not possible
using Embarcaderos XSD inporter.
The outset for this article is to
generate Delphi support for
reading and writing XML files
that adhere to the Google In the right pane, its possible to see which classes
merchants rating data kbmMW's XSD converter have found. And in the left
specifications. pane, any warnings or errors would have been listed.

FINALLY ITS EASY


TO CREATE AND READ
XML FILES
Issue Nr 3 2015 BLAISE PASCAL MAGAZINE 43
GENERATE DELPHI SUPPORT FOR READING
AND WRITING XML FILES PAGE 2
unit merchant_reviews;
The converted file looks like this (snippets only shown):
// ==========================================================================
// Generated by kbmMW XSD Converter
// 5/11/2015 01:08:59
// Based on: C:\svn_c4d\kbmmw\trunk\ConvertXSD\GoogleMerchantFeedback\merchant_reviews.xsd
// ==========================================================================

// Log:
// Converted without errors or warnings.

type

(*$HPPEMIT 'namespace Merchant_reviews {'*)


{$SCOPEDENUMS ON}
TLanguageCode =
(aa,ab,ae,af,ak,am,an,ar,&as,av,ay,az,ba,be,bg,bh,bi,bm,bn,bo,br,bs,ca,ce,ch,co,cr,cs,cu,cv,cy,da,de,dv,dz,ee,el,en
,eo,es,et,eu,fa,ff,fi,fj,fo,fr,fy,ga,gd,gl,gn,gu,gv,ha,he,hi,ho,hr,ht,hu,hy,hz,ia,id,ie,ig,ii,ik,io,&is,it,iu,ja,jv
,ka,kg,ki,kj,kk,kl,km,kn,ko,kr,ks,ku,kv,kw,ky,la,lb,lg,li,ln,lo,lt,lu,lv,mg,mh,mi,mk,ml,mn,mr,ms,mt,my,na,nb,nd,ne,
ng,nl,nn,no,nr,nv,ny,oc,oj,om,&or,os,pa,pi,pl,ps,pt,qu,rm,rn,ro,ru,rw,sa,sc,sd,se,sg,si,sk,sl,sm,sn,so,sq,sr,ss,st,
su,sv,sw,ta,te,tg,th,ti,tk,tl,tn,&to,tr,ts,tt,tw,ty,ug,uk,ur,uz,ve,vi,vo,wa,wo,xh,yi,yo,za,zh,zu);
TCountryCode =

Ttype_1 = (singleton,group);
TNonEmptyString = kbmMWNullable<string>;
Ttype = (summary,detail);
Treviewer_type = (user,editorial,aggregator);
Tcollection_method = (unsolicited,point_of_sale,after_fulfillment);

const
CLanguageCode : array[TLanguageCode] of string =
('aa','ab','ae','af','ak','am','an','ar','as','av','ay','az','ba','be','bg','bh','bi','bm','bn','bo','br','bs','ca'
,'ce','ch','co','cr','cs','cu','cv','cy','da','de','dv','dz','ee','el','en','eo','es','et','eu','fa','ff','fi','fj'
,'fo','fr','fy','ga','gd','gl','gn','gu','gv','ha','he','hi','ho','hr','ht','hu','hy','hz','ia','id','ie','ig','ii'
,'ik','io','is','it','iu','ja','jv','ka','kg','ki','kj','kk','kl','km','kn','ko','kr','ks','ku','kv','kw','ky','la'
,'lb','lg','li','ln','lo','lt','lu','lv','mg','mh','mi','mk','ml','mn','mr','ms','mt','my','na','nb','nd','ne','ng'
,'nl','nn','no','nr','nv','ny','oc','oj','om','or','os','pa','pi','pl','ps','pt','qu','rm','rn','ro','ru','rw','sa'
,'sc','sd','se','sg','si','sk','sl','sm','sn','so','sq','sr','ss','st','su','sv','sw','ta','te','tg','th','ti','tk'
,'tl','tn','to','tr','ts','tt','tw','ty','ug','uk','ur','uz','ve','vi','vo','wa','wo','xh','yi','yo','za','zh','zu'
);
CCountryCode : array[TCountryCode] of string =

Ctype_1 : array[Ttype_1] of string = ('singleton','group');


Ctype : array[Ttype] of string = ('summary','detail');
Creviewer_type : array[Treviewer_type] of string = ('user','editorial','aggregator');
Ccollection_method : array[Tcollection_method] of string = ('unsolicited','point_of_sale','after_fulfillment');

[kbmMW_Root('Review',[mwrfIncludeOnlyTagged])]

When one is creating a project that needs


to read or write XML files that adhere to
the Google merchants ratings XSD, one
simply need to include this file in the uses
clause, and then use kbmMW's
serialization and deserialization methods
to convert XML or JSON to objects or
objects to XML or JSON.
At
https://developers.google.com/
merchant-review-feeds/sample
there is a sample XML file provided by
Google, which we will use to see that we
can convert the XML to objects.

A new VCL application is created (it could


be a Firemonkey application too, kbmMW
works in both environments).

44 Issue Nr 3 2015 BLAISE PASCAL MAGAZINE


GENERATE DELPHI SUPPORT FOR READING
AND WRITING XML FILES PAGE 3

In its uses clause we add the generated


merchant_review unit:
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
merchant_reviews;

Before serializing or deserializing we need to let kbmMW have full support for timezones, and you
kbmMW know about the classes that are part of the can operate the TkbmMWDateTime in much the
merchant_reviews unit. This is done in the same way as you would with a TDateTime.
OnFormCreate event in this sample: Ok.. then let us try to serialize the FFeed object
procedure TForm1.FormCreate(Sender: TObject); back to XML again potentially after we have made
begin changes to it, or perhaps even built a new Tfeed
Tmerchant_reviews.RegisterStreamableObjects;
instance from scratch.
end;
procedure TForm1.btnSaveClick(Sender: TObject);
Now everything is ready for streaming. Lets put var xmlm:TkbmMWXMLMarshal; xml:TkbmMWDOMXML;
some code in the load XML buttons event handler to begin
if FFeed=nil then exit;
load a Google merchants ratings XML file and have it
accessible via standard Delphi objects: xmlm:=TkbmMWXMLMarshal.Create;
try
procedure TForm1.btnLoadClick(Sender: TObject); xmlm.Typed:=false;
var xml:=xmlm.ValueToDOMXML(FFeed);
xmlm:TkbmMWXMLMarshal; xml:TkbmMWDOMXML; if xml=nil then
m:TMerchant; r:TReview; d:TNonEmptyString; begin
begin Memo1.Lines.Add('xml=null');
xml:=TkbmMWDOMXML.Create; exit;
try end;
xml.LoadFromFile('merchant_reviews.xml'); finally
xmlm:=TkbmMWXMLMarshal.Create; xmlm.Free;
try FFeed:=TFeed(xmlm.ValueFromDOMXML(TFeed,xml)); end;
Use the FFeed object for what you want. xml.Typed:=false;
finally xmlm.Free; xml.AutoIndent:=true;
end; xml.AutoLineFeed:=true;
finally xml.Update;
xml.Free; xml.SaveToFile('newfeed.xml');
end; xml.Free;
end;

Its that simple to convert the XML to true Delphi


objects! Now you can access all fields/attributes
like this:
Memo1.Lines.Add('FFeed.merchant.review.reviewer_id='+r.reviewer_id.ValueOr['<null>']);
Memo1.Lines.Add('FFeed.merchant.review.reviewer_type='+Creviewer_type[r.reviewer_type]);
Memo1.Lines.Add('FFeed.merchant.review.review_date='+r.review_date.ISO8601String);
Memo1.Lines.Add('FFeed.merchant.review.is_spam='+BoolToStr(r.is_spam.ValueOr[false]));

Notice the optional use of .ValueOr[]. The reason


is that for example reviewer_id is a nullable It's as simple as that! Now a newfeed.xml file will
value. kbmMW understands the difference between for have been generated based on the FFeed object.
example a null string value and an empty string. It will be data wise exact the same as the original xml
Similarly kbmMW understands nullable file we deserialized, although it may differ in
integers, singles, doubles, Booleans, order or in filtering out empty XML nodes.
dates, times, date/times etc. What if you would want to send the Ffeed object
Also notice the access of the review_date instance to a browser, connected to a kbmMW
property. We want to display it as an ISO8601 application server that is acting as a web server?
formatted string in this case, but we could also have The browser usually understands Javascript, and
asked it to be shown as a RFC1123 formattet string often jQuery is used for sending requests to a
instead or as a local date/time or as a GMT webserver and receiving responses in what is called
date/time. an AJAX operation (Async Javascript and XML).

Issue Nr 3 2015 BLAISE PASCAL MAGAZINE 45


GENERATE DELPHI SUPPORT FOR READING
AND WRITING XML FILES PAGE 4

As such, the browser typically do support parsing The string can be sent directly to the browser as a
XML to an extent, potentially via 3rdparty XML response to the browsers GET or POST request,
Javascript libraries. However Javascript supports a giving the mimetype application/json.
native textual object notation, that is more compact What makes the serialization/deserialization magic
than XML and faster for it to read. JSON is the name happen is the combination of attributes given on the
for that notation (Javascript Object Notation). Delphi types, and an advanced and intelligent built in
So a better choice is to serialize the Ffeed object to mechanism that understands the combination of
JSON and send that JSON stream to the browser. attributes and the type and relations between the
The browser would see the streamed data as true defined Delphi types that are to be
Javascript objects upon reception. serialized/deserialized.
In kbmMW its simple to serialize to JSON: kbmMW's serializer is probably one of the most
advanced on the market, and is even
procedure TForm1.btnSaveJSONClick(Sender: TObject);
var jm:TkbmMWJSONMarshal; s:string; included in the free kbmMW CodeGear Edition.
begin A Delphi class can be decorated with a
if FFeed=nil then exit;
number of attributes that hints to the
jm:=TkbmMWJSONMarshal.Create; serializer/deserializer how it should go
try about its operation.
s:=jm.ValueToString(FFeed);
This is a short explanation of the basic forms
Now the string s contains the JSON data of various attributes currently understood
by kbmMW:
finally jm.Free;
end;
end;

[kbmMW_Ignore] Can be placed in front of any field or property to ensure that that particular field/property is not serialized. Eg.
[kbmMW_Ignore] property SomeValue:string read.
[kbmMW_NotNull] Can be placed in front of any field/property to indicate that the field must NOT take the value of NULL (undefined).
If it does, an exception will be raised upon serialization or deserialization.
[kbmMW_Element(..)] Place in front of any field/property to indicate that the value should be serialized as an element (a child node in XML).
Its also possible to specify the name of the child object like this: [kbmMW_Element('someName')]
[kbmMW_Attribute(..)] Similar to the kbmMW_Element, except that it directs that the value must be put in an attribute (in the parent node in
XML). For JSON it will work the same as kbmMW_Element. This attribute also accepts a naming argument.
[kbmMW_Root(..)] Specifies default naming of a class, and what parts of it should automatically be serialized/deserialized like all published
properties, all public properties or only properties/fields tagged with kbmMW_Attribute or kbmMW_Element attributes.
[kbmMW_Null(..)] Indicate that the element can take the value of NULL. Optionally a default value can be provided, which will be used in
case the value in the XML/JSON indicates NULL.
[kbmMW_Validate()] Validates a property/field or a complete class instance for its values and raises an exception if a value is out of spec.
A complete expression which can refer to any field in the class can be given. If the expression evaluates to false,
then an exception will be raised upon serialization/deserialization time.
Eg. [kbmMW_Validate('$someName=22')] accepts only the value 22 in the someName field.

Further there are special attributes:


[kbmMW_ConditionalType()] Controls (in combination with [kbmMW_Root]l) serialization and deserialization
of colletions containing different types of child objects within the same
collection.[kbmMW_Dataset(..)]
[kbmMW_Dataset(..)] Controls serialization/deserialization of fields/properties that are of type
[kbmMW_DatasetRow(..)] TkbmCustomMemTable or descendants.
[kbmMW_DatasetField(..)]
[kbmMW_DatasetVersion(..)]
[kbmMW_DatasetDefinition(..)]
[kbmMW_DatasetData(..)]

46 Issue Nr 3 2015 BLAISE PASCAL MAGAZINE


GENERATE DELPHI SUPPORT FOR READING
AND WRITING XML FILES PAGE 5 - END

Finally its possible to register custom As kbmMW is a modular framework, one can choose
serialization/deserialization code for handling only to use its XML capabilities, its JSON capabilities,
special serialization/deserialization requirements. its serialization capabilities, its application server
It already comes with such for handling capabilities, its database capabilities, its stream
kbmMWNullable<..> types, TkbmMWDateTime storage capabilities, its memory table or its async
types, TStream types/descendants and messaging capabilities etc without having to use all
TkbmCustomMemtable descendants. other parts of the kbmMW framework.
I hope this has given an appetizer for how versatile
the kbmMW object serialization/deserialization But obviously, you will get the best of the best if
framework is. you take the plunge and choose to take advantage
of all the kbmMW features you need in your
As an example of a fairly complex XSD that kbmMW applications as all parts are designed to work in
effortless converts and serializes/deserializes perfect harmony with each other.
accordingly to, but that even Delphi XE8 fails
converting, is the Personal Health Record XSD /Kim Madsen / C4D
found here:
http://www.recordsforliving.com/ There is extra code you can download from your
Schemas/2006-04/PHR- subscription site....
Model/R4L_PHRModel.xsd

You can find sample data here:


http://www.recordsforliving.com/Persona
lHealthRecords/SamplePHRs.aspx

30,00 including VAT 39


including the printed book,
ebook and shipping

Quick answers to common problems

See our special offer:


if you take out a subscription
Delphi Cookbook for two years the book
50 hands-on recipes to master the power of Delphi for
will cost you only 10,00
cross-platform and mobile development on Windows,
HOICE A
Mac OS X, Android, and iOS SC
R BLAISE
M
TO

AZING

PASCAL
DI

MAGAZINE
Daniele Teti E

http://www.blaisepascal.eu/daniele_teti_book/DanieleTeti.html

Issue Nr 3 2015 BLAISE PASCAL MAGAZINE 47


EXTREME PERFORMANCE
NOW FASTER THAN EVER!

COMPOELOPERS
DEV
4 NENTS
- Now faster than ever!
- Improved publish/subscribe message queues
- Improved XML/JSON marshalling support
- Delphi/C++Builder/RAD Studio XE8
- Native high performance 100% developer Supports Delphi/C++Builder/RAD Studio 2009
defined application server with support for to XE8 (32 bit, 64 bit and OSX where applicable).
loadbalancing and failover kbmMW for XE5 to XE8 includes full support for
- Native high performance JSON and XML Android and IOS (client and server).!
(DOM and SAX) for easy integration with
kbmMemTable is the fastest and most feature rich
external systems in memory table for Embarcadero products.
- Native support for RTTI assisted object
marshalling to and from XML/JSON, now also - Easily supports large datasets
with new fullfeatured XML schema with millions of records
(XSD) import - Easy data streaming support
- High speed, unified database access - Optional to use native SQL engine
(35+ supported database APIs) with - Supports nested transactions and undo
- Native and fast build in M/D,
connection pooling, metadata and
aggregation /grouping,
data caching on all tiers range selection features
- Multi head access to the application server, - Advanced indexing features for
via AJAX, native binary, Publish/Subscribe, extreme performance
SOAP, XML, RTMP from web browsers,
embedded devices, linked application Warning!
servers, PCs, mobile devices, Java systems kbmMemTable and kbmMW
and many more clients
- Full FastCGI hosting support. Host PHP/Ruby
are highly addictive!
Once used, and you are hooked for life!
/Perl/Python applications in kbmMW!
- KBMMW V. 4.80 AMQP support
( Advanced Message Queuing Protocol)
- Added AMQP 0.91 client side gateway
support and sample.
- Updated StreamSec TLS transport plugin
component (by StreamSec).

COMPONENTS
4
- Improved performance on Indy TCP/IP
Client messaging transport for large number
of inbound messages.
DEVELOPERS
EESB, SOA,MoM, EAI TOOLS FOR INTELLIGENT SOLUTIONS. kbmMW IS THE PREMIERE N-TIER PRODUCT FOR DELPHI /
C++BUILDER BDS DEVELOPMENT FRAMEWORK FOR WIN 32 / 64, .NET AND LINUX WITH CLIENTS RESIDING ON WIN32 / 64,
.NET, LINUX, UNIX MAINFRAMES, MINIS, EMBEDDED DEVICES, SMART PHONES AND TABLETS.

Anda mungkin juga menyukai