Olive.Random/Olive.Random.pas

121 lines
3.9 KiB
ObjectPascal
Raw Normal View History

2020-05-18 17:40:57 -07:00
{********************************************************}
{ }
{ Olive BBS }
{ }
{ Copyleft (ↄ) 2020 Olive BBS }
{ }
{ This file is part of Olive BBS }
{ }
{ Olive BBS is free software: you can redistribute it }
{ and/or modify it under the terms of the GNU General }
{ Public License as published by the Free Software }
{ Foundation, either version 3 of the License, or }
{ (at your option) any later version. }
{ }
{ Olive BBS is distributed in the hope that it will }
{ be useful, but WITHOUT ANY WARRANTY; without even }
{ the implied warranty of MERCHANTABILITY or FITNESS }
{ FOR A PARTICULAR PURPOSE. See the GNU General }
{ Public License for more details. }
{ }
{ You should have received a copy of the GNU General }
{ Public License along with Olive BBS. If not, see }
{ <http://www.gnu.org/licenses/>. }
{ }
{********************************************************}
{ ___ ___ ___ }
{ ( ).-. ( ) ( ) }
{ .--. | |( __)___ ___ .--. | |.-. | |.-. .--. }
{ / \| |(''"( )( / \| / \| / \ / _ \ }
{ | .-. | | | | | | | | .-. | .-. | .-. |. .' `. ; }
{ | | | | | | | | | | | | | | | | | | | || ' | | }
{ | | | | | | | | | | | |/ | | | | | | |_\_`.(___) }
{ | | | | | | | | | | | ' _.| | | | | | ( ). '. }
{ | ' | | | | | ' ' ; | .'.-| ' | | ' | || | `\ | }
{ ' `-' | | | | \ `' /' `-' ' `-' ;' `-' ; ; '._,' ' }
{ `.__.(___(___) '_.' `.__.' `.__. `.__. '.___.' }
{ }
{********************************************************}
{$mode objfpc}{$H+}
{$interfaces corba}
Unit Olive.Random;
interface
uses
Objects,
Classes,
SysUtils,
Olive.Random.RandomInterface,
{$IF DEFINED(LINUX)}
Olive.Random.LinuxRandom
{$ELSEIF DEFINED(WINDOWS)}
Olive.Random.WinRandom
{$ELSEIF DEFINED(BSD)}
Olive.Random.BSDRandom
{$ELSE}
Olive.Random.Generic;
{$ENDIF}
;
type
PRandom = ^TRandom;
TRandom = class (RandomTrait, RandomInterface)
private
FRandomGenerator : RandomInterface;
procedure SetRandomGenerator(GeneratorClass : RandomInterface);
public
constructor Init;
destructor Destroy; override;
procedure SetDefaultGenerator;
function GetBytes(NBytes : SizeUInt) : TBytes;
function GetString(NBytes : SizeUInt) : AnsiString;
property RandomGenerator : RandomInterface read FRandomGenerator write SetRandomGenerator;
end;
implementation
constructor TRandom.Init;
begin
SetDefaultGenerator;
end;
destructor TRandom.Destroy;
begin
inherited Destroy;
end;
procedure TRandom.SetRandomGenerator(GeneratorClass : RandomInterface);
begin
FRandomGenerator := GeneratorClass;
end;
procedure TRandom.SetDefaultGenerator;
begin
{$IF DEFINED(LINUX)}
FRandomGenerator := LinuxRandom.Create;
{$ELSEIF DEFINED(WINDOWS)}
FRandomGenerator := WinRandom.Create;
{$ELSEIF DEFINED(BSD)}
FRandomGenerator := BSDRandom.Create;
{$ELSE}
FRandomGenerator := RandomGeneric.Create;
{$ENDIF}
end;
function TRandom.GetBytes(NBytes : SizeUInt) : TBytes;
begin
SetLength(Result, NBytes);
Result := FRandomGenerator.GetBytes(NBytes);
end;
function TRandom.GetString(NBytes : SizeUInt) : AnsiString;
begin
SetLength(Result, NBytes);
Result := FRandomGenerator.GetString(NBytes);
end;
end.