Skip to content

karoloortiz/Delphi_Utils_Library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

255 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

KLib - Modern Delphi Utility Library

Version License Delphi Express-like API Async-Await Json Serialization Template Engine

A comprehensive utility library for Delphi providing async/await patterns, Json serialization/deserialization, database abstractions, Windows services, Http server Express.js - like, template engine, networking, Utils functions and UI components.


✨ Features

  • πŸ”„ Async/Promise - JavaScript-style Promises with then/catch/finally chaining
  • 🌐 REST API Server - Express.js-like HTTP server with routing, middleware, and helpers
  • πŸͺŸ Windows Services - Complete service framework with threading and HTTP adapters
  • πŸ”§ 100+ Validation Functions - Comprehensive validation library for files, services, networks
  • 🎨 UI Components - Ready-to-use VCL forms (Message, Wait, RTF, Presentation)
  • πŸ“ Fluent String API - Method chaining for string operations with C#-like extensions
  • 🧡 Threading - Enhanced thread management with events and callbacks
  • πŸ“Š JSON Serialization and Deserialization - Attribute-based RTTI with validation
  • πŸ”Œ Dependency Injection - Lightweight DI container with singleton/transient lifetimes
  • πŸ“… Date Range Utilities - Advanced algorithms for splitting and processing date ranges
  • πŸ–ΌοΈ Helper Extensions - Class helpers for arrays, string lists, and UI controls
  • πŸ“„ Template Engine - Jinja2-inspired template rendering with inheritance, macros, 48+ filters, autoescape, sandbox, and caching
  • πŸ—„οΈ Database - Coming soon: MySQL, SQLite, PostgreSQL unified interface

🎯 Library Philosophy

Born from Practical Experience

KLib emerged from years of real-world Delphi development, addressing a common challenge faced by developers: repetitive code patterns that resurface across multiple projects. Instead of rewriting the same validation logic, string utilities, async patterns, and service frameworks for every new application, KLib consolidates these solutions into a single, well-architected library.

The DRY Principle in Action

Don't Repeat Yourself - this fundamental software development principle guided KLib's creation. By extracting common functionality that Delphi developers frequently reimplement, KLib provides:

  • Consistent implementations of complex patterns
  • Battle-tested solutions for recurring challenges
  • Maintainable codebase with centralized improvements
  • Rapid development through ready-to-use components

Clean Code Foundation

Every component in KLib adheres to clean code principles:

// Before: Repetitive validation code across projects
if not DirectoryExists('C:\Data') then
  raise Exception.Create('Directory not found');
if not FileExists('config.ini') then
  raise Exception.Create('Config file missing');
// ... more repetitive checks

// After: Clean, expressive validation
validateThatDirectoryExists('C:\Data');
validateThatFileExists('config.ini');
validateThatServiceIsRunning('MySQL');

Unified Solution for Common Challenges

KLib addresses the fragmentation of utility code that typically spreads across:

  • Copy-pasted code snippets between projects
  • Inconsistent implementations of similar functionality
  • Reinvented solutions for solved problems
  • Disconnected utility functions without unified architecture

πŸš€ Why Choose KLib?

Enterprise-Grade Delphi Solutions

KLib delivers modern programming paradigms to Delphi development, bringing patterns typically found in JavaScript, C#, and Python to the Object Pascal ecosystem.

1. JavaScript-Style Promises for Delphi

Full ES6 Promise implementation for asynchronous programming:

TPromise.Create(procedure(resolve, reject: TCallBack)
  begin
    // Background work
    DoHeavyProcessing();
    resolve('Done!');
  end)
  ._then(procedure(result: string)
    begin
      ShowMessage(result);
    end)
  ._catch(procedure(error: string)
    begin
      LogError(error);
    end)
  ._finally;

// Promise.All - wait for multiple operations
TPromise.all([FetchUsers, FetchOrders, FetchProducts])
  ._then(procedure(msg: string)
    begin
      RefreshUI();
    end)
  ._finally;

Clean asynchronous code without callback nesting complexity.


2. Fluent String API

Method chaining for string operations with C#-inspired syntax:

uses KLib.mystring;

var sql: mystring;
sql := 'SELECT * FROM users WHERE id = :id AND status = :status';
sql.setParamAsInteger('id', 123)
   .setParamAsString('status', 'active')
   .saveToFile('query.sql');

// HTML escaping + quoting in one line
var safeHTML := userInput.escapeHTML().doubleQuote();

// Encryption/decryption built-in
var encrypted := password.encrypt('mySecretKey');
var decrypted := encrypted.decrypt('mySecretKey');

Streamlined string manipulation with reduced intermediate variable overhead.


3. Comprehensive Validation Framework

Extensive validation library with 100+ functions:

uses KLib.Validate;

// Service validation
validateThatServiceExists('MyWindowsService');
validateThatServiceIsRunning('MySQL');

// Security validation
validateThatUserIsAdmin();
validateThatUserBelongsToGroup('Administrators');

// Path validation
validateThatDirectoryExists('C:\Data');
validateThatFileExists('config.ini');
validateThatPathExists('\\server\share');

// FTP/Network validation
validateThatFTPCredentialsAreValid(host, user, pass);
validateThatPortIsOpen('localhost', 8080);

// XML validation
validateThatXMLIsWellFormed(xmlString);

Production-ready validation eliminating repetitive validation code.


4. Attribute-Based JSON Serialization

RTTI-powered serialization with built-in validation:

uses KLib.Generics.JSON, KLib.Generics.Attributes;

type
  TUserDTO = record
    [Required]
    [CustomName('user_id')]
    id: Integer;

    [Required]
    [MinValue(3)]
    [MaxValue(50)]
    username: string;

    [DefaultValue('user@example.com')]
    email: string;

    [Ignore]  // Won't be serialized
    internalFlag: Boolean;
  end;

var
  user: TUserDTO;
  json: string;
begin
  // Automatic validation + serialization
  json := TJSONGenerics.getJSONAsString<TUserDTO>(user);

  // Automatic deserialization + validation
  user := TJSONGenerics.getParsedJSON<TUserDTO>(json);
end;

Declarative data contracts with automatic validation enforcement.


5. Zero-Boilerplate Windows Services

Streamlined Windows service creation:

procedure TMyService.ServiceCreate(Sender: TObject);
begin
  serviceApp := TThreadAdapter.Create(
    procedure  // Your service logic
    begin
      while not TMyThread(TThread.CurrentThread).isStopped do
      begin
        ProcessQueue();
        Sleep(5000);
      end;
    end,
    procedure(error: string)  // Error handling
    begin
      LogToEventLog(error);
    end
  );
end;

// Install: MyService.exe --install
// Uninstall: MyService.exe --uninstall

Rapid service development focusing on business logic rather than infrastructure.


6. Date Range Processing

Advanced date algorithms for business applications:

uses KLib.DateTimeUtils;

// Split date range into months
var months := splitByMonths(StartDate, EndDate);

// Divide date range into N parts
var divisions := divideDateRange(StartDate, EndDate, 10);

for var period in months do
begin
  GenerateReport(period.StartDate, period.EndDate);
end;

Enterprise reporting utilities for common date range challenges.


7. Express.js-Style REST API

Modern HTTP server with familiar web framework patterns:

uses KLib.MyIdHTTPServer;

var app := TMyIdHTTPServer.create(8080);
var router := app.getRouter;

// RESTful routes with parameters
router.get('/api/users/:id', procedure(req, res, params)
begin
  var userId := params['id'];
  var user := GetUserById(userId);
  res.jsonSuccess(user);
end);

router.post('/api/users', procedure(req, res, params)
begin
  var userData := req.parseJSON;
  CreateUser(userData);
  res.jsonSuccess(nil, 'User created');
end);

// Middleware support
app.use(procedure(req, res, next)
begin
  LogRequest(req.Document);
  next();
end);

// CORS, static files, error handling
app.enableCors := true;

Web-developer friendly API patterns for building REST services.


8. Class Helper Extensions

Extend built-in Delphi types with useful methods:

uses KLib.ArrayHelper, KLib.StringListHelper;

// Array helpers
if myArray.Contains(searchValue) then
  index := myArray.IndexOf(searchValue);

// StringList helpers
myList.AddIfNotExists('unique-value');
var items := myList.ToArray;

Enhanced productivity with intuitive extension methods.


9. Dependency Injection Container

Lightweight DI container with lifetime management:

uses KLib.DiContainer;

// Register services
Container.RegisterSingleton<ILogger>(TFileLogger);
Container.RegisterTransient<IRepository>(TUserRepository);

// Resolve dependencies
var logger := Container.Resolve<ILogger>;
var repo := Container.Resolve<IRepository>;

Testable architecture with minimal configuration overhead.


10. Jinja2-Inspired Template Engine

Full-featured template engine for generating HTML, emails, reports or any text output:

uses KLib.Template;

type
  TOrder = record
    customer: string;
    items: TArray<TItem>;
    total: Double;
  end;

var
  _order: TOrder;
begin
  // Render from file (auto-cached after first call)
  Result := TTemplate.renderFromFile<TOrder>('templates\order.html', _order);
end;

order.html:

<h1>Order for {{ customer }}</h1>
<table>
  {% for item in items %}
  <tr>
    <td>{{ item.name | escape }}</td>
    <td>{{ item.price | round }}</td>
  </tr>
  {% endfor %}
</table>
<p>Total: {{ total | default(0) }}</p>

Key features:

  • 23 statement types: if/elif/else, for, set, macro, block/extends, include/import, switch/case, while, with, raw, compress, attempt/recover, and more
  • 48+ built-in filters: string, numeric, array, hash (md5/sha256), date, base64, and more
  • Template inheritance: {% extends 'base.html' %} + {% block %} + {{ super() }}
  • Macros: reusable components with default/named arguments
  • Autoescape: automatic HTML escaping with | safe bypass
  • Sandbox mode: restrict filesystem access, limit recursion and output
  • Thread-safe: MREW lock, multiple renders can execute in parallel
  • Token caching: files parsed once, re-evaluated with different data
  • Custom filters: register your own string β†’ string or TValue β†’ TValue filters
  • Error reporting: ETemplateError with template name, line and column

See KLib.Template.Guide.md for the full syntax reference.


Production-Ready Quality

  • βœ… Memory Safe: All resources properly managed with FreeAndNil
  • βœ… Thread Safe: Promise operations run in isolated threads with automatic COM initialization
  • βœ… Exception Safe: try-finally blocks throughout the codebase
  • βœ… Battle Tested: Production-proven in enterprise environments since 2020
  • βœ… Clear BSD License: Commercial-friendly licensing without restrictions

πŸš€ Quick Start

Promises & Async Programming

uses KLib.Promise, KLib.Asyncify;

// Simple promise
procedure ExecuteAsync;
var
  promise: TPromise;
begin
  promise := TPromise.Create(
    procedure(resolve: TCallBack; reject: TCallBack)
    begin
      // Do work in background thread
      Sleep(1000);
      resolve('Done!');
    end,
    procedure(value: string)  // then
    begin
      ShowMessage('Success: ' + value);
    end,
    procedure(value: string)  // catch
    begin
      ShowMessage('Error: ' + value);
    end
  );
end;

// Promise chaining
TPromise.Create(...)
  ._then(procedure(value: string)
    begin
      // Step 1
    end)
  ._then(procedure(value: string)
    begin
      // Step 2
    end)
  ._finally;

// Promise.All - wait for multiple operations
TPromise.all([Job1, Job2, Job3])
  ._then(procedure(value: string)
    begin
      ShowMessage('All completed!');
    end)
  ._finally;

Windows Services Development

uses KLib.MyService, KLib.ServiceApp.ThreadAdapter;

// Create a Windows service with background thread
procedure TMyCustomService.ServiceCreate(Sender: TObject);
begin
  serviceApp := TThreadAdapter.Create(
    procedure  // Main executor
    begin
      while not TMyThread(TThread.CurrentThread).isStopped do
      begin
        // Your service logic here
        Sleep(1000);
      end;
    end,
    procedure(msg: string)  // Reject callback
    begin
      LogError(msg);
    end
  );
end;

Professional UI Components

uses KLib.MessageForm, KLib.WaitForm;

// Show message dialog
var
  params: TMessageFormCreate;
  result: TMessageFormResult;
begin
  params.title := 'Confirm Action';
  params.text := 'Are you sure?';
  params.confirmButtonCaption := 'Yes';
  params.cancelButtonCaption := 'No';
  params.colorRGB := '52, 152, 219';  // Blue theme

  result := TMessageForm.showMessage(params);
  if result.isConfirmButtonPressed then
    ShowMessage('Confirmed!');
end;

// Execute with loading dialog
TWaitForm.showExecuteMethodAndWait(
  procedure
  begin
    // Long-running operation
    ProcessLargeFile();
  end,
  'Processing, please wait...'
);

πŸ“¦ Installation

Option 1: Manual Installation

  1. Clone or download this repository
  2. Add src/boundaries/Delphi_Utils_Library to your Delphi library path
  3. Add required units to your uses clause

Option 2: Git Submodule Integration

git submodule add https://github.com/karoloortiz/Delphi_Utils_Library.git libs/klib

Add libs/klib to your Delphi library path.


βš™οΈ Optional Dependencies

UI Components Requirements

Define these conditionals in your project options for enhanced UI components:

Project Options β†’ Delphi Compiler β†’ Conditional defines:

KLIB_RAIZE

Supported third-party libraries:

  • DevExpress VCL (v15.2.4+) - Advanced UI controls - DevExpress
  • Raize Components - Professional VCL components - Raize Software

Note: FormUtils components require these symbols AND corresponding libraries. Core KLib functionality works without third-party dependencies.

Network Security Requirements

  • OpenSSL Libraries - HTTPS/SSL support - Indy OpenSSL Binaries Clone repo with assets (git lfs), and add KLib.Assets.rc to your project.

πŸ“š Core Modules

Async & Threading

Unit Description
KLib.Promise ES6-style Promise implementation
KLib.Promise.All Wait for multiple promises
KLib.Asyncify Convert any method to async
KLib.AsyncMethod Object-oriented async wrapper
KLib.AsyncMethods Parallel execution manager
KLib.MyThread Enhanced thread with stop support
KLib.ListOfThreads Thread pool management
KLib.MyEvent Custom event synchronization

Windows Services

Unit Description
KLib.MyService Base Windows service class
KLib.WindowsService Service installation/management
KLib.ServiceApp.ThreadAdapter Thread-based service adapter
KLib.ServiceApp.HttpServerAdapter HTTP server adapter
KLib.Windows.EventLog Windows Event Log integration

UI Forms (VCL)

Unit Description
KLib.MessageForm Customizable message dialog
KLib.WaitForm Loading/progress form
KLib.RTFForm RTF viewer with confirmation
KLib.PresentationForm Multi-slide presentation/wizard

Networking (Indy)

Unit Description
KLib.MyIdHTTP Enhanced HTTP client
KLib.MyIdHTTPServer HTTP server wrapper
KLib.MyIdFTP FTP client wrapper
KLib.Indy Indy utilities

Data & Serialization

Unit Description
KLib.Generics.Json Generic Json serialization
KLib.Generics.Ini Generic INI mapping
KLib.Generics.ShellParams Command-line parsing
KLib.XML XML utilities
KLib.IniFiles INI file helpers

Utilities

Unit Description
KLib.Utils General utilities
KLib.Common Common functions
KLib.FileSystem FileSystem functions
KLib.DateTimeUtils Date / time utils functions
KLib.StringUtils String utils functions
KLib.Csv Csv utils functions
KLib.Common Common functions
KLib.Types Common types and interfaces
KLib.Constants Application constants
KLib.Validate Validation utilities
KLib.mystring String manipulation
KLib.sqlstring SQL string helpers
KLib.Math Math utilities
KLib.Graphics Color and image helpers
KLib.MyEncoding Encoding utilities
KLib.ArrayHelper Array extensions
KLib.StringListHelper TStringList extensions
KLib.CheckBoxHelper TCheckBox extensions

Windows Integration

Unit Description
KLib.Windows Windows API utilities
KLib.MemoryRam Memory/Ram info
KLib.VC_Redist Visual C++ Redistributable
KLib.ZplPrinter ZPL printer support

Template Engine

Unit Description
KLib.Template Public API β€” render, renderFromFile, configuration
KLib.Template.Lexer Tokenizer with custom delimiter support
KLib.Template.Evaluator Expression evaluator and statement processor
KLib.Template.Filters 48+ built-in filters and custom filter registry
KLib.Template.Cache Token cache with file modification tracking
KLib.Template.Exceptions ETemplateError with template name, line, column

Dependency Injection

Unit Description
KLib.DiContainer DI container (Singleton/Transient)

πŸ“– Examples & Documentation

Explore the examples/ directory for comprehensive implementations:

  • Async Examples - Promises, Promise.All, async methods
  • Service Examples - Windows service templates
  • Form Examples - UI component demos
  • Database Examples - Coming soon

πŸ› οΈ Compatibility Matrix

  • Delphi Versions: Delphi XE7 and later (tested up to Delphi 11.x)
  • Platforms: Windows (Win32, Win64)
  • Framework: VCL (FireMonkey/FMX not supported)

🀝 Contributing

We welcome community contributions! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“ License

This project is licensed under the Clear BSD License - see the LICENSE file for details.

Copyright (c) 2020 by Karol De Nery Ortiz LLave. All rights reserved.
zitrokarol@gmail.com

πŸ’‘ Support

If KLib enhances your Delphi projects:


πŸ—ΊοΈ Development Roadmap


Professional Delphi utilities for modern development

Made with ❀️ for the Delphi community