Delphi_SysUtils中文翻譯

************************系統實用例程********************************
Delphi / Kylix 跨平臺運行時庫(System Utilities)
Copyright (c) 1995-2002 Borland Softwrare Corporation
*******************************************************************
SysUtils單元;
1. 常量(Const)
1) 文件打開方式
{$以下用於Linux環境}
fmOpenRead       = O_RDONLY;
fmOpenWrite      = O_WRONLY;
fmOpenReadWrite  = O_RDWR;
//  fmShareCompat 不被支持
fmShareExclusive = $0010;
fmShareDenyWrite = $0020;
//  fmShareDenyRead  不被支持
fmShareDenyNone  = $0030;
{$ENDIF}
{$以下用於MsWindows}
fmOpenRead       = $0000;
fmOpenWrite      = $0001;
fmOpenReadWrite  = $0002;
fmShareCompat    = $0000 platform; // DOS 兼容模式不portable
fmShareExclusive = $0010;
fmShareDenyWrite = $0020;
fmShareDenyRead  = $0030 platform; // 只寫在所有平臺上都不被支持
fmShareDenyNone  = $0040;
{$ENDIF}

2) 文件屬性
faReadOnly  = $00000001 platform;
faHidden    = $00000002 platform;
faSysFile   = $00000004 platform;
faVolumeID  = $00000008 platform;
faDirectory = $00000010;
faArchive   = $00000020 platform;
faSymLink   = $00000040 platform;
faAnyFile   = $0000003F;

3) 時間單位
HoursPerDay   = 24; 每天24小時
MinsPerHour   = 60;  每小時60分
SecsPerMin    = 60; 每分鐘60秒
MSecsPerSec   = 1000; 每秒1000毫秒
MinsPerDay    = HoursPerDay * MinsPerHour; 每天的分鐘數
SecsPerDay    = MinsPerDay * SecsPerMin; 每天秒數
MSecsPerDay   = SecsPerDay * MSecsPerSec; 每天毫秒數
DateDelta = 693594;  每天介於 1/1/0001  和 12/31/1899  之間
UnixDateDelta = 25569; { 介於TDateTime 基數(12/31/1899) 和Unix time_t 基數(1/1/1970)之間的天數 }
2. 類型定義(type)
TSysCharSet = set of Char;  //設置字符類型標準:所有ANSI字符的集合
TIntegerSet = set of 0..SizeOf(Integer) * 8 - 1;    { 設定整數的訪問類型:0-31的整數集合 }
//類型轉換
// WordRec使訪問一個Word類型變量或者其他16位變量(SmallInt)的高位和低位字節變得更容易
WordRec = packed record  
case Integer of
0: (Lo, Hi: Byte);
1: (Bytes: array [0..1] of Byte);
end;

LongRec使訪問一個LongWord類型變量或者其它32位變量(Single)高位和低位字變得更容易。
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使訪問一個Int64類型變量或者其它64位變量(Double)高位和低位字變得更容易。
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;
//一般數組
PByteArray = ^TByteArray;
TByteArray = array[0..32767] of Byte;
PWordArray = ^TWordArray;
TWordArray = array[0..16383] of Word;
TProcedure = procedure; //一般程序指針
TFileName = type string; //一般文件類型
TSearchRec = record //用於搜索記錄的結構(由函數 FindFirst, FindNext, 和 FindClose 使用)
Time: Integer;
Size: Integer;
Attr: Integer;
Name: TFileName;
ExcludeAttr: Integer;
{$以下用於MsWindows平臺}
FindHandle: THandle  platform;
FindData: TWin32FindData  platform;
{$ENDIF}
{$以下用於Linux環境}
Mode: mode_t  platform;
FindHandle: Pointer  platform;
PathOnly: String  platform;
Pattern: String  platform;
{$ENDIF}
end;
TFloatValue = (fvExtended, fvCurrency); 浮點類型格式化代碼
TFloatFormat = (ffGeneral, ffExponent, ffFixed, ffNumber, ffCurrency); FloatToText格式化代碼
TFloatRec = packed record  // FloatToDecimal 結果的記錄型結構
Exponent: Smallint;
Negative: Boolean;
Digits: array[0..20] of Char;
end;
TTimeStamp = record        //日期和時間的記錄結構
Time: Integer;      從0:00開始的的毫秒數
Date: Integer;      {從1/1/0001以後的天的加數 }
end;
TMbcsByteType = (mbSingleByte, mbLeadByte, mbTrailByte); 多字節字符集的字節類型
TSysLocale = packed record   //本地系統信息記錄類型
DefaultLCID: Integer;
PriLangID: Integer;
SubLangID: Integer;
FarEast: Boolean;
MiddleEast: Boolean;
end;
{$以下用於MsWindows平臺}
TLangRec = packed record  //被使用的語言
FName: string;
FLCID: LCID;
FExt: string;
end;
TLanguages = class  //存儲系統支持的語言
private
……..
Public
…….
end platform;
{$ENDIF}
{以下用於Linux環境}
TEraRange = record
StartDate : Integer;         // 從12/31/1899 (TDateTime 基數)的全部天數
EndDate   : Integer;         //從12/31/1899 (TDateTime 基數)的全部天數
Direction : Char;
end;
{$ENDIF}

Exception = class(TObject)  //異常
private
……..
public
……….
end;
ExceptClass = class of Exception;
EAbort = class(Exception);
EHeapException = class(Exception)
private
AllowFree: Boolean;
public
procedure FreeInstance; override;
end;
EOutOfMemory = class(EHeapException);
EInOutError = class(Exception)
public
ErrorCode: Integer;
end;

{以下用於MsWindows平臺}
PExceptionRecord = ^TExceptionRecord;
TExceptionRecord = record
ExceptionCode: Cardinal;
ExceptionFlags: Cardinal;
ExceptionRecord: PExceptionRecord;
ExceptionAddress: Pointer;
NumberParameters: Cardinal;
ExceptionInformation: array[0..14] of Cardinal;
end;
{$ENDIF}
EExternal = class(Exception)
public
{$以下用於MsWindows平臺}
ExceptionRecord: PExceptionRecord platform;
{$ENDIF}
{$以下用於Linux環境}
ExceptionAddress: LongWord platform;
AccessAddress: LongWord platform;
SignalNumber: Integer platform;
{$ENDIF}
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);
{以下用於Linux環境}
EQuit = class(EExternal)  end platform;
{$ENDIF}
{以下用於Linux環境}
ECodesetConversion = class(Exception)  end platform;
{$ENDIF}
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;
{以下用於MsWindows平臺}
EWin32Error = class(EOSError)
end deprecated;
{$ENDIF}
ESafecallException = class(Exception);
{以下用於Linux環境}
3. 符號(Signals)

外部異常或符號由Delphi RTL默認被轉換到語言異常。在Linux下,一個Delphi應用程序安裝符號處理機來誘捕原始符號並轉換他們。Delphi庫默認不安裝處理機。所以你要是執行一個標準庫,類似Apache DSO, 你想要將符號轉換到能夠捕獲的語言異常,就必須人工安裝符號鉤,用Delphi RTL提供的接口。
對於大多數的庫,安裝符號處理機是恰當簡便的。在初始時間調用HookSignal(RTL_SIGDEFAULT),在關閉時調用UnhookSignal(RTL_SIGNALDEFAULT),將會爲一組符號安裝處理機,Delphi應用程序被RTL正常鉤住。
有時候會有一些使以上的初始化不能工作的事情。適當的設置一個符號處理機的行爲,然後當整理完後恢復到以前狀態。如果你有兩個庫lib1和lib2,lib1安裝了符號處理機,然後lib2也安裝了一個,如果要恢復符號處理機則那些庫就必須以適當的次序卸載,或者符號處理機能夠被留在矛盾而致命的地方。爲了防止這種可能,允許你無論在何種情況下在外部庫中發現這種行爲都能較好的管理符號處理機,我們提供了一組4個接口來容許你在緊急事件中剪裁Delphi符號處理機掛鉤/脫鉤,他們是:
InquireSignal
AbandonSignalHandler
HookSignal
UnhookSignal

InquireSignal 允許你看符號處理機的狀態,也就是你能發現在你之下抓取它的人。
AbandonSignalHandler 告訴RTL從不脫鉤一個特定的符號處理機。如果你發現一個符號處理機將要不安全的返回到以前狀態就需要使用它。例如,如果前一個符號處理機被一個後來被卸載的庫安裝。
HookSignal/UnhookSignal 設置符號處理機映射某個信號爲語言異常。其它的看下面的InquireSignal等註釋。
const
RTL_SIGINT          = 0;    // 用戶中斷User interrupt (SIGINT)
RTL_SIGFPE          = 1;    // 浮點異常(SIGFPE)
RTL_SIGSEGV         = 2;    // 分割違背 (SIGSEGV)
RTL_SIGILL          = 3;    //非法指令(SIGILL)
RTL_SIGBUS          = 4;    // 總線Bus 錯誤 (SIGBUS)
RTL_SIGQUIT         = 5;    // 用戶中斷(SIGQUIT)
RTL_SIGLAST         = RTL_SIGQUIT; // 已用於內部,不能用
RTL_SIGDEFAULT      = -1;   // 意味着我們捕獲一組符號Means all of a set of signals that the we capture
// normally.  這是當前全部進程
// signals.  你不能傳遞這個到InquireSignal.

type
//TSignalState 給定的符號處理機的狀態,由InquireSignal送回。看以下InquireSignal。
TSignalState = (ssNotHooked, ssHooked, ssOverridden);

var
如果DeferUserInterrupts設置,我們不能引發SIGINT 、SIGQUIT 這樣的異常,而是當信號到達時設置SIGINTIssued或SIGQUITIssued,並且要忍受OS發出的信號。這個引起GUI 應用程序偶然的延遲實際信號處理直到它是安全
DeferUserInterrupts: Boolean;
SIGINTIssued: Boolean;
SIGQUITIssued: Boolean;
{$ENDIF}

{$以下用於Linux環境}
const
MAX_PATH = 4095;  // From /usr/include/linux/limits.h PATH_MAX
{$ENDIF}
var

{ 空字串和null 字符指針.這些常量只爲向後兼容而提供。  }
EmptyStr: string = '';
NullStr: PString = @EmptyStr;
EmptyWideStr: WideString = '';
NullWideStr: PWideString = @EmptyWideStr;

{$以下用於MsWindows平臺}
Win32 平臺標識符,將會是下列數值之一:
VER_PLATFORM_WIN32s
VER_PLATFORM_WIN32_WINDOWS
VER_PLATFORM_WIN32_NT

Win32Platform: Integer = 0;    //數值看WINDOWS.PAS
Win32 OS 版本信息見TOSVersionInfo.dwMajorVersion/dwMinorVersion/dwBuildNumber
Win32MajorVersion: Integer = 0;
Win32MinorVersion: Integer = 0;
Win32BuildNumber: Integer = 0;


Win32CSDVersion: string = ''; // Win32 OS 以外版本信息字符串見TOSVersionInfo.szCSDVersion
4. 實用函數過程
1) Win32 OS 版本測試函數
function CheckWin32Version(AMajor: Integer; AMinor: Integer = 0): Boolean;

GetFileVersion 返回一個32位的文件的二進制版本數,它一般有助版本號和輔版本號,一般沒有release 或build 號。 如果失敗,則返回Cardinal(-1)
function GetFileVersion(const AFileName: string): Cardinal;

{$ENDIF}

2) 貨幣和日期/時間格式化選項
這些變量初始數值用Win32 API 中的GetLocaleInfo 函數從系統註冊表取得,每個變量指定常量LOCALE_XXXX的來描述這些初始值

CurrencyString – 定義浮點數轉換爲十進制數後的需用的貨幣符號,初始值來自於LOCALE_SCURRENCY.
CurrencyFormat -定義浮點數轉換爲十進制數後的貨幣符號的位置和分離程度,可能的值爲:
0 = '$1'
1 = '1$'
2 = '$ 1'
3 = '1 $'
初始值來自於LOCALE_ICURRENCY.
NegCurrFormat –定義負數浮點數轉換爲十進制數後的貨幣格式,可能的值爲:
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 $)'
初始值來自於LOCALE_INEGCURR.
ThousandSeparator – 多於一千時的千分位分離符位於數值的左邊,初始值來自於LOCALE_STHOUSAND.  #0 值即使有此指示符也不應當輸出指示符
DecimalSeparator – 用於分離一個數的小數部分的整數,其初值來自LOCALE_SDECIMAL.  DecimalSeparator 必須是個非零值
CurrencyDecimals – 貨幣值中小數點右邊的小數位數,初值來自LOCALE_ICURRDIGITS.
DateSeparator – 分離符:用於分離日期數值的年、月、日部分,初值來自於LOCATE_SDATE.
ShortDateFormat – 用於將日期型數值轉換爲適合編輯的短字符串。對日期和時間格式化字符串的完整描述,請查閱FormatDate 函數。短日期格式應當只使用日期分離符和m, mm, d, dd, yy, 和 yyyy 格式特定。初值來自於LOCALE_SSHORTDATE。
LongDateFormat -用於將日期型數值轉換爲適合顯示而不是編輯的長字符串。對日期和時間格式化字符串的完整描述,請查閱FormatDate 函數。其初值來自於LOCALE_SLONGDATE。
TimeSeparator – 用於分離時間型數值的時、分、秒。其初值來自於LOCALE_STIME.。
TimeAMString – 時間後綴用於在12小時制上午的後綴,初值來自於LOCALE_S1159。
TimePMString – 12小時制下午的後綴,初值來自於LOCALE_S2359。
ShortTimeFormat – 此格式化字符用於時間值轉換爲只有小時和分鐘的短字符串。其默認值從LOCALE_ITIME 和 LOCALE_ITLZERO計算得到
LongTimeFormat -此格式化字符用於時間值轉換爲含有小時、分鐘和秒的長短字符串。其默認值從LOCALE_ITIME 和 LOCALE_ITLZERO計算得到。
ShortMonthNames – 字符串數組包含有短月份名,格式字串中的 mmm 特定格式被傳送給FormatDate 會使得短月份名被替換。其默認值從 LOCALE_SABBREVMONTHNAME 系統本地入口取出。
LongMonthNames -字符串數組包含有長月份名。格式化字串中的mmmm 格式特定符被傳送給FormatDate 會使得長月份名被替換。其缺省值是從LOCALE_SMONTHNAME 系統本地入口取出。
ShortDayNames -字符串數組包含有短天名,格式字串中的 ddd特定格式被傳送給FormatDate 會使得短天名被替換。其默認值取自於LOCALE_SABBREVDAYNAME 系統本地入口。
LongDayNames -字符串數組包含有長天名,格式字串中的 dddd特定格式被傳送給FormatDate 會使得長天名被替換。其默認值取自於LOCALE_SDAYNAME 系統本地入口。
ListSeparator – 字符用於分開一個列表的項目,其初值來自於LOCALE_SLIST。
TwoDigitYearCenturyWindow – 當將字符型日期轉換爲數字日期時,世紀被添加到兩位的年中。這個值在提取世紀值之前被從本年度減去。這能延長現有的存依賴於2位數年數據登錄項的應用程序壽命。對2000年(Y2k) 問題的最好的解決辦法是不再接受兩位數的年,在數據登錄項中只接受4位的年,從而根本上消除世紀的模糊性
示例:
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;

3) 線程安全流通和日期/時間格式化
TFormatSettings 記錄型被設計成允許線程安全格式化,相同的全局變量描述在上面。每個使用全局變量被重載的格式化例程都需要TformatSettings型的附加參數。
一個TFormatSettings 記錄型在使用之前必須有內容。使用GetLocaleFormatSettings函數能夠使其基於給定環境。注意一些格式指定符仍然需要指定線程現場設置(類似於period/era 名字)。
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;
const
MaxEraCount = 7;
var
EraNames: array [1..MaxEraCount] of string;
EraYearOffsets: array [1..MaxEraCount] of Integer;
{以下用於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}
{以下用於MsWindows平臺}
function Languages: TLanguages;
{$ENDIF}

4) 內存管理例程。
AllocMem-在堆上分配給定大小的塊。被分配的緩衝區設置爲0。若要消除這個緩衝區請使用FreeMem 標準過程。
function AllocMem(Size: Cardinal): Pointer;

5) 退出過程處理
把給定的過程添加到運行時庫的退出過程列表中。當應用程序終止時, 它的退出過程被執行,執行順序是最後的過程先運行,也就是後進先出。
procedure AddExitProc(Proc: TProcedure);

6) 字符串處理例程

function NewStr(const S: string): PString; deprecated; //在堆上分配一個字串,只是爲向後兼容而提供。
procedure DisposeStr(P: PString); deprecated;  //消除一個由NewStr 分配的字串指針。只是爲向後兼容而提供
procedure AssignStr(var P: PString; const S: string); deprecated; //賦值一個新的動態的已分配字串到給定的字串指針。只是爲向後兼容而提供
procedure AppendStr(var Dest: string; const S: string); deprecated; //添加 S 到Dest 的末尾.(Dest := Dest + S)。只是爲向後兼容而提供。
function UpperCase(const S: string): string; //轉換給定的ASCII字符爲大寫,它只對7位的ASCII字符中的'a' - 'z' 起作用。 對於8位的國際字符集用AnsiUpperCase。
function LowerCase(const S: string): string; //轉換給定的ASCII字符爲小寫,它只對7位的ASCII字符中的'A' - 'Z' 起作用。 對於8位的國際字符集用AnsiUpperCase。
function CompareStr(const S1, S2: string): Integer;  //比較字串 S1 和 S2, 有大小寫敏感(case-sensitivity). 如果S1<S2,返回值小於0;S1=S2,返回值等於0;S1>S2,返回值大小於。這個比較操作是基於每個字符的8位序數值並且不受當前的用戶環境影響
function CompareMem(P1, P2: Pointer; Length: Integer): Boolean; assembler; //比較由P1和P2分別指示的Length長度的內存區域。如果相同則返回True 。
function CompareText(const S1, S2: string): Integer;  //比較S1 和S2, 沒有大小寫敏感(case-sensitivity)。返回值與CompareStr類同。這個比較操作是基於每個字符的8位序數值('a'..'z' 變爲'A'..'Z' 後)並且不受當前的用戶環境影響
function SameText(const S1, S2: string): Boolean; //比較S1 和S2,沒有大小寫敏感。如果S1=S2,返回true ,也就是如果CompareText 將返回0。SameText 象CompareText一樣有8位限制 。
function AnsiUpperCase(const S: string): string; //轉換給定字串中的全部字符爲大寫,這個轉換使用用戶環境。
function AnsiLowerCase(const S: string): string;  //轉換給定字串中的全部字符爲小寫,這個轉換使用用戶環境。
function AnsiCompareStr(const S1, S2: string): Integer; 比較S1和S2,有大小寫敏感。這個比較操作受控於用戶當前環境。返回值與CompareStr相同。
function AnsiSameStr(const S1, S2: string): Boolean;   //比較S1和S2,有大小寫敏感。這個比較操作受控於用戶當前環境。如果AnsiCompareStr返回0,則它返回True。
function AnsiCompareText(const S1, S2: string): Integer;  //比較S1和S2,沒有大小寫敏感。這個比較操作受控於用戶當前環境。返回值與CompareStr相同。
function AnsiSameText(const S1, S2: string): Boolean;  //比較S1 和S2, 沒有大小寫敏感。這個比較操作受控於用戶當前環境。如果AnsiCompareText返回0,則它返回True。
function AnsiStrComp(S1, S2: PChar): Integer; //比較S1 和S2,有大小寫敏感。這個比較操作受控於用戶當前環境。返回值與CompareStr相同。
function AnsiStrIComp(S1, S2: PChar): Integer; 比較S1和S2,沒有大小寫敏感。這個比較操作受控於用戶當前環境。返回值與CompareStr相同。
function AnsiStrLComp(S1, S2: PChar; MaxLen: Cardinal): Integer;  比較S1和S2,有大小寫敏感。最大直到MaxLen 長度。這個比較操作受控於用戶當前環境。返回值與CompareStr相同。
function AnsiStrLIComp(S1, S2: PChar; MaxLen: Cardinal): Integer;  比較S1和S2,沒有大小寫敏感。最大直到MaxLen 長度。這個比較操作受控於用戶當前環境。返回值與CompareStr相同。
function AnsiStrLower(Str: PChar): PChar;  轉換給定字串的全部字符爲小寫,使用用戶當前環境。
function AnsiStrUpper(Str: PChar): PChar; 轉換給定字串的全部字符爲大寫,使用用戶當前環境。
function AnsiLastChar(const S: string): PChar;   返回一個指到字串中的最後字符。這個函數支持多字節字符集
function AnsiStrLastChar(P: PChar): PChar; 返回一個指到字串中的最後字符。這個函數支持多字節字符集。
function WideUpperCase(const S: WideString): WideString; 轉換給定字串的全部字符爲大寫。
function WideLowerCase(const S: WideString): WideString; 轉換給定字串的全部字符爲小寫。
function WideCompareStr(const S1, S2: WideString): Integer; 比較S1 和S2,有大小寫敏感。返回值與CompareStr相同。
 
function WideSameStr(const S1, S2: WideString): Boolean;  比較S1 和S2,有大小寫敏感。如果WideCompareStr返回0,則它返回True。
function WideCompareText(const S1, S2: WideString): Integer; 比較S1 和S2,沒有大小寫敏感。返回值與CompareStr相同。
function WideSameText(const S1, S2: WideString): Boolean; 比較S1 和S2,沒有大小寫敏感。如果WideCompareText返回0,則它返回True。
function Trim(const S: string): string; overload; 從給定字串中去除首尾空格和控制符。
function Trim(const S: WideString): WideString; overload; 從給定字串中去除首尾空格和控制符。
function TrimLeft(const S: string): string; overload; 從給定字串中去除首端空格和控制符。
function TrimLeft(const S: WideString): WideString; overload; 從給定字串中去除首端空格和控制符。
function TrimRight(const S: string): string; overload; 從給定字串中去除尾空格和控制符。
function TrimRight(const S: WideString): WideString; overload; 從給定字串中去除尾空格和控制符。
function QuotedStr(const S: string): string; 返回加引號的字串。單引號分別被插入到字串的首尾端,並且對於字串中只有一個單引號,另一個都被添加。
function AnsiQuotedStr(const S: string; Quote: Char): string; 返回加給定引號字符的字串。引號分別被插入到字串的首尾端,並且字串中每個引號都是成對的。這個函數支持多字節字符集 (MBCS)。
function AnsiExtractQuotedStr(var Src: PChar; Quote: Char): string;  去除字符串的首尾引號,如果源字串首字符不是引號,則返回一個空的字串。函數從源串首端複製到遇到第二個引號或第一個零(Null)字符爲止。這個源參數指向首個引號後面的字符串。如果源字串不包含有相匹配的結束引號,則其被指向最終的Null字符,這個函數支持多字節字符(MBCS)。
function AnsiDequotedStr(const S: string; AQuote: Char): string;  是AnsiExtractQuotedStr 函數的簡化
{ AdjustLineBreaks 調整在給定的字串中所有的換行符爲指定的格式。當格式是 tlbsCRLF 時,函數改變所有CR字符和LF字符爲CR/LF對。 當格式是 tlbsLF 的時候,函數改變所有的CR/LF對和CR字符爲LF字符。 }
function AdjustLineBreaks(const S: string; Style: TTextLineBreakStyle ={以下用於Linux環境} tlbsLF {END Linux環境}{以下用於MSWINDOWS環境} tlbsCRLF {END MSWINDOWS環境}): string;
function IsValidIdent(const Ident: string): Boolean; 若給定字串是有效的標識符則返回爲True。標識符是由['A'..'Z', 'a'..'z', '_']開始的並可能跟隨['A'..'Z', 'a'..'z','0..'9', '_']的有效字串。

{ IntToStr 轉換給定的數值爲十進制字串表示。}
function IntToStr(Value: Integer): string; overload;
function IntToStr(Value: Int64): string; overload;

{ IntToHex 用指定的最小位數轉換給定數值爲十六進制字串表示。}
function IntToHex(Value: Integer; Digits: Integer): string; overload;
function IntToHex(Value: Int64; Digits: Integer): string; overload;

{ StrToInt 轉換給定字串爲整數值,若字串不包含有效的數值,則產生一個EconvertError異常。 }
function StrToInt(const S: string): Integer;
function StrToIntDef(const S: string; Default: Integer): Integer;
function TryStrToInt(const S: string; out Value: Integer): Boolean; 出現錯誤返回False

{與上面函數類似的Int64 函數}
function StrToInt64(const S: string): Int64;
function StrToInt64Def(const S: string; const Default: Int64): Int64;
function TryStrToInt64(const S: string; out Value: Int64): Boolean;
var
TrueBoolStrs: array of String;
FalseBoolStrs: array of String;

const
DefaultTrueBoolStr = 'True';   // DO NOT LOCALIZE
DefaultFalseBoolStr = 'False'; // DO NOT LOCALIZE
StrToBool 轉換給定的字串爲布爾值。若字串不包含有效的數值,則產生一個EconvertError異常。
function StrToBool(const S: string): Boolean;
function StrToBoolDef(const S: string; const Default: Boolean): Boolean;
function TryStrToBool(const S: string; out Value: Boolean): Boolean;
function BoolToStr(B: Boolean; UseBoolStrs: Boolean = False): string; 依次轉換布爾值爲字串。其總是拾取TrueStrs/FalseStrs 隊列的第一個元素。
function LoadStr(Ident: Integer): string; 從應用程序的可執行文件或關聯的資源模塊加載由Ident給出的字串資源。若字串資源不存在,則返回一個空串。
function FmtLoadStr(Ident: Integer; const Args: array of const): string; 從應用程序的可執行文件或關聯的資源模塊加載由Ident給出的字串資源。並且使用給定的格式主題。

7) { 文件管理例程}
function FileOpen(const FileName: string; Mode: LongWord): Integer;  使用指定的訪問模式打開指定的文件。訪問模式是由fmOpenXXXX和fmShareXXXX等形式的常量表示,若成功訪問則返回一個正數表示的文件句柄,若返回-1則表示有錯誤。

{ FileCreate

function FileCreate(const FileName: string): Integer; overload; 用指定的名字建立一個新文件。若成功建立則返回一個正數表示的文件句柄,若返回-1則表示有錯誤。在Linux中,調用FileCreate(FileName, DEFFILEMODE)來建立。

{

function FileCreate(const FileName: string; Rights: Integer): Integer; overload; 這是FileCreate 的第二個版本,讓你在剛建立的文件上指定存取權。這個存取權參數在Win32系統中被忽略
function FileRead(Handle: Integer; var Buffer; Count: LongWord): Integer; 讀取Handle句柄指定的文件的Count 字節長度到Buffer指定的緩存中。返回實際讀取的字節數,如果已到達文件末尾則它比Count 少。如果產生錯誤則返回-1
function FileWrite(Handle: Integer; const Buffer; Count: LongWord): Integer; 從Buffer緩衝區寫Count字節到句柄Handle指定的文件。返回實際寫入的字節數,如果產生錯誤則返回-1
FileSeek :句柄Handle給定的文件的指針移至相對於起點Origin偏移Offset的位置處,如果 Origin=0,則意味着從文件的開始處算起;Origin=1則表示從當前位置算起;Origin=2表示從文件結尾算起(EOF)。返回值是新的當前位置(相對於文件的開始處)。如果發生錯誤則返回-1。
function FileSeek(Handle, Offset, Origin: Integer): Integer; overload;
function FileSeek(Handle: Integer; const Offset: Int64; Origin: Integer): Int64; overload;
procedure FileClose(Handle: Integer); 關閉指定文件
function FileAge(const FileName: string): Integer; 返回指定文件的日期和時間戳。其返回值用FileDateToDateTime函數能夠被轉換爲TdateTime值。如果文件不存在則返回-1。
function FileExists(const FileName: string): Boolean; 返回布爾值指示文件是否存在。
function DirectoryExists(const Directory: string): Boolean; 返回布爾值指示目錄是否存在(並且是真實的目錄)。
function ForceDirectories(Dir: string): Boolean; 確保一個指定路徑的所有目錄存在。如果哪一級目錄不存在則建立(目錄)。返回真值表明成功。如果當前用戶在給定路徑下沒有足夠的文件訪問權來建立目錄則會失敗。
FindFirst 在Path路徑中搜索具有Attr屬性的第一個文件,結果被送回F(TsearchRec)。如果搜索成功則返回0,其它返回值是系統錯誤碼,調用FindFirst後,總是調用FindClose。FindFirst 其後總是跟隨FindNext 和FindClose 序列:
Result := FindFirst(Path, Attr, SearchRec);
while Result = 0 do
begin
ProcessSearchRec(SearchRec);
Result := FindNext(SearchRec);
end;
FindClose(SearchRec);
//ProcessSearchRec 是用戶自定義代碼
function FindFirst(const Path: string; Attr: Integer; var F: TSearchRec): Integer;
function FindNext(var F: TSearchRec): Integer;   返回符合條件(FindFirst序列)的下一個文件F。如果成功則返回0,其它返回值是系統錯誤碼
procedure FindClose(var F: TSearchRec); 終止一個FindFirst/FindNext 序列,釋放內存和系統資源。每個FindFirst/FindNext 序列必須由FindClose來結束

function FileGetDate(Handle: Integer): Integer; 返回由句柄給定的文件的OS日期時間戳。若句柄無效則返回-1。FileDateToDateTime函數能夠將返回值轉換爲一個TdateTime值。
function FileSetDate(const FileName: string; Age: Integer): Integer; overload;設置由FileName指定的文件的OS日期時間戳爲Age。DateTimeToFileDate函數可將TdateTime值轉換爲OS日期時間戳。如果成功則返回0,其它返回值是系統錯誤碼

{以下用於MsWindows平臺}
function FileSetDate(Handle: Integer; Age: Integer): Integer; overload; platform; 在Unix平臺句柄是無效的,因爲它不能只用一個句柄來設置文件的修改時間。也沒有辦法獲得一個打開的文件的名字。
function FileGetAttr(const FileName: string): Integer; platform; 返回文件FileName的屬性。這個屬性能夠使用前面已定義的faXXXX常量的AND-ing來檢查,如果發生錯誤則返回-1
function FileSetAttr(const FileName: string; Attr: Integer): Integer; platform; 設置文件FileName的屬性值爲Attr,這個屬性值是由OR-ing適當的faXXXX常量形成的。成功則返回0,其它返回值是系統錯誤碼 。
{$ENDIF}
function FileIsReadOnly(const FileName: string): Boolean;  爲當前進程和用戶ID檢查給定文件是否爲只讀。若文件不存在則返回False。(在調用FileIsReadOnly之前檢查FileExists)這個函數是平臺portable.
function FileSetReadOnly(const FileName: string; ReadOnly: Boolean): Boolean; 設置文件爲只讀。這個文件必須存在並且當前用戶ID必須是文件的擁有者。在Unix系統中,這個函數試圖設置或移除文件上全部三用戶(user, group, and other)的寫許可,如果想要同意局部的許可(只許所有者User),用平臺指定函數如chmod。如果文件成功改變則返回True,有錯誤則返回False。這個函數是平臺portable.
function DeleteFile(const FileName: string): Boolean; 刪除文件FileName。若成功刪除則返回True,有錯誤則返回False
function RenameFile(const OldName, NewName: string): Boolean; 將文件名OldName改名爲NewName。成功改名則返回True,發生錯誤則返回False。
function ChangeFileExt(const FileName, Extension: string): string; 改變文件名FileName的擴展名爲Extension,FileName可以有或無擴展名,Extension可以是一個空字串或一個以句點開頭的最多三個字符的字串。
function ExtractFilePath(const FileName: string): string; 取出給定FileName的驅動器和目錄部分。結果字串是FileName最左邊的字符,包含有:和/分離符的路徑信息。若FileName不包含驅動器和目錄部分則返回空。
function ExtractFileDir(const FileName: string): string; 取出文件名的路徑部分,去掉最後的反斜槓/,可以把本函數的結果給任何一個有關目錄操作的函數使用。如果文件名不包含路徑部分則返回空字符。
function ExtractFileDrive(const FileName: string): string; 返回文件名的盤符部分,或者是通用命名約定中的主機以及共享名,若文件名中不包含盤符,則返回空字串
function ExtractFileName(const FileName: string): string; 返回一個路徑中的文件名部分,並且包含擴展名;若FileName以一個冒號或反斜槓結尾,則本函數返回空字串。
function ExtractFileExt(const FileName: string): string; 返回文件名的擴展名部分,幷包括他的小數點,若沒有擴展名則返回空字串
function ExpandFileName(const FileName: string): string; 返回文件的絕對路徑包括盤符及目錄
type
TFilenameCaseMatch = (mkNone, mkExactMatch, mkSingleMatch, mkAmbiguous);

function ExpandFileNameCase(const FileName: string; out MatchFound: TFilenameCaseMatch):  string; 象ExpandFilename返回一個完整的的文件名, 但是,在區分大小寫字母的文件系統中完全匹配搜索時執行的是大小寫字母相同對待的策略。有益於懶惰用戶輸入便於使用的文件名, 或者用在不區分大小寫系統(Win32)建立的文件名數據轉換爲用於區分大小的文件系統(Linux)時。輸出參數 MatchFound 指出在文件系統搜索中查找了出何種匹配類型的文件,其結果基於以下:
(以難度或複雜性爲序)
mkExactMatch:  區分大小寫。  Result := ExpandFileName(FileName).
mkSingleMatch: 給定路徑中不區分大小寫方式嚴密匹配出一個文件Result := ExpandFileName(FileName as found in file system).
mkAmbiguous: 給定路徑中不區分大小寫方式匹配出許多文件,這要考慮錯誤問題Result := ExpandFileName(First matching filename found).
mkNone:  文件根本找不到Result := ExpandFileName(FileName).
注意,由於此函數要搜索文件系統,所以特殊情況下(文件名模糊或不存在)比ExpandFileName慢,當你輸入了一個不確定的名字並且你想要最好的結果時才使用此函數。
function ExpandUNCFileName(const FileName: string): string; 返回一個充分擴展了絕對路徑的文件名,磁盤符被擴展成通用命名約定(UNC),例如(//Server/sharename
function ExtractRelativePath(const BaseName, DestName: string): string; 返回一個相對於給定
BaseName目錄的路徑DestName的文件名,在Windows中去除公共路徑添加’../’,在Linux系統中則是‘../’

{以下用於MsWindows平臺}

function ExtractShortPathName(const FileName: string): string; 將文件名轉換爲短文件名(8.3)。若FileName已是一個短文件名,則返回相同的文件名。若文件不存在,則返回一個空串。
{$ENDIF}

function FileSearch(const Name, DirList: string): string; 在DirList目錄中,查找Name文件。查找通常由當前目錄開始,然後再查找DirList中所列出的目錄。其中目錄之間用分號隔開,Name必須是一個單純的文件名或是一個相對路徑,函數返回的是整個路徑名,若找不到文件則返回空字串。

{以下用於MsWindows平臺}
function DiskFree(Drive: Byte): Int64; 返回磁盤上可用字節數。1爲A盤,2爲B盤,以此類推。Drive爲0,則返回缺省盤的信息。此函數可有效的處理超過2GB的磁盤,除非在Win95A下,因爲Win95A不支持大硬盤分區。如果磁盤無效則返回-1。
function DiskSize(Drive: Byte): Int64;返回磁盤整體字節數,參數同DiskFree
{$ENDIF}
function FileDateToDateTime(FileDate: Integer): TDateTime; 轉換文件的‘修改時間’爲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; 轉換TdateTime類型爲文件‘修改時間’
function GetCurrentDir: string; 返回當前的驅動器和工作目錄。

function SetCurrentDir(const Dir: string): Boolean; 設置Dir爲當前工作目錄,成功返回True否則返回False。若不想在I/O結果上浪費時間,用此函數而不是ChDir。
function CreateDir(const Dir: string): Boolean; 創建Dir目錄,成功返回True否則False。若不想在I/O結果上浪費時間,用此函數而不是MkDir。
function RemoveDir(const Dir: string): Boolean; 刪除一個空的目錄,返回True則成功,False
則失敗。若不想在I/O結果上浪費時間,用此函數而不是RmDir。

8) PChar 例程
{const 參數幫助簡化C++ 代碼.  對pascal 代碼無影響}
function StrLen(const Str: PChar): Cardinal; 返回Str的字符個數(即長度),不包括Null終結符
function StrEnd(const Str: PChar): PChar;返回指向Str終結符(Null)的指針
function StrMove(Dest: PChar; const Source: PChar; Count: Cardinal): PChar; 將Source字符串中Count個字節的內容完全複製到Dest中去。返回值是Dest,源緩衝區和目標緩衝區可以重疊。
function StrCopy(Dest: PChar; const Source: PChar): PChar; 將Source 中最多MaxLen個字節複製到Dest中去。必須確保Dest有足夠的空間,返回Dest

function StrECopy(Dest:PChar; const Source: PChar): PChar; 將Source複製到Dest中去。返回指向Dest字串最後Null字符的指針

function StrLCopy(Dest: PChar; const Source: PChar; MaxLen: Cardinal): PChar; 將Source 中最多MaxLen個字符複製到Dest中去。如果Source是一個多字節字串,必須確保第MaxLen個字節沒有落在一個多字節字符中間。
function StrPCopy(Dest: PChar; const Source: string): PChar; 將AnsiString字符串複製成爲一個PChar字符串。必須確保Dest有足夠空間,返回Dest
function StrPLCopy(Dest: PChar; const Source: string; MaxLen: Cardinal): PChar; 將AnsiString字串中最多MaxLen個字符複製Pchar字串。如果Source是一個多字節字串,必須確保第MaxLen個字節沒有落在一個多字節字符中間。
function StrCat(Dest: PChar; const Source: PChar): PChar; 將Source複製到Dest尾部上去。必須確保Dest有足夠空間,返回值Dest
function StrLCat(Dest: PChar; const Source: PChar; MaxLen: Cardinal): PChar; 將Source 中最多MaxLen個字符複製到Dest尾部中去。如果Source是一個多字節字串,必須確保第MaxLen個字節沒有落在一個多字節字符中間。返回值是Dest。
function StrComp(const Str1, Str2: PChar): Integer; 比較Str1和Str2。若Str1〈Str2則返回負數,Str1=Str2則返回0,Str1>Str2則返回正數。
function StrIComp(const Str1, Str2: PChar): Integer; 比較Str1和Str2。無大小寫區別,返回值與StrComp相同
function StrLComp(const Str1, Str2: PChar; MaxLen: Cardinal): Integer; 比較Str1和Str2。字串最多Max:Len個字符,返回值與StrComp相同
function StrLIComp(const Str1, Str2: PChar; MaxLen: Cardinal): Integer; 比較Str1和Str2。無大小寫區別,字串最多Max:Len個字符,返回值與StrComp相同

function StrScan(const Str: PChar; Chr: Char): PChar; 返回指向字串Str中第一個字符Char的指針,如果不存在則返回NIL。Null終結符也被算作字串的一部分。
function StrRScan(const Str: PChar; Chr: Char): PChar; 返回指向字串Str中最後一個字符Char的指針,如果不存在則返回NIL。Null終結符也被算作字串的一部分。
function StrPos(const Str1, Str2: PChar): PChar; 返回指向字串Str1中第一個字符串Str2的指針,如果不存在則返回NIL。
function StrUpper(Str: PChar): PChar; 轉換Str爲大寫並返回
function StrLower(Str: PChar): PChar; 轉換Str爲小寫並返回
function StrPas(const Str: PChar): string; 轉換Str爲Pascal型的字串。現在Delphi自動完成這個工作。此函數只是爲向後兼容
function StrAlloc(Size: Cardinal): PChar; 在堆上分配一個大小爲Size的緩衝區並返回它的指針,這個緩衝區並沒有被初始化,要釋放他,用StrDispose。
function StrBufSize(const Str: PChar): Cardinal; 返回由StrAlloc或者StrNew分配的字節數目。對於其它PChar函數不適用。
function StrNew(const Str: PChar): PChar; 在堆上分配空間給Str的備份並返回指向這個備份的指針。不要把本函數與NewStr函數混淆。可以用函數StrDispose函數來釋放這個字符串。
procedure StrDispose(Str: PChar); 釋放由StrNew和StrAlloc函數所分配內存的字符串。若Str是NIL,則什麼都不做就返回。不要把它與DisposeStr函數相混淆。

9) 字符串格式化程序

{Format函數通過使用Format參數來格式化由Args參數中的variant開放數組。格式化字串包括無格式對象字符(objects—plain)和格式特定符兩種類型。無格式字符被逐個的複製到結果字串中去,格式特定符從主題列表取出主題並且應用格式。
格式化特定符有以下形式
"%" [index ":"] ["-"] [width] ["." prec] type

一個格式特定符使用一個%開始,在其後是以下序列:
-  [index ":"]:可選的Args的索引特定符。 當Index爲0時表示第一個參數,若沒有索引值則每個標識符對應於Args數組中連續的元素。使用索引值可以在一個格式字串中重複使用同一個參數,例如‘%d %d %0:d %d', [10, 20]’產生10 20 10 20的結果。
- ["-"]:可選的左對齊指示符。
- [width]:可選的寬度特定符。用來設定格式化後的字串字符個數。若實際格式化後的值長度小於Width個字符,則將會在字串的左邊加上空格,如果在Width前使用-,則空格加在右邊。
- ["." prec]可選的精度特定符。
- type:轉換類型字符,是一個字母,大小寫均可:

d  :格式化一個有符號的十進制整數。Precision確定了要顯示的數字的最小個數,並且還會在需要的時候在左邊加上0。Args的值必須是一個整數。
u  :格式化一個無符號十進制整數。其餘與’d’類似
e  :用科學計數(scientific)法形式格式化一個浮點數。Precision確定了要顯示的數字的最小個數,缺省值爲15。Args的值必須是一個浮點數或Currency值。
f : 用定點小數(Fixed)法形式格式化一個浮點數。Precision確定了要顯示的小數點後的數字個數,缺省值爲2。Args必須是一個浮點數或Currency值。
g  :通用計數法(General)形式格式化一個浮點數。用科學計數法或定點小數法轉換爲最短的十進制顯示格式。Precision確定了要顯示的數字的有效數字個數,默認15位。Args的值必須是一個浮點數或Currency值。
n :Number。採用定點小數的形式並且在每三個一組的整數間加上千位符。Args的值必須是一個浮點數或Currency值。
m  Money. 格式化一個Currency值。Precision確定了小數點後保留多少位,缺省值由CurrencyDecimals 全局變量提供。貨幣格式由CurrencyFormat、NegCurrFormat 決定。這個轉換由參數CurrencyString、CurrencyFormat、NegCurrFormat、ThousandSeparator、DecimalSeparator和CurrencyDecimals全局變量決定,這些初始值都由操作系統的本地設置提供。通過Windows的區域面板來調整。Args的值必須是一個浮點數或Currency值。
p :Pointer. 把指針格式化爲十六進制數。Args必須是一個指針。形式如"XXXX:YYYY", XXXX 是段碼,YYYY 爲偏移量。
s  String. 格式化一個字符或者字串。Precision用來確定至多顯示多少個字符。Args必須是一個字符、字串、或Pchar值。
x  Hexadecimal. 格式化一個無符號十六進制數。Precision用來確定至少顯示多少個字符,不足時補0。Args必須是一個整數。
對於浮點格式,使用小數和千分位符均由全局變量DecimalSeparator和ThousandSeparator獲得
可以在格式字符串中設定好Index, width, 和precision 的值,或者使用星號*。例如‘%*:*.*f’,星號表示Args數組的下一個參數將提供這個數值。
這個Format 函數能與其它格式化函數結合,例如:
S := Format('Your total was %s on %s', [FormatFloat('$#,##0.00;;zero', Total),FormatDateTime('mm/dd/yy', Date)]);

每個字串格式化程序都要使用全局變量separators, decimals, date/time formats 等等,有一個重載的帶有TformatSettings類型參數的等值函數,這個附加參數提供的格式化信息要強於全局變量,請參看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 過程採用Format函數相同的形式格式化一個字串,但他並不返回格式化的字串,而是把它存儲在Result中
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 函數格式化一個字串並把結果保存在Buffer中,必須確認Buffer的長度足夠保存結果。Buffer作爲返回值返回。爲了避免緩衝區益處,請使用StrLFmt函數。
function StrFmt(Buffer, Format: PChar; const Args: array of const): PChar; overload;
function StrFmt(Buffer, Format: PChar; const Args: array of const; const FormatSettings: TFormatSettings): PChar; overload;

StrLFmt 格式一個字串並把結果保存在Buffer中。至多MaxLen個字節將寫入Buffer中,而且不包括結尾的#0字節,#0字節通常用來加載一個格式化了的字串後面。返回Buffer值
function StrLFmt(Buffer: PChar; MaxBufLen: Cardinal; Format: PChar; const Args: array of const): PChar; overload;
function StrLFmt(Buffer: PChar; MaxBufLen: Cardinal; Format: PChar;const Args: array of const; const FormatSettings: TFormatSettings): PChar; overload;

{ FormatBuf 格式化一個字串並把結果保存在Buffer中。最多BufLen個字節將被寫進Buffer中,包括結尾的#0字節。格式字串的長度由FmtLen給出。在Buffer中結果字串的實際長度被返回。返回值總是小於等於BufLen。
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;

WideFormat 格式化WideString字串。作用與Format相同
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 格式化WideString字串,結果返回到Result中,其餘與FmtStr相同。
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 格式化一個WideString字串並把結果保存在Buffer中。最多BufLen個UNICODE字符將被寫進Buffer中,包括結尾的#0字符。格式UNICODE字串的長度由FmtLen給出。在Buffer中結果UNICODE字串的實際長度被返回。返回值總是小於等於BufLen。注意: BufLen, FmtLen 和返回值總是UNICODE字符的長度,不是字節數。 要計算實際的字節數用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;

10) 浮點轉換程序
每個浮點轉換程序都使用全局變量來表示小數位、分離符等格式,也有一個等價的多了一個TformatSettings參數的重載函數。這個附加參數所提供的格式信息要強於全局變量。

FloatToStr 轉換浮點數爲字串表示。這個轉換使用15位通用數字(general number)格式,更詳細的描述見FloatToStrF函數
function FloatToStr(Value: Extended): string; overload;
function FloatToStr(Value: Extended; const FormatSettings: TFormatSettings): string; overload;

CurrToStr 轉換currency 值爲字串格式,使用通用數字(general number)格式,更詳細的信息見CurrToStrF 函數
function CurrToStr(Value: Currency): string; overload;
function CurrToStr(Value: Currency; const FormatSettings: TFormatSettings): string; overload;

將會排列一個數值使其落在有效可接受的幣值範圍之內
常量定義
Windows平臺:
MinCurrency: Currency = -922337203685477.5807;  //!! overflow?
MaxCurrency: Currency =  922337203685477.5807;  //!! overflow?
Linux平臺
 MinCurrency: Currency =+ 1  //!! overflow?
 MaxCurrency: Currency =- 1 //!! overflow?
function FloatToCurr(const Value: Extended): Currency;
function TryFloatToCurr(const Value: Extended; out AResult: Currency): Boolean;

FloatToStrF 轉換浮點值爲格式化了的字串。Format參數決定這個數字是如何轉換的。Precision 參數決定有效數字的個數,Single數是7位,Double數是15位, Extended數是18位。Digits參數依賴選定的Format表達不同的意義。
以下是Format參數可能的值:
ffGeneral – 通用數字格式(General number)。以使字串最短的原則採用定點小數或科學計數法的格式來表示數字:儘可能的使用定點小數計數法,不行的話再用科學計數法。尾部的0都被刪除,若沒有小數則小數點也被刪除。若整數部分小於等於指定的精度(Precision)採用定點格式,超過Precision 或者小於0.00001則採用科學計數法。Digits參數指定指數最小位數(0-4)。
ffExponent – 科學計數法。轉換出形如"-d.ddd...E+dddd"的字串。在小數點前總是有一位數字。數字總長度用Precision來限定,指數以"E"開頭其後跟一個最多不超過4位的正負數,Digits決定了指數的位數。
ffFixed – 定點格式。轉換出形如"-ddd.ddd..."的字串。小數點之前至少有一位數字,小數位數由Digits參數指定(必須介於0-18之間)。若整數部分多於Precision個數位則結果會變爲科學計數法表示(ffExponent格式)
ffNumber – 數字格式(Number)。轉換出形如"-d,ddd,ddd.ddd..."的字串。ffNumber格式符合ffFixed格式,只是結果字串中包含千分位符。
ffCurrency – 貨幣格式(Currency)。轉換爲貨幣格式的字串。其受全局變量CurrencyString、 CurrencyFormat、NegCurrFormat、ThousandSeparator和DecimalSeparator的影響,這些全局變量的初值由操作系統的本地設置來提供,如Windows下的區域面板,小數位數由Digits決定(0-18)。

對於所有格式符,均由全局變量DecimalSeparator 和ThousandSeparator來決定小數和千分位的分離符。
若給定值是NAN ,則結果字串是'NAN'。若給定值是正無窮則結果字串是'INF'。若給定值是負無窮則結果字串是'-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 轉換貨幣值爲其字串形式。其使用方法與FloatToStrF相同,只是其最長精度爲19位(小數點後19位小數)
function CurrToStrF(Value: Currency; Format: TFloatFormat; Digits: Integer): string; overload;
function CurrToStrF(Value: Currency; Format: TFloatFormat; Digits: Integer; const FormatSettings: TFormatSettings): string; overload;

FloatToText 使用指定的格式、精度、位數轉換浮點值爲其字符數組形式。這是底層函數。值參數必須是一個Extended 或Currency參數的變量,由ValueType參數指出。結果字串被存儲在Buffer中並被返回,Buffer要有足夠大。結果字串沒有NULL終結符,更詳細的描述參看FloatToStrF函數
function FloatToText(BufferArg: PChar; const Value; ValueType: TFloatValue; Format: TFloatFormat; Precision, Digits: Integer): Integer; overload;
function FloatToText(BufferArg: PChar; const Value; ValueType: TFloatValue; Format: TFloatFormat; Precision, Digits: Integer; const FormatSettings: TFormatSettings): Integer; overload;

FormatFloat 使用Format參數來格式化一個浮點值,它包含三部分,用分號隔開,三個部分均是可選的。只有一部分時,格式符應用於所有值;只有兩部分時,第一部分應用於正數和0,第二部分應用於負數;三部分都有時,第一部分應用於正數,第二部分應用於負數,第三部分應用於0。如果負數和0值部分是空的,也就是在界定符分號之間沒有內容,則用正數值部分代替。如果正數值部分是空的或者全部格式符都是空的,則使用15位有效數字的通用浮點格式(general floating-point),也就是FloatToStrF 調用ffGeneral 格式。如果小數點左邊超過18位並且沒有指定科學計數法符號也會使用通用浮點格式。每部分的格式標識符描述了一個格式或者直接表示出一個數值,這個數值可以四捨五入到小數點後相應位數。對於0標識符,格式符中小數點兩側的0的個數決定了輸出字串的的精度。以下是Format參數:
0 :數字佔位符。如果這個位置需要一個數字的話,格式化一個數字;否則顯示0。
# :數字佔位符。若這位置需要一個數字則格式化成一個數字。
.  :小數點(由全局變量DecimalSeparator決定形式)。第一個小數點決定了格式化字串中小數點的位置(其後的均無效)。若格式中沒有包含小數點標識符,數值將被四捨五入爲整數。
,  :千分位符(由全局變量ThousandSeparator決定),整數部分每隔三個數字就使用一次這個千分位符,這個標識符可以被包含在一個格式中的任何部分
E+、E-、e+、e- :科學計數符號。不同的標識符代表不同的指數表現形式。'E+'和'e+'表示對於正指數要顯示出正號,'E-'和'e-'則表示省略正指數的正號。
'xx'、"xx" :引號裏的字符被原封不動的複製爲格式化後的字串(去掉引號之後)。可以在引號裏使用其它格式的標識符。
;  : 隔開格式化標識符中的整數、負數和0三部分。
以下是一些事例:
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 根據Format參數來格式化Currency值爲一個字串。詳細的用法見FormatFloat 函數。
function FormatCurr(const Format: string; Value: Currency): string; overload;
function FormatCurr(const Format: string; Value: Currency; const FormatSettings: TFormatSettings): string; overload;

FloatToTextFmt 轉換根據Format所確定的格式對一個浮點值進行格式化。Value參數必須是Extended或Currency類型,由ValueType參數指明。結果字串被包存在Buffer中並且返回字符個數。結果字串沒有#0終結符。其它參看FormatFloat函數
function FloatToTextFmt(Buf: PChar; const Value; ValueType: TFloatValue; Format: PChar): Integer; overload;
function FloatToTextFmt(Buf: PChar; const Value; ValueType: TFloatValue; Format: PChar; const FormatSettings: TFormatSettings): Integer; overload;

StrToFloat 轉換字串爲浮點值。這個字串必須是由可選的正負號、小數點、E、e和數字組成。字串的首位空格被忽略。由全局變量DecimalSeparator決定小數點的形式,字串中不許有千分位符和貨幣符號。字串不是有效的數字形式則產生一個EconvertError異常
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 轉換字串爲Currency類型。其餘同StrToFloat函數
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 轉換Buffer中有Null終結符的字串爲浮點數並返回給變量Value。Value參數必須是Extended或Currency類型, 且與ValueType參數相匹配。若轉換成功返回True,否則返回False。更詳細表述參看StrToFloat函數。
function TextToFloat(Buffer: PChar; var Value; ValueType: TFloatValue): Boolean; overload;
function TextToFloat(Buffer: PChar; var Value; ValueType: TFloatValue; const FormatSettings: TFormatSettings): Boolean; overload;

FloatToDecimal 轉換浮點數爲十進制格式。是底層的格式化程序,其它格式化函數會調用它,但是一般應用程序很少直接調用它。Value參數必須是Extended或Currency類型,而且要有ValueType參數來指明。對於Extended類型的值,Precision參數指明有效數字的位數,在1-18位之間。對於Currency類型,Precision被忽略,並且默認爲19位。Decimals指明整數部分的位數。Precision和Decimals參數一起控制如何舍入。不管數字有多大,爲了產生有效的結果,爲Decimals參數指定9999。結果被存儲在TfloatRec記錄類型中。
以下是TfloatRec類型成員:
Exponent – 指數。若數字絕對值小於1則指數爲負數。若數字是NAN (非數)則指數爲-32768。若數字爲INF 或-INF (正負無窮)則指數爲32767。
Negative - 若數字爲負數則爲True 。若爲0或正數則是False。
Digits – 包含Null終結符的最多18(Extended)或19 (Currency)位的有效數字。小數點不被存儲其中。尾部的0被刪除,若結果數字是0、NAN、或INF,Digits則爲空但是存有Null終結符。
procedure FloatToDecimal(var Result: TFloatRec; const Value; ValueType: TFloatValue; Precision, Decimals: Integer);

11) 日期/時間支持程序
function DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;

function TimeStampToDateTime(const TimeStamp: TTimeStamp): TDateTime;
function MSecsToTimeStamp(MSecs: Comp): TTimeStamp;
function TimeStampToMSecs(const TimeStamp: TTimeStamp): Comp;

EncodeDate 編碼日期Year, Month和Day爲TdateTime類型值。Year必須介於1-9999之間;Month 必須介於1-12之間;Day必須介於1-N之間,N是指定月的最大數。若超出上述範圍則產生一個EconvertError異常。結果值是介於12/30/1899和給定日期之間的天數
function EncodeDate(Year, Month, Day: Word): TDateTime;

{ EncodeTime 編碼時間Hour, Min(ute), Sec(ond)和MilliSecond爲TdateTime值。Hour必須介於0-23之間;Minute必須介於0-59之間,;Second必須介於0-59之間;MilliSecond必須介於0-999之間。若超出上述範圍則產生一個EconvertError異常。結果值是介於0-1(不包括1)之間時間值,這個值指的是佔全天時間的比值(總是小於1)。0表示是午夜,0.5表示是中午,0.75表示是下午6:00
function EncodeTime(Hour, Min, Sec, MSec: Word): TDateTime;

代替EncodeDate和EncodeTime函數,但不產生異常。當給定的值有效是返回True,否則返回False。其餘完全相同
function TryEncodeDate(Year, Month, Day: Word; out Date: TDateTime): Boolean;
function TryEncodeTime(Hour, Min, Sec, MSec: Word; out Time: TDateTime): Boolean;

DecodeDate 解碼日期DateTime值爲年Year、月Month、日Day輸出。若給定的日期值小於0則輸出全爲0。
procedure DecodeDate(const DateTime: TDateTime; var Year, Month, Day: Word);

這個函數與DecodeDate功能類似,但是返回更多的信息。DOW是星期。函數返回值指示出Year是否是閏年。
function DecodeDateFully(const DateTime: TDateTime; var Year, Month, Day, DOW: Word): Boolean;
function InternalDecodeDate(const DateTime: TDateTime; var Year, Month, Day, DOW: Word): Boolean; //用於Linux環境

DecodeTime 解碼給定時間值DaTime爲時Hour、分Min、秒Sec、毫秒MSec輸出。
procedure DecodeTime(const DateTime: TDateTime; var Hour, Min, Sec, MSec: Word);

{以下用於MsWindows平臺}
DateTimeToSystemTime將Delphi的TdateTime值的日期和時間轉換爲Win32 API的TsystemTime格式
procedure DateTimeToSystemTime(const DateTime: TDateTime; var SystemTime: TSystemTime);

SystemTimeToDateTime 將Win32 API的日期和時間轉換爲Delphi的TdateTime格式。
function SystemTimeToDateTime(const SystemTime: TSystemTime): TDateTime;
{$ENDIF}

DayOfWeek 返回給定日期是星期幾。結果是介於1-7的整數,對應星期日到星期六。這個函數不是適應ISO 8601的,具體請參看DateUtils單元
function DayOfWeek(const DateTime: TDateTime): Word;

Date 返回當前日期
function Date: TDateTime;

Time 返回當前時間。
function Time: TDateTime;
{以下用於Linux環境}
{ clashes with Time in <X11/Xlib.h>, use GetTime instead }
{$EXTERNALSYM Time}
{$ENDIF}
function GetTime: TDateTime;

Now 返回當前的日期和時間。
function Now: TDateTime;

Currentyear 返回當前日期中的年
function CurrentYear: Word;

IncMonth 給DateTime參數加上NumberOfMonths個月並返回結果。若NumberOfMonths參數爲負,將返回N月之前的日期數。若日期數大於該月的最大日期數,則自動設爲該月的最大日期數。輸入的時間數自動複製進DateTime結果中。
function IncMonth(const DateTime: TDateTime; NumberOfMonths: Integer = 1): TDateTime;

直接用年月日表示的IncMonth函數的優化版本,關於NumberOfMonths可以參照IncMonth函數的註釋。
procedure IncAMonth(var Year, Month, Day: Word; NumberOfMonths: Integer = 1);

ReplaceTime 用給定的時間值替換DateTime參數的時間部分,若日期在1900年之前(日期值小於0)就需要調整這個標記。
procedure ReplaceTime(var DateTime: TDateTime; const NewTime: TDateTime);

ReplaceDate用給定的日期值替換DateTime參數的日期部分,若日期爲負數就需要調整這個標記。
procedure ReplaceDate(var DateTime: TDateTime; const NewDate: TDateTime);

IsLeapYear 判斷Year是否是閏年。
function IsLeapYear(Year: Word): Boolean;

type
PDayTable = ^TDayTable;
TDayTable = array[1..12] of Word;
MonthDays數組能很快地找出每個月的天數。這是一個二維數組,第一位是布爾類型,閏年表示爲True,否則爲False。第二維表示月份,,範圍是1-12。
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));

每個日期/時間格式化程序都使用separators、decimals等全局變量,同時也有一個等價的具有TformatSettings類型參數的重載函數。這個附加的參數提供了比全局變量強的格式化信息,具體參看TformatSettings參數。

DateToStr 轉換給定TdateTime值的日期部分爲一個字串。這個轉換格式由全局變量ShortDateFormat決定。
function DateToStr(const DateTime: TDateTime): string; overload;
function DateToStr(const DateTime: TDateTime;const FormatSettings: TFormatSettings): string; overload;

TimeToStr 轉換給定TdateTime值的時間部分爲一個字串 ,使用全局變量LongTimeFormat 來決定格式
function TimeToStr(const DateTime: TDateTime): string; overload;
function TimeToStr(const DateTime: TDateTime; const FormatSettings: TFormatSettings): string; overload;

DateTimeToStr 轉換給定日期和時間爲字串。結果字串的形式由全局變量ShortDateFormat和LongTimeFormat決定。 給定的值的小數部分不爲0則輸出字串包含時間部分。
function DateTimeToStr(const DateTime: TDateTime): string; overload;
function DateTimeToStr(const DateTime: TDateTime; const FormatSettings: TFormatSettings): string; overload;

StrToDate 轉換字串爲日期值。這個字串必須由2或3個數字及分離符組成,分離符的類型由全局變量DateSeparator決定。月日年的次序由全局變量ShortDateFormat決定,可能的形式有月日年m/d/y、日月年d/m/y、和年月日y/m/d。若字串由兩個數字組成,則被解釋成當年的某個日期(月/日或日/月)。Year必須介於0-99之間並且假定處於當前的世紀中;若給定的字串不是有效的日期則會產生EconvertError異常
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 轉換字串爲時間值。字串必須是由2或3個數字及分離符組成。分離符由全局變量TimeSeparator決定。AM或PM指示符可選,按時、分、秒(可選)順序。若時間後跟隨AM或PM指示符則被假定爲12小時格式,否則爲24小時格式。若給定字串不是有效的時間格式則產生EconvertError異常。
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 轉換字串爲日期時間值,字串必須包含一個日期值和可選的尾隨其後時間值。字串的日期和時間部分必須遵循由StrToDate和StrToTime函數描述的格式。
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 使用Format參數格式化日期-時間值爲一個字串。以下是Format參數指示符:
 c  日期採用的格式是全局變量ShortDateFormat指明的,接着是一個空格,再接下來是一個採用全局變量LongTimeFormat表示的時間。若正好是0點,則時間信息省略。
d       顯示天時沒有前導0 (1-31)。
dd      顯示天時有前導0 (01-31)。
ddd     使用全局變量ShortDayNames規定的格式來表示星期幾,即縮寫詞(Sun-Sat)。
dddd    使用全局變量LongDayNames 規定的格式來表示星期幾,即全名稱(Sunday-Saturday)
ddddd   使用全局變量ShortDateFormat規定的格式來表示日期信息。
dddddd  使用全局變量LongDateFormat規定的格式來表示日期信息。
g       採用短紀年名格式(只對於日本和臺灣)。
gg      採用全紀年名格式。
e       在當前的紀年方式下格式化年代信息,同時去掉開頭的0(只用於韓國、日本和臺灣)。
ee      在當前的紀年方式下格式化年代信息,同時保留開頭的0,(只用於日本、韓國和臺灣)。
m      格式化月份信息並且去除前導0(1-12)。若m特定符緊緊跟隨一個h或hh特定符,則分鐘先於月份顯示。
mm      格式化月份信息並保留前導0(01-12)。若mm特定符緊緊跟隨一個h或hh特定符,則分鐘先於月份顯示。
mmm     由全局變量ShortMonthNames 規定的格式顯示月份,即縮寫詞 (Jan-Dec)
mmmm   由全局變量LongMonthNames規定的格式顯示月份,即全名稱 (January-December)
yy      顯示兩位數的年 (00-99)。
yyyy    顯示四位數的年 (0000-9999)。
h       格式化小時信息並且去除前導0 (0-23)。若格式化字串中包含AM/PM標識符時,則採用12小時表示法。
hh      格式化小時信息並且保留前導0 (00-23)。若格式化字串中包含AM/PM標識符時,則採用12小時表示法。
n       顯示分鐘信息並去除前導0 (0-59)。
nn      顯示分鐘信息並保留前導0 (00-59)。
s       格式化秒信息並去除前導0 (0-59)。
ss      格式化秒信息並保留前導0 (00-59)。
z       格式化毫秒信息並去除前導0 (0-999)。
zzz     格式化毫秒信息並去除前導0 (000-999)
t       用全局變量ShortTimeFormat規定的格式顯示時間信息。
tt      用全局變量LongTimeFormat規定的格式顯示時間信息。
am/pm   爲前面的h或hh特定符顯示爲12小時制。從午夜開始到中午之前顯示am,在中午及中午以後顯示pm。至於am/pm的大小寫則與格式化字串中的寫法一致
a/p     用法與am/pm一致,只是顯示爲a或p。
ampm   用法與am/pm一致。只是上午顯示的字符是由全局變量TimeAMString規定的(am),下午顯示的字符由全局變量TimePMString規定(pm)。
/       顯示由全局變量DateSeparator規定的日期分離符
:       顯示由全局變量TimeSeparator規定的時間分離符。
'xx'、"xx"    直接複製引號內的字符(不包括引號)。

格式化字符不區分大小寫(除了am/pm和a/p標誌)。其它字符被原樣複製。若Format是空字串,則缺省的格式爲c。以下是一些事例:

S := FormatDateTime('"The meeting is on" dddd, mmmm d, yyyy, ' +'"at" hh:mm AM/PM', StrToDateTime('2/15/95 10:30am'));
S字串顯示爲:The meeting is on Wednesday, February 15, 1995 at 10:30 AM
function FormatDateTime(const Format: string;DateTime: TDateTime): string; overload;
function FormatDateTime(const Format: string; DateTime: TDateTime;const FormatSettings: TFormatSettings): string; overload;

DateTimeToString 轉換DateTime的日期時間爲一個字串並通過變量Result返回。其它詳細描述參看FormatDateTime函數。
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 轉換一個有效的數值爲一個合理的日期。
定義常量:
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;

12) 系統錯誤信息

返回錯誤碼ErrorCode的文本。應用程序應該報告的是Windows的錯誤文本而不是代碼。Ewin32Error類型在他的異常信息中自動包含了錯誤消息文本內容,但是EinOutError則沒有提供Windows錯誤碼的格式化。
function SysErrorMessage(ErrorCode: Integer): string;

13) 初始化文件支持
在Windows環境中Locale中查找LocaleType的值,並返回結果字串。若Locale或LocaleType無效則返回Default值。GetFormatSettings函數會調用這個函數以載入一些本地化變量。
function GetLocaleStr(Locale, LocaleType: Integer; const Default: string): string; platform;
function GetLocaleChar(Locale, LocaleType: Integer; Default: Char): Char; platform;

GetFormatSettings 從操作系統中獲取數值來恢復全部本地變量(日期、時間、數字和貨幣的格式),一個圖形用戶界面GUI應用程序會在這些格式化設置被更改後自動調用這個函數(也就是在接收到Wm_WinIniChange消息後)。
GetFormatSettings設置的變量:
CurrencyDecimals:貨幣值的位數;
CurrencyFormat:正的貨幣值表示格式;
CurrencyString:標誌貨幣值的符號;
DateSepararor:隔開日期個部分的分隔符;
DecimalSeparator:隔開整數和小數部分的分隔符(小數點);
EraName:亞洲地區使用年代的名字;
EraYearOffsets:亞洲地區使用年代的年份;
ListSeparator:在一個列表中隔開各個項目的分隔符;
LongDayNames:每週各星期的全名錶示;
LongMonthNames:每個月份的全名錶示;
LongTimeFormat:時間的長格式;
NegCurrFormat:負貨幣值的表示格式;
ShortDateFormat:日期的簡短格式;
ShortDayName:每週各星期的簡略表示;
ShortMonthNames:每個月份的簡略表示;
ShortTimeFormat:時間的簡短格式;
ThousandSeparator:千分位符;
TimeAMString:表示上午的字符串;
TimeP MString:表示下午的字符串;
TimeSeparator:時間的分隔符。
procedure GetFormatSettings;

GetLocaleFormatSettings 用操作系統的值來加載到指定區域(LCID)的本地變量(date, time, number,currency formats)中。 這個值被儲存在FormatSettings記錄中
{以下用於MsWindows平臺}
procedure GetLocaleFormatSettings(LCID: Integer; var FormatSettings: TFormatSettings);
{$ENDIF}

14) 異常處理程序

{以下用於Linux環境}
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 用於線程內部的支持,你自己不能調用這個過程。
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 被用於內部適當的控制FPU 並且適當的產生一個外部OS異常對象。你自己不能調用這個過程。
procedure SignalConverter(ExceptionEIP: LongWord; FaultAddr: LongWord; ErrorCode: LongWord);

看看下面的線程變量聲明的註釋。只能使用以下過程和函數來訪問線程變量,因爲你不能越過包來使用線程變量。
procedure SetSafeCallExceptionMsg(const Msg: String);
procedure SetSafeCallExceptionAddr(Addr: Pointer);
function GetSafeCallExceptionMsg: String;
function GetSafeCallExceptionAddr: Pointer;

HookOSExceptionsProc 只限於內部使用而不能在常規場合使用,並且不能不斷的設置這個變量
變量聲明:
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}

暫停程序運行milliseconds 毫秒,milliseconds參數在Windows中指的是毫秒,在Linux中則指的是秒。在Windows中這個函數直接調用系統API函數。
procedure Sleep(milliseconds: Cardinal);{$IFDEF MSWINDOWS} stdcall; {$ENDIF}
{以下用於MsWindows平臺}
(*$EXTERNALSYM Sleep*)
{$ENDIF}

function GetModuleName(Module: HMODULE): string;

爲了一個異常而格式化一個字符串。把異常對象和地址作爲開始的兩個參數。字符串緩衝區和它的大小是接下來的兩個參數。函數返回格式化後的字符串的長度。異常信息包含了模塊名、異常地址、異常類型名以及異常消息(若異常對象是從Exception中繼承而來的)。
function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer; Buffer: PChar; Size: Integer): Integer;

顯示一個異常消息。在控制檯應用程序中,異常消息通常寫入Output文件中,消息則顯示在一個對話框中。至於對話框的文本內容則通過調用ExceptionErrorMessage來獲得。
procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);

報告一個Eabort異常。GUI應用程序將忽略掉這個異常,因此可以調用Abort過程來報告一個用戶看不見的異常。
procedure Abort;

報告一個EOutOfMemory異常。SysUtils單元會預先分配一個單獨的EOutOfMemory對象,這裏也使用到了OutOfMemory過程。免得想要分配空間給一個新的異常對象時卻發現沒有可分配的空間了。當你正在寫一個內存管理器時,不需要調用OutOfMemory過程來報告一個錯誤,相應的,只用確認內存分配或者重分配函數返回nil,其它的工作Delphi會處理。
procedure OutOfMemoryError;

調用Windows的API函數MessageBeep(0),它播放的是系統聲音。
procedure Beep;

15) MBCS 函數

LeadBytes 變量定義多字節字符集中(日本、中國等)字符的用於指示的前導字節,應根據本地代碼表來設置這個變量。對於西文國家來說,這個設置總是空的。
變量聲明
LeadBytes: set of Char = [];

ByteType 返回字串S中第Index個位置上的字節類型。對於西文國家總是返回mbSingleByte。遠東多字節地區可能返回mbLeadByte,指出這是多字節字符的首字節。而返回值mbTrailByte,則指出是首字節之後的字節。首字節之後可能有一個或多個字節跟隨,這取決於本地的字符集編碼和操作系統平臺。Index參數總是有效的,這意味着若其越界則結果變得不可預知。
function ByteType(const S: string; Index: Integer): TMbcsByteType;

StrByteType 與ByteType函數類似,只是用於有Null終結符的Pchar字串。
function StrByteType(Str: PChar; Index: Cardinal): TMbcsByteType;

ByteToCharLen 返回MBCS字串的字符數目,掃描長度爲MaxLen個字節。在多字節字符集中,字符的長度小於字節的長度。
function ByteToCharLen(const S: string; MaxLen: Integer): Integer;

CharToByteLen 返回MBCS字串S的字節長度,掃描MaxLen個字符。
function CharToByteLen(const S: string; MaxLen: Integer): Integer;

ByteToCharIndex 返回MBCS字串指定的第Index個字節處所對應的字符索引值,索引以1開始。若Index超出範圍((Index <= 0)或(Index > Length(S)))則返回0。
function ByteToCharIndex(const S: string; Index: Integer): Integer;

CharToByteIndex返回MBCS字串指定的第Index個字符處所對應的字節索引值,索引以1開始。若Index超出範圍((Index <= 0)或(Index > Length(S))或(Result可能> Length(S)))則返回0。
function CharToByteIndex(const S: string; Index: Integer): Integer;

StrCharLength 返回Str中第一個字符的字節數。在Windows中,多字節字符的長度是2個字節。在Linux中,多字節字符長度是6個字節(UTF-8)。
function StrCharLength(const Str: PChar): Integer;

StrNextChar 返回Str字串指針位置後面的字符指針,即下一個字符的指針。
function StrNextChar(const Str: PChar): PChar;

CharLength 返回字串S中第Index字節處的字符的字節數
function CharLength(const S: String; Index: Integer): Integer;

NextCharIndex 返回字串S第Index字符後面的字符的首字節的索引值,即下一個字符的索引值。
function NextCharIndex(const S: String; Index: Integer): Integer;

IsPathDelimiter 若在字節S[Index]處的字符是一個路徑分隔符('/' 或'/')則返回True,並且不是MBCS頭或尾字節。
function IsPathDelimiter(const S: string; Index: Integer): Boolean;

IsDelimiter 若在字節S[Index]出於任何分隔符相匹配則返回True,並且不是MBCS頭或尾字節。字串S可以包含多字節字符,分隔符一定是單字節字符。
function IsDelimiter(const Delimiters, S: string; Index: Integer): Boolean;

IncludeTrailingPathDelimiter 返回一個結尾有路徑分隔符PathDelimiter(/或/)的路徑,這函數是MBCS纔有的。
function IncludeTrailingPathDelimiter(const S: string): string;

IncludeTrailingBackslash 是函數IncludeTrailingPathDelimiter的舊名稱。
function IncludeTrailingBackslash(const S: string): string; platform;

ExcludeTrailingPathDelimiter返回一個結尾沒有有路徑分隔符PathDelimiter(/或/)的路徑,這函數是MBCS纔有的。
function ExcludeTrailingPathDelimiter(const S: string): string;

ExcludeTrailingBackslash 是函數ExcludeTrailingPathDelimiter的舊名稱
function ExcludeTrailingBackslash(const S: string): string; platform;

LastDelimiter 在整個字串S中匹配任何一個Delimiters分隔符(除了NULL(#0)),返回最右側的分隔符的字節索引值,即最後的那個分隔符的字節索引。S可以包含多字節字符,分隔符一定是單字節的並且不是Null字符。例如:LastDelimiter('/.:', 'c:/filename.ext') 返回值是12.
function LastDelimiter(const Delimiters, S: string): Integer;

AnsiCompareFileName文件名的比較。支持Windows下的全長度日本字符(Zenkaku)的DOS文件名,這種情況下,ASII字符將佔用兩個字節。在Windows非MBCS下,此函數與AnsiCompareText (區分大小寫)相同。在Linux下,與AnsiCompareStr (區分大小寫)相同。對於普通文件名比較,應當使用函數AnsiCompareText。
function AnsiCompareFileName(const S1, S2: string): Integer;

function SameFileName(const S1, S2: string): Boolean;

AnsiLowerCaseFileName文件名轉換爲小寫。支持在Windows下的全長度日本字符(Zenkaku)的DOS文件名,這種情況下,ASII字符將佔用兩個字節。在非MBCS下,此函數與AnsiLowerCase相同。
function AnsiLowerCaseFileName(const S: string): string;

AnsiUpperCaseFileName 文件名轉換爲大寫。支持在Windows下的全長度日本字符(Zenkaku)的DOS文件名,這種情況下,ASII字符將佔用兩個字節。在非MBCS下,此函數與AnsiUpperCase相同。
function AnsiUpperCaseFileName(const S: string): string;

AnsiPos:  與Pos函數功能一樣只是支持MBCS。
function AnsiPos(const Substr, S: string): Integer;

AnsiStrPos: 與StrPos函數功能一樣只是支持MBCS。
function AnsiStrPos(Str, SubStr: PChar): PChar;

AnsiStrRScan: 與StrRScan函數功能一樣只是支持MBCS。
function AnsiStrRScan(Str: PChar; Chr: Char): PChar;

AnsiStrScan: 與StrScan函數功能一樣只是支持MBCS。
function AnsiStrScan(Str: PChar; Chr: Char): PChar;

StringReplace 在字串S中,用newpattern 字串替換其中的oldpattern字串(區分大小寫)。S可以包含多字節字符。Flags屬性是rfReplaceAll則替換所有的oldpattern。若是rfIgnoreCase則比較時忽略大小寫
type
TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase);
function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;

WrapText 在字串Line中掃描字符BreakChars;並且在最後的BreakChar位置處(在MaxCol之前)插入字串BreakStr,但是不能插入到引號串內(包括單引號和雙引號)。
function WrapText(const Line, BreakStr: string; const BreakChars: TSysCharSet; MaxCol: Integer): string; overload;
function WrapText(const Line: string; MaxCol: Integer = 45): string; overload;

FindCmdLineSwitch 決定Switch參數怎樣作爲命令行參數傳遞到應用程序。witchChars參數指出哪個字符爲命令分隔符(通常是-/)。The IgnoreCase 參數爲False則表明Switch參數必須完全相同(區分大小寫),否則忽略大小寫
常量定義:
SwitchChars = ['/','-'];  //Windows下
SwitchChars = ['-'];  //Linux下
function FindCmdLineSwitch(const Switch: string; const Chars: TSysCharSet; IgnoreCase: Boolean): Boolean; overload;

這是函數FindCmdLineSwitch的簡化版。有效的命令行開關字符在不同的平臺下會改變,例如在Linux下'/' 不能被作爲開關字符因爲他是一個路徑分隔符。這個函數Switch參數區分大小寫(相當於前的函數的IgnoreCase屬性爲False)。
function FindCmdLineSwitch(const Switch: string): Boolean; overload;

這個SwitchChars定義與上面相同
function FindCmdLineSwitch(const Switch: string; IgnoreCase: Boolean): Boolean; overload;

FreeAndNil 釋放Obj對象實例並且設置其變量爲Nil。 
procedure FreeAndNil(var Obj);

16) 接口支持程序

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;
function CreateGUID(out Guid: TGUID): HResult; stdcall; //Windows
function StringToGUID(const S: string): TGUID;
function GUIDToString(const GUID: TGUID): string;
function IsEqualGUID(const guid1, guid2: TGUID): Boolean;
{以下用於MsWindows平臺}
stdcall;  {$EXTERNALSYM IsEqualGUID}
{$ENDIF}

17) 包支持程序

包信息標記
常量定義
pfNeverBuild = $00000001; //外部建立
pfDesignOnly = $00000002; //設計時刻包程序
pfRunOnly = $00000004; //運行時包程序
pfIgnoreDupUnits = $00000008; //不檢查是否有重複的單元
pfModuleTypeMask = $C0000000; //模塊類型的掩碼
pfExeModule = $00000000;  //可執行程序(在Delphi5中是$C0000000)
pfPackageModule = $40000000; //包程序DLL
pfProducerMask = $0C000000; //建立本包程序的編譯器的標誌掩碼
pfV3Produced =  $00000000; //Delphi3或者Builder3
pfProducerUndefined = $04000000; //未知的編譯器
pfBCB4Produced = $08000000; //C++Builder4或者更新的版本
pfDelphi4Produced = $0C000000; //Delphi4或者更新的版本
pfLibraryModule = $80000000;  //其他的DLL

單元信息標記
常量定義
ufMainUnit = $01;  //保程序或工程的主要單元
ufPackageUnit = $02;  //包程序源單元(.dpk文件)
ufWeakUnit = $04; //單元是弱連接的
ufOrgWeakUnit = $08; //在其自身定義包程序中被弱連接的單元
ufImplicitUnit = $10; //單元隱含的連接

ufWeakPackageUnit = ufPackageUnit or ufWeakUnit;

Var   PkgLoadingMode: Integer = RTLD_LAZY;  //Linux

回收過程類型給GetPackageInfo函數使用。Name是包元素的實際名字,若IsUnit是True則Name是被包含單元的名字,若是False則是必須的包。Param被傳送到函數GetPackageInfo。
類型(type)定義:
TNameType = (ntContainsUnit, ntRequiresPackage, ntDcpBpiName);
TPackageInfoProc = procedure (const Name: string; NameType: TNameType; Flags: Byte; Param: Pointer);

LoadPackage 加載一個包程序DLL,檢查是否有重複的單元,並且調用所有被包含單元的初始化塊(initialization)部分。返回最新載入DLL的模塊句柄。若Name指明的程序不正確,則產生一個EpackageError異常。
function LoadPackage(const Name: string): HMODULE;

UnloadPackage 與LoadPackage函數相反,調用所有被包含單元的結束化塊(finalization)卸載包DLL。必須保證程序中不再有不安全的指針,例如對類的引用,對事件的引用或者是對包程序中方法的引用。
procedure UnloadPackage(Module: HMODULE);

GetPackageInfo 訪問給定Module包的信息表並且列舉所有的被包含單元和必須的包。此過程爲一個已載入模塊(程序、庫函數或者是包程序)及相關包程序的所有單元調用InfoProc方法。Param是一個不透明的指針,可以直接傳遞給InfoProc。此過程會把包程序標誌設置爲Flags,這些標誌如上所示。若模塊不是Delphi或者C++Builder模塊,則產生一個GetPackageInfo異常。
procedure GetPackageInfo(Module: HMODULE; Param: Pointer; var Flags: Integer; InfoProc: TPackageInfoProc);

GetPackageDescription 返回由ModuleName指定的模塊的描述資源,若描述資源不存在,則返回空串。若文件不存在或其它錯誤則產生一個EPackageError異常。描述字串通過$D或者$Description編譯指示字來標記,並且會被編譯存儲爲一個名叫DESCRIPTION的RCDATA資源。
function GetPackageDescription(ModuleName: PChar): string;

InitializePackage 確認並初始化給定的包DLL。若包沒有初始化部分,則會報告一個異常。LoadfPackage函數會調用這個過程,因此沒有理由自己調用這個過程。
procedure InitializePackage(Module: HMODULE);

FinalizePackage 結束給定的包DLL。若包沒有結束化部分,則報告一個異常。UnloadPackage函數會調用這個過程,因此沒有理由自己調用這個過程。
procedure FinalizePackage(Module: HMODULE);

RaiseLastOSError 用GetLastError函數找回在調用OS或系統庫函數時發生的最近一次的錯誤碼。若其返回一個錯誤碼,則其將會產生一個由錯誤碼和系統提供的消息組合而成的EOSError異常。
procedure RaiseLastOSError;

{以下用於MsWindows平臺}
procedure RaiseLastWin32Error; deprecated;  // 用RaiseLastOSError

Win32Check 用來檢查Win32 API函數的返回值,返回一個布爾值表明WIN32 API函數能否正常返回。若WIN32 API函數返回False,則Win32Check調用RaiseLastOSError來產生一個異常。若WIN32 API函數返回True,則Win32Check返回True。
function Win32Check(RetVal: BOOL): BOOL; platform;
{$ENDIF}

18) 終止過程支持

類型定義(type):
TTerminateProc = function: Boolean;

Call AddTerminateProc 添加一個終止過程到系統終止過程列表中。Delphi在應用程序終止前調用終止過程列表中所有的函數。若應用程序安全終止則用戶自定義的TermProc函數將返回True,否則返回False。只要終止過程列表中任何函數返回False,應用程序就不會終止。
procedure AddTerminateProc(TermProc: TTerminateProc);

CallTerminateProcs 當一個應用程序正要終止時此函數被VCL調用。如果系統的終止過程列表中的所有函數都返回True,此函數就返回True。 這個函數只能由Delphi來調用,不能被直接調用。
function CallTerminateProcs: Boolean;

function GDAL: LongWord;
procedure RCS;
procedure RPR;


HexDisplayPrefix 變量表示用來作爲十六進制數前綴的字符。對於Pascal是$,對於C++是0x。這個字符只用於顯示,不影響字串-整數轉換程序。
var
HexDisplayPrefix: string = '$';

{以下用於MsWindows平臺}
Win95下的Win32 API函數GetDiskFreeSpace不支持超過2GB的磁盤分區。Win NT4.0和Win95 OSR2中新的Win32 函數GetDiskFreeSpaceEx支持超過2GB的分區。GetDiskFreeSpaceEx 函數指針變量在啓動時被初始化爲指向當前的OS API函數(若系統中有此函數),或者是指向內部的Delphi函數(系統中沒有)。當運行在Win95 pre-OSR2以下版本時,此函數的結果仍侷限於2GB範圍, 指示不用擔心你自己來寫API函數了。
var
GetDiskFreeSpaceEx: function (Directory: PChar; var FreeAvailable, TotalSpace: TLargeInteger; TotalFree: PLargeInteger): Bool stdcall = nil;

SafeLoadLibrary 安全調用LoadLibrary, 即使被請求的文件不能被加載也不會彈出Win32錯誤消息對話框。SafeLoadLibrary也會越過LoadLibrary保護當前FPU控制字 (precision, exception masks) (防止萬一你在DLL的初始化中載入Hammers FPU控制字,也會像MS DLLs一樣執行)
function SafeLoadLibrary(const FileName: string; ErrorMode: UINT = SEM_NOOPENFILEERRORBOX): HMODULE;

{$ENDIF}

{以下用於Linux環境}
SafeLoadLibrary 調用LoadLibrary,越過LoadLibrary保護當前FPU控制字 (精度、異常標誌) (在共享對象中防止萬一你在它的初始化中載入Hammers FPU控制字,也會像MS DLLs一樣執行)
function SafeLoadLibrary(const FileName: string; Dummy: LongWord = 0): HMODULE;
{$ENDIF}

19) 線程同步

IReadWriteSync 是一個爲普通讀寫同步的抽象接口。一些執行程序可以允許同時發生讀取,而寫總是被禁止。最壞的事情是類表現到同樣一個TRTLCriticalSection ,也就是讀和寫鎖定阻礙全部其它線程。
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;

{以下用於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;  // 簡易格式
function GetEnvironmentVariable(const Name: string): string; overload;

{以下用於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}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章