D7X – Delphi 7 eXtension library
  1. 1. Structure
  2. 2. Classes
    1. 2.1. CallbackHash.pas
    2. 2.2. ColorConsole.pas
    3. 2.3. CommandLine.pas
      1. 2.3.1. Example
    4. 2.4. DataProviders.pas
    5. 2.5. FileMask.pas
    6. 2.6. FileStreamW.pas
    7. 2.7. IniFilesW.pas
    8. 2.8. SQLite.pas
    9. 2.9. Stacks.pas
    10. 2.10. Streamer.pas
    11. 2.11. StringsW.pas
    12. 2.12. Threads.pas
  3. 3. Components
    1. 3.1. Labels.pas
    2. 3.2. MenuSpeedButton.pas
    3. 3.3. PNGSpeedButton.pas
    4. 3.4. UnicodeDialogs.pas
  4. 4. Functions
    1. 4.1. Base64.pas
    2. 4.2. CRC32.pas
    3. 4.3. PopupMenuEvents.pas
    4. 4.4. RPNit.pas
    5. 4.5. StrConv.pas
    6. 4.6. InetUtils.pas
    7. 4.7. StringUtils.pas
    8. 4.8. Utils.pas

During my long and fruity friendship with Delphi, particularly the 7th version, I have colelcted a large number of helpful standalone classes, VCL components and functions that I’ve been using in various projects. Not so long ago I decided to put them in a separate build which I called D7X (aka «Delphi 7 eXtension» library).

One of the main points behind D7X is complete Unicode support in each and every component. You will see that pascalString’s are extremely rare in the code.

http://proger.i-forge.net/Мои проги/Delphi/D7X/github.png

It is available from GitHub.

Structure

D7X contains lots of handy stuff which is too troublesome (and probably not necessary) to describe in details so only a brief overview is given below. Some of D7X classes have separate articles already published and they will be listed as well.

Directory structure is as follows:

Classes

CallbackHash.pas

This unit contains two generic «callback» classes.

TCallbackHash – a collection of object callbacks (TMethod); each callback might have a name (Key). After a callback was added it can be retrieved using ToMethod and ToMethodBy methods that will set given variable to the callback; that variable can be used as a function to call the method.

TCallbackHash has most of standard string list features – callbacks can be added, removed, replaced; their names can be duplicated or case-insensitive.

The second class – TArgList – represents an abstract «argument list». Its constructor gets a string that is then parsed into ordered arguments (using a virtual method, by default splitting it by spaces). Then this object can be passed around and its arguments can be accessed as strings or integers (raising exceptions if argument type is invalid or no argument with given index existed).

ColorConsole.pas

http://proger.i-forge.net/Мои проги/Delphi/D7X/colcon.png

ColorConsole is a complex unit implementing sophisticated and extensible formatted console output. It allows line centering, colorizing, repeating and so on.

More details – in its own article.

CommandLine.pas

A set of classes for creating console applications in Delphi. It handles localization, console output including formatting using ColorConsole.pas, exception handling, command line processing (options, aliases, etc.) and other common tasks.

Main classes present in this unit are:

Example

This is a basic console application using CommandLine.pas:

pascalprogram D7X_CL;

{$APPTYPE CONSOLE}

uses Windows, SysUtils, MMSystem, StringsW, CommandLine, MConfig, MTasks;

TD7X_CL = class (TCLColorApplication)
protected
  function TryDoingTask(Task: WideString): Boolean; override;
public
  procedure SetInfo; override;
end;

{ TD7X_CL }

procedure TD7X_CL.SetInfo;
begin
  Name := 'Test D7X console app';
  Version := $0102;         // v1.02
  Author := 'Myself';
  WWW := 'http://proger.i-forge.net';
  BuildDate := $21022012;   // DD MM YYYY

  Help := 'Help text goes here.';
  HelpDetails := 'This is shown when ran as "help" task.';

  TaskAliases['h'] := 'help';
end;

function TD7X_CL.TryDoingTask(Task: WideString): Boolean;
begin
  Result := True;

  if Task = 'test' then
    ConsoleWriteLn('{<{wi Test} task called.}')
    else
      Result := inherited TryDoingTask(Task);
end;

{ Entry point }

var
  App: TD7X_CL;
begin
  App := TD7X_CL.Create;
  try
    App.Run;
  finally
    ExitCode := App.ExitCode;
    App.Free;
  end;
end.

DataProviders.pas

«Data providers» is my version of Delphi Streams. Standard streams lack some really useful methods and so I’d decided to make my own variant while writing one of my VN tools.

Basic TDataProvider declares methods for writing numbers, ANSI and Unicode strings and arrays, saving and restoring stream positions, comparing current input, copying data from other data provider or stream, saving current data to a file, etc.

FileMask.pas

This small unit is used to resolve file name masks with controlled recursion depth, case sensitivity and other parameters.

It contains two classes:

Typically you would create an instance of TMaskResolver, sets its Mask to some string and then either use HasMore and Next methods within a loop or use single PutNextTo method like this:

pascalvar
  FileName: WideString;
...
  while MaskResolver.PutNextTo(FileName) do
    Memo1.Lines.Add(FileName);

FileStreamW.pas

One of the most useful classes – Unicode TFileStream. Apart from Unicode Create method it adds several others:

LoadUnicodeFrom
Class function used to load an entire text file into a string. It hanles conversions from the following codepages:
  1. UTF-8 – as detected by EF BB BF signature;
  2. Unicode – FF FE signature;
  3. Unicode Big-Endian – FE FF signature;
  4. ANSI (OEM) codepage. Depending on the passed AsIsAnsi argument (which is False by default) FileStreamW will either convert file from OEM to Unicode or leave it as is and simply compliment each of its bytes with high-order zero byte.
CreateCustom
Opens file in fmCreate mode with given share access mode (standard TFIleStream.Create always creates files with exclusive access). Also allows one new flag – fmForcePath – which will create file path if it didn’t exist before opening it.

IniFilesW.pas

Ported Unicode IniFiles.pas. Uses ported StringsW.pas. In general it’s identical to the standard Delphi unit but extended with several new methods (e.g. CopyAllFrom, ReadSectionsPrefixed, etc.) added to the end of class declarations.

SQLite.pas

Delphi 7 interface to SQLite 3 database engine.

Stacks.pas

Lightweight number and string stack implementations. Unlike standard Delphi TStack and children these are implemented using contiguous memory blocks (arrays) instead of lists.

Streamer.pas

A template-class for writing binary data into a stream. It handles file header («magic» signature, version and flags), compression (using Zlib) and checksum (using Adler32 from Zlib).

Works for both reading and writing – reading verifies stream and raises exceptions on wrong signature, checksum or other inconsistency.

The main goal of this class is to provide uniform way to read and write binary data delegating handling of compression and other wrapper features to a separated class.

StringsW.pas

Unicode version of standard Delphi TStrings classes from Classes.pas. Similarly to IniFilesW.pas this unit adds several helpful methods (like CopyTo, LineBreak, JoinValues, etc.) to the end of some -W classes.

Threads.pas

Two lightweight wrappers for WinAPI threads and multimedia timers. Note that the latter uses timeSetEvent and other functions that have become obsolete by now.

TSingleThread provides means for starting, terminating, waiting for a single thread, getting its exit code and attaching OnFinish/OnException callbacks.

TMMTimer is similar, accepting a procedure or object callback and calling it with specified intervals.

Components

Labels.pas

This unit contains several Unicode label VCL components:

MenuSpeedButton.pas

This simple component extends TSpeedButton with PopupMenu property that is shown under the button when it’s clicked. The menu can be customized to show under a different control and on its left, right or center.

PNGSpeedButton.pas

Extends TSpeedButton with many useful properties allowing to:

UnicodeDialogs.pas

One of the first D7X units – ported Unicode versions of standard TOpenDialog and TSaveDialog. Has the same properties but all of them including Title and Files are Unicode (WideString).

Functions

Base64.pas

This unit implements Base64 algorithm using Windows CryptoAPI (if it’s available (since Windows XP)) or 3rd party Base64 implementation in native Delphi code. Both parts have been tested to produce matching results (see Tests\Base64).

Main exported functions are:

pascalfunction Base64Encode(Binary: String): String;
function Base64Decode(Str: String): String;

// use CryptoAPI for calculating Base64 if it's available (enabled by default).
procedure TryToUseCrypt32;
// force using built-in Base64 implementation.
procedure DoNotUseCrypt32;

CRC32.pas

3rd party CRC32 implementation using preinitialized CRC table.

PopupMenuEvents.pas

Replaces standard Delphi VCL Popuplist with custom object that activates 3 global callbacks:

RPNit.pas

Reverse Polish Notation unIT – or simply RPNit is an engine for calculating expressions written in postfix or notation. It can be extended with custom operators, functions, variables and even operand types.

Technically this is a set of classes but it was expected to be internal so that only a bunch of top-level functions would be exported.

It is used in my ApiHook – Win32 API hooking tool and Lightpath projects.

StrConv.pas

Small wrapper around standard Windows MultiByteToWideChar and WideCharToMultiByte functions. Apart from this it also converts codepage and charset names into IDs and vice-versa by looking up the registry at HKCL\MIME\Database\Codepage\ and Charset\.

InetUtils.pas

A great variety of Internet-related functions – downloading, uploading, MIME encoding, query building, URL encoding and so on. Mostly wraps around WinAPI functions like InternetReadFile. For example, you can fetch a remote URL in one call with pascalInetDownloadTo('temp.html', 'http://google.com');.

StringUtils.pas

One of the two core function units (see Utils.pas) – a collection of all possible functions dealing with Unicode strings – from PosW, Expode, DetectEolnStyleIn and FormatSize to MaskMatch (wildcard matching), Quote and WrapText.

Utils.pas

One of the two core function units (see StringUtils.pas) – general-purpose routines that don’t primarily deal with strings (for this StringUtils.pas is used).

This unit contains functions like WriteArray, ParamStrW, ExtractFilePath, CopyToClipboard, ReadRegValue, GetProcessModules and others.