From 67c1c58793d611d6c88968d982e6bba3507923f3 Mon Sep 17 00:00:00 2001 From: mattias Date: Sun, 11 Jun 2023 21:33:01 +0200 Subject: [PATCH] codetools: moved DateToCfgStr to LazConfigStorage --- components/codetools/codecache.pas | 2 +- components/codetools/fileprocs.pas | 124 +++--------------- .../codetools/tests/testbasiccodetools.pas | 2 +- components/ideintf/inputhistory.pas | 3 +- components/lazutils/lazconfigstorage.pas | 110 +++++++++++++++- packager/packagelinks.pas | 2 +- 6 files changed, 130 insertions(+), 113 deletions(-) diff --git a/components/codetools/codecache.pas b/components/codetools/codecache.pas index 8e85cb91e6..1d02c58b6e 100644 --- a/components/codetools/codecache.pas +++ b/components/codetools/codecache.pas @@ -41,7 +41,7 @@ uses // Codetools SourceLog, LinkScanner, FileProcs, DirectoryCacher, // LazUtils - LazFileUtils, LazFileCache, Laz2_XMLCfg, LazDbgLog; + LazFileUtils, LazFileCache, Laz2_XMLCfg, LazDbgLog, LazConfigStorage; const IncludeLinksFileVersion = 2; diff --git a/components/codetools/fileprocs.pas b/components/codetools/fileprocs.pas index cb5f32c3d1..edc665d988 100644 --- a/components/codetools/fileprocs.pas +++ b/components/codetools/fileprocs.pas @@ -44,7 +44,8 @@ uses // CodeTools CodeToolsStrConsts, // LazUtils - LazUtilities, LazLoggerBase, LazFileCache, LazFileUtils, LazUTF8, LazStringUtils; + LazUtilities, LazLoggerBase, LazFileCache, LazFileUtils, LazUTF8, + LazStringUtils, LazConfigStorage; type TFPCStreamSeekType = int64; @@ -141,10 +142,10 @@ const ('', '.inc', '.pp', '.pas'); // store date locale independent, thread safe -const DateAsCfgStrFormat='YYYYMMDD'; -const DateTimeAsCfgStrFormat='YYYY/MM/DD HH:NN:SS'; -function DateToCfgStr(const Date: TDateTime; const aFormat: string = DateAsCfgStrFormat): string; -function CfgStrToDate(const s: string; out Date: TDateTime; const aFormat: string = DateAsCfgStrFormat): boolean; +const DateAsCfgStrFormat=LazConfigStorage.DateAsCfgStrFormat; +const DateTimeAsCfgStrFormat=LazConfigStorage.DateTimeAsCfgStrFormat; +function DateToCfgStr(const Date: TDateTime; const aFormat: string = DateAsCfgStrFormat): string; deprecated 'use LazConfigStorage'; +function CfgStrToDate(const s: string; out Date: TDateTime; const aFormat: string = DateAsCfgStrFormat): boolean; deprecated 'use LazConfigStorage'; procedure CTIncreaseChangeStamp(var ChangeStamp: integer); inline; procedure CTIncreaseChangeStamp64(var ChangeStamp: int64); inline; @@ -263,6 +264,17 @@ uses Unix; {$ENDIF} +function DateToCfgStr(const Date: TDateTime; const aFormat: string): string; +begin + Result:=LazConfigStorage.DateToCfgStr(Date,aFormat); +end; + +function CfgStrToDate(const s: string; out Date: TDateTime; + const aFormat: string): boolean; +begin + Result:=LazConfigStorage.CfgStrToDate(s,Date,aFormat); +end; + procedure CTIncreaseChangeStamp(var ChangeStamp: integer); begin LazFileCache.LUIncreaseChangeStamp(ChangeStamp); @@ -1427,108 +1439,6 @@ begin end; end; -function DateToCfgStr(const Date: TDateTime; const aFormat: string): string; -var - NeedDate: Boolean; - NeedTime: Boolean; - Year: word; - Month: word; - Day: word; - Hour: word; - Minute: word; - Second: word; - MilliSecond: word; - p: Integer; - w: Word; - StartP: Integer; - s: String; - l: Integer; -begin - Result:=aFormat; - NeedDate:=false; - NeedTime:=false; - for p:=1 to length(aFormat) do - case aFormat[p] of - 'Y','M','D': NeedDate:=true; - 'H','N','S','Z': NeedTime:=true; - end; - if NeedDate then - DecodeDate(Date,Year,Month,Day); - if NeedTime then - DecodeTime(Date,Hour,Minute,Second,MilliSecond); - p:=1; - while p<=length(aFormat) do begin - case aFormat[p] of - 'Y': w:=Year; - 'M': w:=Month; - 'D': w:=Day; - 'H': w:=Hour; - 'N': w:=Minute; - 'S': w:=Second; - 'Z': w:=MilliSecond; - else - inc(p); - continue; - end; - StartP:=p; - repeat - inc(p); - until (p>length(aFormat)) or (aFormat[p]<>aFormat[p-1]); - l:=p-StartP; - s:=IntToStr(w); - if length(s)l then - raise Exception.Create('date format does not fit'); - ReplaceSubstring(Result,StartP,l,s); - p:=StartP+length(s); - end; - //debugln('DateToCfgStr "',Result,'"'); -end; - -function CfgStrToDate(const s: string; out Date: TDateTime; - const aFormat: string): boolean; - - procedure AddDecimal(var d: word; c: char); inline; - begin - d:=d*10+ord(c)-ord('0'); - end; - -var - i: Integer; - Year, Month, Day, Hour, Minute, Second, MilliSecond: word; -begin - //debugln('CfgStrToDate "',s,'"'); - if length(s)<>length(aFormat) then begin - Date:=0.0; - exit(false); - end; - try - Year:=0; - Month:=0; - Day:=0; - Hour:=0; - Minute:=0; - Second:=0; - MilliSecond:=0; - for i:=1 to length(aFormat) do begin - case aFormat[i] of - 'Y': AddDecimal(Year,s[i]); - 'M': AddDecimal(Month,s[i]); - 'D': AddDecimal(Day,s[i]); - 'H': AddDecimal(Hour,s[i]); - 'N': AddDecimal(Minute,s[i]); - 'S': AddDecimal(Second,s[i]); - 'Z': AddDecimal(MilliSecond,s[i]); - end; - end; - Date:=ComposeDateTime(EncodeDate(Year,Month,Day),EncodeTime(Hour,Minute,Second,MilliSecond)); - Result:=true; - except - Result:=false; - end; -end; - procedure DebugLn(Args: array of const); begin LazLoggerBase.Debugln(Args); diff --git a/components/codetools/tests/testbasiccodetools.pas b/components/codetools/tests/testbasiccodetools.pas index b4a126ccb9..75f712e446 100644 --- a/components/codetools/tests/testbasiccodetools.pas +++ b/components/codetools/tests/testbasiccodetools.pas @@ -28,7 +28,7 @@ interface uses fpcunit, testregistry, contnrs, Classes, SysUtils, // LazUtils - LazStringUtils, + LazStringUtils, LazConfigStorage, // CodeTools FileProcs, BasicCodeTools, DefineTemplates; diff --git a/components/ideintf/inputhistory.pas b/components/ideintf/inputhistory.pas index 3310f2ce01..81ccef376c 100644 --- a/components/ideintf/inputhistory.pas +++ b/components/ideintf/inputhistory.pas @@ -42,8 +42,7 @@ uses Dialogs, // LazUtils LazFileCache, LazFileUtils, LazLoggerBase, LazUTF8, AvgLvlTree, Laz2_XMLCfg, - // Codetools - FileProcs, + LazConfigStorage, // IdeConfig DiffPatch, LazConf, RecentListProcs, IdeXmlConfigProcs, // IdeIntf diff --git a/components/lazutils/lazconfigstorage.pas b/components/lazutils/lazconfigstorage.pas index 477281d8a2..18f5f7fe8b 100644 --- a/components/lazutils/lazconfigstorage.pas +++ b/components/lazutils/lazconfigstorage.pas @@ -20,7 +20,7 @@ interface uses Classes, SysUtils, typinfo, Laz_AVL_Tree, // LazUtils - LazLoggerBase, AvgLvlTree; + LazLoggerBase, AvgLvlTree, LazStringUtils; type { TConfigStorage } @@ -147,6 +147,12 @@ procedure LoadStringToStringTree(Config: TConfigStorage; const Path: string; procedure SaveStringToStringTree(Config: TConfigStorage; const Path: string; Tree: TStringToStringTree); +// store date locale independent, thread safe +const DateAsCfgStrFormat='YYYYMMDD'; +const DateTimeAsCfgStrFormat='YYYY/MM/DD HH:NN:SS'; +function DateToCfgStr(const Date: TDateTime; const aFormat: string = DateAsCfgStrFormat): string; +function CfgStrToDate(const s: string; out Date: TDateTime; const aFormat: string = DateAsCfgStrFormat): boolean; + function CompareConfigMemStorageNames(p1, p2: PChar): integer; function CompareConfigMemStorageNodes(Node1, Node2: Pointer): integer; function ComparePCharWithConfigMemStorageNode(aPChar, ANode: Pointer): integer; @@ -193,6 +199,108 @@ begin end; end; +function DateToCfgStr(const Date: TDateTime; const aFormat: string): string; +var + NeedDate: Boolean; + NeedTime: Boolean; + Year: word; + Month: word; + Day: word; + Hour: word; + Minute: word; + Second: word; + MilliSecond: word; + p: Integer; + w: Word; + StartP: Integer; + s: String; + l: Integer; +begin + Result:=aFormat; + NeedDate:=false; + NeedTime:=false; + for p:=1 to length(aFormat) do + case aFormat[p] of + 'Y','M','D': NeedDate:=true; + 'H','N','S','Z': NeedTime:=true; + end; + if NeedDate then + DecodeDate(Date,Year,Month,Day); + if NeedTime then + DecodeTime(Date,Hour,Minute,Second,MilliSecond); + p:=1; + while p<=length(aFormat) do begin + case aFormat[p] of + 'Y': w:=Year; + 'M': w:=Month; + 'D': w:=Day; + 'H': w:=Hour; + 'N': w:=Minute; + 'S': w:=Second; + 'Z': w:=MilliSecond; + else + inc(p); + continue; + end; + StartP:=p; + repeat + inc(p); + until (p>length(aFormat)) or (aFormat[p]<>aFormat[p-1]); + l:=p-StartP; + s:=IntToStr(w); + if length(s)l then + raise Exception.Create('date format does not fit'); + ReplaceSubstring(Result,StartP,l,s); + p:=StartP+length(s); + end; + //debugln('DateToCfgStr "',Result,'"'); +end; + +function CfgStrToDate(const s: string; out Date: TDateTime; + const aFormat: string): boolean; + + procedure AddDecimal(var d: word; c: char); inline; + begin + d:=d*10+ord(c)-ord('0'); + end; + +var + i: Integer; + Year, Month, Day, Hour, Minute, Second, MilliSecond: word; +begin + //debugln('CfgStrToDate "',s,'"'); + if length(s)<>length(aFormat) then begin + Date:=0.0; + exit(false); + end; + try + Year:=0; + Month:=0; + Day:=0; + Hour:=0; + Minute:=0; + Second:=0; + MilliSecond:=0; + for i:=1 to length(aFormat) do begin + case aFormat[i] of + 'Y': AddDecimal(Year,s[i]); + 'M': AddDecimal(Month,s[i]); + 'D': AddDecimal(Day,s[i]); + 'H': AddDecimal(Hour,s[i]); + 'N': AddDecimal(Minute,s[i]); + 'S': AddDecimal(Second,s[i]); + 'Z': AddDecimal(MilliSecond,s[i]); + end; + end; + Date:=ComposeDateTime(EncodeDate(Year,Month,Day),EncodeTime(Hour,Minute,Second,MilliSecond)); + Result:=true; + except + Result:=false; + end; +end; + function CompareConfigMemStorageNames(p1, p2: PChar): integer; // compare strings till / or #0 begin diff --git a/packager/packagelinks.pas b/packager/packagelinks.pas index c95e82fd10..5e15499f95 100644 --- a/packager/packagelinks.pas +++ b/packager/packagelinks.pas @@ -44,7 +44,7 @@ uses Forms, // LazUtils Laz2_XMLCfg, LazFileCache, LazFileUtils, FileUtil, LazUtilities, LazTracer, - AvgLvlTree, + AvgLvlTree, LazConfigStorage, // Codetools FileProcs, CodeToolManager, // BuildIntf