Skip to content

This repository contains Free Pascal code snippets that I've put together while learning.

License

Notifications You must be signed in to change notification settings

ikelaiah/free-pascal-snippets

Repository files navigation


Logo

Free Pascal Snippets

A collection of Free Pascal snippets that I put together while learning Object Pascal.
This repo is part of my study note — Free Pascal and Lazarus Cookbook.
Explore the docs »

Explore Contents · Read Free Pascal and Lazarus Cookbook · View Cookbook repo

About The Project

Demo Screen 1

This repository contains a collection of Free Pascal code snippets (work in progress) that I put togehter while studying Object Pascal with the Lazarus IDE and the Free Pascal Compiler (FPC).

It is a part of the Free Pascal and Lazarus Cookbook project.

(back to top)

Contents

Code Structure

All about structuring Pascal source files.

  • ProgramStructure. Structure of a Pascal program.
  • SimpleProgram. An example of a Pascal program.
  • UnitStructure. Structure of a Unit file.
  • SimpleProgramWithUnit. An example of creating and using a unit file.

Hello World

The starting point for everyone.

  • HelloWorldSimple. An example of a Hello World program.
  • HelloWorldPause. An example of a Hello World program and pause console at the end.
  • HelloWorldAlt. An example of a Hello World program with more readable directives.

Command Line Arguments

All about parsing command line arguments and options.

  • CLSimple. Parsing arguments using ParamStr and ParamCount.
  • GetOptSimple. An example of using GetOpt to parse command line flags.

Date Time

  • DateTimeBenchmark. A simple way to profile your program.
  • DateTimeBetween. Finding time difference between two timestamps.
  • DateTimeComparison. Comparing date times.
  • DateTimeCurrent. Getting the current date time.
  • DateTimeUnix. Getting date time in unix notation.
  • ParseDate. A simple example of parsing a string into a TDateTime object using ScanDateTime.

File Handling

All about file handling; reading and writing.

  • ClassicNewTextFile. Create a new text file the classical way.
  • ClassicNewTextFileOrganised. Create a new text file the classical way but more organised.
  • TFileStreamNewTextFile. Create a text file the object way.
  • TFileStreamNewTextFileOrganised. Create a text file the object way but more organised.
  • ClassicCreateBlankTextFile. The classical way of creating a blank file.
  • TFileStreamCreateBlankTextFile. The object way of creating a blank file.
  • TStreamReaderReadFile. Read a text file using TStreamReader.
  • TStreadReaderReadFileTidy. Read a text file using TStreamReader but more tidy.

FuncProc

  • ParamModifierConst. Ensuring function arguments won't be changed in a function or procedure.
  • ParamModifierVar. Moifying named parameters from a function or procedure.

Immutability

Ensuring const behaving like const in other programing languages.

Loops

  • Loops - Examples of loops in Free Pascal.

Lists

All about building lists in Free Pascal.

  • StaticArray. Building and processing static arrays.
  • DynamicArray. Building and processing dynamic arrays.
  • StringList. Create a list of string.
  • SimpleIntegerList. Create a list of integer using Generics.Collections unit and sort it.
  • TListCustomComparison. Crete a custom comparer for a TList<T> list.
  • FGLIntegerList. Create a list of integer using fgl unit and sort it.
  • AppendFPGList. Append two TFPGList<integer> lists.
  • LGIntegerList. Create a list of integer using TGVector<T> from LGenerics unit.
  • LGIntegerListSort. Sort a TGVector<integer> list.

Numbers

All things about numbers.

  • RandomNumberSimple. Generate a random number, simplest way.
  • RandomNumberBetween. Generate a random integer number between two numbers.
  • RandomRealNumberBetween. Generate a random real number between two numbers.
  • RandomRealNumberList. Create a list of random number using LGenerics.
  • RandomRealNumberListv2. Create a list of random number using a dynamic array.

Directory, Paths and Filenames (DirsPathsFiles)

Snippets relating to Folders, Paths and Filesnames.

  • CheckDirExists - Check if a folder exists.
  • GetHomeDir - Get user home dir in Free Pascal.
  • ListAllFiles - List files recursively using wildcards.

Regex

Examples on using regex expressions.

  • RegexSimpleExample. A simple pattern matching using regex.
  • ReplaceDateSeparators. An example of replacing date separators using regex.

Threading

Using threads in Free Pascal.

  • CreateThreads. A simple example of creating threads using TThread.

(back to top)

Networking

Getting Started

Prerequisites

Install both the Free Pascal Compiler (FPC) and Lazarus IDE. Use fpcupdeluxe.

You can find the latest installer here; https://github.com/LongDirtyAnimAlf/fpcupdeluxe/releases.

Installation

None. Just download or git clone the repo to your local drive.

(back to top)

Usage

Using Lazarus IDE

  1. Launch your Lazarus IDE.
  2. Top bar menu, go to Projects - Open Project ....
  3. Navigate to the snippet folder you'd like to see, and open the .lpi file.

Using your favourite text editor

  1. Navigate to an example folder.
  2. Open the .lpr file. This is the program source code.

License

Distributed under the MIT License. See LICENSE.md for more information.

(back to top)

Contact

Iwan Kelaiah - [email protected]

Project Link: https://github.com/ikelaiah/free-pascal-snippets

(back to top)

Acknowledgments

(back to top)

About

This repository contains Free Pascal code snippets that I've put together while learning.

Topics

Resources

License

Stars

Watchers

Forks

Languages