{*******************************************************}
{ }
{ CodeGear Delphi Runtime Library }
{ }
{ Copyright(c) 1995-2010 Embarcadero Technologies, Inc. }
{ }
{ Copyright and license exceptions noted in source }
{ }
{*******************************************************}
{*******************************************************}
{ System Utilities Unit }
{*******************************************************}
unit SysUtils;
{$H+,B-,R-}
{$WARN SYMBOL_PLATFORM OFF}
{$WARN UNSAFE_TYPE OFF}
interface
uses
{$IFDEF MSWINDOWS}
Windows,
{$ENDIF}
{$IFDEF POSIX}
Types,
{$IFDEF MACOSX}
MiniLibc,
{$ELSE !MACOSX}
Libc,
{$ENDIF !MACOSX}
{$ENDIF}
SysConst;
const
{ File open modes }
{$IFDEF POSIX}
fmOpenRead = O_RDONLY;
fmOpenWrite = O_WRONLY;
fmOpenReadWrite = O_RDWR;
// fmShareCompat not supported
{$IFDEF LINUX} // need to get MAC OS/X values
fmShareExclusive = $0010;
fmShareDenyWrite = $0020;
// fmShareDenyRead not supported
fmShareDenyNone = $0030;
{$ENDIF LINUX}
{$ENDIF}
{$IFDEF MSWINDOWS}
fmOpenRead = $0000;
fmOpenWrite = $0001;
fmOpenReadWrite = $0002;
fmShareCompat = $0000 platform; // DOS compatibility mode is not portable
fmShareExclusive = $0010;
fmShareDenyWrite = $0020;
fmShareDenyRead = $0030 platform; // write-only not supported on all platforms
fmShareDenyNone = $0040;
{$ENDIF}
{ File attribute constants }
faReadOnly = $00000001 platform;
faHidden = $00000002 platform;
faSysFile = $00000004 platform;
faVolumeID = $00000008 platform deprecated; // not used in Win32
faDirectory = $00000010;
faArchive = $00000020 platform;
faSymLink = $00000040 platform;
faNormal = $00000080 platform;
faTemporary = $00000100 platform;
faAnyFile = $000001FF;
{ Units of time }
HoursPerDay = 24;
MinsPerHour = 60;
SecsPerMin = 60;
MSecsPerSec = 1000;
MinsPerDay = HoursPerDay * MinsPerHour;
SecsPerDay = MinsPerDay * SecsPerMin;
SecsPerHour = SecsPerMin * MinsPerHour;
MSecsPerDay = SecsPerDay * MSecsPerSec;
{ Days between 1/1/0001 and 12/31/1899 }
DateDelta = 693594;
{ Days between TDateTime basis (12/31/1899) and Unix time_t basis (1/1/1970) }
UnixDateDelta = 25569;
type
TBytes = array of Byte;
{ Standard Character set type }
TSysCharSet = set of AnsiChar;
{ Set access to an integer }
TIntegerSet = set of 0..SizeOf(Integer) * 8 - 1;
{ Type conversion records }
WordRec = packed record
case Integer of
0: (Lo, Hi: Byte);
1: (Bytes: array [0..1] of Byte);
end;
LongRec = packed record
case Integer of
0: (Lo, Hi: Word);
1: (Words: array [0..1] of Word);
2: (Bytes: array [0..3] of Byte);
end;
Int64Rec = packed record
case Integer of
0: (Lo, Hi: Cardinal);
1: (Cardinals: array [0..1] of Cardinal);
2: (Words: array [0..3] of Word);
3: (Bytes: array [0..7] of Byte);
end;
{ General arrays }
PByteArray = ^TByteArray;
TByteArray = array[0..32767] of Byte;
PWordArray = ^TWordArray;
TWordArray = array[0..16383] of Word;
{ Generic procedure pointer }
TProcedure = procedure;
{ Generic filename type }
TFileName = type string;
{ Search record used by FindFirst, FindNext, and FindClose }
TSearchRec = record
Time: Integer;
Size: Int64;
Attr: Integer;
Name: TFileName;
ExcludeAttr: Integer;
{$IFDEF MSWINDOWS}
FindHandle: THandle platform;
FindData: TWin32FindData platform;
{$ENDIF MSWINDOWS}
{$IFDEF POSIX}
Mode: mode_t platform;
FindHandle: Pointer platform;
PathOnly: String platform;
Pattern: String platform;
{$ENDIF POSIX}
end;
{ FloatToText, FloatToTextFmt, TextToFloat, and FloatToDecimal type codes }
TFloatValue = (fvExtended, fvCurrency);
{ FloatToText format codes }
TFloatFormat = (ffGeneral, ffExponent, ffFixed, ffNumber, ffCurrency);
{ FloatToDecimal result record }
TFloatRec = packed record
Exponent: Smallint;
Negative: Boolean;
Digits: array[0..20] of AnsiChar;
end;
{ Date and time record }
TTimeStamp = record
Time: Integer; { Number of milliseconds since midnight }
Date: Integer; { One plus number of days since 1/1/0001 }
end;
{ MultiByte Character Set (MBCS) byte type }
TMbcsByteType = (mbSingleByte, mbLeadByte, mbTrailByte);
{ System Locale information record }
TSysLocale = packed record
DefaultLCID: Integer;
PriLangID: Integer;
SubLangID: Integer;
FarEast: Boolean;
MiddleEast: Boolean;
end;
{$IFDEF MSWINDOWS}
{ This is used by TLanguages }
TLangRec = packed record
FName: string;
FLCID: LCID;
FExt: string;
end;
{ This stores the languages that the system supports }
TLanguages = class
private
FSysLangs: array of TLangRec;
class destructor Destroy;
function LocalesCallback(LocaleID: PChar): Integer; stdcall;
function GetExt(Index: Integer): string;
function GetID(Index: Integer): string;
function GetLCID(Index: Integer): LCID;
function GetName(Index: Integer): string;
function GetNameFromLocaleID(ID: LCID): string;
function GetNameFromLCID(const ID: string): string;
function GetCount: integer;
public
constructor Create;
function IndexOf(ID: LCID): Integer;
property Count: Integer read GetCount;
property Name[Index: Integer]: string read GetName;
property NameFromLocaleID[ID: LCID]: string read GetNameFromLocaleID;
property NameFromLCID[const ID: string]: string read GetNameFromLCID;
property ID[Index: Integer]: string read GetID;
property LocaleID[Index: Integer]: LCID read GetLCID;
property Ext[Index: Integer]: string read GetExt;
end platform;
{$ENDIF}
{$IFDEF LINUX}
TEraRange = record
StartDate : Integer; // whole days since 12/31/1899 (TDateTime basis)
EndDate : Integer; // whole days since 12/31/1899 (TDateTime basis)
// Direction : Char;
end;
{$ENDIF}
{ Exceptions }
{$IFDEF MSWINDOWS}
PExceptionRecord = ^TExceptionRecord;
TExceptionRecord = record
ExceptionCode: Cardinal;
ExceptionFlags: Cardinal;
ExceptionRecord: PExceptionRecord;
ExceptionAddress: Pointer;
NumberParameters: Cardinal;
case {IsOsException:} Boolean of
True: (ExceptionInformation : array [0..14] of Longint);
False: (ExceptAddr: Pointer; ExceptObject: Pointer);
end;
{$ENDIF}
Exception = class(TObject)
private
FMessage: string;
FHelpContext: Integer;
FInnerException: Exception;
FStackInfo: Pointer;
FAcquireInnerException: Boolean;
class constructor Create;
class destructor Destroy;
protected
procedure SetInnerException;
procedure SetStackInfo(AStackInfo: Pointer);
function GetStackTrace: string;
{$IFDEF MSWINDOWS}
// This virtual function will be called right before this exception is about to be
// raised. In the case of an external non-Delphi exception, this is called soon after
// the object is created since the "raise" condition is already in progress.
procedure RaisingException(P: PExceptionRecord); virtual;
{$ENDIF}
public
constructor Create(const Msg: string);
constructor CreateFmt(const Msg: string; const Args: array of const);
constructor CreateRes(Ident: Integer); overload;
constructor CreateRes(ResStringRec: PResStringRec); overload;
constructor CreateResFmt(Ident: Integer; const Args: array of const); overload;
constructor CreateResFmt(ResStringRec: PResStringRec; const Args: array of const); overload;
constructor CreateHelp(const Msg: string; AHelpContext: Integer);
constructor CreateFmtHelp(const Msg: string; const Args: array of const;
AHelpContext: Integer);
constructor CreateResHelp(Ident: Integer; AHelpContext: Integer); overload;
constructor CreateResHelp(ResStringRec: PResStringRec; AHelpContext: Integer); overload;
constructor CreateResFmtHelp(ResStringRec: PResStringRec; const Args: array of const;
AHelpContext: Integer); overload;
constructor CreateResFmtHelp(Ident: Integer; const Args: array of const;
AHelpContext: Integer); overload;
destructor Destroy; override;
function GetBaseException: Exception; virtual;
function ToString: string; override;
property BaseException: Exception read GetBaseException;
property HelpContext: Integer read FHelpContext write FHelpContext;
property InnerException: Exception read FInnerException;
property Message: string read FMessage write FMessage;
property StackTrace: string read GetStackTrace;
property StackInfo: Pointer read FStackInfo;
{$IFDEF MSWINDOWS}
class var
// Hook this function to return an opaque data structure that contains stack information
// for the given exception information record. This function will be called when the
// exception is about to be raised or if this is an external exception such as an
// Access Violation, called soon after the object is created.
GetExceptionStackInfoProc: function (P: PExceptionRecord): Pointer;
// This function is called to return a string representation of the above opaque
// data structure
GetStackInfoStringProc: function (Info: Pointer): string;
// This function is called when the destructor is called to clean up any data associated
// with the given opaque data structure.
CleanUpStackInfoProc: procedure (Info: Pointer);
// Use this function to raise an exception instance from within an exception handler and
// you want to "acquire" the active exception and chain it to the new exception and preserve
// the context. This will cause the FInnerException field to get set with the exception
// in currently in play.
// You should only call this procedure from within an except block where the this new
// exception is expected to be handled elsewhere.
class procedure RaiseOuterException(E: Exception); static;
// Provide another method that does the same thing as RaiseOuterException, but uses the
// C++ vernacular of "throw"
class procedure ThrowOuterException(E: Exception); static;
{$ENDIF}
end;
EArgumentException = class(Exception);
EArgumentOutOfRangeException = class(EArgumentException);
EPathTooLongException = class(Exception);
ENotSupportedException = class(Exception);
EDirectoryNotFoundException = class(Exception);
EFileNotFoundException = class(Exception);
ENoConstructException = class(Exception);
ExceptClass = class of Exception;
EAbort = class(Exception);
EHeapException = class(Exception)
private
AllowFree: Boolean;
{$IFDEF MSWINDOWS}
protected
procedure RaisingException(P: PExceptionRecord); override;
{$ENDIF}
public
procedure FreeInstance; override;
end;
EOutOfMemory = class(EHeapException);
EInOutError = class(Exception)
public
ErrorCode: Integer;
end;
EExternal = class(Exception)
public
{$IFDEF MSWINDOWS}
ExceptionRecord: PExceptionRecord platform;
{$ENDIF}
{$IF defined(LINUX) or defined(MACOSX)}
ExceptionAddress: LongWord platform;
AccessAddress: LongWord platform;
SignalNumber: Integer platform;
{$IFEND LINUX or MACOSX}
end;
EExternalException = class(EExternal);
EIntError = class(EExternal);
EDivByZero = class(EIntError);
ERangeError = class(EIntError);
EIntOverflow = class(EIntError);
EMathError = class(EExternal);
EInvalidOp = class(EMathError);
EZeroDivide = class(EMathError);
EOverflow = class(EMathError);
EUnderflow = class(EMathError);
EInvalidPointer = class(EHeapException);
EInvalidCast = class(Exception);
EConvertError = class(Exception);
EAccessViolation = class(EExternal);
EPrivilege = class(EExternal);
EStackOverflow = class(EExternal)
end deprecated;
EControlC = class(EExternal);
{$IFDEF LINUX}
EQuit = class(EExternal) end platform;
{$ENDIF}
{$IFDEF POSIX}
ECodesetConversion = class(Exception) end platform;
{$ENDIF POSIX}
EVariantError = class(Exception);
EPropReadOnly = class(Exception);
EPropWriteOnly = class(Exception);
EAssertionFailed = class(Exception);
{$IFNDEF PC_MAPPED_EXCEPTIONS}
EAbstractError = class(Exception) end platform;
{$ENDIF}
EIntfCastError = class(Exception);
EInvalidContainer = class(Exception);
EInvalidInsert = class(Exception);
EPackageError = class(Exception);
EOSError = class(Exception)
public
ErrorCode: DWORD;
end;
{$IFDEF MSWINDOWS}
EWin32Error = class(EOSError)
end deprecated;
{$ENDIF}
ESafecallException = class(Exception);
EMonitor = class(Exception);
EMonitorLockException = class(EMonitor);
ENoMonitorSupportException = class(EMonitor);
EProgrammerNotFound = class(Exception);
{$IF defined(LINUX) or defined(MACOSX)}
{
Signals
External exceptions, or signals, are, by default, converted to language
exceptions by the Delphi RTL. Under Linux, a Delphi application installs
signal handlers to trap the raw signals, and convert them. Delphi libraries
do not install handlers by default. So if you are implementing a standalone
library, such as an Apache DSO, and you want to have signals converted to
language exceptions that you can catch, you must install signal hooks
manually, using the interfaces that the Delphi RTL provides.
For most libraries, installing signal handlers is pretty
straightforward. Call HookSignal(RTL_SIGDEFAULT) at initialization time,
and UnhookSignal(RTL_SIGNALDEFAULT) at shutdown. This will install handlers
for a set of signals that the RTL normally hooks for Delphi applications.
There are some cases where the above initialization will not work properly:
The proper behaviour for setting up signal handlers is to set
a signal handler, and then later restore the signal handler to its previous
state when you clean up. If you have two libraries lib1 and lib2, and lib1
installs a signal handler, and then lib2 installs a signal handler, those
libraries have to uninstall in the proper order if they restore signal
handlers, or the signal handlers can be left in an inconsistent and
potentially fatal state. Not all libraries behave well with respect to
installing signal handlers. To hedge against this possibility, and allow
you to manage signal handlers better in the face of whatever behaviour
you may find in external libraries, we provide a set of four interfaces to
allow you to tailor the Delphi signal handler hooking/unhooking in the
event of an emergency. These are:
InquireSignal
AbandonSignalHandler
HookSignal
UnhookSignal
InquireSignal allows you to look at the state of a signal handler, so
that you can find out if someone grabbed it out from under you.
AbandonSignalHandler tells the RTL never to unhook a particular
signal handler. This can be used if you find a case where it would
be unsafe to return to the previous state of signal handling. For
example, if the previous signal handler was installed by a library
which has since been unloaded.
HookSignal/UnhookSignal setup signal handlers that map certain signals
into language exceptions.
See additional notes at InquireSignal, et al, below.
}
const
RTL_SIGINT = 0; // User interrupt (SIGINT)
RTL_SIGFPE = 1; // Floating point exception (SIGFPE)
RTL_SIGSEGV = 2; // Segmentation violation (SIGSEGV)
RTL_SIGILL = 3; // Illegal instruction (SIGILL)
RTL_SIGBUS = 4; // Bus error (SIGBUS)
RTL_SIGQUIT = 5; // User interrupt (SIGQUIT)
RTL_SIGLAST = RTL_SIGQUIT; // Used internally. Don't use this.
RTL_SIGDEFAULT = -1; // Means all of a set of signals that the we capture
// normally. This is currently all of the preceding
// signals. You cannot pass this to InquireSignal.
type
{ TSignalState is the state of a given signal handler, as returned by
InquireSignal. See InquireSignal, below.
}
TSignalState = (ssNotHooked, ssHooked, ssOverridden);
var
{
If DeferUserInterrupts is set, we do not raise either SIGINT or SIGQUIT as
an exception, instead, we set SIGINTIssued or SIGQUITIssued when the
signal arrives, and swallow the signal where the OS issued it. This gives
GUI applications the chance to defer the actual handling of the signal
until a time when it is safe to do so.
}
DeferUserInterrupts: Boolean;
SIGINTIssued: Boolean;
SIGQUITIssued: Boolean;
{$IFEND LINUX or MACOSX}
{$IFNDEF MSWINDOWS}
const
MAX_PATH =
{$IFDEF LINUX}
4095; // From /usr/include/linux/limits.h PATH_MAX
{$ENDIF LINUX}
{$IFDEF MACOSX}
1024;
{$ENDIF MACOSX}
{$ENDIF !MSWINDOWS}
var
{ Empty string and null string pointer. These constants are provided for
backwards compatibility only. }
EmptyStr: string = '';
NullStr: PString = @EmptyStr;
EmptyWideStr: WideString = '';
NullWideStr: PWideString = @EmptyWideStr;
EmptyAnsiStr: AnsiString = '';
NullAnsiStr: PAnsiString = @EmptyAnsiStr;
{$IFDEF MSWINDOWS}
{ Win32 platform identifier. This will be one of the following values:
VER_PLATFORM_WIN32s
VER_PLATFORM_WIN32_WINDOWS
VER_PLATFORM_WIN32_NT
See WINDOWS.PAS for the numerical values. }
Win32Platform: Integer = 0;
{ Win32 OS version information -
see TOSVersionInfo.dwMajorVersion/dwMinorVersion/dwBuildNumber }
Win32MajorVersion: Integer = 0;
Win32MinorVersion: Integer = 0;
Win32BuildNumber: Integer = 0;
{ Win32 OS extra version info string -
see TOSVersionInfo.szCSDVersion }
Win32CSDVersion: string = '';
{ Win32 OS version tester }
function CheckWin32Version(AMajor: Integer; AMinor: Integer = 0): Boolean;
{ GetFileVersion returns the most significant 32 bits of a file's binary
version number. Typically, this includes the major and minor version placed
together in one 32-bit integer. It generally does not include the release
or build numbers. It returns Cardinal(-1) if it failed. }
function GetFileVersion(const AFileName: string): Cardinal;
{$ENDIF}
{ Currency and date/time formatting options
The initial values of these variables are fetched from the system registry
using the GetLocaleInfo function in the Win32 API. The description of each
variable specifies the LOCALE_XXXX constant used to fetch the initial
value.
CurrencyString - Defines the currency symbol used in floating-point to
decimal conversions. The initial value is fetched from LOCALE_SCURRENCY.
CurrencyFormat - Defines the currency symbol placement and separation
used in floating-point to decimal conversions. Possible values are:
0 = '$1'
1 = '1$'
2 = '$ 1'
3 = '1 $'
The initial value is fetched from LOCALE_ICURRENCY.
NegCurrFormat - Defines the currency format for used in floating-point to
decimal conversions of negative numbers. Possible values are:
0 = '($1)' 4 = '(1$)' 8 = '-1 $' 12 = '$ -1'
1 = '-$1' 5 = '-1$' 9 = '-$ 1' 13 = '1- $'
2 = '$-1' 6 = '1-$' 10 = '1 $-' 14 = '($ 1)'
3 = '$1-' 7 = '1$-' 11 = '$ 1-' 15 = '(1 $)'
The initial value is fetched from LOCALE_INEGCURR.
ThousandSeparator - The character used to separate thousands in numbers
with more than three digits to the left of the decimal separator. The
initial value is fetched from LOCALE_STHOUSAND. A value of #0 indicates
no thousand separator character should be output even if the format string
specifies thousand separators.
DecimalSeparator - The character used to separate the integer part from
the fractional part of a number. The initial value is fetched from
LOCALE_SDECIMAL. DecimalSeparator must be a non-zero value.
CurrencyDecimals - The number of digits to the right of the decimal point
in a currency amount. The initial value is fetched from LOCALE_ICURRDIGITS.
DateSeparator - The character used to separate the year, month, and day
parts of a date value. The initial value is fetched from LOCATE_SDATE.
ShortDateFormat - The format string used to convert a date value to a
short string suitable for editing. For a complete description of date and
time format strings, refer to the documentation for the FormatDate
function. The short date format should only use the date separator
character and the m, mm, d, dd, yy, and yyyy format specifiers. The
initial value is fetched from LOCALE_SSHORTDATE.
LongDateFormat - The format string used to convert a date value to a long
string suitable for display but not for editing. For a complete description
of date and time format strings, refer to the documentation for the
FormatDate function. The initial value is fetched from LOCALE_SLONGDATE.
TimeSeparator - The character used to separate the hour, minute, and
second parts of a time value. The initial value is fetched from
LOCALE_STIME.
TimeAMString - The suffix string used for time values between 00:00 and
11:59 in 12-hour clock format. The initial value is fetched from
LOCALE_S1159.
TimePMString - The suffix string used for time values between 12:00 and
23:59 in 12-hour clock format. The initial value is fetched from
LOCALE_S2359.
ShortTimeFormat - The format string used to convert a time value to a
short string with only hours and minutes. The default value is computed
from LOCALE_ITIME and LOCALE_ITLZERO.
LongTimeFormat - The format string used to convert a time value to a long
string with hours, minutes, and seconds. The default value is computed
from LOCALE_ITIME and LOCALE_ITLZERO.
ShortMonthNames - Array of strings containing short month names. The mmm
format specifier in a format string passed to FormatDate causes a short
month name to be substituted. The default values are fecthed from the
LOCALE_SABBREVMONTHNAME system locale entries.
LongMonthNames - Array of strings containing long month names. The mmmm
format specifier in a format string passed to FormatDate causes a long
month name to be substituted. The default values are fecthed from the
LOCALE_SMONTHNAME system locale entries.
ShortDayNames - Array of strings containing short day names. The ddd
format specifier in a format string passed to FormatDate causes a short
day name to be substituted. The default values are fecthed from the
LOCALE_SABBREVDAYNAME system locale entries.
LongDayNames - Array of strings containing long day names. The dddd
format specifier in a format string passed to FormatDate causes a long
day name to be substituted. The default values are fecthed from the
LOCALE_SDAYNAME system locale entries.
ListSeparator - The character used to separate items in a list. The
initial value is fetched from LOCALE_SLIST.
TwoDigitYearCenturyWindow - Determines what century is added to two
digit years when converting string dates to numeric dates. This value
is subtracted from the current year before extracting the century.
This can be used to extend the lifetime of existing applications that
are inextricably tied to 2 digit year data entry. The best solution
to Year 2000 (Y2k) issues is not to accept 2 digit years at all - require
4 digit years in data entry to eliminate century ambiguities.
Examples:
Current TwoDigitCenturyWindow Century StrToDate() of:
Year Value Pivot '01/01/03' '01/01/68' '01/01/50'
-------------------------------------------------------------------------
1998 0 1900 1903 1968 1950
2002 0 2000 2003 2068 2050
1998 50 (default) 1948 2003 1968 1950
2002 50 (default) 1952 2003 1968 2050
2020 50 (default) 1970 2003 2068 2050
}
var
CurrencyString: string;
CurrencyFormat: Byte;
NegCurrFormat: Byte;
ThousandSeparator: Char;
DecimalSeparator: Char;
CurrencyDecimals: Byte;
DateSeparator: Char;
ShortDateFormat: string;
LongDateFormat: string;
TimeSeparator: Char;
TimeAMString: string;
TimePMString: string;
ShortTimeFormat: string;
LongTimeFormat: string;
ShortMonthNames: array[1..12] of string;
LongMonthNames: array[1..12] of string;
ShortDayNames: array[1..7] of string;
LongDayNames: array[1..7] of string;
SysLocale: TSysLocale;
TwoDigitYearCenturyWindow: Word = 50;
ListSeparator: Char;
{ Thread safe currency and date/time formatting
The TFormatSettings record is designed to allow thread safe formatting,
equivalent to the gloabal variables described above. Each of the
formatting routines that use the gloabal variables have overloaded
equivalents, requiring an additional parameter of type TFormatSettings.
A TFormatSettings record must be populated before use. This can be done
using the GetLocaleFormatSettings function, which will populate the
record with values based on the given locale (using the Win32 API
function GetLocaleInfo). Note that some format specifiers still require
specific thread locale settings (such as period/era names).
}
type
TFormatSettings = record
CurrencyFormat: Byte;
NegCurrFormat: Byte;
ThousandSeparator: Char;
DecimalSeparator: Char;
CurrencyDecimals: Byte;
DateSeparator: Char;
TimeSeparator: Char;
ListSeparator: Char;
CurrencyString: string;
ShortDateFormat: string;
LongDateFormat: string;
TimeAMString: string;
TimePMString: string;
ShortTimeFormat: string;
LongTimeFormat: string;
ShortMonthNames: array[1..12] of string;
LongMonthNames: array[1..12] of string;
ShortDayNames: array[1..7] of string;
LongDayNames: array[1..7] of string;
TwoDigitYearCenturyWindow: Word;
end;
TLocaleOptions = (loInvariantLocale, loUserLocale);
const
MaxEraCount = 7;
var
EraNames: array [1..MaxEraCount] of string;
EraYearOffsets: array [1..MaxEraCount] of Integer;
{$IFDEF LINUX}
EraRanges : array [1..MaxEraCount] of TEraRange platform;
EraYearFormats: array [1..MaxEraCount] of string platform;
EraCount: Byte platform;
{$ENDIF}
const
PathDelim = {$IFDEF MSWINDOWS} '\'; {$ELSE} '/'; {$ENDIF}
DriveDelim = {$IFDEF MSWINDOWS} ':'; {$ELSE} ''; {$ENDIF}
PathSep = {$IFDEF MSWINDOWS} ';'; {$ELSE} ':'; {$ENDIF}
{$IFDEF MSWINDOWS}
function Languages: TLanguages;
{$ENDIF}
{ Exit procedure handling }
{ AddExitProc adds the given procedure to the run-time library's exit
procedure list. When an application terminates, its exit procedures are
executed in reverse order of definition, i.e. the last procedure passed
to AddExitProc is the first one to get executed upon termination. }
procedure AddExitProc(Proc: TProcedure);
{ String handling routines }
{ NewStr allocates a string on the heap. NewStr is provided for backwards
compatibility only. }
function NewStr(const S: AnsiString): PAnsiString; deprecated;
{ DisposeStr disposes a string pointer that was previously allocated using
NewStr. DisposeStr is provided for backwards compatibility only. }
procedure DisposeStr(P: PAnsiString); deprecated;
{ AssignStr assigns a new dynamically allocated string to the given string
pointer. AssignStr is provided for backwards compatibility only. }
procedure AssignStr(var P: PAnsiString; const S: AnsiString); deprecated;
{ AppendStr appends S to the end of Dest. AppendStr is provided for
backwards compatibility only. Use "Dest := Dest + S" instead. }
procedure AppendStr(var Dest: AnsiString; const S: AnsiString); deprecated;
{ UpperCase converts all ASCII characters in the given string to upper case.
The conversion affects only 7-bit ASCII characters between 'a' and 'z'. To
convert 8-bit international characters, use AnsiUpperCase. }
function UpperCase(const S: string): string; overload;
function UpperCase(const S: string; LocaleOptions: TLocaleOptions): string; overload; inline;
{ LowerCase converts all ASCII characters in the given string to lower case.
The conversion affects only 7-bit ASCII characters between 'A' and 'Z'. To
convert 8-bit international characters, use AnsiLowerCase. }
function LowerCase(const S: string): string; overload;
function LowerCase(const S: string; LocaleOptions: TLocaleOptions): string; overload; inline;
{ CompareStr compares S1 to S2, with case-sensitivity. The return value is
less than 0 if S1 < S2, 0 if S1 = S2, or greater than 0 if S1 > S2. The
compare operation is based on the 8-bit ordinal value of each character
and is not affected by the current user locale. }
function CompareStr(const S1, S2: string): Integer; overload;
function CompareStr(const S1, S2: string; LocaleOptions: TLocaleOptions): Integer; overload;
{ SameStr compares S1 to S2, with case-sensitivity. Returns true if
S1 and S2 are the equal, that is, if CompareStr would return 0. }
function SameStr(const S1, S2: string): Boolean; overload;
function SameStr(const S1, S2: string; LocaleOptions: TLocaleOptions): Boolean; overload;
{ CompareMem performs a binary compare of Length bytes of memory referenced
by P1 to that of P2. CompareMem returns True if the memory referenced by
P1 is identical to that of P2. }
function CompareMem(P1, P2: Pointer; Length: Integer): Boolean; assembler;
{ CompareText compares S1 to S2, without case-sensitivity. The return value
is the same as for CompareStr. The compare operation is based on the 8-bit
ordinal value of each character, after converting 'a'..'z' to 'A'..'Z',
and is not affected by the current user locale. }
function CompareText(const S1, S2: string): Integer; overload;
function CompareText(const S1, S2: string; LocaleOptions: TLocaleOptions): Integer; overload;
{ SameText compares S1 to S2, without case-sensitivity. Returns true if
S1 and S2 are the equal, that is, if CompareText would return 0. SameText
has the same 8-bit limitations as CompareText }
function SameText(const S1, S2: string): Boolean; overload;
function SameText(const S1, S2: string; LocaleOptions: TLocaleOptions): Boolean; overload;
{ AnsiUpperCase converts all characters in the given string to upper case.
The conversion uses the current user locale. }
function AnsiUpperCase(const S: string): string; overload;
{ AnsiLowerCase converts all characters in the given string to lower case.
The conversion uses the current user locale. }
function AnsiLowerCase(const S: string): string; overload;
{ AnsiCompareStr compares S1 to S2, with case-sensitivity. The compare
operation is controlled by the current user locale. The return value
is the same as for CompareStr. }
function AnsiCompareStr(const S1, S2: string): Integer; overload;
{ AnsiSameStr compares S1 to S2, with case-sensitivity. The compare
operation is controlled by the current user locale. The return value
is True if AnsiCompareStr would have returned 0. }
function AnsiSameStr(const S1, S2: string): Boolean; inline; overload;
{ AnsiCompareText compares S1 to S2, without case-sensitivity. The compare
operation is controlled by the current user locale. The return value
is the same as for CompareStr. }
function AnsiCompareText(const S1, S2: string): Integer; overload;
{ AnsiSameText compares S1 to S2, without case-sensitivity. The compare
operation is controlled by the current user locale. The return value
is True if AnsiCompareText would have returned 0. }
function AnsiSameText(const S1, S2: string): Boolean; inline; overload;
{ AnsiStrComp compares S1 to S2, with case-sensitivity. The compare
operation is controlled by the current user locale. The return value
is the same as for CompareStr. }
function AnsiStrComp(S1, S2: PAnsiChar): Integer; inline; overload;
function AnsiStrComp(S1, S2: PWideChar): Integer; inline; overload;
{ AnsiStrIComp compares S1 to S2, without case-sensitivity. The compare
operation is controlled by the current user locale. The return value
is the same as for CompareStr. }
function AnsiStrIComp(S1, S2: PAnsiChar): Integer; inline; overload;
function AnsiStrIComp(S1, S2: PWideChar): Integer; inline; overload;
{ AnsiStrLComp compares S1 to S2, with case-sensitivity, up to a maximum
length of MaxLen bytes. The compare operation is controlled by the
current user locale. The return value is the same as for CompareStr. }
function AnsiStrLComp(S1, S2: PAnsiChar; MaxLen: Cardinal): Integer; overload;
function AnsiStrLComp(S1, S2: PWideChar; MaxLen: Cardinal): Integer; inline; overload;
{ AnsiStrLIComp compares S1 to S2, without case-sensitivity, up to a maximum
length of MaxLen bytes. The compare operation is controlled by the
current user locale. The return value is the same as for CompareStr. }
function AnsiStrLIComp(S1, S2: PAnsiChar; MaxLen: Cardinal): Integer; overload;
function AnsiStrLIComp(S1, S2: PWideChar; MaxLen: Cardinal): Integer; inline; overload;
{ AnsiStrLower converts all characters in the given string to lower case.
The conversion uses the current user locale. }
function AnsiStrLower(Str: PAnsiChar): PAnsiChar; overload;
function AnsiStrLower(Str: PWideChar): PWideChar; inline; overload;
{ AnsiStrUpper converts all characters in the given string to upper case.
The conversion uses the current user locale. }
function AnsiStrUpper(Str: PAnsiChar): PAnsiChar; overload;
function AnsiStrUpper(Str: PWideChar): PWideChar; inline; overload;
{ AnsiLastChar returns a pointer to the last full character in the string.
This function supports multibyte characters }
function AnsiLastChar(const S: AnsiString): PAnsiChar; overload;
function AnsiLastChar(const S: UnicodeString): PWideChar; overload;
{ AnsiStrLastChar returns a pointer to the last full character in the string.
This function supports multibyte characters. }
function AnsiStrLastChar(P: PAnsiChar): PAnsiChar; overload;
function AnsiStrLastChar(P: PWideChar): PWideChar; overload;
{ WideUpperCase converts all characters in the given string to upper case. }
function WideUpperCase(const S: WideString): WideString;
{ WideLowerCase converts all characters in the given string to lower case. }
function WideLowerCase(const S: WideString): WideString;
{ WideCompareStr compares S1 to S2, with case-sensitivity. The return value
is the same as for CompareStr. }
function WideCompareStr(const S1, S2: WideString): Integer;
{ WideSameStr compares S1 to S2, with case-sensitivity. The return value
is True if WideCompareStr would have returned 0. }
function WideSameStr(const S1, S2: WideString): Boolean; inline;
{ WideCompareText compares S1 to S2, without case-sensitivity. The return value
is the same as for CompareStr. }
function WideCompareText(const S1, S2: WideString): Integer;
{ WideSameText compares S1 to S2, without case-sensitivity. The return value
is True if WideCompareText would have returned 0. }
function WideSameText(const S1, S2: WideString): Boolean; inline;
{ Trim trims leading and trailing spaces and control characters from the
given string. }
function Trim(const S: string): string; overload;
{ TrimLeft trims leading spaces and control characters from the given
string. }
function TrimLeft(const S: string): string; overload;
{ TrimRight trims trailing spaces and control characters from the given
string. }
function TrimRight(const S: string): string; overload;
{ QuotedStr returns the given string as a quoted string. A single quote
character is inserted at the beginning and the end of the string, and
for each single quote character in the string, another one is added. }
function QuotedStr(const S: string): string; overload;
{ AnsiQuotedStr returns the given string as a quoted string, using the
provided Quote character. A Quote character is inserted at the beginning
and end of the string, and each Quote character in the string is doubled.
This function supports multibyte character strings (MBCS). }
function AnsiQuotedStr(const S: string; Quote: Char): string; overload;
{ AnsiExtractQuotedStr removes the Quote characters from the beginning and end
of a quoted string, and reduces pairs of Quote characters within the quoted
string to a single character. If the first character in Src is not the Quote
character, the function returns an empty string. The function copies
characters from the Src to the result string until the second solitary
Quote character or the first null character in Src. The Src parameter is
updated to point to the first character following the quoted string. If
the Src string does not contain a matching end Quote character, the Src
parameter is updated to point to the terminating null character in Src.
This function supports multibyte character strings (MBCS). }
function AnsiExtractQuotedStr(var Src: PAnsiChar; Quote: AnsiChar): AnsiString; overload;
function AnsiExtractQuotedStr(var Src: PWidechar; Quote: WideChar): UnicodeString; overload;
{ AnsiDequotedStr is a simplified version of AnsiExtractQuotedStr }
function AnsiDequotedStr(const S: string; AQuote: Char): string; overload;
{ AdjustLineBreaks adjusts all line breaks in the given string to the
indicated style.
When Style is tlbsCRLF, the function changes all
CR characters not followed by LF and all LF characters not preceded
by a CR into CR/LF pairs.
When Style is tlbsLF, the function changes all CR/LF pairs and CR characters
not followed by LF to LF characters. }
function AdjustLineBreaks(const S: string; Style: TTextLineBreakStyle =
{$IFDEF LINUX} tlbsLF {$ENDIF}
{$IFDEF MACOSX} tlbsLF {$ENDIF}
{$IFDEF MSWINDOWS} tlbsCRLF {$ENDIF}): string; overload;
{ IsValidIdent returns true if the given string is a valid identifier. An
identifier is defined as an alphabetic letter or '_' followed by zero or
more alphabetic letters, digits or '_'. In some cases identifiers may
contain dots, for example with nested types.
For more information see: http://msdn.microsoft.com/en-us/library/aa664670(VS.71).aspx }
function IsValidIdent(const Ident: string; AllowDots: Boolean = False): Boolean;
{ IntToStr converts the given value to its decimal string representation. }
function IntToStr(Value: Integer): string; overload;
function IntToStr(Value: Int64): string; overload;
{ UIntToStr converts the given unsigned value to its decimal string representation. }
function UIntToStr(Value: Cardinal): string; overload;
function UIntToStr(Value: UInt64): string; overload;
{ IntToHex converts the given value to a hexadecimal string representation
with the minimum number of digits specified. }
function IntToHex(Value: Integer; Digits: Integer): string; overload;
function IntToHex(Value: Int64; Digits: Integer): string; overload;
{ StrToInt converts the given string to an integer value. If the string
doesn't contain a valid value, an EConvertError exception is raised. }
function StrToInt(const S: string): Integer; overload;
function StrToIntDef(const S: string; Default: Integer): Integer; overload;
function TryStrToInt(const S: string; out Value: Integer): Boolean; overload;
{ Similar to the above functions but for Int64 instead }
function StrToInt64(const S: string): Int64; overload;
function StrToInt64Def(const S: string; const Default: Int64): Int64; overload;
function TryStrToInt64(const S: string; out Value: Int64): Boolean; overload;
{ StrToBool converts the given string to a boolean value. If the string
doesn't contain a valid value, an EConvertError exception is raised.
BoolToStr converts boolean to a string value that in turn can be converted
back into a boolean. BoolToStr will always pick the first element of
the TrueStrs/FalseStrs arrays. }
var
TrueBoolStrs: array of String;
FalseBoolStrs: array of String;
const
DefaultTrueBoolStr = 'True'; // DO NOT LOCALIZE
DefaultFalseBoolStr = 'False'; // DO NOT LOCALIZE
function StrToBool(const S: string): Boolean; overload;
function StrToBoolDef(const S: string; const Default: Boolean): Boolean; overload;
function TryStrToBool(const S: string; out Value: Boolean): Boolean; overload;
function BoolToStr(B: Boolean; UseBoolStrs: Boolean = False): string;
{ LoadStr loads the string resource given by Ident from the application's
executable file or associated resource module. If the string resource
does not exist, LoadStr returns an empty string. }
function LoadStr(Ident: Integer): string;
{ FmtLoadStr loads the string resource given by Ident from the application's
executable file or associated resource module, and uses it as the format
string in a call to the Format function with the given arguments. }
function FmtLoadStr(Ident: Integer; const Args: array of const): string;
{ File management routines }
{ FileOpen opens the specified file using the specified access mode. The
access mode value is constructed by OR-ing one of the fmOpenXXXX constants
with one of the fmShareXXXX constants. If the return value is positive,
the function was successful and the value is the file handle of the opened
file. A return value of -1 indicates that an error occurred. }
function FileOpen(const FileName: string; Mode: LongWord): Integer;
{ FileCreate creates a new file by the specified name. If the return value
is positive, the function was successful and the value is the file handle
of the new file. A return value of -1 indicates that an error occurred.
On Linux, this calls FileCreate(FileName, DEFFILEMODE) to create
the file with read and write access for the current user only. }
function FileCreate(const FileName: string): Integer; overload; inline;
{ This second version of FileCreate lets you specify the access rights to put on the newly
created file. The access rights parameter is ignored on Win32 }
function FileCreate(const FileName: string; Rights: Integer): Integer; overload; inline;
{ This third version of FileCreate lets you specify the share mode for the newly
created file. }
function FileCreate(const FileName: string; Mode: LongWord; Rights: Integer): Integer; overload;
{ FileRead reads Count bytes from the file given by Handle into the buffer
specified by Buffer. The return value is the number of bytes actually
read; it is less than Count if the end of the file was reached. The return
value is -1 if an error occurred. }
function FileRead(Handle: Integer; var Buffer; Count: LongWord): Integer;
{ FileWrite writes Count bytes to the file given by Handle from the buffer
specified by Buffer. The return value is the number of bytes actually
written, or -1 if an error occurred. }
function FileWrite(Handle: Integer; const Buffer; Count: LongWord): Integer;
{ FileSeek changes the current position of the file given by Handle to be
Offset bytes relative to the point given by Origin. Origin = 0 means that
Offset is relative to the beginning of the file, Origin = 1 means that
Offset is relative to the current position, and Origin = 2 means that
Offset is relative to the end of the file. The return value is the new
current position, relative to the beginning of the file, or -1 if an error
occurred. }
function FileSeek(Handle, Offset, Origin: Integer): Integer; overload;
function FileSeek(Handle: Integer; const Offset: Int64; Origin: Integer): Int64; overload;
{ FileClose closes the specified file. }
procedure FileClose(Handle: Integer); inline;
{ FileAge returns the date-and-time stamp of the specified file. The return
value can be converted to a TDateTime value using the FileDateToDateTime
function. The return value is -1 if the file does not exist. This version
does not support date-and-time stamps prior to 1980 and after 2107. }
function FileAge(const FileName: string): Integer; overload; deprecated;
{ FileAge retrieves the date-and-time stamp of the specified file as a
TDateTime. This version supports all valid NTFS date-and-time stamps
and returns a boolean value that indicates whether the specified
file exists. }
{$IFDEF MSWINDOWS}
function FileAge(const FileName: string; out FileDateTime: TDateTime): Boolean; overload;
{$ENDIF}
{ FileExists returns a boolean value that indicates whether the specified
file exists. }
function FileExists(const FileName: string): Boolean;
{ DirectoryExists returns a boolean value that indicates whether the
specified directory exists (and is actually a directory) }
function DirectoryExists(const Directory: string): Boolean;
{ ForceDirectories ensures that all the directories in a specific path exist.
Any portion that does not already exist will be created. Function result
indicates success of the operation. The function can fail if the current
user does not have sufficient file access rights to create directories in
the given path. }
function ForceDirectories(Dir: string): Boolean;
{ FindFirst searches the directory given by Path for the first entry that
matches the filename given by Path and the attributes given by Attr. The
result is returned in the search record given by SearchRec. The return
value is zero if the function was successful. Otherwise the return value
is a system error code. After calling FindFirst, always call FindClose.
FindFirst is typically used with FindNext and FindClose as follows:
Result := FindFirst(Path, Attr, SearchRec);
while Result = 0 do
begin
ProcessSearchRec(SearchRec);
Result := FindNext(SearchRec);
end;
FindClose(SearchRec);
where ProcessSearchRec represents user-defined code that processes the
information in a search record. }
function FindFirst(const Path: string; Attr: Integer;
var F: TSearchRec): Integer;
{ FindNext returs the next entry that matches the name and attributes
specified in a previous call to FindFirst. The search record must be one
that was passed to FindFirst. The return value is zero if the function was
successful. Otherwise the return value is a system error code. }
function FindNext(var F: TSearchRec): Integer;
{ FindClose terminates a FindFirst/FindNext sequence and frees memory and system
resources allocated by FindFirst.
Every FindFirst/FindNext must end with a call to FindClose. }
procedure FindClose(var F: TSearchRec);
{ FileGetDate returns the OS date-and-time stamp of the file given by
Handle. The return value is -1 if the handle is invalid. The
FileDateToDateTime function can be used to convert the returned value to
a TDateTime value. }
function FileGetDate(Handle: Integer): Integer;
{ FileSetDate sets the OS date-and-time stamp of the file given by FileName
to the value given by Age. The DateTimeToFileDate function can be used to
convert a TDateTime value to an OS date-and-time stamp. The return value
is zero if the function was successful. Otherwise the return value is a
system error code. }
function FileSetDate(const FileName: string; Age: Integer): Integer; overload;
{$IFDEF MSWINDOWS}
{ FileSetDate by handle is not available on Unix platforms because there
is no standard way to set a file's modification time using only a file
handle, and no standard way to obtain the file name of an open
file handle. }
function FileSetDate(Handle: Integer; Age: Integer): Integer; overload; platform;
{ FileGetAttr returns the file attributes of the file given by FileName. The
attributes can be examined by AND-ing with the faXXXX constants defined
above. A return value of -1 indicates that an error occurred. }
function FileGetAttr(const FileName: string): Integer; platform;
{ FileSetAttr sets the file attributes of the file given by FileName to the
value given by Attr. The attribute value is formed by OR-ing the
appropriate faXXXX constants. The return value is zero if the function was
successful. Otherwise the return value is a system error code. }
function FileSetAttr(const FileName: string; Attr: Integer): Integer; platform;
{$ENDIF}
{ FileIsReadOnly tests whether a given file is read-only for the current
process and effective user id. If the file does not exist, the
function returns False. (Check FileExists before calling FileIsReadOnly)
This function is platform portable. }
function FileIsReadOnly(const FileName: string): Boolean; inline;
{ FileSetReadOnly sets the read only state of a file. The file must
exist and the current effective user id must be the owner of the file.
On Unix systems, FileSetReadOnly attempts to set or remove
all three (user, group, and other) write permissions on the file.
If you want to grant partial permissions (writeable for owner but not
for others), use platform specific functions such as chmod.
The function returns True if the file was successfully modified,
False if there was an error. This function is platform portable. }
function FileSetReadOnly(const FileName: string; ReadOnly: Boolean): Boolean;
{ DeleteFile deletes the file given by FileName. The return value is True if
the file was successfully deleted, or False if an error occurred. }
function DeleteFile(const FileName: string): Boolean; inline;
{ RenameFile renames the file given by OldName to the name given by NewName.
The return value is True if the file was successfully renamed, or False if
an error occurred. }
function RenameFile(const OldName, NewName: string): Boolean; inline;
{ IsAssembly returns a boolean value that indicates whether the specified
file is a .NET assembly. }
function IsAssembly(const FileName: string): Boolean;
{ ChangeFileExt changes the extension of a filename. FileName specifies a
filename with or without an extension, and Extension specifies the new
extension for the filename. The new extension can be a an empty string or
a period followed by up to three characters. }
function ChangeFileExt(const FileName, Extension: string): string; overload;
{ ChangeFilePath changes the path of a filename. FileName specifies a
filename with or without an extension, and Path specifies the new
path for the filename. The new path is not required to contain the trailing
path delimiter. }
function ChangeFilePath(const FileName, Path: string): string; overload;
{ ExtractFilePath extracts the drive and directory parts of the given
filename. The resulting string is the leftmost characters of FileName,
up to and including the colon or backslash that separates the path
information from the name and extension. The resulting string is empty
if FileName contains no drive and directory parts. }
function ExtractFilePath(const FileName: string): string; overload;
{ ExtractFileDir extracts the drive and directory parts of the given
filename. The resulting string is a directory name suitable for passing
to SetCurrentDir, CreateDir, etc. The resulting string is empty if
FileName contains no drive and directory parts. }
function ExtractFileDir(const FileName: string): string; overload;
{ ExtractFileDrive extracts the drive part of the given filename. For
filenames with drive letters, the resulting string is '<drive>:'.
For filenames with a UNC path, the resulting string is in the form
'\\<servername>\<sharename>'. If the given path contains neither
style of filename, the result is an empty string. }
function ExtractFileDrive(const FileName: string): string; overload;
{ ExtractFileName extracts the name and extension parts of the given
filename. The resulting string is the leftmost characters of FileName,
starting with the first character after the colon or backslash that
separates the path information from the name and extension. The resulting
string is equal to FileName if FileName contains no drive and directory
parts. }
function ExtractFileName(const FileName: string): string; overload;
{ ExtractFileExt extracts the extension part of the given filename. The
resulting string includes the period character that separates the name
and extension parts. The resulting string is empty if the given filename
has no extension. }
function ExtractFileExt(const FileName: string): string; overload;
{ ExpandFileName expands the given filename to a fully qualified filename.
The resulting string consists of a drive letter, a colon, a root relative
directory path, and a filename. Embedded '.' and '..' directory references
are removed. }
function ExpandFileName(const FileName: string): string; overload;
{ ExpandFilenameCase returns a fully qualified filename like ExpandFilename,
but performs a case-insensitive filename search looking for a close match
in the actual file system, differing only in uppercase versus lowercase of
the letters. This is useful to convert lazy user input into useable file
names, or to convert filename data created on a case-insensitive file
system (Win32) to something useable on a case-sensitive file system (Linux).
The MatchFound out parameter indicates what kind of match was found in the
file system, and what the function result is based upon:
( in order of increasing difficulty or complexity )
mkExactMatch: Case-sensitive match. Result := ExpandFileName(FileName).
mkSingleMatch: Exactly one file in the given directory path matches the
given filename on a case-insensitive basis.
Result := ExpandFileName(FileName as found in file system).
mkAmbiguous: More than one file in the given directory path matches the
given filename case-insensitively.
In many cases, this should be considered an error.
Result := ExpandFileName(First matching filename found).
mkNone: File not found at all. Result := ExpandFileName(FileName).
Note that because this function has to search the file system it may be
much slower than ExpandFileName, particularly when the given filename is
ambiguous or does not exist. Use ExpandFilenameCase only when you have
a filename of dubious orgin - such as from user input - and you want
to make a best guess before failing. }
type
TFilenameCaseMatch = (mkNone, mkExactMatch, mkSingleMatch, mkAmbiguous);
function ExpandFileNameCase(const FileName: string;
out MatchFound: TFilenameCaseMatch): string; overload;
{ ExpandUNCFileName expands the given filename to a fully qualified filename.
This function is the same as ExpandFileName except that it will return the
drive portion of the filename in the format '\\<servername>\<sharename> if
that drive is actually a network resource instead of a local resource.
Like ExpandFileName, embedded '.' and '..' directory references are
removed. }
function ExpandUNCFileName(const FileName: string): string; overload;
{ ExtractRelativePath will return a file path name relative to the given
BaseName. It strips the common path dirs and adds '..\' on Windows,
and '../' on Linux for each level up from the BaseName path. Note: Directories
passed in should include trailing backslashes}
function ExtractRelativePath(const BaseName, DestName: string): string; overload;
{$IFDEF MSWINDOWS}
{ ExtractShortPathName will convert the given filename to the short form
by calling the GetShortPathName API. Will return an empty string if
the file or directory specified does not exist }
function ExtractShortPathName(const FileName: string): string; overload;
{$ENDIF}
{ FileSearch searches for the file given by Name in the list of directories
given by DirList. The directory paths in DirList must be separated by
PathSep chars. The search always starts with the current directory of the
current drive. The returned value is a concatenation of one of the
directory paths and the filename, or an empty string if the file could not
be located. }
function FileSearch(const Name, DirList: string): string;
{$IFDEF MSWINDOWS}
{ DiskFree returns the number of free bytes on the specified drive number,
where 0 = Current, 1 = A, 2 = B, etc. DiskFree returns -1 if the drive
number is invalid. }
function DiskFree(Drive: Byte): Int64;
{ DiskSize returns the size in bytes of the specified drive number, where
0 = Current, 1 = A, 2 = B, etc. DiskSize returns -1 if the drive number
is invalid. }
function DiskSize(Drive: Byte): Int64;
{$ENDIF}
{ FileDateToDateTime converts an OS date-and-time value to a TDateTime
value. The FileAge, FileGetDate, and FileSetDate routines operate on OS
date-and-time values, and the Time field of a TSearchRec used by the
FindFirst and FindNext functions contains an OS date-and-time value. }
function FileDateToDateTime(FileDate: Integer): TDateTime;
{ DateTimeToFileDate converts a TDateTime value to an OS date-and-time
value. The FileAge, FileGetDate, and FileSetDate routines operate on OS
date-and-time values, and the Time field of a TSearchRec used by the
FindFirst and FindNext functions contains an OS date-and-time value. }
function DateTimeToFileDate(DateTime: TDateTime): Integer;
{ GetCurrentDir returns the current directory. }
function GetCurrentDir: string;
{ SetCurrentDir sets the current directory. The return value is True if
the current directory was successfully changed, or False if an error
occurred. }
function SetCurrentDir(const Dir: string): Boolean;
{ CreateDir creates a new directory. The return value is True if a new
directory was successfully created, or False if an error occurred. }
function CreateDir(const Dir: string): Boolean;
{ RemoveDir deletes an existing empty directory. The return value is
True if the directory was successfully deleted, or False if an error
occurred. }
function RemoveDir(const Dir: string): Boolean;
{ PChar routines }
{ const params help simplify C++ code. No effect on pascal code }
{ StrLen returns the number of characters in Str, not counting the null
terminator. }
function StrLen(const Str: PAnsiChar): Cardinal; overload;
function StrLen(const Str: PWideChar): Cardinal; overload;
{ StrEnd returns a pointer to the null character that terminates Str. }
function StrEnd(const Str: PAnsiChar): PAnsiChar; overload;
function StrEnd(const Str: PWideChar): PWideChar; overload;
{ StrMove copies exactly Count characters from Source to Dest and returns
Dest. Source and Dest may overlap. }
function StrMove(Dest: PAnsiChar; const Source: PAnsiChar; Count: Cardinal): PAnsiChar; overload;
function StrMove(Dest: PWideChar; const Source: PWideChar; Count: Cardinal): PWideChar; overload;
{ StrCopy copies Source to Dest and returns Dest. }
function StrCopy(Dest: PAnsiChar; const Source: PAnsiChar): PAnsiChar; overload;
function StrCopy(Dest: PWideChar; const Source: PWideChar): PWideChar; overload;
{ StrECopy copies Source to Dest and returns StrEnd(Dest). }
function StrECopy(Dest: PAnsiChar; const Source: PAnsiChar): PAnsiChar; overload;
function StrECopy(Dest: PWideChar; const Source: PWideChar): PWideChar; overload;
{ StrLCopy copies at most MaxLen characters from Source to Dest and
returns Dest. }
function StrLCopy(Dest: PAnsiChar; const Source: PAnsiChar; MaxLen: Cardinal): PAnsiChar; overload;
function StrLCopy(Dest: PWideChar; const Source: PWideChar; MaxLen: Cardinal): PWideChar; overload;
{ StrPCopy copies the Pascal style string Source into Dest and
returns Dest. }
function StrPCopy(Dest: PAnsiChar; const Source: AnsiString): PAnsiChar; overload;
function StrPCopy(Dest: PWideChar; const Source: UnicodeString): PWideChar; overload;
{ StrPLCopy copies at most MaxLen characters from the Pascal style string
Source into Dest and returns Dest. }
function StrPLCopy(Dest: PAnsiChar; const Source: AnsiString;
MaxLen: Cardinal): PAnsiChar; overload;
function StrPLCopy(Dest: PWideChar; const Source: UnicodeString;
MaxLen: Cardinal): PWideChar; overload;
{ StrCat appends a copy of Source to the end of Dest and returns Dest. }
function StrCat(Dest: PAnsiChar; const Source: PAnsiChar): PAnsiChar; overload;
function StrCat(Dest: PWideChar; const Source: PWideChar): PWideChar; overload;
{ StrLCat appends at most MaxLen - StrLen(Dest) characters from Source to
the end of Dest, and returns Dest. }
function StrLCat(Dest: PAnsiChar; const Source: PAnsiChar; MaxLen: Cardinal): PAnsiChar; overload;
function StrLCat(Dest: PWideChar; const Source: PWideChar; MaxLen: Cardinal): PWideChar; overload;
{ StrComp compares Str1 to Str2. The return value is less than 0 if
Str1 < Str2, 0 if Str1 = Str2, or greater than 0 if Str1 > Str2. }
function StrComp(const Str1, Str2: PAnsiChar): Integer; overload;
function StrComp(const Str1, Str2: PWideChar): Integer; overload;
{ StrIComp compares Str1 to Str2, without case sensitivity. The return
value is the same as StrComp. }
function StrIComp(const Str1, Str2: PAnsiChar): Integer; overload;
function StrIComp(const Str1, Str2: PWideChar): Integer; overload;
{ StrLComp compares Str1 to Str2, for a maximum length of MaxLen
characters. The return value is the same as StrComp. }
function StrLComp(const Str1, Str2: PAnsiChar; MaxLen: Cardinal): Integer; overload;
function StrLComp(const Str1, Str2: PWideChar; MaxLen: Cardinal): Integer; overload;
{ StrLIComp compares Str1 to Str2, for a maximum length of MaxLen
characters, without case sensitivity. The return value is the same
as StrComp. }
function StrLIComp(const Str1, Str2: PAnsiChar; MaxLen: Cardinal): Integer; overload;
function StrLIComp(const Str1, Str2: PWideChar; MaxLen: Cardinal): Integer; overload;
{ StrScan returns a pointer to the first occurrence of Chr in Str. If Chr
does not occur in Str, StrScan returns NIL. The null terminator is
considered to be part of the string. }
function StrScan(const Str: PAnsiChar; Chr: AnsiChar): PAnsiChar; overload;
function StrScan(const Str: PWideChar; Chr: WideChar): PWideChar; overload;
{ StrRScan returns a pointer to the last occurrence of Chr in Str. If Chr
does not occur in Str, StrRScan returns NIL. The null terminator is
considered to be part of the string. }
function StrRScan(const Str: PAnsiChar; Chr: AnsiChar): PAnsiChar; overload;
function StrRScan(const Str: PWideChar; Chr: WideChar): PWideChar; overload;
{ TextPos: Same as StrPos but is case insensitive }
function TextPos(Str, SubStr: PAnsiChar): PAnsiChar; overload;
function TextPos(Str, SubStr: PWideChar): PWideChar; overload;
{ StrPos returns a pointer to the first occurrence of Str2 in Str1. If
Str2 does not occur in Str1, StrPos returns NIL. }
function StrPos(const Str1, Str2: PAnsiChar): PAnsiChar; overload;
function StrPos(const Str1, Str2: PWideChar): PWideChar; overload;
{ StrUpper converts Str to upper case and returns Str. }
function StrUpper(Str: PAnsiChar): PAnsiChar; overload;
function StrUpper(Str: PWideChar): PWideChar; overload;
{ StrLower converts Str to lower case and returns Str. }
function StrLower(Str: PAnsiChar): PAnsiChar; overload;
function StrLower(Str: PWideChar): PWideChar; overload;
{ StrPas converts Str to a Pascal style string. This function is provided
for backwards compatibility only. To convert a null terminated string to
a Pascal style string, use a string type cast or an assignment. }
function StrPas(const Str: PAnsiChar): AnsiString; overload;
function StrPas(const Str: PWideChar): UnicodeString; overload;
{ StrAlloc allocates a buffer of the given size on the heap. The size of
the allocated buffer is encoded in a four byte header that immediately
preceeds the buffer. To dispose the buffer, use StrDispose. }
function AnsiStrAlloc(Size: Cardinal): PAnsiChar;
function WideStrAlloc(Size: Cardinal): PWideChar;
function StrAlloc(Size: Cardinal): PChar;
{ StrBufSize returns the allocated size of the given buffer, not including
the two byte header. }
function StrBufSize(const Str: PAnsiChar): Cardinal; overload;
function StrBufSize(const Str: PWideChar): Cardinal; overload;
{ StrNew allocates a copy of Str on the heap. If Str is NIL, StrNew returns
NIL and doesn't allocate any heap space. Otherwise, StrNew makes a
duplicate of Str, obtaining space with a call to the StrAlloc function,
and returns a pointer to the duplicated string. To dispose the string,
use StrDispose. }
function StrNew(const Str: PAnsiChar): PAnsiChar; overload;
function StrNew(const Str: PWideChar): PWideChar; overload;
{ StrDispose disposes a string that was previously allocated with StrAlloc
or StrNew. If Str is NIL, StrDispose does nothing. }
procedure StrDispose(Str: PAnsiChar); overload;
procedure StrDispose(Str: PWideChar); overload;
{ String formatting routines }
{ The Format routine formats the argument list given by the Args parameter
using the format string given by the Format parameter.
Format strings contain two types of objects--plain characters and format
specifiers. Plain characters are copied verbatim to the resulting string.
Format specifiers fetch arguments from the argument list and apply
formatting to them.
Format specifiers have the following form:
"%" [index ":"] ["-"] [width] ["." prec] type
A format specifier begins with a % character. After the % come the
following, in this order:
- an optional argument index specifier, [index ":"]
- an optional left-justification indicator, ["-"]
- an optional width specifier, [width]
- an optional precision specifier, ["." prec]
- the conversion type character, type
The following conversion characters are supported:
d Decimal. The argument must be an integer value. The value is converted
to a string of decimal digits. If the format string contains a precision
specifier, it indicates that the resulting string must contain at least
the specified number of digits; if the value has less digits, the
resulting string is left-padded with zeros.
u Unsigned decimal. Similar to 'd' but no sign is output.
e Scientific. The argument must be a floating-point value. The value is
converted to a string of the form "-d.ddd...E+ddd". The resulting
string starts with a minus sign if the number is negative, and one digit
always precedes the decimal point. The total number of digits in the
resulting string (including the one before the decimal point) is given
by the precision specifer in the format string--a default precision of
15 is assumed if no precision specifer is present. The "E" exponent
character in the resulting string is always followed by a plus or minus
sign and at least three digits.
f Fixed. The argument must be a floating-point value. The value is
converted to a string of the form "-ddd.ddd...". The resulting string
starts with a minus sign if the number is negative. The number of digits
after the decimal point is given by the precision specifier in the
format string--a default of 2 decimal digits is assumed if no precision
specifier is present.
g General. The argument must be a floating-point value. The value is
converted to the shortest possible decimal string using fixed or
scientific format. The number of significant digits in the resulting
string is given by the precision specifier in the format string--a
default precision of 15 is assumed if no precision specifier is present.
Trailing zeros are removed from the resulting string, and a decimal
point appears only if necessary. The resulting string uses fixed point
format if the number of digits to the left of the decimal point in the
value is less than or equal to the specified precision, and if the
value is greater than or equal to 0.00001. Otherwise the resulting
string uses scientific format.
n Number. The argument must be a floating-point value. The value is
converted to a string of the form "-d,ddd,ddd.ddd...". The "n" format
corresponds to the "f" format, except that the resulting string
contains thousand separators.
m Money. The argument must be a floating-point value. The value is
converted to a string that represents a currency amount. The conversion
is controlled by the CurrencyString, CurrencyFormat, NegCurrFormat,
ThousandSeparator, DecimalSeparator, and CurrencyDecimals global
variables, all of which are initialized from locale settings provided
by the operating system. For example, Currency Format preferences can be
set in the International section of the Windows Control Panel. If the format
string contains a precision specifier, it overrides the value given
by the CurrencyDecimals global variable.
p Pointer. The argument must be a pointer value. The value is converted
to a string of the form "XXXX:YYYY" where XXXX and YYYY are the
segment and offset parts of the pointer expressed as four hexadecimal
digits.
s String. The argument must be a character, a string, or a PChar value.
The string or character is inserted in place of the format specifier.
The precision specifier, if present in the format string, specifies the
maximum length of the resulting string. If the argument is a string
that is longer than this maximum, the string is truncated.
x Hexadecimal. The argument must be an integer value. The value is
converted to a string of hexadecimal digits. If the format string
contains a precision specifier, it indicates that the resulting string
must contain at least the specified number of digits; if the value has
less digits, the resulting string is left-padded with zeros.
Conversion characters may be specified in upper case as well as in lower
case--both produce the same results.
For all floating-point formats, the actual characters used as decimal and
thousand separators are obtained from the DecimalSeparator and
ThousandSeparator global variables.
Index, width, and precision specifiers can be specified directly using
decimal digit string (for example "%10d"), or indirectly using an asterisk
charcater (for example "%*.*f"). When using an asterisk, the next argument
in the argument list (which must be an integer value) becomes the value
that is actually used. For example "Format('%*.*f', [8, 2, 123.456])" is
the same as "Format('%8.2f', [123.456])".
A width specifier sets the minimum field width for a conversion. If the
resulting string is shorter than the minimum field width, it is padded
with blanks to increase the field width. The default is to right-justify
the result by adding blanks in front of the value, but if the format
specifier contains a left-justification indicator (a "-" character
preceding the width specifier), the result is left-justified by adding
blanks after the value.
An index specifier sets the current argument list index to the specified
value. The index of the first argument in the argument list is 0. Using
index specifiers, it is possible to format the same argument multiple
times. For example "Format('%d %d %0:d %d', [10, 20])" produces the string
'10 20 10 20'.
The Format function can be combined with other formatting functions. For
example
S := Format('Your total was %s on %s', [
FormatFloat('$#,##0.00;;zero', Total),
FormatDateTime('mm/dd/yy', Date)]);
which uses the FormatFloat and FormatDateTime functions to customize the
format beyond what is possible with Format.
Each of the string formatting routines that uses global variables for
formatting (separators, decimals, date/time formats etc.), has an
overloaded equivalent requiring a parameter of type TFormatSettings. This
additional parameter provides the formatting information rather than the
global variables. For more information see the notes at TFormatSettings. }
function Format(const Format: string;
const Args: array of const): string; overload;
function Format(const Format: string; const Args: array of const;
const FormatSettings: TFormatSettings): string; overload;
{ FmtStr formats the argument list given by Args using the format string
given by Format into the string variable given by Result. For further
details, see the description of the Format function. }
procedure FmtStr(var Result: string; const Format: string;
const Args: array of const); overload;
procedure FmtStr(var Result: string; const Format: string;
const Args: array of const; const FormatSettings: TFormatSettings); overload;
{ StrFmt formats the argument list given by Args using the format string
given by Format into the buffer given by Buffer. It is up to the caller to
ensure that Buffer is large enough for the resulting string. The returned
value is Buffer. For further details, see the description of the Format
function. }
function StrFmt(Buffer, Format: PAnsiChar;
const Args: array of const): PAnsiChar; overload;
function StrFmt(Buffer, Format: PAnsiChar; const Args: array of const;
const FormatSettings: TFormatSettings): PAnsiChar; overload;
function StrFmt(Buffer, Format: PWideChar;
const Args: array of const): PWideChar; overload;
function StrFmt(Buffer, Format: PWideChar; const Args: array of const;
const FormatSettings: TFormatSettings): PWideChar; overload;
{ StrLFmt formats the argument list given by Args using the format string
given by Format into the buffer given by Buffer. The resulting string will
contain no more than MaxBufLen characters, not including the null terminator.
The returned value is Buffer. For further details, see the description of
the Format function. }
function StrLFmt(Buffer: PAnsiChar; MaxBufLen: Cardinal; Format: PAnsiChar;
const Args: array of const): PAnsiChar; overload;
function StrLFmt(Buffer: PAnsiChar; MaxBufLen: Cardinal; Format: PAnsiChar;
const Args: array of const;
const FormatSettings: TFormatSettings): PAnsiChar; overload;
function StrLFmt(Buffer: PWideChar; MaxBufLen: Cardinal; Format: PWideChar;
const Args: array of const): PWideChar; overload;
function StrLFmt(Buffer: PWideChar; MaxBufLen: Cardinal; Format: PWideChar;
const Args: array of const;
const FormatSettings: TFormatSettings): PWideChar; overload;
{ FormatBuf formats the argument list given by Args using the format string
given by Format and FmtLen into the buffer given by Buffer and BufLen.
The Format parameter is a reference to a buffer containing FmtLen
characters, and the Buffer parameter is a reference to a buffer of BufLen
characters. The returned value is the number of characters actually stored
in Buffer. The returned value is always less than or equal to BufLen. For
further details, see the description of the Format function. }
function FormatBuf(var Buffer; BufLen: Cardinal; const Format;
FmtLen: Cardinal; const Args: array of const): Cardinal; overload;
function FormatBuf(var Buffer; BufLen: Cardinal; const Format;
FmtLen: Cardinal; const Args: array of const;
const FormatSettings: TFormatSettings): Cardinal; overload;
function FormatBuf(Buffer: PWideChar; BufLen: Cardinal; const Format;
FmtLen: Cardinal; const Args: array of const): Cardinal; overload;
function FormatBuf(var Buffer: UnicodeString; BufLen: Cardinal; const Format;
FmtLen: Cardinal; const Args: array of const): Cardinal; overload;
function FormatBuf(Buffer: PWideChar; BufLen: Cardinal; const Format;
FmtLen: Cardinal; const Args: array of const;
const FormatSettings: TFormatSettings): Cardinal; overload;
function FormatBuf(var Buffer: UnicodeString; BufLen: Cardinal; const Format;
FmtLen: Cardinal; const Args: array of const;
const FormatSettings: TFormatSettings): Cardinal; overload;
{ The WideFormat routine formats the argument list given by the Args parameter
using the format WideString given by the Format parameter. This routine is
the WideString equivalent of Format. For further details, see the description
of the Format function. }
function WideFormat(const Format: WideString;
const Args: array of const): WideString; overload;
function WideFormat(const Format: WideString;
const Args: array of const;
const FormatSettings: TFormatSettings): WideString; overload;
{ WideFmtStr formats the argument list given by Args using the format WideString
given by Format into the WideString variable given by Result. For further
details, see the description of the Format function. }
procedure WideFmtStr(var Result: WideString; const Format: WideString;
const Args: array of const); overload;
procedure WideFmtStr(var Result: WideString; const Format: WideString;
const Args: array of const; const FormatSettings: TFormatSettings); overload;
{ WideFormatBuf formats the argument list given by Args using the format string
given by Format and FmtLen into the buffer given by Buffer and BufLen.
The Format parameter is a reference to a buffer containing FmtLen
UNICODE characters (WideChar), and the Buffer parameter is a reference to a
buffer of BufLen UNICODE characters (WideChar). The return value is the number
of UNICODE characters actually stored in Buffer. The return value is always
less than or equal to BufLen. For further details, see the description of the
Format function.
Important: BufLen, FmtLen and the return result are always the number of
UNICODE characters, *not* the number of bytes. To calculate the number of bytes
multiply them by SizeOf(WideChar). }
function WideFormatBuf(var Buffer; BufLen: Cardinal; const Format;
FmtLen: Cardinal; const Args: array of const): Cardinal; overload;
function WideFormatBuf(var Buffer; BufLen: Cardinal; const Format;
FmtLen: Cardinal; const Args: array of const;
const FormatSettings: TFormatSettings): Cardinal; overload;
{ Floating point conversion routines }
{ Each of the floating point conversion routines that uses global variables
for formatting (separators, decimals, etc.), has an overloaded equivalent
requiring a parameter of type TFormatSettings. This additional parameter
provides the formatting information rather than the global variables. For
more information see the notes at TFormatSettings. }
{ FloatToStr converts the floating-point value given by Value to its string
representation. The conversion uses general number format with 15
significant digits. For further details, see the description of the
FloatToStrF function. }
function FloatToStr(Value: Extended): string; overload;
function FloatToStr(Value: Extended;
const FormatSettings: TFormatSettings): string; overload;
{ CurrToStr converts the currency value given by Value to its string
representation. The conversion uses general number format. For further
details, see the description of the CurrToStrF function. }
function CurrToStr(Value: Currency): string; overload;
function CurrToStr(Value: Currency;
const FormatSettings: TFormatSettings): string; overload;
{ FloatToCurr will range validate a value to make sure it falls
within the acceptable currency range }
const
MinCurrency: Currency = -922337203685477.5807 {$IFDEF LINUX} + 1 {$ENDIF}; //!! overflow?
MaxCurrency: Currency = 922337203685477.5807 {$IFDEF LINUX} - 1 {$ENDIF}; //!! overflow?
function FloatToCurr(const Value: Extended): Currency;
function TryFloatToCurr(const Value: Extended; out AResult: Currency): Boolean;
{ FloatToStrF converts the floating-point value given by Value to its string
representation. The Format parameter controls the format of the resulting
string. The Precision parameter specifies the precision of the given value.
It should be 7 or less for values of type Single, 15 or less for values of
type Double, and 18 or less for values of type Extended. The meaning of the
Digits parameter depends on the particular format selected.
The possible values of the Format parameter, and the meaning of each, are
described below.
ffGeneral - General number format. The value is converted to the shortest
possible decimal string using fixed or scientific format. Trailing zeros
are removed from the resulting string, and a decimal point appears only
if necessary. The resulting string uses fixed point format if the number
of digits to the left of the decimal point in the value is less than or
equal to the specified precision, and if the value is greater than or
equal to 0.00001. Otherwise the resulting string uses scientific format,
and the Digits parameter specifies the minimum number of digits in the
exponent (between 0 and 4).
ffExponent - Scientific format. The value is converted to a string of the
form "-d.ddd...E+dddd". The resulting string starts with a minus sign if
the number is negative, and one digit always precedes the decimal point.
The total number of digits in the resulting string (including the one
before the decimal point) is given by the Precision parameter. The "E"
exponent character in the resulting string is always followed by a plus
or minus sign and up to four digits. The Digits parameter specifies the
minimum number of digits in the exponent (between 0 and 4).
ffFixed - Fixed point format. The value is converted to a string of the
form "-ddd.ddd...". The resulting string starts with a minus sign if the
number is negative, and at least one digit always precedes the decimal
point. The number of digits after the decimal point is given by the Digits
parameter--it must be between 0 and 18. If the number of digits to the
left of the decimal point is greater than the specified precision, the
resulting value will use scientific format.
ffNumber - Number format. The value is converted to a string of the form
"-d,ddd,ddd.ddd...". The ffNumber format corresponds to the ffFixed format,
except that the resulting string contains thousand separators.
ffCurrency - Currency format. The value is converted to a string that
represents a currency amount. The conversion is controlled by the
CurrencyString, CurrencyFormat, NegCurrFormat, ThousandSeparator, and
DecimalSeparator global variables, all of which are initialized from
locale settings provided by the operating system. For example,
Currency Format preferences can be set in the International section
of the Windows Control Panel.
The number of digits after the decimal point is given by the Digits
parameter--it must be between 0 and 18.
For all formats, the actual characters used as decimal and thousand
separators are obtained from the DecimalSeparator and ThousandSeparator
global variables.
If the given value is a NAN (not-a-number), the resulting string is 'NAN'.
If the given value is positive infinity, the resulting string is 'INF'. If
the given value is negative infinity, the resulting string is '-INF'. }
function FloatToStrF(Value: Extended; Format: TFloatFormat;
Precision, Digits: Integer): string; overload;
function FloatToStrF(Value: Extended; Format: TFloatFormat;
Precision, Digits: Integer;
const FormatSettings: TFormatSettings): string; overload;
{ CurrToStrF converts the currency value given by Value to its string
representation. A call to CurrToStrF corresponds to a call to
FloatToStrF with an implied precision of 19 digits. }
function CurrToStrF(Value: Currency; Format: TFloatFormat;
Digits: Integer): string; overload;
function CurrToStrF(Value: Currency; Format: TFloatFormat;
Digits: Integer; const FormatSettings: TFormatSettings): string; overload;
{ FloatToText converts the given floating-point value to its decimal
representation using the specified format, precision, and digits. The
Value parameter must be a variable of type Extended or Currency, as
indicated by the ValueType parameter. The resulting string of characters
is stored in the given buffer, and the returned value is the number of
characters stored. The resulting string is not null-terminated. For
further details, see the description of the FloatToStrF function. }
function FloatToText(BufferArg: PAnsiChar; const Value; ValueType: TFloatValue;
Format: TFloatFormat; Precision, Digits: Integer): Integer; overload;
function FloatToText(BufferArg: PWideChar; const Value; ValueType: TFloatValue;
Format: TFloatFormat; Precision, Digits: Integer): Integer; overload;
function FloatToText(BufferArg: PAnsiChar; const Value; ValueType: TFloatValue;
Format: TFloatFormat; Precision, Digits: Integer;
const FormatSettings: TFormatSettings): Integer; overload;
function FloatToText(BufferArg: PWideChar; const Value; ValueType: TFloatValue;
Format: TFloatFormat; Precision, Digits: Integer;
const FormatSettings: TFormatSettings): Integer; overload;
{ FormatFloat formats the floating-point value given by Value using the
format string given by Format. The following format specifiers are
supported in the format string:
0 Digit placeholder. If the value being formatted has a digit in the
position where the '0' appears in the format string, then that digit
is copied to the output string. Otherwise, a '0' is stored in that
position in the output string.
# Digit placeholder. If the value being formatted has a digit in the
position where the '#' appears in the format string, then that digit
is copied to the output string. Otherwise, nothing is stored in that
position in the output string.
. Decimal point. The first '.' character in the format string
determines the location of the decimal separator in the formatted
value; any additional '.' characters are ignored. The actual
character used as a the decimal separator in the output string is
determined by the DecimalSeparator global variable, which is initialized
from locale settings obtained from the operating system.
, Thousand separator. If the format string contains one or more ','
characters, the output will have thousand separators inserted between
each group of three digits to the left of the decimal point. The
placement and number of ',' characters in the format string does not
affect the output, except to indicate that thousand separators are
wanted. The actual character used as a the thousand separator in the
output is determined by the ThousandSeparator global variable, which
is initialized from locale settings obtained from the operating system.
E+ Scientific notation. If any of the strings 'E+', 'E-', 'e+', or 'e-'
E- are contained in the format string, the number is formatted using
e+ scientific notation. A group of up to four '0' characters can
e- immediately follow the 'E+', 'E-', 'e+', or 'e-' to determine the
minimum number of digits in the exponent. The 'E+' and 'e+' formats
cause a plus sign to be output for positive exponents and a minus
sign to be output for negative exponents. The 'E-' and 'e-' formats
output a sign character only for negative exponents.
'xx' Characters enclosed in single or double quotes are output as-is, and
"xx" do not affect formatting.
; Separates sections for positive, negative, and zero numbers in the
format string.
The locations of the leftmost '0' before the decimal point in the format
string and the rightmost '0' after the decimal point in the format string
determine the range of digits that are always present in the output string.
The number being formatted is always rounded to as many decimal places as
there are digit placeholders ('0' or '#') to the right of the decimal
point. If the format string contains no decimal point, the value being
formatted is rounded to the nearest whole number.
If the number being formatted has more digits to the left of the decimal
separator than there are digit placeholders to the left of the '.'
character in the format string, the extra digits are output before the
first digit placeholder.
To allow different formats for positive, negative, and zero values, the
format string can contain between one and three sections separated by
semicolons.
One section - The format string applies to all values.
Two sections - The first section applies to positive values and zeros, and
the second section applies to negative values.
Three sections - The first section applies to positive values, the second
applies to negative values, and the third applies to zeros.
If the section for negative values or the section for zero values is empty,
that is if there is nothing between the semicolons that delimit the
section, the section for positive values is used instead.
If the section for positive values is empty, or if the entire format string
is empty, the value is formatted using general floating-point formatting
with 15 significant digits, corresponding to a call to FloatToStrF with
the ffGeneral format. General floating-point formatting is also used if
the value has more than 18 digits to the left of the decimal point and
the format string does not specify scientific notation.
The table below shows some sample formats and the results produced when
the formats are applied to different values:
Format string 1234 -1234 0.5 0
-----------------------------------------------------------------------
1234 -1234 0.5 0
0 1234 -1234 1 0
0.00 1234.00 -1234.00 0.50 0.00
#.## 1234 -1234 .5
#,##0.00 1,234.00 -1,234.00 0.50 0.00
#,##0.00;(#,##0.00) 1,234.00 (1,234.00) 0.50 0.00
#,##0.00;;Zero 1,234.00 -1,234.00 0.50 Zero
0.000E+00 1.234E+03 -1.234E+03 5.000E-01 0.000E+00
#.###E-0 1.234E3 -1.234E3 5E-1 0E0
----------------------------------------------------------------------- }
function FormatFloat(const Format: string; Value: Extended): string; overload;
function FormatFloat(const Format: string; Value: Extended;
const FormatSettings: TFormatSettings): string; overload;
{ FormatCurr formats the currency value given by Value using the format
string given by Format. For further details, see the description of the
FormatFloat function. }
function FormatCurr(const Format: string; Value: Currency): string; overload;
function FormatCurr(const Format: string; Value: Currency;
const FormatSettings: TFormatSettings): string; overload;
{ FloatToTextFmt converts the given floating-point value to its decimal
representation using the specified format. The Value parameter must be a
variable of type Extended or Currency, as indicated by the ValueType
parameter. The resulting string of characters is stored in the given
buffer, and the returned value is the number of characters stored. The
resulting string is not null-terminated. For further details, see the
description of the FormatFloat function. }
function FloatToTextFmt(Buf: PAnsiChar; const Value; ValueType: TFloatValue;
Format: PAnsiChar): Integer; overload;
function FloatToTextFmt(Buf: PAnsiChar; const Value; ValueType: TFloatValue;
Format: PAnsiChar; const FormatSettings: TFormatSettings): Integer; overload;
function FloatToTextFmt(Buf: PWideChar; const Value; ValueType: TFloatValue;
Format: PWideChar): Integer; overload;
function FloatToTextFmt(Buf: PWideChar; const Value; ValueType: TFloatValue;
Format: PWideChar; const FormatSettings: TFormatSettings): Integer; overload;
{ StrToFloat converts the given string to a floating-point value. The string
must consist of an optional sign (+ or -), a string of digits with an
optional decimal point, and an optional 'E' or 'e' followed by a signed
integer. Leading and trailing blanks in the string are ignored. The
DecimalSeparator global variable defines the character that must be used
as a decimal point. Thousand separators and currency symbols are not
allowed in the string. If the string doesn't contain a valid value, an
EConvertError exception is raised. }
function StrToFloat(const S: string): Extended; overload;
function StrToFloat(const S: string;
const FormatSettings: TFormatSettings): Extended; overload;
function StrToFloatDef(const S: string;
const Default: Extended): Extended; overload;
function StrToFloatDef(const S: string; const Default: Extended;
const FormatSettings: TFormatSettings): Extended; overload;
function TryStrToFloat(const S: string; out Value: Extended): Boolean; overload;
function TryStrToFloat(const S: string; out Value: Extended;
const FormatSettings: TFormatSettings): Boolean; overload;
function TryStrToFloat(const S: string; out Value: Double): Boolean; overload;
function TryStrToFloat(const S: string; out Value: Double;
const FormatSettings: TFormatSettings): Boolean; overload;
function TryStrToFloat(const S: string; out Value: Single): Boolean; overload;
function TryStrToFloat(const S: string; out Value: Single;
const FormatSettings: TFormatSettings): Boolean; overload;
{ StrToCurr converts the given string to a currency value. For further
details, see the description of the StrToFloat function. }
function StrToCurr(const S: string): Currency; overload;
function StrToCurr(const S: string;
const FormatSettings: TFormatSettings): Currency; overload;
function StrToCurrDef(const S: string;
const Default: Currency): Currency; overload;
function StrToCurrDef(const S: string; const Default: Currency;
const FormatSettings: TFormatSettings): Currency; overload;
function TryStrToCurr(const S: string; out Value: Currency): Boolean; overload;
function TryStrToCurr(const S: string; out Value: Currency;
const FormatSettings: TFormatSettings): Boolean; overload;
{ TextToFloat converts the null-terminated string given by Buffer to a
floating-point value which is returned in the variable given by Value.
The Value parameter must be a variable of type Extended or Currency, as
indicated by the ValueType parameter. The return value is True if the
conversion was successful, or False if the string is not a valid
floating-point value. For further details, see the description of the
StrToFloat function. }
function TextToFloat(Buffer: PAnsiChar; var Value;
ValueType: TFloatValue): Boolean; overload;
function TextToFloat(Buffer: PAnsiChar; var Value; ValueType: TFloatValue;
const FormatSettings: TFormatSettings): Boolean; overload;
function TextToFloat(Buffer: PWideChar; var Value;
ValueType: TFloatValue): Boolean; overload;
function TextToFloat(Buffer: PWideChar; var Value; ValueType: TFloatValue;
const FormatSettings: TFormatSettings): Boolean; overload;
function HashName(Name: PAnsiChar): Cardinal;
{ FloatToDecimal converts a floating-point value to a decimal representation
that is suited for further formatting. The Value parameter must be a
variable of type Extended or Currency, as indicated by the ValueType
parameter. For values of type Extended, the Precision parameter specifies
the requested number of significant digits in the result--the allowed range
is 1..18. For values of type Currency, the Precision parameter is ignored,
and the implied precision of the conversion is 19 digits. The Decimals
parameter specifies the requested maximum number of digits to the left of
the decimal point in the result. Precision and Decimals together control
how the result is rounded. To produce a result that always has a given
number of significant digits regardless of the magnitude of the number,
specify 9999 for the Decimals parameter. The result of the conversion is
stored in the specified TFloatRec record as follows:
Exponent - Contains the magnitude of the number, i.e. the number of
significant digits to the right of the decimal point. The Exponent field
is negative if the absolute value of the number is less than one. If the
number is a NAN (not-a-number), Exponent is set to -32768. If the number
is INF or -INF (positive or negative infinity), Exponent is set to 32767.
Negative - True if the number is negative, False if the number is zero
or positive.
Digits - Contains up to 18 (for type Extended) or 19 (for type Currency)
significant digits followed by a null terminator. The implied decimal
point (if any) is not stored in Digits. Trailing zeros are removed, and
if the resulting number is zero, NAN, or INF, Digits contains nothing but
the null terminator. }
procedure FloatToDecimal(var Result: TFloatRec; const Value;
ValueType: TFloatValue; Precision, Decimals: Integer);
{ Date/time support routines }
function DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;
function TimeStampToDateTime(const TimeStamp: TTimeStamp): TDateTime;
function MSecsToTimeStamp(MSecs: Comp): TTimeStamp;
function TimeStampToMSecs(const TimeStamp: TTimeStamp): Comp;
{ EncodeDate encodes the given year, month, and day into a TDateTime value.
The year must be between 1 and 9999, the month must be between 1 and 12,
and the day must be between 1 and N, where N is the number of days in the
specified month. If the specified values are not within range, an
EConvertError exception is raised. The resulting value is the number of
days between 12/30/1899 and the given date. }
function EncodeDate(Year, Month, Day: Word): TDateTime;
{ EncodeTime encodes the given hour, minute, second, and millisecond into a
TDateTime value. The hour must be between 0 and 23, the minute must be
between 0 and 59, the second must be between 0 and 59, and the millisecond
must be between 0 and 999. If the specified values are not within range, an
EConvertError exception is raised. The resulting value is a number between
0 (inclusive) and 1 (not inclusive) that indicates the fractional part of
a day given by the specified time. The value 0 corresponds to midnight,
0.5 corresponds to noon, 0.75 corresponds to 6:00 pm, etc. }
function EncodeTime(Hour, Min, Sec, MSec: Word): TDateTime;
{ Instead of generating errors the following variations of EncodeDate and
EncodeTime simply return False if the parameters given are not valid.
Other than that, these functions are functionally the same as the above
functions. }
function TryEncodeDate(Year, Month, Day: Word; out Date: TDateTime): Boolean;
function TryEncodeTime(Hour, Min, Sec, MSec: Word; out Time: TDateTime): Boolean;
{ DecodeDate decodes the integral (date) part of the given TDateTime value
into its corresponding year, month, and day. If the given TDateTime value
is less than or equal to zero, the year, month, and day return parameters
are all set to zero. }
procedure DecodeDate(const DateTime: TDateTime; var Year, Month, Day: Word);
{ This variation of DecodeDate works similarly to the above function but
returns more information. The result value of this function indicates
whether the year decoded is a leap year or not. }
function DecodeDateFully(const DateTime: TDateTime; var Year, Month, Day,
DOW: Word): Boolean;
{$IFDEF LINUX}
function InternalDecodeDate(const DateTime: TDateTime; var Year, Month, Day, DOW: Word): Boolean;
{$ENDIF}
{ DecodeTime decodes the fractional (time) part of the given TDateTime value
into its corresponding hour, minute, second, and millisecond. }
procedure DecodeTime(const DateTime: TDateTime; var Hour, Min, Sec, MSec: Word);
{$IFDEF MSWINDOWS}
{ DateTimeToSystemTime converts a date and time from Delphi's TDateTime
format into the Win32 API's TSystemTime format. }
procedure DateTimeToSystemTime(const DateTime: TDateTime; var SystemTime: TSystemTime);
{ SystemTimeToDateTime converts a date and time from the Win32 API's
TSystemTime format into Delphi's TDateTime format. }
function SystemTimeToDateTime(const SystemTime: TSystemTime): TDateTime;
{$ENDIF}
{ DayOfWeek returns the day of the week of the given date. The result is an
integer between 1 and 7, corresponding to Sunday through Saturday.
This function is not ISO 8601 compliant, for that see the DateUtils unit. }
function DayOfWeek(const DateTime: TDateTime): Word;
{ Date returns the current date. }
function Date: TDateTime;
{ Time returns the current time. }
function Time: TDateTime;
{$IFDEF LINUX}
{ clashes with Time in <X11/Xlib.h>, use GetTime instead }
{$EXTERNALSYM Time}
{$ENDIF}
function GetTime: TDateTime;
{ Now returns the current date and time, corresponding to Date + Time. }
function Now: TDateTime;
{ Current year returns the year portion of the date returned by Now }
function CurrentYear: Word;
{ IncMonth returns Date shifted by the specified number of months.
NumberOfMonths parameter can be negative, to return a date N months ago.
If the input day of month is greater than the last day of the resulting
month, the day is set to the last day of the resulting month.
Input time of day is copied to the DateTime result. }
function IncMonth(const DateTime: TDateTime; NumberOfMonths: Integer = 1): TDateTime;
{ Optimized version of IncMonth that works with years, months and days
directly. See above comments for more detail as to what happens to the day
when incrementing months }
procedure IncAMonth(var Year, Month, Day: Word; NumberOfMonths: Integer = 1);
{ ReplaceTime replaces the time portion of the DateTime parameter with the given
time value, adjusting the signs as needed if the date is prior to 1900
(Date value less than zero) }
procedure ReplaceTime(var DateTime: TDateTime; const NewTime: TDateTime);
{ ReplaceDate replaces the date portion of the DateTime parameter with the given
date value, adjusting as needed for negative dates }
procedure ReplaceDate(var DateTime: TDateTime; const NewDate: TDateTime);
{ IsLeapYear determines whether the given year is a leap year. }
function IsLeapYear(Year: Word): Boolean;
type
PDayTable = ^TDayTable;
TDayTable = array[1..12] of Word;
{ The MonthDays array can be used to quickly find the number of
days in a month: MonthDays[IsLeapYear(Y), M] }
const
MonthDays: array [Boolean] of TDayTable =
((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31));
{ Each of the date/time formatting routines that uses global variables
for formatting (separators, decimals, etc.), has an overloaded equivalent
requiring a parameter of type TFormatSettings. This additional parameter
provides the formatting information rather than the global variables. For
more information see the note at TFormatSettings. }
{ DateToStr converts the date part of the given TDateTime value to a string.
The conversion uses the format specified by the ShortDateFormat global
variable. }
function DateToStr(const DateTime: TDateTime): string; overload; inline;
function DateToStr(const DateTime: TDateTime;
const FormatSettings: TFormatSettings): string; overload; inline;
{ TimeToStr converts the time part of the given TDateTime value to a string.
The conversion uses the format specified by the LongTimeFormat global
variable. }
function TimeToStr(const DateTime: TDateTime): string; overload; inline;
function TimeToStr(const DateTime: TDateTime;
const FormatSettings: TFormatSettings): string; overload; inline;
{ DateTimeToStr converts the given date and time to a string. The resulting
string consists of a date and time formatted using the ShortDateFormat and
LongTimeFormat global variables. Time information is included in the
resulting string only if the fractional part of the given date and time
value is non-zero. }
function DateTimeToStr(const DateTime: TDateTime): string; overload; inline;
function DateTimeToStr(const DateTime: TDateTime;
const FormatSettings: TFormatSettings): string; overload; inline;
{ StrToDate converts the given string to a date value. The string must
consist of two or three numbers, separated by the character defined by
the DateSeparator global variable. The order for month, day, and year is
determined by the ShortDateFormat global variable--possible combinations
are m/d/y, d/m/y, and y/m/d. If the string contains only two numbers, it
is interpreted as a date (m/d or d/m) in the current year. Year values
between 0 and 99 are assumed to be in the current century. If the given
string does not contain a valid date, an EConvertError exception is
raised. }
function StrToDate(const S: string): TDateTime; overload;
function StrToDate(const S: string;
const FormatSettings: TFormatSettings): TDateTime; overload;
function StrToDateDef(const S: string;
const Default: TDateTime): TDateTime; overload;
function StrToDateDef(const S: string; const Default: TDateTime;
const FormatSettings: TFormatSettings): TDateTime; overload;
function TryStrToDate(const S: string; out Value: TDateTime): Boolean; overload;
function TryStrToDate(const S: string; out Value: TDateTime;
const FormatSettings: TFormatSettings): Boolean; overload;
{ StrToTime converts the given string to a time value. The string must
consist of two or three numbers, separated by the character defined by
the TimeSeparator global variable, optionally followed by an AM or PM
indicator. The numbers represent hour, minute, and (optionally) second,
in that order. If the time is followed by AM or PM, it is assumed to be
in 12-hour clock format. If no AM or PM indicator is included, the time
is assumed to be in 24-hour clock format. If the given string does not
contain a valid time, an EConvertError exception is raised. }
function StrToTime(const S: string): TDateTime; overload;
function StrToTime(const S: string;
const FormatSettings: TFormatSettings): TDateTime; overload;
function StrToTimeDef(const S: string;
const Default: TDateTime): TDateTime; overload;
function StrToTimeDef(const S: string; const Default: TDateTime;
const FormatSettings: TFormatSettings): TDateTime; overload;
function TryStrToTime(const S: string; out Value: TDateTime): Boolean; overload;
function TryStrToTime(const S: string; out Value: TDateTime;
const FormatSettings: TFormatSettings): Boolean; overload;
{ StrToDateTime converts the given string to a date and time value. The
string must contain a date optionally followed by a time. The date and
time parts of the string must follow the formats described for the
StrToDate and StrToTime functions. }
function StrToDateTime(const S: string): TDateTime; overload;
function StrToDateTime(const S: string;
const FormatSettings: TFormatSettings): TDateTime; overload;
function StrToDateTimeDef(const S: string;
const Default: TDateTime): TDateTime; overload;
function StrToDateTimeDef(const S: string; const Default: TDateTime;
const FormatSettings: TFormatSettings): TDateTime; overload;
function TryStrToDateTime(const S: string;
out Value: TDateTime): Boolean; overload;
function TryStrToDateTime(const S: string; out Value: TDateTime;
const FormatSettings: TFormatSettings): Boolean; overload;
{ FormatDateTime formats the date-and-time value given by DateTime using the
format given by Format. The following format specifiers are supported:
c Displays the date using the format given by the ShortDateFormat
global variable, followed by the time using the format given by
the LongTimeFormat global variable. The time is not displayed if
the fractional part of the DateTime value is zero.
d Displays the day as a number without a leading zero (1-31).
dd Displays the day as a number with a leading zero (01-31).
ddd Displays the day as an abbreviation (Sun-Sat) using the strings
given by the ShortDayNames global variable.
dddd Displays the day as a full name (Sunday-Saturday) using the strings
given by the LongDayNames global variable.
ddddd Displays the date using the format given by the ShortDateFormat
global variable.
dddddd Displays the date using the format given by the LongDateFormat
global variable.
g Displays the period/era as an abbreviation (Japanese and
Taiwanese locales only).
gg Displays the period/era as a full name.
e Displays the year in the current period/era as a number without
a leading zero (Japanese, Korean and Taiwanese locales only).
ee Displays the year in the current period/era as a number with
a leading zero (Japanese, Korean and Taiwanese locales only).
m Displays the month as a number without a leading zero (1-12). If
the m specifier immediately follows an h or hh specifier, the
minute rather than the month is displayed.
mm Displays the month as a number with a leading zero (01-12). If
the mm specifier immediately follows an h or hh specifier, the
minute rather than the month is displayed.
mmm Displays the month as an abbreviation (Jan-Dec) using the strings
given by the ShortMonthNames global variable.
mmmm Displays the month as a full name (January-December) using the
strings given by the LongMonthNames global variable.
yy Displays the year as a two-digit number (00-99).
yyyy Displays the year as a four-digit number (0000-9999).
h Displays the hour without a leading zero (0-23).
hh Displays the hour with a leading zero (00-23).
n Displays the minute without a leading zero (0-59).
nn Displays the minute with a leading zero (00-59).
s Displays the second without a leading zero (0-59).
ss Displays the second with a leading zero (00-59).
z Displays the millisecond without a leading zero (0-999).
zzz Displays the millisecond with a leading zero (000-999).
t Displays the time using the format given by the ShortTimeFormat
global variable.
tt Displays the time using the format given by the LongTimeFormat
global variable.
am/pm Uses the 12-hour clock for the preceding h or hh specifier, and
displays 'am' for any hour before noon, and 'pm' for any hour
after noon. The am/pm specifier can use lower, upper, or mixed
case, and the result is displayed accordingly.
a/p Uses the 12-hour clock for the preceding h or hh specifier, and
displays 'a' for any hour before noon, and 'p' for any hour after
noon. The a/p specifier can use lower, upper, or mixed case, and
the result is displayed accordingly.
ampm Uses the 12-hour clock for the preceding h or hh specifier, and
displays the contents of the TimeAMString global variable for any
hour before noon, and the contents of the TimePMString global
variable for any hour after noon.
/ Displays the date separator character given by the DateSeparator
global variable.
: Displays the time separator character given by the TimeSeparator
global variable.
'xx' Characters enclosed in single or double quotes are displayed as-is,
"xx" and do not affect formatting.
Format specifiers may be written in upper case as well as in lower case
letters--both produce the same result.
If the string given by the Format parameter is empty, the date and time
value is formatted as if a 'c' format specifier had been given.
The following example:
S := FormatDateTime('"The meeting is on" dddd, mmmm d, yyyy, ' +
'"at" hh:mm AM/PM', StrToDateTime('2/15/95 10:30am'));
assigns 'The meeting is on Wednesday, February 15, 1995 at 10:30 AM' to
the string variable S. }
function FormatDateTime(const Format: string;
DateTime: TDateTime): string; overload; inline;
function FormatDateTime(const Format: string; DateTime: TDateTime;
const FormatSettings: TFormatSettings): string; overload;
{ DateTimeToString converts the date and time value given by DateTime using
the format string given by Format into the string variable given by Result.
For further details, see the description of the FormatDateTime function. }
procedure DateTimeToString(var Result: string; const Format: string;
DateTime: TDateTime); overload;
procedure DateTimeToString(var Result: string; const Format: string;
DateTime: TDateTime; const FormatSettings: TFormatSettings); overload;
{ FloatToDateTime will range validate a value to make sure it falls
within the acceptable date range }
const
MinDateTime: TDateTime = -657434.0; { 01/01/0100 12:00:00.000 AM }
MaxDateTime: TDateTime = 2958465.99999; { 12/31/9999 11:59:59.999 PM }
function FloatToDateTime(const Value: Extended): TDateTime;
function TryFloatToDateTime(const Value: Extended; out AResult: TDateTime): Boolean;
{ System error messages }
function SysErrorMessage(ErrorCode: Cardinal): string;
{ Initialization file support }
function GetLocaleStr(Locale, LocaleType: Integer; const Default: string): string; platform;
function GetLocaleChar(Locale, LocaleType: Integer; Default: Char): Char; platform;
{ GetFormatSettings resets all locale-specific variables (date, time, number,
currency formats, system locale) to the values provided by the operating system. }
procedure GetFormatSettings;
{ GetLocaleFormatSettings loads locale-specific variables (date, time, number,
currency formats) with values provided by the operating system for the
specified locale (LCID). The values are stored in the FormatSettings record. }
{$IFDEF MSWINDOWS}
procedure GetLocaleFormatSettings(LCID: Integer;
var FormatSettings: TFormatSettings);
{$ENDIF}
{ Exception handling routines }
{$IFDEF POSIX}
{ InquireSignal is used to determine the state of an OS signal handler.
Pass it one of the RTL_SIG* constants, and it will return a TSignalState
which will tell you if the signal has been hooked, not hooked, or overriden
by some other module. You can use this function to determine if some other
module has hijacked your signal handlers, should you wish to reinstall your
own. This is a risky proposition under Linux, and is only recommended as a
last resort. Do not pass RTL_SIGDEFAULT to this function.
}
function InquireSignal(RtlSigNum: Integer): TSignalState;
{ AbandonSignalHandler tells the RTL to leave a signal handler
in place, even if we believe that we hooked it at startup time.
Once you have called AbandonSignalHandler with a specific signal number,
neither UnhookSignal nor the RTL will restore any previous signal handler
under any condition.
}
procedure AbandonSignalHandler(RtlSigNum: Integer);
{ HookSignal is used to hook individual signals, or an RTL-defined default
set of signals. It does not test whether a signal has already been
hooked, so it should be used in conjunction with InquireSignal. It is
exposed to enable users to hook signals in standalone libraries, or in the
event that an external module hijacks the RTL installed signal handlers.
Pass RTL_SIGDEFAULT if you want to hook all the signals that the RTL
normally hooks at startup time.
}
procedure HookSignal(RtlSigNum: Integer);
{ UnhookSignal is used to remove signal handlers installed by HookSignal.
It can remove individual signal handlers, or the RTL-defined default set
of signals. If OnlyIfHooked is True, then we will only unhook the signal
if the signal handler has been hooked, and has not since been overriden by
some foreign handler.
}
procedure UnhookSignal(RtlSigNum: Integer; OnlyIfHooked: Boolean = True);
{ HookOSExceptions is used internally by thread support. DON'T call this
function yourself. }
procedure HookOSExceptions;
{ MapSignal is used internally as well. It maps a signal and associated
context to an internal value that represents the type of Exception
class to raise. }
function MapSignal(SigNum: Integer; Context: PSigContext): LongWord;
{ SignalConverter is used internally to properly reinit the FPU and properly
raise an external OS exception object. DON'T call this function yourself. }
procedure SignalConverter(ExceptionEIP: LongWord; FaultAddr: LongWord; ErrorCode: LongWord);
{
See the comment at the threadvar declarations for these below. The access
to these has been implemented through getter/setter functions because you
cannot use threadvars across packages.
}
procedure SetSafeCallExceptionMsg(const Msg: String);
procedure SetSafeCallExceptionAddr(Addr: Pointer);
function GetSafeCallExceptionMsg: String;
function GetSafeCallExceptionAddr: Pointer;
{ HookOSExceptionsProc is used internally and cannot be used in a conventional
manner. DON'T ever set this variable. }
var
HookOSExceptionsProc: procedure = nil platform deprecated;
{ LoadLibrary / FreeLibrary are defined here only for convenience. On Linux,
they map directly to dlopen / dlclose. Note that module loading semantics
on Linux are not identical to Windows. }
function LoadLibrary(ModuleName: PChar): HMODULE;
function FreeLibrary(Module: HMODULE): LongBool;
{ GetProcAddress does what it implies. It performs the same function as the like
named function under Windows. dlsym does not quite have the same sematics as
GetProcAddress as it will return the address of a symbol in another module if
it was not found in the given HMODULE. This function will verify that the 'Proc'
is actually found within the 'Module', and if not returns nil }
function GetProcAddress(Module: HMODULE; Proc: PChar): Pointer;
{ Given a module name, this function will return the module handle. There is no
direct equivalent in Linux so this function provides that capability. Also
note, this function is specific to glibc. }
function GetModuleHandle(ModuleName: PChar): HMODULE;
{ This function works just like GetModuleHandle, except it will look for a module
that matches the given base package name. For example, given the base package
name 'package', the actual module name is, by default, 'bplpackage.so'. This
function will search for the string 'package' within the module name. }
function GetPackageModuleHandle(PackageName: PChar): HMODULE;
{$ENDIF POSIX}
{ In Linux, the parameter to sleep() is in whole seconds. In Windows, the
parameter is in milliseconds. To ease headaches, we implement a version
of sleep here for Linux that takes milliseconds and calls a Linux system
function with sub-second resolution. This maps directly to the Windows
API on Windows. }
procedure Sleep(milliseconds: Cardinal);{$IFDEF MSWINDOWS} stdcall; {$ENDIF}
{$IFDEF MSWINDOWS}
(*$EXTERNALSYM Sleep*)
{$ENDIF}
function GetModuleName(Module: HMODULE): string;
function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer;
Buffer: PChar; Size: Integer): Integer;
procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);
procedure Abort;
procedure OutOfMemoryError;
procedure Beep; inline;
{ MBCS functions }
{ LeadBytes is a char set that indicates which char values are lead bytes
in multibyte character sets (Japanese, Chinese, etc).
This set is always empty for western locales. }
var
LeadBytes: set of AnsiChar = [];
(*$EXTERNALSYM LeadBytes*)
(*$HPPEMIT 'namespace Sysutils {'*)
(*$HPPEMIT 'extern PACKAGE System::Set<Byte, 0, 255> LeadBytes;'*)
(*$HPPEMIT '} // namespace Sysutils'*)
{ ByteType indicates what kind of byte exists at the Index'th byte in S.
Western locales always return mbSingleByte. Far East multibyte locales
may also return mbLeadByte, indicating the byte is the first in a multibyte
character sequence, and mbTrailByte, indicating that the byte is one of
a sequence of bytes following a lead byte. One or more trail bytes can
follow a lead byte, depending on locale charset encoding and OS platform.
Parameters are assumed to be valid. }
function ByteType(const S: AnsiString; Index: Integer): TMbcsByteType; overload;
function ByteType(const S: UnicodeString; Index: Integer): TMbcsByteType; overload;
{ StrByteType works the same as ByteType, but on null-terminated PChar strings }
function StrByteType(Str: PAnsiChar; Index: Cardinal): TMbcsByteType; overload;
function StrByteType(Str: PWideChar; Index: Cardinal): TMbcsByteType; overload;
{ ByteToCharLen returns the character length of a MBCS string, scanning the
string for up to MaxLen bytes. In multibyte character sets, the number of
characters in a string may be less than the number of bytes. }
function ByteToCharLen(const S: AnsiString; MaxLen: Integer): Integer; overload; inline;
function ByteToCharLen(const S: UnicodeString; MaxLen: Integer): Integer; overload; inline; deprecated 'Use ElementToCharLen.';
function ElementToCharLen(const S: AnsiString; MaxLen: Integer): Integer; overload;
function ElementToCharLen(const S: UnicodeString; MaxLen: Integer): Integer; overload;
{ CharToByteLen returns the byte length of a MBCS string, scanning the string
for up to MaxLen characters. }
function CharToByteLen(const S: AnsiString; MaxLen: Integer): Integer; overload; inline;
function CharToByteLen(const S: UnicodeString; MaxLen: Integer): Integer; overload; inline; deprecated 'Use CharToElementLen.';
function CharToElementLen(const S: AnsiString; MaxLen: Integer): Integer; overload;
function CharToElementLen(const S: UnicodeString; MaxLen: Integer): Integer; overload;
{ ByteToCharIndex returns the 1-based character index of the Index'th byte in
a MBCS string. Returns zero if Index is out of range:
(Index <= 0) or (Index > Length(S)) }
function ByteToCharIndex(const S: AnsiString; Index: Integer): Integer; overload; inline;
function ByteToCharIndex(const S: UnicodeString; Index: Integer): Integer; overload; inline; deprecated 'Use ElementToCharIndex.';
function ElementToCharIndex(const S: AnsiString; Index: Integer): Integer; overload;
function ElementToCharIndex(const S: UnicodeString; Index: Integer): Integer; overload;
{ CharToByteIndex returns the 1-based byte index of the Index'th character
in a MBCS string. Returns zero if Index or Result are out of range:
(Index <= 0) or (Index > Length(S)) or (Result would be > Length(S)) }
function CharToByteIndex(const S: AnsiString; Index: Integer): Integer; overload; inline;
function CharToByteIndex(const S: UnicodeString; Index: Integer): Integer; overload; inline; deprecated 'Use CharToElementIndex.';
function CharToElementIndex(const S: AnsiString; Index: Integer): Integer; overload;
function CharToElementIndex(const S: UnicodeString; Index: Integer): Integer; overload;
{ StrCharLength returns the number of bytes required by the first character
in Str. In Windows, multibyte characters can be up to two bytes in length.
In Linux, multibyte characters can be up to six bytes in length (UTF-8). }
function StrCharLength(const Str: PAnsiChar): Integer; overload;
function StrCharLength(const Str: PWideChar): Integer; overload;
{ StrNextChar returns a pointer to the first byte of the character following
the character pointed to by Str. }
function StrNextChar(const Str: PAnsiChar): PAnsiChar; inline; overload;
function StrNextChar(const Str: PWideChar): PWideChar; overload;
{ CharLength returns the number of bytes required by the character starting
at bytes S[Index]. }
function CharLength(const S: AnsiString; Index: Integer): Integer; overload;
function CharLength(const S: UnicodeString; Index: Integer): Integer; overload;
{ NextCharIndex returns the byte index of the first byte of the character
following the character starting at S[Index]. }
function NextCharIndex(const S: UnicodeString; Index: Integer): Integer; overload;
function NextCharIndex(const S: AnsiString; Index: Integer): Integer; overload;
{ IsLeadChar returns whether or not the Char is part of a multi-character sequence }
function IsLeadChar(C: AnsiChar): Boolean; overload; inline;
function IsLeadChar(C: WideChar): Boolean; overload; inline;
{ CharInSet tests whether or not the given character is in the given set of lower
characters }
function CharInSet(C: AnsiChar; const CharSet: TSysCharSet): Boolean; overload; inline;
function CharInSet(C: WideChar; const CharSet: TSysCharSet): Boolean; overload; inline;
{ IsPathDelimiter returns True if the character at byte S[Index]
is a PathDelimiter ('\' or '/'), and it is not a MBCS lead or trail byte. }
function IsPathDelimiter(const S: string; Index: Integer): Boolean; overload;
{ IsDelimiter returns True if the character at byte S[Index] matches any
character in the Delimiters string, and the character is not a MBCS lead or
trail byte. S may contain multibyte characters; Delimiters must contain
only single byte characters. }
function IsDelimiter(const Delimiters, S: string; Index: Integer): Boolean; overload;
{ IncludeTrailingPathDelimiter returns the path with a PathDelimiter
('/' or '\') at the end. This function is MBCS enabled. }
function IncludeTrailingPathDelimiter(const S: string): string; overload;
{ IncludeTrailingBackslash is the old name for IncludeTrailingPathDelimiter. }
function IncludeTrailingBackslash(const S: string): string; platform; overload; inline;
{ ExcludeTrailingPathDelimiter returns the path without a PathDelimiter
('\' or '/') at the end. This function is MBCS enabled. }
function ExcludeTrailingPathDelimiter(const S: string): string; overload;
{ ExcludeTrailingBackslash is the old name for ExcludeTrailingPathDelimiter. }
function ExcludeTrailingBackslash(const S: string): string; platform; overload; inline;
{ LastDelimiter returns the byte index in S of the rightmost whole
character that matches any character in Delimiters (except null (#0)).
S may contain multibyte characters; Delimiters must contain only single
byte non-null characters.
Example: LastDelimiter('\.:', 'c:\filename.ext') returns 12. }
function LastDelimiter(const Delimiters, S: string): Integer; overload;
{ FindDelimiter returns the index in S of the character that matches any of
the characters in Delimiters (except null (#)). StartIdx specifies the
index in S at which the search for delimiters will start. }
function FindDelimiter(const Delimiters, S: string; StartIdx: Integer = 1): Integer;
{ AnsiCompareFileName supports DOS file name comparison idiosyncracies
in Far East locales (Zenkaku) on Windows.
In non-MBCS locales on Windows, AnsiCompareFileName is identical to
AnsiCompareText (case insensitive).
On Linux, AnsiCompareFileName is identical to AnsiCompareStr (case sensitive).
For general purpose file name comparisions, you should use this function
instead of AnsiCompareText. }
function AnsiCompareFileName(const S1, S2: string): Integer; inline; overload;
function SameFileName(const S1, S2: string): Boolean; inline; overload;
{ AnsiLowerCaseFileName supports lowercase conversion idiosyncracies of
DOS file names in Far East locales (Zenkaku). In non-MBCS locales,
AnsiLowerCaseFileName is identical to AnsiLowerCase. }
function AnsiLowerCaseFileName(const S: string): string; overload;
{ AnsiUpperCaseFileName supports uppercase conversion idiosyncracies of
DOS file names in Far East locales (Zenkaku). In non-MBCS locales,
AnsiUpperCaseFileName is identical to AnsiUpperCase. }
function AnsiUpperCaseFileName(const S: string): string; overload;
{ AnsiPos: Same as Pos but supports MBCS strings }
function AnsiPos(const Substr, S: string): Integer; overload;
{ AnsiStrPos: Same as StrPos but supports MBCS strings }
function AnsiStrPos(Str, SubStr: PAnsiChar): PAnsiChar; overload;
function AnsiStrPos(Str, SubStr: PWideChar): PWideChar; overload;
{ AnsiStrRScan: Same as StrRScan but supports MBCS strings }
function AnsiStrRScan(Str: PAnsiChar; Chr: AnsiChar): PAnsiChar; overload;
function AnsiStrRScan(Str: PWideChar; Chr: WideChar): PWideChar; inline; overload;
{ AnsiStrScan: Same as StrScan but supports MBCS strings }
function AnsiStrScan(Str: PAnsiChar; Chr: AnsiChar): PAnsiChar; overload;
function AnsiStrScan(Str: PWideChar; Chr: WideChar): PWideChar; overload; inline;
{ StringReplace replaces occurances of <oldpattern> with <newpattern> in a
given string. Assumes the string may contain Multibyte characters }
type
TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase);
function StringReplace(const S, OldPattern, NewPattern: string;
Flags: TReplaceFlags): string; overload;
{ WrapText will scan a string for BreakChars and insert the BreakStr at the
last BreakChar position before MaxCol. Will not insert a break into an
embedded quoted string (both ''' and '"' supported) }
function WrapText(const Line, BreakStr: string; const BreakChars: TSysCharSet;
MaxCol: Integer): string; overload;
function WrapText(const Line: string; MaxCol: Integer = 45): string; overload;
{ FindCmdLineSwitch determines whether the string in the Switch parameter
was passed as a command line argument to the application. SwitchChars
identifies valid argument-delimiter characters (i.e., "-" and "/" are
common delimiters). The IgnoreCase paramter controls whether a
case-sensistive or case-insensitive search is performed. }
const
SwitchChars = {$IFDEF MSWINDOWS} ['/','-']; {$ENDIF}
{$IFDEF LINUX} ['-']; {$ENDIF}
{$IFDEF MACOSX} ['-']; {$ENDIF}
function FindCmdLineSwitch(const Switch: string; const Chars: TSysCharSet;
IgnoreCase: Boolean): Boolean; overload;
{ These versions of FindCmdLineSwitch are convenient for writing portable
code. The characters that are valid to indicate command line switches vary
on different platforms. For example, '/' cannot be used as a switch char
on Linux because '/' is the path delimiter. }
{ This version uses SwitchChars defined above, and IgnoreCase False. }
function FindCmdLineSwitch(const Switch: string): Boolean; overload;
{ This version uses SwitchChars defined above. }
function FindCmdLineSwitch(const Switch: string; IgnoreCase: Boolean): Boolean; overload;
{ FreeAndNil frees the given TObject instance and sets the variable reference
to nil. Be careful to only pass TObjects to this routine. }
procedure FreeAndNil(var Obj); inline;
{ Interface support routines }
function Supports(const Instance: IInterface; const IID: TGUID; out Intf): Boolean; overload;
function Supports(const Instance: TObject; const IID: TGUID; out Intf): Boolean; overload;
function Supports(const Instance: IInterface; const IID: TGUID): Boolean; overload;
function Supports(const Instance: TObject; const IID: TGUID): Boolean; overload;
function Supports(const AClass: TClass; const IID: TGUID): Boolean; overload;
function CreateGUID(out Guid: TGUID): HResult;
{$IFDEF MSWINDOWS}
stdcall;
{$ENDIF}
function StringToGUID(const S: string): TGUID;
function GUIDToString(const GUID: TGUID): string;
function IsEqualGUID(const guid1, guid2: TGUID): Boolean;
{$IFDEF MSWINDOWS}
stdcall; {$EXTERNALSYM IsEqualGUID}
{$ENDIF}
{ Package support routines }
{ Package Info flags }
const
pfNeverBuild = $00000001;
pfDesignOnly = $00000002;
pfRunOnly = $00000004;
pfIgnoreDupUnits = $00000008;
pfModuleTypeMask = $C0000000;
pfExeModule = $00000000;
pfPackageModule = $40000000;
pfProducerMask = $0C000000;
pfV3Produced = $00000000;
pfProducerUndefined = $04000000;
pfBCB4Produced = $08000000;
pfDelphi4Produced = $0C000000;
pfLibraryModule = $80000000;
pfConsumerMask = $00F00000;
pfConsumerCompat = $00000000;
pfConsumerDelphi = $00100000;
pfConsumerBCB = $00200000;
{ Unit info flags }
const
ufMainUnit = $01;
ufPackageUnit = $02;
ufWeakUnit = $04;
ufOrgWeakUnit = $08;
ufImplicitUnit = $10;
ufWeakPackageUnit = ufPackageUnit or ufWeakUnit;
{$IFDEF POSIX}
var
PkgLoadingMode: Integer = RTLD_LAZY;
{$ENDIF POSIX}
{ Procedure type of the callback given to GetPackageInfo. Name is the actual
name of the package element. If IsUnit is True then Name is the name of
a contained unit; a required package if False. Param is the value passed
to GetPackageInfo }
type
TNameType = (ntContainsUnit, ntRequiresPackage, ntDcpBpiName);
TPackageInfoProc = procedure (const Name: string; NameType: TNameType; Flags: Byte; Param: Pointer);
TValidatePackageProc = function (Module: HMODULE): Boolean;
{ LoadPackage loads a given package DLL, checks for duplicate units and
calls the initialization blocks of all the contained units. Duplicate unit checks are
bypassed if the AValidatePackage function returns "True." Be warned that this may cause
strange and unpredictable behaviour if two packages are loaded that contain the same
units and types. }
function LoadPackage(const Name: string): HMODULE; overload;
function LoadPackage(const Name: string; AValidatePackage: TValidatePackageProc): HMODULE; overload;
{ UnloadPackage does the opposite of LoadPackage by calling the finalization
blocks of all contained units, then unloading the package DLL }
procedure UnloadPackage(Module: HMODULE);
{ GetPackageInfo accesses the given package's info table and enumerates
all the contained units and required packages }
procedure GetPackageInfo(Module: HMODULE; Param: Pointer; var Flags: Integer;
InfoProc: TPackageInfoProc);
{ GetPackageDescription loads the description resource from the package
library. If the description resource does not exist,
an empty string is returned. }
function GetPackageDescription(ModuleName: PChar): string;
{ InitializePackage validates and initializes the given package DLL }
procedure InitializePackage(Module: HMODULE); overload;
procedure InitializePackage(Module: HMODULE; AValidatePackage: TValidatePackageProc); overload;
{ FinalizePackage finalizes the given package DLL }
procedure FinalizePackage(Module: HMODULE);
{ RaiseLastOSError calls GetLastError to retrieve the code for
the last occuring error in a call to an OS or system library function.
If GetLastError returns an error code, RaiseLastOSError raises
an EOSError exception with the error code and a system-provided
message associated with with error. }
procedure RaiseLastOSError; overload;
procedure RaiseLastOSError(LastError: Integer); overload;
{$IFDEF MSWINDOWS}
procedure RaiseLastWin32Error; deprecated 'Use RaiseLastOSError';
{ Win32Check is used to check the return value of a Win32 API function }
{ which returns a BOOL to indicate success. If the Win32 API function }
{ returns False (indicating failure), Win32Check calls RaiseLastOSError }
{ to raise an exception. If the Win32 API function returns True, }
{ Win32Check returns True. }
function Win32Check(RetVal: BOOL): BOOL; platform;
{$ENDIF MSWINDOWS}
{ Termination procedure support }
type
TTerminateProc = function: Boolean;
{ Call AddTerminateProc to add a terminate procedure to the system list of }
{ termination procedures. Delphi will call all of the function in the }
{ termination procedure list before an application terminates. The user- }
{ defined TermProc function should return True if the application can }
{ safely terminate or False if the application cannot safely terminate. }
{ If one of the functions in the termination procedure list returns False, }
{ the application will not terminate. }
procedure AddTerminateProc(TermProc: TTerminateProc);
{ CallTerminateProcs is called by VCL when an application is about to }
{ terminate. It returns True only if all of the functions in the }
{ system's terminate procedure list return True. This function is }
{ intended only to be called by Delphi, and it should not be called }
{ directly. }
function CallTerminateProcs: Boolean;
function GDAL: LongWord;
procedure RCS;
procedure RPR;
{ HexDisplayPrefix contains the prefix to display on hexadecimal
values - '$' for Pascal syntax, '0x' for C++ syntax. This is
for display only - this does not affect the string-to-integer
conversion routines. }
var
HexDisplayPrefix: string = '$';
{$IFDEF MSWINDOWS}
{ The GetDiskFreeSpace Win32 API does not support partitions larger than 2GB
under Win95. A new Win32 function, GetDiskFreeSpaceEx, supports partitions
larger than 2GB but only exists on Win NT 4.0 and Win95 OSR2.
The GetDiskFreeSpaceEx function pointer variable below will be initialized
at startup to point to either the actual OS API function if it exists on
the system, or to an internal Delphi function if it does not. When running
on Win95 pre-OSR2, the output of this function will still be limited to
the 2GB range reported by Win95, but at least you don't have to worry
about which API function to call in code you write. }
var
GetDiskFreeSpaceEx: function (Directory: PChar; var FreeAvailable,
TotalSpace: TLargeInteger; TotalFree: PLargeInteger): Bool stdcall = nil;
{ SafeLoadLibrary calls LoadLibrary, disabling normal Win32 error message
popup dialogs if the requested file can't be loaded. SafeLoadLibrary also
preserves the current FPU control word (precision, exception masks) across
the LoadLibrary call (in case the DLL you're loading hammers the FPU control
word in its initialization, as many MS DLLs do)}
function SafeLoadLibrary(const FileName: string;
ErrorMode: UINT = SEM_NOOPENFILEERRORBOX): HMODULE;
{$ENDIF MSWINDOWS}
{$IFDEF POSIX}
{ SafeLoadLibrary calls LoadLibrary preserves the current FPU control
word (precision, exception masks) across the LoadLibrary call (in
case the shared object you're loading hammers the FPU control
word in its initialization, as many MS DLLs do) }
function SafeLoadLibrary(const FileName: string;
Dummy: LongWord = 0): HMODULE;
{$ENDIF POSIX}
{ Thread synchronization }
{ IReadWriteSync is an abstract interface for general read/write synchronization.
Some implementations may allow simultaneous readers, but writers always have
exclusive locks.
Worst case is that this class behaves identical to a TRTLCriticalSection -
that is, read and write locks block all other threads. }
type
IReadWriteSync = interface
['{7B108C52-1D8F-4CDB-9CDF-57E071193D3F}']
procedure BeginRead;
procedure EndRead;
function BeginWrite: Boolean;
procedure EndWrite;
end;
TSimpleRWSync = class(TInterfacedObject, IReadWriteSync)
private
FLock: TRTLCriticalSection;
public
constructor Create;
destructor Destroy; override;
procedure BeginRead;
procedure EndRead;
function BeginWrite: Boolean;
procedure EndWrite;
end;
{ TThreadLocalCounter
This class implements a lightweight non-blocking thread local storage
mechanism specifically built for tracking per-thread recursion counts
in TMultiReadExclusiveWriteSynchronizer. This class is intended for
Delphi RTL internal use only. In the future it may be generalized
and "hardened" for general application use, but until then leave it alone.
Rules of Use:
The tls object must be opened to gain access to the thread-specific data
structure. If a threadinfo block does not exist for the current thread,
Open will allocate one. Every call to Open must be matched with a call
to Close. The pointer returned by Open is invalid after the matching call
to Close (or Delete).
The thread info structure is unique to each thread. Once you have it, it's
yours. You don't need to guard against concurrent access to the thread
data by multiple threads - your thread is the only thread that will ever
have access to the structure that Open returns. The thread info structure
is allocated and owned by the tls object. If you put allocated pointers
in the thread info make sure you free them before you delete the threadinfo
node.
When thread data is no longer needed, call the Delete method on the pointer.
This must be done between calls to Open and Close. You should not use the
thread data after calling Delete.
Important: Do not keep the tls object open for long periods of time.
In particular, be careful not to wait on a thread synchronization event or
critical section while you have the tls object open. It's much better to
open and close the tls object before and after the blocking event than to
leave the tls object open while waiting.
Implementation Notes:
The main purpose of this storage class is to provide thread-local storage
without using limited / problematic OS tls slots and without requiring
expensive blocking thread synchronization. This class performs no
blocking waits or spin loops! (except for memory allocation)
Thread info is kept in linked lists to facilitate non-blocking threading
techniques. A hash table indexed by a hash of the current thread ID
reduces linear search times.
When a node is deleted, its thread ID is stripped and its Active field is
set to zero, meaning it is available to be recycled for other threads.
Nodes are never removed from the live list or freed while the class is in
use. All nodes are freed when the class is destroyed.
Nodes are only inserted at the front of the list (each list in the hash table).
The linked list management relies heavily on InterlockedExchange to perform
atomic node pointer replacements. There are brief windows of time where
the linked list may be circular while a two-step insertion takes place.
During that brief window, other threads traversing the lists may see
the same node more than once more than once. (pun!) This is fine for what this
implementation needs. Don't do anything silly like try to count the
nodes during a traversal.
}
type
PThreadInfo = ^TThreadInfo;
TThreadInfo = record
Next: PThreadInfo;
ThreadID: Cardinal;
Active: Integer;
RecursionCount: Cardinal;
end;
TThreadLocalCounter = class
private
FHashTable: array [0..15] of PThreadInfo;
function HashIndex: Byte;
function Recycle: PThreadInfo;
public
destructor Destroy; override;
procedure Open(var Thread: PThreadInfo);
procedure Delete(var Thread: PThreadInfo);
procedure Close(var Thread: PThreadInfo);
end;
{$IFDEF MSWINDOWS}
{ TMultiReadExclusiveWriteSynchronizer minimizes thread serialization to gain
read access to a resource shared among threads while still providing complete
exclusivity to callers needing write access to the shared resource.
(multithread shared reads, single thread exclusive write)
Read locks are allowed while owning a write lock.
Read locks can be promoted to write locks within the same thread.
(BeginRead, BeginWrite, EndWrite, EndRead)
Note: Other threads have an opportunity to modify the protected resource
when you call BeginWrite before you are granted the write lock, even
if you already have a read lock open. Best policy is not to retain
any info about the protected resource (such as count or size) across a
write lock. Always reacquire samples of the protected resource after
acquiring or releasing a write lock.
The function result of BeginWrite indicates whether another thread got
the write lock while the current thread was waiting for the write lock.
Return value of True means that the write lock was acquired without
any intervening modifications by other threads. Return value of False
means another thread got the write lock while you were waiting, so the
resource protected by the MREWS object should be considered modified.
Any samples of the protected resource should be discarded.
In general, it's better to just always reacquire samples of the protected
resource after obtaining a write lock. The boolean result of BeginWrite
and the RevisionLevel property help cases where reacquiring the samples
is computationally expensive or time consuming.
RevisionLevel changes each time a write lock is granted. You can test
RevisionLevel for equality with a previously sampled value of the property
to determine if a write lock has been granted, implying that the protected
resource may be changed from its state when the original RevisionLevel
value was sampled. Do not rely on the sequentiality of the current
RevisionLevel implementation (it will wrap around to zero when it tops out).
Do not perform greater than / less than comparisons on RevisionLevel values.
RevisionLevel indicates only the stability of the protected resource since
your original sample. It should not be used to calculate how many
revisions have been made.
}
type
TMultiReadExclusiveWriteSynchronizer = class(TInterfacedObject, IReadWriteSync)
private
FSentinel: Integer;
FReadSignal: THandle;
FWriteSignal: THandle;
FWaitRecycle: Cardinal;
FWriteRecursionCount: Cardinal;
tls: TThreadLocalCounter;
FWriterID: Cardinal;
FRevisionLevel: Cardinal;
procedure BlockReaders;
procedure UnblockReaders;
procedure UnblockOneWriter;
procedure WaitForReadSignal;
procedure WaitForWriteSignal;
{$IFDEF DEBUG_MREWS}
procedure Debug(const Msg: string);
{$ENDIF}
public
constructor Create;
destructor Destroy; override;
procedure BeginRead;
procedure EndRead;
function BeginWrite: Boolean;
procedure EndWrite;
property RevisionLevel: Cardinal read FRevisionLevel;
end;
{$ELSE}
type
TMultiReadExclusiveWriteSynchronizer = TSimpleRWSync;
{$ENDIF}
type
TMREWSync = TMultiReadExclusiveWriteSynchronizer; // short form
function GetEnvironmentVariable(const Name: string): string; overload;
{$IFDEF LINUX}
function InterlockedIncrement(var I: Integer): Integer;
function InterlockedDecrement(var I: Integer): Integer;
function InterlockedExchange(var A: Integer; B: Integer): Integer;
function InterlockedExchangeAdd(var A: Integer; B: Integer): Integer;
{$ENDIF}
// Utility function for .NET source compatibility
function DelegatesEqual(A, B: Pointer): Boolean; inline;
// Utility functions for Unicode support
function ByteLength(const S: string): Integer; inline;
type
TCharArray = array of Char;
// Record and Class for Delphi Native Stringbuilder
type
TCharSearch = record
ArrayPtr: PChar;
MatchPtr: PChar;
end;
//[Serializable, DefaultMember('Chars'), ComVisible(true)]
TStringBuilder = class
private const
DefaultCapacity = $10;
private
function GetCapacity: Integer;
procedure SetCapacity(Value: Integer);
function GetChars(Index: Integer): Char;
procedure SetChars(Index: Integer; Value: Char);
function GetLength: Integer; inline;
procedure Set_Length(Value: Integer);
function GetMaxCapacity: Integer;
procedure ExpandCapacity;
procedure ReduceCapacity;
procedure CheckBounds(Index: Integer);
function _Replace(Index: Integer; const Old, New: string): Boolean;
protected
FData: TCharArray;
FLength: Integer;
FMaxCapacity: Integer;
public
constructor Create; overload;
constructor Create(aCapacity: Integer); overload;
constructor Create(const Value: string); overload;
constructor Create(aCapacity: Integer; aMaxCapacity: Integer); overload;
constructor Create(const Value: string; aCapacity: Integer); overload;
constructor Create(const Value: string; StartIndex: Integer; Length: Integer; aCapacity: Integer); overload;
function Append(const Value: Boolean): TStringBuilder; overload;
function Append(const Value: Byte): TStringBuilder; overload;
function Append(const Value: Char): TStringBuilder; overload;
function Append(const Value: Currency): TStringBuilder; overload;
function Append(const Value: Double): TStringBuilder; overload;
function Append(const Value: Smallint): TStringBuilder; overload;
function Append(const Value: Integer): TStringBuilder; overload;
function Append(const Value: Int64): TStringBuilder; overload;
function Append(const Value: TObject): TStringBuilder; overload;
function Append(const Value: Shortint): TStringBuilder; overload;
function Append(const Value: Single): TStringBuilder; overload;
function Append(const Value: string): TStringBuilder; overload;
function Append(const Value: UInt64): TStringBuilder; overload;
function Append(const Value: TCharArray): TStringBuilder; overload;
function Append(const Value: Word): TStringBuilder; overload;
function Append(const Value: Cardinal): TStringBuilder; overload;
function Append(const Value: Char; RepeatCount: Integer): TStringBuilder; overload;
function Append(const Value: TCharArray; StartIndex: Integer; CharCount: Integer): TStringBuilder; overload;
function Append(const Value: string; StartIndex: Integer; Count: Integer): TStringBuilder; overload;
function AppendFormat(const Format: string; const Args: array of const): TStringBuilder; overload;
function AppendLine: TStringBuilder; overload;
function AppendLine(const Value: string): TStringBuilder; overload;
procedure Clear;
procedure CopyTo(SourceIndex: Integer; const Destination: TCharArray; DestinationIndex: Integer; Count: Integer);
function EnsureCapacity(aCapacity: Integer): Integer;
function Equals(StringBuilder: TStringBuilder): Boolean; reintroduce;
function Insert(Index: Integer; const Value: Boolean): TStringBuilder; overload;
function Insert(Index: Integer; const Value: Byte): TStringBuilder; overload;
function Insert(Index: Integer; const Value: Char): TStringBuilder; overload;
function Insert(Index: Integer; const Value: Currency): TStringBuilder; overload;
function Insert(Index: Integer; const Value: Double): TStringBuilder; overload;
function Insert(Index: Integer; const Value: Smallint): TStringBuilder; overload;
function Insert(Index: Integer; const Value: Integer): TStringBuilder; overload;
function Insert(Index: Integer; const Value: TCharArray): TStringBuilder; overload;
function Insert(Index: Integer; const Value: Int64): TStringBuilder; overload;
function Insert(Index: Integer; const Value: TObject): TStringBuilder; overload;
function Insert(Index: Integer; const Value: Shortint): TStringBuilder; overload;
function Insert(Index: Integer; const Value: Single): TStringBuilder; overload;
function Insert(Index: Integer; const Value: string): TStringBuilder; overload;
function Insert(Index: Integer; const Value: Word): TStringBuilder; overload;
function Insert(Index: Integer; const Value: Cardinal): TStringBuilder; overload;
function Insert(Index: Integer; const Value: UInt64): TStringBuilder; overload;
function Insert(Index: Integer; const Value: string; count: Integer): TStringBuilder; overload;
function Insert(Index: Integer; const Value: TCharArray; startIndex: Integer; charCount: Integer): TStringBuilder; overload;
function Remove(StartIndex: Integer; RemLength: Integer): TStringBuilder;
function Replace(const OldChar: Char; const NewChar: Char): TStringBuilder; overload;
function Replace(const OldValue: string; const NewValue: string): TStringBuilder; overload;
function Replace(const OldChar: Char; const NewChar: Char; StartIndex: Integer; Count: Integer): TStringBuilder; overload;
function Replace(const OldValue: string; const NewValue: string; StartIndex: Integer; Count: Integer): TStringBuilder; overload;
function ToString: string; overload; override;
function ToString(StartIndex: Integer; StrLength: Integer): string; reintroduce; overload;
property Capacity: Integer read GetCapacity write SetCapacity;
property Chars[index: Integer]: Char read GetChars write SetChars; default;
property Length: Integer read GetLength write Set_Length;
property MaxCapacity: Integer read GetMaxCapacity;
end;
EEncodingError = class(Exception);
TEncoding = class
strict private
class var
FASCIIEncoding: TEncoding;
FBigEndianUnicodeEncoding: TEncoding;
FDefaultEncoding: TEncoding;
FUnicodeEncoding: TEncoding;
FUTF7Encoding: TEncoding;
FUTF8Encoding: TEncoding;
class destructor Destroy;
class function GetASCII: TEncoding; static;
class function GetBigEndianUnicode: TEncoding; static;
class function GetDefault: TEncoding; static;
class function GetUnicode: TEncoding; static;
class function GetUTF7: TEncoding; static;
class function GetUTF8: TEncoding; static;
strict protected
FIsSingleByte: Boolean;
FMaxCharSize: Integer;
function GetByteCount(Chars: PChar; CharCount: Integer): Integer; overload; virtual; abstract;
function GetBytes(Chars: PChar; CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; overload; virtual; abstract;
function GetCharCount(Bytes: PByte; ByteCount: Integer): Integer; overload; virtual; abstract;
function GetChars(Bytes: PByte; ByteCount: Integer; Chars: PChar; CharCount: Integer): Integer; overload; virtual; abstract;
public
class function Convert(Source, Destination: TEncoding; Bytes: TBytes): TBytes; overload;
class function Convert(Source, Destination: TEncoding; Bytes: TBytes; StartIndex, Count: Integer): TBytes; overload;
class procedure FreeEncodings;
class function IsStandardEncoding(AEncoding: TEncoding): Boolean; static;
class function GetBufferEncoding(const Buffer: TBytes; var AEncoding: TEncoding): Integer; static;
function GetByteCount(const Chars: TCharArray): Integer; overload;
function GetByteCount(const Chars: TCharArray; CharIndex, CharCount: Integer): Integer; overload;
function GetByteCount(const S: string): Integer; overload;
function GetByteCount(const S: string; CharIndex, CharCount: Integer): Integer; overload;
function GetBytes(const Chars: TCharArray): TBytes; overload;
function GetBytes(const Chars: TCharArray; CharIndex, CharCount: Integer;
var Bytes: TBytes; ByteIndex: Integer): Integer; overload;
function GetBytes(const S: string): TBytes; overload;
function GetBytes(const S: string; CharIndex, CharCount: Integer;
var Bytes: TBytes; ByteIndex: Integer): Integer; overload;
function GetCharCount(const Bytes: TBytes): Integer; overload;
function GetCharCount(const Bytes: TBytes; ByteIndex, ByteCount: Integer): Integer; overload;
function GetChars(const Bytes: TBytes): TCharArray; overload;
function GetChars(const Bytes: TBytes; ByteIndex, ByteCount: Integer): TCharArray; overload;
function GetChars(const Bytes: TBytes; ByteIndex, ByteCount: Integer;
var Chars: TCharArray; CharIndex: Integer): Integer; overload;
class function GetEncoding(CodePage: Integer): TEncoding; static;
function GetMaxByteCount(CharCount: Integer): Integer; virtual; abstract;
function GetMaxCharCount(ByteCount: Integer): Integer; virtual; abstract;
function GetPreamble: TBytes; virtual; abstract;
function GetString(const Bytes: TBytes): string; overload;
function GetString(const Bytes: TBytes; ByteIndex, ByteCount: Integer): string; overload;
class property ASCII: TEncoding read GetASCII;
class property BigEndianUnicode: TEncoding read GetBigEndianUnicode;
class property Default: TEncoding read GetDefault;
property IsSingleByte: Boolean read FIsSingleByte;
class property Unicode: TEncoding read GetUnicode;
class property UTF7: TEncoding read GetUTF7;
class property UTF8: TEncoding read GetUTF8;
end;
TMBCSEncoding = class(TEncoding)
private
FCodePage: Cardinal;
FMBToWCharFlags: Cardinal;
FWCharToMBFlags: Cardinal;
strict protected
function GetByteCount(Chars: PChar; CharCount: Integer): Integer; overload; override;
function GetBytes(Chars: PChar; CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; overload; override;
function GetCharCount(Bytes: PByte; ByteCount: Integer): Integer; overload; override;
function GetChars(Bytes: PByte; ByteCount: Integer; Chars: PChar; CharCount: Integer): Integer; overload; override;
public
constructor Create; overload; virtual;
constructor Create(CodePage: Integer); overload; virtual;
constructor Create(CodePage, MBToWCharFlags, WCharToMBFlags: Integer); overload; virtual;
function GetMaxByteCount(CharCount: Integer): Integer; override;
function GetMaxCharCount(ByteCount: Integer): Integer; override;
function GetPreamble: TBytes; override;
end;
TUTF7Encoding = class(TMBCSEncoding)
strict protected
function GetByteCount(Chars: PChar; CharCount: Integer): Integer; overload; override;
function GetBytes(Chars: PChar; CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; overload; override;
function GetCharCount(Bytes: PByte; ByteCount: Integer): Integer; overload; override;
function GetChars(Bytes: PByte; ByteCount: Integer; Chars: PChar; CharCount: Integer): Integer; overload; override;
public
constructor Create; override;
function GetMaxByteCount(CharCount: Integer): Integer; override;
function GetMaxCharCount(ByteCount: Integer): Integer; override;
end;
TUTF8Encoding = class(TUTF7Encoding)
public
constructor Create; override;
function GetMaxByteCount(CharCount: Integer): Integer; override;
function GetMaxCharCount(ByteCount: Integer): Integer; override;
function GetPreamble: TBytes; override;
end;
TUnicodeEncoding = class(TEncoding)
strict protected
function GetByteCount(Chars: PChar; CharCount: Integer): Integer; overload; override;
function GetBytes(Chars: PChar; CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; overload; override;
function GetCharCount(Bytes: PByte; ByteCount: Integer): Integer; overload; override;
function GetChars(Bytes: PByte; ByteCount: Integer; Chars: PChar; CharCount: Integer): Integer; overload; override;
public
constructor Create; virtual;
function GetMaxByteCount(CharCount: Integer): Integer; override;
function GetMaxCharCount(ByteCount: Integer): Integer; override;
function GetPreamble: TBytes; override;
end;
TBigEndianUnicodeEncoding = class(TUnicodeEncoding)
strict protected
function GetBytes(Chars: PChar; CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; overload; override;
function GetChars(Bytes: PByte; ByteCount: Integer; Chars: PChar; CharCount: Integer): Integer; overload; override;
public
function GetPreamble: TBytes; override;
end;
// TBytes/string conversion routines
function BytesOf(const Val: RawByteString): TBytes; overload;
function BytesOf(const Val: UnicodeString): TBytes; overload;
function BytesOf(const Val: WideChar): TBytes; overload;
function BytesOf(const Val: AnsiChar): TBytes; overload;
function StringOf(const Bytes: TBytes): UnicodeString;
function PlatformBytesOf(const Value: string): TBytes;
function PlatformStringOf(const Value: TBytes): UnicodeString;
function WideStringOf(const Value: TBytes): UnicodeString;
function WideBytesOf(const Value: UnicodeString): TBytes;
// Generic Anonymous method declarations
type
TProc = reference to procedure;
TProc<T> = reference to procedure (Arg1: T);
TProc<T1,T2> = reference to procedure (Arg1: T1; Arg2: T2);
TProc<T1,T2,T3> = reference to procedure (Arg1: T1; Arg2: T2; Arg3: T3);
TProc<T1,T2,T3,T4> = reference to procedure (Arg1: T1; Arg2: T2; Arg3: T3; Arg4: T4);
TFunc<TResult> = reference to function: TResult;
TFunc<T,TResult> = reference to function (Arg1: T): TResult;
TFunc<T1,T2,TResult> = reference to function (Arg1: T1; Arg2: T2): TResult;
TFunc<T1,T2,T3,TResult> = reference to function (Arg1: T1; Arg2: T2; Arg3: T3): TResult;
TFunc<T1,T2,T3,T4,TResult> = reference to function (Arg1: T1; Arg2: T2; Arg3: T3; Arg4: T4): TResult;
TPredicate<T> = reference to function (Arg1: T): Boolean;
{$IFDEF MSWINDOWS}
{ GetDefaultFallbackLanguages retrieves the current DefaultFallbackLanguages string. }
function GetDefaultFallbackLanguages: string;
{ SetDefaultFallbackLanguages set new default fallback languages. }
procedure SetDefaultFallbackLanguages(const Languages: string);
{ PreferredUILanguages retrieves the preferred UI languages for the user's
default UI langauges at runtime. This function uses System.GetUILanguages
with GetUserDefaultUILanguage Windows API and DefaultFallbackLanguages setting. }
function PreferredUILanguages: string;
type
ILanguageEnumerator = interface
function MoveNext: Boolean;
function GetCurrent: string;
property Current: string read GetCurrent;
end;
ILanguageEnumerable = interface
function GetEnumerator: ILanguageEnumerator;
end;
{ PreferredUILanguageList retrieves a ILanguageEnumerable interface
to use for-in loop statement. For example:
for Language in PreferredUILanguageList do }
function PreferredUILanguageList: ILanguageEnumerable;
{ LocaleFileExists returns a boolean value that indicates whether the specified
file existes with preferred UI language subdirectories. }
function LocaleFileExists(const FileName: string): Boolean;
{ GetLocaleFile retuens a string value that indicate a file path whether
the specified file exists with preferred UI language subdirectories. }
function GetLocaleFile(const FileName: string): string;
{ LocaleDirectoryExists returns a boolean value that indicates whether the
specified directory exists with preferred UI language subdirectories. }
function LocaleDirectoryExists(const Directory: string): Boolean;
{ GetLocaleDirectory retuens a string value that indicate a file path whether
the specified directory exists with preferred UI language subdirectories. }
function GetLocaleDirectory(const Directory: string): String;
{$ENDIF}
{$SCOPEDENUMS ON}
type
TUncertainState = (Maybe, Yes, No);
{$SCOPEDENUMS OFF}
implementation
{$IFDEF MSWINDOWS}
uses
ImageHlp, StrUtils, RTLConsts, Math, Character;
{$ENDIF}
{$IFDEF POSIX}
{
Exceptions raised in methods that are safecall will be filtered
through the virtual method SafeCallException on the class. The
implementation of this method under Linux has the option of setting
the following thread vars: SafeCallExceptionMsg, SafeCallExceptionAddr.
If these are set, then the implementation of SafeCallError here will
reraise a generic exception based on these. One might consider actually
having the SafeCallException implementation store off the exception
object itself, but this raises the issue that the exception object
might have to live a long time (if an external application calls a
Delphi safecall method). Since an arbitrary exception object could
be holding large resources hostage, we hold only the string and
address as a hedge.
}
threadvar
SafeCallExceptionMsg: String;
SafeCallExceptionAddr: Pointer;
procedure SetSafeCallExceptionMsg(const Msg: String);
begin
SafeCallExceptionMsg := Msg;
end;
procedure SetSafeCallExceptionAddr(Addr: Pointer);
begin
SafeCallExceptionAddr := Addr;
end;
function GetSafeCallExceptionMsg: String;
begin
Result := SafeCallExceptionMsg;
end;
function GetSafeCallExceptionAddr: Pointer;
begin
Result := SafeCallExceptionAddr;
end;
{$ENDIF POSIX}
{ Utility routines }
procedure DivMod(Dividend: Integer; Divisor: Word;
var Result, Remainder: Word);
asm
PUSH EBX
MOV EBX,EDX
MOV EDX,EAX
SHR EDX,16
DIV BX
MOV EBX,Remainder
MOV [ECX],AX
MOV [EBX],DX
POP EBX
end;
{$IFDEF PIC}
function GetGOT: Pointer; export;
begin
asm
MOV Result,EBX
end;
end;
{$ENDIF}
procedure ConvertError(ResString: PResStringRec); local;
begin
raise EConvertError.CreateRes(ResString);
end;
procedure ConvertErrorFmt(ResString: PResStringRec; const Args: array of const); local;
begin
raise EConvertError.CreateResFmt(ResString, Args);
end;
{$IFDEF MSWINDOWS}
{$EXTERNALSYM CoCreateGuid}
function CoCreateGuid(out guid: TGUID): HResult; stdcall; external 'ole32.dll' name 'CoCreateGuid';
function CreateGUID(out Guid: TGUID): HResult;
begin
Result := CoCreateGuid(Guid);
end;
//function CreateGUID; external 'ole32.dll' name 'CoCreateGuid';
{$ENDIF}
{$IFDEF LINUX}
{ CreateGUID }
{ libuuid.so implements the tricky code to create GUIDs using the
MAC address of the network adapter plus other flavor bits.
libuuid.so is currently distributed with the ext2 file system
package, but does not depend upon the ext2 file system libraries.
Ideally, libuuid.so should be distributed separately.
If you do not have libuuid.so.1 on your Linux distribution, you
can extract the library from the e2fsprogs RPM.
Note: Do not use the generic uuid_generate function in libuuid.so.
In the current implementation (e2fsprogs-1.19), uuid_generate
gives preference to generating guids entirely from random number
streams over generating guids based on the NIC MAC address.
No matter how "random" a random number generator is, it will
never produce guids that can be guaranteed unique across all
systems on the planet. MAC-address based guids are guaranteed
unique because the MAC address of the NIC is guaranteed unique
by the manufacturer.
For this reason, we call uuid_generate_time instead of the
generic uuid_generate. uuid_generate_time constructs the guid
using the MAC address, and falls back to randomness if no NIC
can be found. }
var
libuuidHandle: Pointer;
uuid_generate_time: procedure (out Guid: TGUID) cdecl;
function CreateGUID(out Guid: TGUID): HResult;
const
E_NOTIMPL = HRESULT($80004001);
begin
Result := E_NOTIMPL;
if libuuidHandle = nil then
begin
libuuidHandle := dlopen('libuuid.so.1', RTLD_LAZY);
if libuuidHandle = nil then Exit;
uuid_generate_time := dlsym(libuuidHandle, 'uuid_generate_time');
if @uuid_generate_time = nil then Exit;
end;
uuid_generate_time(Guid);
Result := 0;
end;
{$ENDIF}
{$IFDEF MSWINDOWS}
function StringFromCLSID(const clsid: TGUID; out psz: PWideChar): HResult; stdcall;
external 'ole32.dll' name 'StringFromCLSID';
procedure CoTaskMemFree(pv: Pointer); stdcall;
external 'ole32.dll' name 'CoTaskMemFree';
function CLSIDFromString(psz: PWideChar; out clsid: TGUID): HResult; stdcall;
external 'ole32.dll' name 'CLSIDFromString';
{$ENDIF MSWINDOWS}
function StringToGUID(const S: string): TGUID;
{$IFDEF MSWINDOWS}
begin
if not Succeeded(CLSIDFromString(PWideChar(WideString(S)), Result)) then
ConvertErrorFmt(@SInvalidGUID, [s]);
end;
{$ENDIF MSWINDOWS}
{$IFDEF POSIX}
procedure InvalidGUID;
begin
ConvertErrorFmt(@SInvalidGUID, [s]);
end;
function HexChar(c: Char): Byte;
begin
case c of
'0'..'9': Result := Byte(c) - Byte('0');
'a'..'f': Result := (Byte(c) - Byte('a')) + 10;
'A'..'F': Result := (Byte(c) - Byte('A')) + 10;
else
InvalidGUID;
Result := 0;
end;
end;
function HexByte(p: PChar): Char;
begin
Result := Char((HexChar(p[0]) shl 4) + HexChar(p[1]));
end;
var
i: Integer;
src, dest: PChar;
begin
if ((Length(S) <> 38) or (s[1] <> '{')) then InvalidGUID;
dest := @Result;
src := PChar(s);
Inc(src);
for i := 0 to 3 do
dest[i] := HexByte(src+(3-i)*2);
Inc(src, 8);
Inc(dest, 4);
if src[0] <> '-' then InvalidGUID;
Inc(src);
for i := 0 to 1 do
begin
dest^ := HexByte(src+2);
Inc(dest);
dest^ := HexByte(src);
Inc(dest);
Inc(src, 4);
if src[0] <> '-' then InvalidGUID;
inc(src);
end;
dest^ := HexByte(src);
Inc(dest);
Inc(src, 2);
dest^ := HexByte(src);
Inc(dest);
Inc(src, 2);
if src[0] <> '-' then InvalidGUID;
Inc(src);
for i := 0 to 5 do
begin
dest^ := HexByte(src);
Inc(dest);
Inc(src, 2);
end;
end;
{$ENDIF POSIX}
{$IFDEF MSWINDOWS}
function GUIDToString(const GUID: TGUID): string;
var
P: PWideChar;
begin
if not Succeeded(StringFromCLSID(GUID, P)) then
ConvertError(@SInvalidGUID);
Result := string(P);
CoTaskMemFree(P);
end;
{$ENDIF}
{$IFDEF POSIX}
function GUIDToString(const GUID: TGUID): string;
begin
SetLength(Result, 38);
StrLFmt(PChar(Result), 38,'{%.8x-%.4x-%.4x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x}', // do not localize
[GUID.D1, GUID.D2, GUID.D3, GUID.D4[0], GUID.D4[1], GUID.D4[2], GUID.D4[3],
GUID.D4[4], GUID.D4[5], GUID.D4[6], GUID.D4[7]]);
end;
{$ENDIF POSIX}
{$IFDEF MSWINDOWS}
function IsEqualGUID; external 'ole32.dll' name 'IsEqualGUID';
{$ENDIF MSWINDOWS}
{$IFDEF POSIX}
function IsEqualGUID(const guid1, guid2: TGUID): Boolean;
var
a, b: PIntegerArray;
begin
a := PIntegerArray(@guid1);
b := PIntegerArray(@guid2);
Result := (a^[0] = b^[0]) and (a^[1] = b^[1]) and (a^[2] = b^[2]) and (a^[3] = b^[3]);
end;
{$ENDIF POSIX}
{ Exit procedure handling }
type
PExitProcInfo = ^TExitProcInfo;
TExitProcInfo = record
Next: PExitProcInfo;
SaveExit: Pointer;
Proc: TProcedure;
end;
var
ExitProcList: PExitProcInfo = nil;
procedure DoExitProc;
var
P: PExitProcInfo;
Proc: TProcedure;
begin
P := ExitProcList;
ExitProcList := P^.Next;
ExitProc := P^.SaveExit;
Proc := P^.Proc;
Dispose(P);
Proc;
end;
procedure AddExitProc(Proc: TProcedure);
var
P: PExitProcInfo;
begin
New(P);
P^.Next := ExitProcList;
P^.SaveExit := ExitProc;
P^.Proc := Proc;
ExitProcList := P;
ExitProc := @DoExitProc;
end;
{ String handling routines }
{ Put these (IsLeadChar, CharInSet) here so that they're compiled before being used so they're properly
inlined }
function IsLeadChar(C: AnsiChar): Boolean;
begin
Result := C in LeadBytes;
end;
function IsLeadChar(C: WideChar): Boolean;
begin
Result := (C >= #$D800) and (C <= #$DFFF);
end;
function CharInSet(C: AnsiChar; const CharSet: TSysCharSet): Boolean;
begin
Result := C in CharSet;
end;
function CharInSet(C: WideChar; const CharSet: TSysCharSet): Boolean;
begin
Result := (C < #$0100) and (AnsiChar(C) in CharSet);
end;
function NewStr(const S: AnsiString): PAnsiString;
begin
if S = '' then Result := NullAnsiStr else
begin
New(Result);
Result^ := S;
end;
end;
procedure DisposeStr(P: PAnsiString);
begin
if (P <> nil) and (P^ <> '') then Dispose(P);
end;
procedure AssignStr(var P: PAnsiString; const S: AnsiString);
var
Temp: PAnsiString;
begin
Temp := P;
P := NewStr(S);
DisposeStr(Temp);
end;
procedure AppendStr(var Dest: AnsiString; const S: AnsiString);
begin
Dest := Dest + S;
end;
{ Helper function to omit the EnsureUnicodeString call }
function GetStringLength(const S: UnicodeString): Integer; inline;
begin
Result := Longint(S);
if Result <> 0 then
Result := PLongint(Result - 4)^;
end;
{ Helper function to change the case for ANSIfied-UnicodeStrings }
function UpperCaseFromAnsiString(const S: AnsiString): UnicodeString;
var
I: Integer;
P: PChar;
begin
Result := UnicodeString(S); // AnsiString => UnicodeString
if Result <> '' then
begin
P := PChar(Pointer(Result));
for I := Length(Result) downto 1 do
begin
case P^ of
'a'..'z':
P^ := Char(Word(P^) xor $0020);
end;
Inc(P);
end;
end;
end;
(* ***** BEGIN LICENSE BLOCK *****
*
* The function UpperCase is licensed under the CodeGear license terms.
*
* The initial developer of the original code is Fastcode
* Code was modified to to ensure the string payload is ansi
*
* Portions created by the initial developer are Copyright (C) 2002-2004
* the initial developer. All Rights Reserved.
*
* Contributor(s): John O'Harrow, Allen Bauer
*
* ***** END LICENSE BLOCK ***** *)
function UpperCase(const S: string): string;
{$IFNDEF UNICODE}
asm {Size = 134 Bytes}
push ebx
push edi
push esi
test eax, eax {Test for S = NIL}
mov esi, eax {@S}
mov edi, edx {@Result}
mov eax, edx {@Result}
jz @@Null {S = NIL}
mov edx, [esi-4] {Length(S)}
test edx, edx
je @@Null {Length(S) = 0}
mov ebx, edx
call system.@LStrSetLength {Create Result String}
push 0
cmp word ptr [esi-10],1
je @@isAnsi
mov eax,esp
mov edx,esi
call System.@LStrFromUStr
mov esi,[esp]
@@isAnsi:
mov edi, [edi] {@Result}
mov eax, [esi+ebx-4] {Convert the Last 4 Characters of String}
mov ecx, eax {4 Original Bytes}
or eax, $80808080 {Set High Bit of each Byte}
mov edx, eax {Comments Below apply to each Byte...}
sub eax, $7B7B7B7B {Set High Bit if Original <= Ord('z')}
xor edx, ecx {80h if Original < 128 else 00h}
or eax, $80808080 {Set High Bit}
sub eax, $66666666 {Set High Bit if Original >= Ord('a')}
and eax, edx {80h if Orig in 'a'..'z' else 00h}
shr eax, 2 {80h > 20h ('a'-'A')}
sub ecx, eax {Clear Bit 5 if Original in 'a'..'z'}
mov [edi+ebx-4], ecx
sub ebx, 1
and ebx, -4
jmp @@CheckDone
@@Null:
pop esi
pop edi
pop ebx
jmp System.@LStrClr
@@Loop: {Loop converting 4 Character per Loop}
mov eax, [esi+ebx]
mov ecx, eax {4 Original Bytes}
or eax, $80808080 {Set High Bit of each Byte}
mov edx, eax {Comments Below apply to each Byte...}
sub eax, $7B7B7B7B {Set High Bit if Original <= Ord('z')}
xor edx, ecx {80h if Original < 128 else 00h}
or eax, $80808080 {Set High Bit}
sub eax, $66666666 {Set High Bit if Original >= Ord('a')}
and eax, edx {80h if Orig in 'a'..'z' else 00h}
shr eax, 2 {80h > 20h ('a'-'A')}
sub ecx, eax {Clear Bit 5 if Original in 'a'..'z'}
mov [edi+ebx], ecx
@@CheckDone:
sub ebx, 4
jnc @@Loop
mov eax,[esp]
test eax,eax
jz @@noClear
mov eax,esp
call System.@LStrClr
@@noClear:
add esp,4
pop esi
pop edi
pop ebx
end;
{$ELSE}
var
I, Len: Integer;
DstP, SrcP: PChar;
Ch: Char;
begin
if StringElementSize(S) <> 2 then
Result := UpperCaseFromAnsiString(AnsiString(Pointer(S)))
else
begin
Len := GetStringLength(S);
SetLength(Result, Len);
if Len > 0 then
begin
DstP := PChar(Pointer(Result));
SrcP := PChar(Pointer(S));
for I := Len downto 1 do
begin
Ch := SrcP^;
case Ch of
'a'..'z':
Ch := Char(Word(Ch) xor $0020);
end;
DstP^ := Ch;
Inc(DstP);
Inc(SrcP);
end;
end;
end;
end;
{$ENDIF}
function UpperCase(const S: string; LocaleOptions: TLocaleOptions): string;
begin
if LocaleOptions = loUserLocale then
Result := AnsiUpperCase(S)
else
Result := UpperCase(S);
end;
{ Helper function to change the case for ANSIfied-UnicodeStrings }
function LowerCaseFromAnsiString(const S: AnsiString): UnicodeString;
var
I: Integer;
P: PChar;
begin
Result := UnicodeString(S); // AnsiString => UnicodeString
if Result <> '' then
begin
P := PChar(Pointer(Result));
for I := Length(Result) downto 1 do
begin
case P^ of
'A'..'Z':
P^ := Char(Word(P^) or $0020);
end;
Inc(P);
end;
end;
end;
(* ***** BEGIN LICENSE BLOCK *****
*
* The function LowerCase is licensed under the CodeGear license terms.
*
* The initial developer of the original code is Fastcode
* Code was modified to to ensure the string payload is ansi
*
* Portions created by the initial developer are Copyright (C) 2002-2004
* the initial developer. All Rights Reserved.
*
* Contributor(s): John O'Harrow, Allen Bauer
*
* ***** END LICENSE BLOCK ***** *)
function LowerCase(const S: string): string;
{$IFNDEF UNICODE}
asm {Size = 134 Bytes}
push ebx
push edi
push esi
test eax, eax {Test for S = NIL}
mov esi, eax {@S}
mov edi, edx {@Result}
mov eax, edx {@Result}
jz @@Null {S = NIL}
mov edx, [esi-4] {Length(S)}
test edx, edx
je @@Null {Length(S) = 0}
mov ebx, edx
call system.@LStrSetLength {Create Result String}
push 0
cmp word ptr [esi-10],1
je @@isAnsi
mov eax,esp
mov edx,esi
call System.@LStrFromUStr
mov esi,[esp]
@@isAnsi:
mov edi, [edi] {@Result}
mov eax, [esi+ebx-4] {Convert the Last 4 Characters of String}
mov ecx, eax {4 Original Bytes}
or eax, $80808080 {Set High Bit of each Byte}
mov edx, eax {Comments Below apply to each Byte...}
sub eax, $5B5B5B5B {Set High Bit if Original <= Ord('Z')}
xor edx, ecx {80h if Original < 128 else 00h}
or eax, $80808080 {Set High Bit}
sub eax, $66666666 {Set High Bit if Original >= Ord('A')}
and eax, edx {80h if Orig in 'A'..'Z' else 00h}
shr eax, 2 {80h > 20h ('a'-'A')}
add ecx, eax {Set Bit 5 if Original in 'A'..'Z'}
mov [edi+ebx-4], ecx
sub ebx, 1
and ebx, -4
jmp @@CheckDone
@@Null:
pop esi
pop edi
pop ebx
jmp System.@LStrClr
@@Loop: {Loop converting 4 Character per Loop}
mov eax, [esi+ebx]
mov ecx, eax {4 Original Bytes}
or eax, $80808080 {Set High Bit of each Byte}
mov edx, eax {Comments Below apply to each Byte...}
sub eax, $5B5B5B5B {Set High Bit if Original <= Ord('Z')}
xor edx, ecx {80h if Original < 128 else 00h}
or eax, $80808080 {Set High Bit}
sub eax, $66666666 {Set High Bit if Original >= Ord('A')}
and eax, edx {80h if Orig in 'A'..'Z' else 00h}
shr eax, 2 {80h > 20h ('a'-'A')}
add ecx, eax {Set Bit 5 if Original in 'A'..'Z'}
mov [edi+ebx], ecx
@@CheckDone:
sub ebx, 4
jnc @@Loop
mov eax,[esp]
test eax,eax
jz @@noClear
mov eax,esp
call System.@LStrClr
@@noClear:
add esp,4
pop esi
pop edi
pop ebx
end;
{$ELSE}
var
I, Len: Integer;
DstP, SrcP: PChar;
Ch: Char;
begin
if StringElementSize(S) <> 2 then
Result := LowerCaseFromAnsiString(AnsiString(Pointer(S)))
else
begin
Len := GetStringLength(S);
SetLength(Result, Len);
if Len > 0 then
begin
DstP := PChar(Pointer(Result));
SrcP := PChar(Pointer(S));
for I := Len downto 1 do
begin
Ch := SrcP^;
case Ch of
'A'..'Z':
Ch := Char(Word(Ch) or $0020);
end;
DstP^ := Ch;
Inc(DstP);
Inc(SrcP);
end;
end;
end;
end;
{$ENDIF}
function LowerCase(const S: string; LocaleOptions: TLocaleOptions): string;
begin
if LocaleOptions = loUserLocale then
Result := AnsiLowerCase(S)
else
Result := LowerCase(S);
end;
(* ***** BEGIN LICENSE BLOCK *****
*
* The function CompareStr is licensed under the CodeGear license terms.
*
* The initial developer of the original code is Fastcode
* Code was modified to to ensure the string payload is ansi
*
* Portions created by the initial developer are Copyright (C) 2002-2007
* the initial developer. All Rights Reserved.
*
* Contributor(s): Pierre le Riche, Allen Bauer
*
* ***** END LICENSE BLOCK ***** *)
function CompareStr(const S1, S2: string): Integer;
{$IFNDEF UNICODE}
asm
{On entry:
eax = @S1[1]
edx = @S2[1]
On exit:
Result in eax:
0 if S1 = S2,
> 0 if S1 > S2,
< 0 if S1 < S2
Code size:
101 bytes}
cmp eax, edx
je @SameString
{Is either of the strings perhaps nil?}
test eax, edx
jz @PossibleNilString
{Compare the first four characters (there has to be a trailing #0). In random
string compares this can save a lot of CPU time.}
@BothNonNil:
push 0
push 0
cmp word ptr [eax-10],1
jz @leftIsAnsi
push edx
mov edx,eax
mov eax,esp
call System.@LStrFromUStr
pop edx
mov eax,[esp]
@leftIsAnsi:
cmp word ptr [edx-10],1
jz @rightIsAnsi
push eax
lea eax,[esp + 4]
call System.@LStrFromUStr
pop eax
mov edx,[esp + 4]
@rightIsAnsi:
{Compare the first character}
movzx ecx, byte ptr [edx]
cmp cl, [eax]
je @FirstCharacterSame
{First character differs}
movzx eax, byte ptr [eax]
sub eax, ecx
jmp @Done
@FirstCharacterSame:
{Save ebx}
push ebx
{Set ebx = length(S1)}
mov ebx, [eax - 4]
xor ecx, ecx
{Set ebx = length(S1) - length(S2)}
sub ebx, [edx - 4]
{Save the length difference on the stack}
push ebx
{Set ecx = 0 if length(S1) < length(S2), $ffffffff otherwise}
adc ecx, -1
{Set ecx = - min(length(S1), length(S2))}
and ecx, ebx
sub ecx, [eax - 4]
{Adjust the pointers to be negative based}
sub eax, ecx
sub edx, ecx
@CompareLoop:
mov ebx, [eax + ecx]
xor ebx, [edx + ecx]
jnz @Mismatch
add ecx, 4
js @CompareLoop
{All characters match - return the difference in length}
@MatchUpToLength:
pop eax
pop ebx
@Done:
mov ecx,esp
mov edx,[ecx]
or edx,[ecx + 4]
jz @NoClear
push eax
mov eax,ecx
mov edx,2
call System.@LStrArrayClr
pop eax
@NoClear:
pop edx
pop edx
ret
@Mismatch:
bsf ebx, ebx
shr ebx, 3
add ecx, ebx
jns @MatchUpToLength
movzx eax, byte ptr [eax + ecx]
movzx edx, byte ptr [edx + ecx]
sub eax, edx
pop ebx
pop ebx
jmp @Done
{It is the same string}
@SameString:
xor eax, eax
ret
{Good possibility that at least one of the strings are nil}
@PossibleNilString:
test eax, eax
jz @FirstStringNil
test edx, edx
jnz @BothNonNil
{Return first string length: second string is nil}
mov eax, [eax - 4]
ret
@FirstStringNil:
{Return 0 - length(S2): first string is nil}
sub eax, [edx - 4]
end;
{$ELSE}
(* ***** BEGIN LICENSE BLOCK *****
*
* The function CompareStr is licensed under the CodeGear license terms.
*
* The initial developer of the original code is Fastcode
* Code was modified to support word-sized Unicode strings and to ensure
* the string payload is unicode
*
* Portions created by the initial developer are Copyright (C) 2002-2007
* the initial developer. All Rights Reserved.
*
* Contributor(s): Pierre le Riche, Allen Bauer
*
* ***** END LICENSE BLOCK ***** *)
asm
{On entry:
eax = @S1[1]
edx = @S2[1]
On exit:
Result in eax:
0 if S1 = S2,
> 0 if S1 > S2,
< 0 if S1 < S2
Code size:
101 bytes}
cmp eax, edx
je @SameString
{Is either of the strings perhaps nil?}
test eax, edx
jz @PossibleNilString
{Compare the first four characters (there has to be a trailing #0). In random
string compares this can save a lot of CPU time.}
@BothNonNil:
push 0
push 0
cmp word ptr [eax-10],2
jz @leftIsUnicode
push edx
mov edx,eax
mov eax,esp
call System.@UStrFromLStr
pop edx
mov eax,[esp]
@leftIsUnicode:
cmp word ptr [edx-10],2
jz @rightIsUnicode
push eax
lea eax,[esp + 8]
call System.@UStrFromLStr
pop eax
mov edx,[esp + 4]
@rightIsUnicode:
{Compare the first character}
movzx ecx, word ptr [edx]
cmp cx, [eax]
je @FirstCharacterSame
{First character differs}
movzx eax, word ptr [eax]
sub eax, ecx
jmp @Done
@FirstCharacterSame:
{Save ebx}
push ebx
{Set ebx = length(S1)}
mov ebx, [eax - 4]
xor ecx, ecx
{Set ebx = length(S1) - length(S2)}
sub ebx, [edx - 4]
{Save the length difference on the stack}
push ebx
{Set ecx = 0 if length(S1) < length(S2), $ffffffff otherwise}
adc ecx, -1
{Set ecx = - min(length(S1), length(S2))}
and ecx, ebx
sub ecx, [eax - 4]
sal ecx, 1
{Adjust the pointers to be negative based}
sub eax, ecx
sub edx, ecx
@CompareLoop:
mov ebx, [eax + ecx]
xor ebx, [edx + ecx]
jnz @Mismatch
add ecx, 4
js @CompareLoop
{All characters match - return the difference in length}
@MatchUpToLength:
pop eax
pop ebx
@Done:
mov ecx,esp
mov edx,[ecx]
or edx,[ecx + 4]
jz @NoClear
push eax
mov eax,ecx
mov edx,2
call System.@LStrArrayClr
pop eax
@NoClear:
pop edx
pop edx
ret
@Mismatch:
bsf ebx, ebx
shr ebx, 4
add ebx, ebx
add ecx, ebx
jns @MatchUpToLength
movzx eax, word ptr [eax + ecx]
movzx edx, word ptr [edx + ecx]
sub eax, edx
pop ebx
pop ebx
jmp @Done
{It is the same string}
@SameString:
xor eax, eax
ret
{Good possibility that at least one of the strings are nil}
@PossibleNilString:
test eax, eax
jz @FirstStringNil
test edx, edx
jnz @BothNonNil
{Return first string length: second string is nil}
mov eax, [eax - 4]
ret
@FirstStringNil:
{Return 0 - length(S2): first string is nil}
sub eax, [edx - 4]
end;
{$ENDIF}
function CompareStr(const S1, S2: string; LocaleOptions: TLocaleOptions): Integer;
begin
if LocaleOptions = loUserLocale then
Result := AnsiCompareStr(S1, S2)
else
Result := CompareStr(S1, S2);
end;
function SameStr(const S1, S2: string): Boolean;
asm
CMP EAX,EDX
JZ @1
OR EAX,EAX
JZ @2
OR EDX,EDX
JZ @3
MOV ECX,[EAX-4]
CMP ECX,[EDX-4]
JNE @3
CALL CompareStr
TEST EAX,EAX
JNZ @3
@1: MOV AL,1
@2: RET
@3: XOR EAX,EAX
end;
function SameStr(const S1, S2: string; LocaleOptions: TLocaleOptions): Boolean;
begin
if LocaleOptions = loUserLocale then
Result := AnsiSameStr(S1, S2)
else
Result := SameStr(S1, S2);
end;
(* ***** BEGIN LICENSE BLOCK *****
*
* The function CompareMem is licensed under the CodeGear license terms.
*
* The initial developer of the original code is Fastcode
*
* Portions created by the initial developer are Copyright (C) 2002-2004
* the initial developer. All Rights Reserved.
*
* Contributor(s): Aleksandr Sharahov
*
* ***** END LICENSE BLOCK ***** *)
function CompareMem(P1, P2: Pointer; Length: Integer): Boolean; assembler;
asm
add eax, ecx
add edx, ecx
xor ecx, -1
add eax, -8
add edx, -8
add ecx, 9
push ebx
jg @Dword
mov ebx, [eax+ecx]
cmp ebx, [edx+ecx]
jne @Ret0
lea ebx, [eax+ecx]
add ecx, 4
and ebx, 3
sub ecx, ebx
jg @Dword
@DwordLoop:
mov ebx, [eax+ecx]
cmp ebx, [edx+ecx]
jne @Ret0
mov ebx, [eax+ecx+4]
cmp ebx, [edx+ecx+4]
jne @Ret0
add ecx, 8
jg @Dword
mov ebx, [eax+ecx]
cmp ebx, [edx+ecx]
jne @Ret0
mov ebx, [eax+ecx+4]
cmp ebx, [edx+ecx+4]
jne @Ret0
add ecx, 8
jle @DwordLoop
@Dword:
cmp ecx, 4
jg @Word
mov ebx, [eax+ecx]
cmp ebx, [edx+ecx]
jne @Ret0
add ecx, 4
@Word:
cmp ecx, 6
jg @Byte
movzx ebx, word ptr [eax+ecx]
cmp bx, [edx+ecx]
jne @Ret0
add ecx, 2
@Byte:
cmp ecx, 7
jg @Ret1
movzx ebx, byte ptr [eax+7]
cmp bl, [edx+7]
jne @Ret0
@Ret1:
mov eax, 1
pop ebx
ret
@Ret0:
xor eax, eax
pop ebx
end;
(* ***** BEGIN LICENSE BLOCK *****
*
* The function CompareText is licensed under the CodeGear license terms.
*
* The initial developer of the original code is Fastcode
*
* Portions created by the initial developer are Copyright (C) 2002-2004
* the initial developer. All Rights Reserved.
*
* Contributor(s): John O'Harrow
*
* ***** END LICENSE BLOCK ***** *)
function CompareText(const S1, S2: string): Integer;
{$IFNDEF UNICODE}
asm
TEST EAX, EAX
JNZ @@CheckS2
TEST EDX, EDX
JZ @@Ret
MOV EAX, [EDX-4]
NEG EAX
@@Ret:
RET
@@CheckS2:
TEST EDX, EDX
JNZ @@Compare
MOV EAX, [EAX-4]
RET
@@Compare:
PUSH EBX
PUSH EBP
PUSH ESI
PUSH 0
PUSH 0
CMP WORD PTR [EAX-10],1
JE @@S1IsAnsi
PUSH EDX
MOV EDX,EAX
LEA EAX,[ESP+4]
CALL System.@LStrFromUStr
POP EDX
MOV EAX,[ESP]
@@S1IsAnsi:
CMP WORD PTR [EDX-10],1
JE @@S2IsAnsi
PUSH EAX
LEA EAX,[ESP+8]
CALL System.@LStrFromUStr
POP EAX
MOV EDX,[ESP+4]
@@S2IsAnsi:
MOV EBP, [EAX-4] // length(S1)
MOV EBX, [EDX-4] // length(S2)
SUB EBP, EBX // Result if All Compared Characters Match
SBB ECX, ECX
AND ECX, EBP
ADD ECX, EBX // min(length(S1),length(S2)) = Compare Length
LEA ESI, [EAX+ECX] // Last Compare Position in S1
ADD EDX, ECX // Last Compare Position in S2
NEG ECX
JZ @@SetResult // Exit if Smallest Length = 0
@@Loop: // Load Next 2 Chars from S1 and S2
// May Include Null Terminator}
MOVZX EAX, WORD PTR [ESI+ECX]
MOVZX EBX, WORD PTR [EDX+ECX]
CMP EAX, EBX
JE @@Next // Next 2 Chars Match
CMP AL, BL
JE @@SecondPair // First Char Matches
MOV AH, 0
MOV BH, 0
CMP AL, 'a'
JL @@UC1
CMP AL, 'z'
JG @@UC1
SUB EAX, 'a'-'A'
@@UC1:
CMP BL, 'a'
JL @@UC2
CMP BL, 'z'
JG @@UC2
SUB EBX, 'a'-'A'
@@UC2:
SUB EAX, EBX // Compare Both Uppercase Chars
JNE @@Done // Exit with Result in EAX if Not Equal
MOVZX EAX, WORD PTR [ESI+ECX] // Reload Same 2 Chars from S1
MOVZX EBX, WORD PTR [EDX+ECX] // Reload Same 2 Chars from S2
CMP AH, BH
JE @@Next // Second Char Matches
@@SecondPair:
SHR EAX, 8
SHR EBX, 8
CMP AL, 'a'
JL @@UC3
CMP AL, 'z'
JG @@UC3
SUB EAX, 'a'-'A'
@@UC3:
CMP BL, 'a'
JL @@UC4
CMP BL, 'z'
JG @@UC4
SUB EBX, 'a'-'A'
@@UC4:
SUB EAX, EBX // Compare Both Uppercase Chars
JNE @@Done // Exit with Result in EAX if Not Equal
@@Next:
ADD ECX, 2
JL @@Loop // Loop until All required Chars Compared
@@SetResult:
MOV EAX, EBP // All Matched, Set Result from Lengths
@@Done:
MOV ECX,ESP
MOV EDX,[ECX]
OR EDX,[ECX + 4]
JZ @@NoClear
PUSH EAX
MOV EAX,ECX
MOV EDX,2
CALL System.@LStrArrayClr
POP EAX
@@NoClear:
ADD ESP,8
POP ESI
POP EBP
POP EBX
end;
{$ELSE}
(* ***** BEGIN LICENSE BLOCK *****
*
* The function CompareText is licensed under the CodeGear license terms.
*
* The initial developer of the original code is Fastcode
*
* Portions created by the initial developer are Copyright (C) 2002-2004
* the initial developer. All Rights Reserved.
*
* Contributor(s): John O'Harrow
*
* ***** END LICENSE BLOCK ***** *)
asm
TEST EAX, EAX
JNZ @@CheckS2
TEST EDX, EDX
JZ @@Ret
MOV EAX, [EDX-4]
NEG EAX
@@Ret:
RET
@@CheckS2:
TEST EDX, EDX
JNZ @@Compare
MOV EAX, [EAX-4]
RET
@@Compare:
PUSH EBX
PUSH EBP
PUSH ESI
PUSH 0
PUSH 0
CMP WORD PTR [EAX-10],2
JE @@S1IsUnicode
PUSH EDX
MOV EDX,EAX
LEA EAX,[ESP+4]
CALL System.@UStrFromLStr
POP EDX
MOV EAX,[ESP]
@@S1IsUnicode:
CMP WORD PTR [EDX-10],2
JE @@S2IsUnicode
PUSH EAX
LEA EAX,[ESP+8]
CALL System.@UStrFromLStr
POP EAX
MOV EDX,[ESP+4]
@@S2IsUnicode:
MOV EBP, [EAX-4] // length(S1)
MOV EBX, [EDX-4] // length(S2)
SUB EBP, EBX // Result if All Compared Characters Match
SBB ECX, ECX
AND ECX, EBP
ADD ECX, EBX // min(length(S1),length(S2)) = Compare Length
LEA ESI, [EAX+ECX*2] // Last Compare Position in S1
ADD EDX, ECX // Last Compare Position in S2
ADD EDX, ECX // Last Compare Position in S2
NEG ECX
JZ @@SetResult // Exit if Smallest Length = 0
@@Loop: // Load Next 2 Chars from S1 and S2
// May Include Null Terminator}
MOV EAX, [ESI+ECX*2]
MOV EBX, [EDX+ECX*2]
CMP EAX,EBX
JE @@Next // Next 2 Chars Match
CMP AX,BX
JE @@SecondPair // First Char Matches
AND EAX,$0000FFFF
AND EBX,$0000FFFF
CMP EAX, 'a'
JL @@UC1
CMP EAX, 'z'
JG @@UC1
SUB EAX, 'a'-'A'
@@UC1:
CMP EBX, 'a'
JL @@UC2
CMP EBX, 'z'
JG @@UC2
SUB EBX, 'a'-'A'
@@UC2:
SUB EAX,EBX // Compare Both Uppercase Chars
JNE @@Done // Exit with Result in EAX if Not Equal
MOV EAX, [ESI+ECX*2] // Reload Same 2 Chars from S1
MOV EBX, [EDX+ECX*2] // Reload Same 2 Chars from S2
AND EAX,$FFFF0000
AND EBX,$FFFF0000
CMP EAX,EBX
JE @@Next // Second Char Matches
@@SecondPair:
SHR EAX, 16
SHR EBX, 16
CMP EAX, 'a'
JL @@UC3
CMP EAX, 'z'
JG @@UC3
SUB EAX, 'a'-'A'
@@UC3:
CMP EBX, 'a'
JL @@UC4
CMP EBX, 'z'
JG @@UC4
SUB EBX, 'a'-'A'
@@UC4:
SUB EAX,EBX // Compare Both Uppercase Chars
JNE @@Done // Exit with Result in EAX if Not Equal
@@Next:
ADD ECX, 2
JL @@Loop // Loop until All required Chars Compared
@@SetResult:
MOV EAX,EBP // All Matched, Set Result from Lengths
@@Done:
MOV ECX,ESP
MOV EDX,[ECX]
OR EDX,[ECX + 4]
JZ @@NoClear
PUSH EAX
MOV EAX,ECX
MOV EDX,2
CALL System.@LStrArrayClr
POP EAX
@@NoClear:
ADD ESP,8
POP ESI
POP EBP
POP EBX
end;
{$ENDIF}
function CompareText(const S1, S2: string; LocaleOptions: TLocaleOptions): Integer;
begin
if LocaleOptions = loUserLocale then
Result := AnsiCompareText(S1, S2)
else
Result := CompareText(S1, S2);
end;
function SameText(const S1, S2: string): Boolean; assembler;
asm
CMP EAX,EDX
JZ @1
OR EAX,EAX
JZ @2
OR EDX,EDX
JZ @3
MOV ECX,[EAX-4]
CMP ECX,[EDX-4]
JNE @3
CALL CompareText
TEST EAX,EAX
JNZ @3
@1: MOV AL,1
@2: RET
@3: XOR EAX,EAX
end;
function SameText(const S1, S2: string; LocaleOptions: TLocaleOptions): Boolean;
begin
if LocaleOptions = loUserLocale then
Result := AnsiSameText(S1, S2)
else
Result := SameText(S1, S2);
end;
function AnsiUpperCase(const S: string): string;
{$IFDEF MSWINDOWS}
var
Len: Integer;
begin
Len := Length(S);
SetString(Result, PChar(S), Len);
if Len > 0 then
CharUpperBuff(PChar(Result), Len);
end;
{$ENDIF MSWINDOWS}
{$IFDEF POSIX}
begin
Result := WideUpperCase(S);
end;
{$ENDIF POSIX}
function AnsiLowerCase(const S: string): string;
{$IFDEF MSWINDOWS}
var
Len: Integer;
begin
Len := Length(S);
SetString(Result, PChar(S), Len);
if Len > 0 then
CharLowerBuff(PChar(Result), Len);
end;
{$ENDIF MSWINDOWS}
{$IFDEF POSIX}
begin
Result := WideLowerCase(S);
end;
{$ENDIF POSIX}
function AnsiCompareStr(const S1, S2: string): Integer;
begin
{$IFDEF MSWINDOWS}
Result := CompareString(LOCALE_USER_DEFAULT, 0, PChar(S1), Length(S1),
PChar(S2), Length(S2)) - CSTR_EQUAL;
{$ENDIF MSWINDOWS}
{$IFDEF POSIX}
// glibc 2.1.2 / 2.1.3 implementations of strcoll() and strxfrm()
// have severe capacity limits. Comparing two 100k strings may
// exhaust the stack and kill the process.
// Fixed in glibc 2.1.91 and later.
{$IFDEF MACOSX}
{$MESSAGE 'have to fix the pchar references here'}
{$ENDIF}
Result := strcoll(PChar(S1), PChar(S2));
{$ENDIF POSIX}
end;
function AnsiSameStr(const S1, S2: string): Boolean;
begin
Result := AnsiCompareStr(S1, S2) = 0;
end;
function AnsiCompareText(const S1, S2: string): Integer;
begin
{$IFDEF MSWINDOWS}
Result := CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE, PChar(S1),
Length(S1), PChar(S2), Length(S2)) - CSTR_EQUAL;
{$ENDIF MSWINDOWS}
{$IFDEF POSIX}
Result := WideCompareText(S1, S2);
{$ENDIF MSWINDOWS}
end;
function AnsiSameText(const S1, S2: string): Boolean;
begin
Result := AnsiCompareText(S1, S2) = 0;
end;
function AnsiStrComp(S1, S2: PAnsiChar): Integer;
begin
{$IFDEF MSWINDOWS}
Result := CompareStringA(LOCALE_USER_DEFAULT, 0, S1, -1, S2, -1) - CSTR_EQUAL;
{$ENDIF}
{$IFDEF LINUX}
Result := strcoll(S1, S2);
{$ENDIF}
end;
function AnsiStrComp(S1, S2: PWideChar): Integer;
begin
{$IFDEF MACOSX}
RunError(reMacNotImplemented);
{$ELSE !MACOSX}
Result := CompareStringW(LOCALE_USER_DEFAULT, 0, S1, -1, S2, -1) - CSTR_EQUAL;
{$ENDIF !MACOSX}
end;
function AnsiStrIComp(S1, S2: PAnsiChar): Integer;
begin
{$IFDEF MSWINDOWS}
Result := CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE, S1, -1,
S2, -1) - CSTR_EQUAL;
{$ENDIF}
{$IFDEF LINUX}
Result := AnsiCompareText(S1, S2);
{$ENDIF}
end;
function AnsiStrIComp(S1, S2: PWideChar): Integer;
begin
{$IFDEF MACOSX}
RunError(reMacNotImplemented);
{$ELSE !MACOSX}
Result := CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, S1, -1,
S2, -1) - CSTR_EQUAL;
{$ENDIF !MACOSX}
end;
// StrLenLimit: Scan Src for a null terminator up to MaxLen bytes
function StrLenLimit(Src: PChar; MaxLen: Cardinal): Cardinal;
begin
if Src = nil then
begin
Result := 0;
Exit;
end;
Result := MaxLen;
while (Src^ <> #0) and (Result > 0) do
begin
Inc(Src);
Dec(Result);
end;
Result := MaxLen - Result;
end;
{ StrBufLimit: Return a pointer to a buffer that contains no more than MaxLen
bytes of Src, avoiding heap allocation if possible.
If clipped Src length is less than MaxLen, return Src. Allocated = False.
If clipped Src length is less than StaticBufLen, return StaticBuf with a
copy of Src. Allocated = False.
Otherwise, return a heap allocated buffer with a copy of Src. Allocated = True.
}
function StrBufLimit(Src: PChar; MaxLen: Cardinal; StaticBuf: PChar;
StaticBufLen: Cardinal; var Allocated: Boolean): PChar;
var
Len: Cardinal;
begin
Len := StrLenLimit(Src, MaxLen);
Allocated := False;
if Len < MaxLen then
Result := Src
else
begin
if Len < StaticBufLen then
Result := StaticBuf
else
begin
GetMem(Result, Len+1);
Allocated := True;
end;
Move(Src^, Result^, Len);
Result[Len] := #0;
end;
end;
function InternalAnsiStrLComp(S1, S2: PChar; MaxLen: Cardinal; CaseSensitive: Boolean): Integer;
var
Buf1, Buf2: array [0..4095] of Char;
P1, P2: PChar;
Allocated1, Allocated2: Boolean;
begin
// glibc has no length-limited strcoll!
P1 := nil;
P2 := nil;
Allocated1 := False;
Allocated2 := False;
try
P1 := StrBufLimit(S1, MaxLen, Buf1, High(Buf1), Allocated1);
P2 := StrBufLimit(S2, MaxLen, Buf2, High(Buf2), Allocated2);
if CaseSensitive then
Result := AnsiStrComp(P1, P2)
else
Result := AnsiStrIComp(P1, P2);
finally
if Allocated1 then
FreeMem(P1);
if Allocated2 then
FreeMem(P2);
end;
end;
function AnsiStrLComp(S1, S2: PAnsiChar; MaxLen: Cardinal): Integer;
{$IFDEF MSWINDOWS}
begin
Result := CompareStringA(LOCALE_USER_DEFAULT, 0,
S1, MaxLen, S2, MaxLen) - CSTR_EQUAL;
end;
{$ENDIF}
{$IFDEF LINUX}
begin
Result := InternalAnsiStrLComp(S1, S2, MaxLen, True);
end;
{$ENDIF}
function AnsiStrLComp(S1, S2: PWideChar; MaxLen: Cardinal): Integer;
begin
{$IFDEF MACOSX}
RunError(reMacNotImplemented);
{$ELSE !MACOSX}
Result := CompareStringW(LOCALE_USER_DEFAULT, 0,
S1, MaxLen, S2, MaxLen) - CSTR_EQUAL;
{$ENDIF !MACOSX}
end;
function AnsiStrLIComp(S1, S2: PAnsiChar; MaxLen: Cardinal): Integer;
begin
{$IFDEF MSWINDOWS}
Result := CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE,
S1, MaxLen, S2, MaxLen) - CSTR_EQUAL;
{$ENDIF}
{$IFDEF LINUX}
Result := InternalAnsiStrLComp(S1, S2, MaxLen, False);
{$ENDIF}
end;
function AnsiStrLIComp(S1, S2: PWideChar; MaxLen: Cardinal): Integer;
begin
{$IFDEF MACOSX}
RunError(reMacNotImplemented);
{$ELSE !MACOSX}
Result := CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE,
S1, MaxLen, S2, MaxLen) - CSTR_EQUAL;
{$ENDIF !MACOSX}
end;
function AnsiStrLower(Str: PAnsiChar): PAnsiChar;
{$IFDEF MSWINDOWS}
begin
Result := CharLowerA(Str);
end;
{$ENDIF}
{$IFDEF LINUX}
var
Temp: WideString;
Squish: AnsiString;
I: Integer;
begin
Temp := Str; // expand and copy multibyte to widechar
for I := 1 to Length(Temp) do
Temp[I] := WideChar(towlower(UCS4Char(Temp[I])));
Squish := Temp; // reduce and copy widechar to multibyte
if Cardinal(Length(Squish)) > StrLen(Str) then
raise ERangeError.CreateRes(@SRangeError);
Move(Squish[1], Str^, Length(Squish));
Result := Str;
end;
{$ENDIF LINUX}
function AnsiStrLower(Str: PWideChar): PWideChar;
begin
{$IFDEF MACOSX}
RunError(reMacNotImplemented);
{$ELSE !MACOSX}
Result := CharLowerW(Str);
{$ENDIF !MACOSX}
end;
function AnsiStrUpper(Str: PAnsiChar): PAnsiChar;
{$IFDEF MSWINDOWS}
begin
Result := CharUpperA(Str);
end;
{$ENDIF}
{$IFDEF LINUX}
var
Temp: WideString;
Squish: AnsiString;
I: Integer;
begin
Temp := Str; // expand and copy multibyte to widechar
for I := 1 to Length(Temp) do
Temp[I] := WideChar(towupper(UCS4Char(Temp[I])));
Squish := Temp; // reduce and copy widechar to multibyte
if Cardinal(Length(Squish)) > StrLen(Str) then
raise ERangeError.CreateRes(@SRangeError);
Move(Squish[1], Str^, Length(Squish));
Result := Str;
end;
{$ENDIF}
function AnsiStrUpper(Str: PWideChar): PWideChar;
begin
{$IFDEF MACOSX}
RunError(reMacNotImplemented);
{$ELSE !MACOSX}
Result := CharUpperW(Str);
{$ENDIF !MACOSX}
end;
function WideUpperCase(const S: WideString): WideString;
{$IFDEF MSWINDOWS}
var
Len: Integer;
begin
// CharUpperBuffW is stubbed out on Win9x platofmrs
if Win32Platform = VER_PLATFORM_WIN32_NT then
begin
Len := Length(S);
SetString(Result, PWideChar(S), Len);
if Len > 0 then CharUpperBuffW(Pointer(Result), Len);
end
else
Result := WideString(AnsiUpperCase(string(S)));
end;
{$ENDIF}
{$IFDEF LINUX}
var
I: Integer;
P: PWideChar;
begin
SetLength(Result, Length(S));
P := @Result[1];
for I := 1 to Length(S) do
P[I-1] := WideChar(towupper(UCS4Char(S[I])));
end;
{$ENDIF}
function WideLowerCase(const S: WideString): WideString;
{$IFDEF MSWINDOWS}
var
Len: Integer;
begin
// CharLowerBuffW is stubbed out on Win9x platofmrs
if Win32Platform = VER_PLATFORM_WIN32_NT then
begin
Len := Length(S);
SetString(Result, PWideChar(S), Len);
if Len > 0 then CharLowerBuffW(Pointer(Result), Len);
end
else
Result := WideString(AnsiLowerCase(string(S)));
end;
{$ENDIF}
{$IFDEF LINUX}
var
I: Integer;
P: PWideChar;
begin
SetLength(Result, Length(S));
P := @Result[1];
for I := 1 to Length(S) do
P[I-1] := WideChar(towlower(UCS4Char(S[I])));
end;
{$ENDIF}
{$IF DEFINED(MSWINDOWS) AND NOT DEFINED(UNICODE)}
function DumbItDownFor95(const S1, S2: WideString; CmpFlags: Integer): Integer;
var
a1, a2: AnsiString;
begin
a1 := string(s1);
a2 := string(s2);
Result := CompareStringA(LOCALE_USER_DEFAULT, CmpFlags, PAnsiChar(a1), Length(a1),
PAnsiChar(a2), Length(a2)) - CSTR_EQUAL;
end;
{$IFEND}
function WideCompareStr(const S1, S2: WideString): Integer;
{$IFDEF MSWINDOWS}
begin
SetLastError(0);
Result := CompareStringW(LOCALE_USER_DEFAULT, 0, PWideChar(S1), Length(S1),
PWideChar(S2), Length(S2)) - CSTR_EQUAL;
case GetLastError of
0: ;
{$IFNDEF UNICODE}
ERROR_CALL_NOT_IMPLEMENTED: Result := DumbItDownFor95(S1, S2, 0);
{$ENDIF}
else
RaiseLastOSError;
end;
end;
{$ENDIF}
{$IFDEF LINUX}
var
UCS4_S1, UCS4_S2: UCS4String;
begin
UCS4_S1 := WideStringToUCS4String(S1);
UCS4_S2 := WideStringToUCS4String(S2);
// glibc 2.1.2 / 2.1.3 implementations of wcscoll() and wcsxfrm()
// have severe capacity limits. Comparing two 100k strings may
// exhaust the stack and kill the process.
// Fixed in glibc 2.1.91 and later.
SetLastError(0);
Result := wcscoll(PUCS4Chars(UCS4_S1), PUCS4Chars(UCS4_S2));
if GetLastError <> 0 then
RaiseLastOSError;
end;
{$ENDIF}
function WideSameStr(const S1, S2: WideString): Boolean;
begin
Result := WideCompareStr(S1, S2) = 0;
end;
function WideCompareText(const S1, S2: WideString): Integer;
begin
{$IFDEF MSWINDOWS}
SetLastError(0);
Result := CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, PWideChar(S1),
Length(S1), PWideChar(S2), Length(S2)) - CSTR_EQUAL;
case GetLastError of
0: ;
{$IFNDEF UNICODE}
ERROR_CALL_NOT_IMPLEMENTED: Result := DumbItDownFor95(S1, S2, NORM_IGNORECASE);
{$ENDIF}
else
RaiseLastOSError;
end;
{$ENDIF}
{$IFDEF LINUX}
Result := WideCompareStr(WideUpperCase(S1), WideUpperCase(S2));
{$ENDIF}
end;
function WideSameText(const S1, S2: WideString): Boolean;
begin
Result := WideCompareText(S1, S2) = 0;
end;
function Trim(const S: string): string;
var
I, L: Integer;
begin
L := Length(S);
I := 1;
while (I <= L) and (S[I] <= ' ') do Inc(I);
if I > L then Result := '' else
begin
while S[L] <= ' ' do Dec(L);
Result := Copy(S, I, L - I + 1);
end;
end;
function TrimLeft(const S: string): string;
var
I, L: Integer;
begin
L := Length(S);
I := 1;
while (I <= L) and (S[I] <= ' ') do Inc(I);
Result := Copy(S, I, Maxint);
end;
function TrimRight(const S: string): string;
var
I: Integer;
begin
I := Length(S);
while (I > 0) and (S[I] <= ' ') do Dec(I);
Result := Copy(S, 1, I);
end;
function QuotedStr(const S: string): string;
var
I: Integer;
begin
Result := S;
for I := Length(Result) downto 1 do
if Result[I] = '''' then Insert('''', Result, I);
Result := '''' + Result + '''';
end;
function AnsiQuotedStr(const S: string; Quote: Char): string;
var
P, Src, Dest: PChar;
AddCount: Integer;
begin
AddCount := 0;
P := AnsiStrScan(PChar(S), Quote);
while P <> nil do
begin
Inc(P);
Inc(AddCount);
P := AnsiStrScan(P, Quote);
end;
if AddCount = 0 then
begin
Result := Quote + S + Quote;
Exit;
end;
SetLength(Result, Length(S) + AddCount + 2);
Dest := PChar(Result);
Dest^ := Quote;
Inc(Dest);
Src := PChar(S);
P := AnsiStrScan(Src, Quote);
repeat
Inc(P);
Move(Src^, Dest^, (P - Src) * SizeOf(Char));
Inc(Dest, P - Src);
Dest^ := Quote;
Inc(Dest);
Src := P;
P := AnsiStrScan(Src, Quote);
until P = nil;
P := StrEnd(Src);
Move(Src^, Dest^, (P - Src) * SizeOf(Char));
Inc(Dest, P - Src);
Dest^ := Quote;
end;
function AnsiExtractQuotedStr(var Src: PAnsiChar; Quote: AnsiChar): AnsiString;
var
P, Dest: PAnsiChar;
DropCount: Integer;
begin
Result := '';
if (Src = nil) or (Src^ <> Quote) then Exit;
Inc(Src);
DropCount := 1;
P := Src;
Src := AnsiStrScan(Src, Quote);
while Src <> nil do // count adjacent pairs of quote chars
begin
Inc(Src);
if Src^ <> Quote then Break;
Inc(Src);
Inc(DropCount);
Src := AnsiStrScan(Src, Quote);
end;
if Src = nil then Src := StrEnd(P);
if ((Src - P) <= 1) or ((Src - P - DropCount) = 0) then Exit;
if DropCount = 1 then
SetString(Result, P, Src - P - 1)
else
begin
SetLength(Result, Src - P - DropCount);
Dest := PAnsiChar(Result);
Src := AnsiStrScan(P, Quote);
while Src <> nil do
begin
Inc(Src);
if Src^ <> Quote then Break;
Move(P^, Dest^, Src - P);
Inc(Dest, Src - P);
Inc(Src);
P := Src;
Src := AnsiStrScan(Src, Quote);
end;
if Src = nil then Src := StrEnd(P);
Move(P^, Dest^, Src - P - 1);
end;
end;
1922

被折叠的 条评论
为什么被折叠?



