From cfeeb89bc233916e4a173f005e71862bfa595b50 Mon Sep 17 00:00:00 2001 From: Alexey Kulakov Date: Sat, 26 Jul 2014 22:09:21 +0000 Subject: small pascal updates semi-fix for service parameters in Actman30 git-svn-id: http://svn.miranda-ng.org/main/trunk@9957 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/Utils.pas/common.pas | 293 +++++++++++++++++++++------------- plugins/Utils.pas/datetime.pas | 174 ++++++++++++++++++++ plugins/Utils.pas/sparam.pas | 7 +- plugins/Watrack/proto/i_proto_dlg.inc | 16 +- plugins/mRadio/rframeapi.pas | 1 + 5 files changed, 364 insertions(+), 127 deletions(-) create mode 100644 plugins/Utils.pas/datetime.pas diff --git a/plugins/Utils.pas/common.pas b/plugins/Utils.pas/common.pas index d6eafbf0e6..37146dd0fd 100644 --- a/plugins/Utils.pas/common.pas +++ b/plugins/Utils.pas/common.pas @@ -77,7 +77,9 @@ procedure ShellSort(size:integer;Compare,Swap:tSortProc); //----- String processing ----- function FormatStrW (fmt:pWideChar; const arr:array of pWideChar):pWideChar; +function FormatStr (fmt:pAnsiChar; const arr:array of pAnsiChar):pAnsiChar; function FormatSimpleW(fmt:pWideChar; const arr:array of const):pWideChar; +function FormatSimple (fmt:pAnsiChar; const arr:array of const):pAnsiChar; const SIGN_UNICODE = $FEFF; @@ -91,6 +93,8 @@ const // trying to recognize text encoding. Returns CP_ function GetTextFormat(Buffer:pByte;sz:cardinal):integer; +function AdjustLineBreaks(S:pWideChar):pWideChar; + //----- Encoding conversion ----- function WideToCombo(src:PWideChar;var dst;cp:integer=CP_ACP):integer; @@ -162,6 +166,8 @@ function GetPairChar(ch:WideChar):WideChar; overload; //----- String/number conversion ----- +function IntStrLen(Value:int64; base:integer=10):integer; + function IntToHex(dst:pWideChar;Value:int64;Digits:integer=0):pWideChar; overload; function IntToHex(dst:PAnsiChar;Value:int64;Digits:integer=0):PAnsiChar; overload; function IntToStr(dst:pWideChar;Value:int64;Digits:integer=0):pWideChar; overload; @@ -174,26 +180,6 @@ function NumToInt(src:pWideChar):int64; overload; function NumToInt(src:pAnsiChar):int64; overload; //----- Date and Time ----- -const - SecondsPerDay = 24*60*60; - // 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; - // Days between Unix time_t basis (1/1/1970) and Windows timestamp (1/1/1601) - WinDateDelta = 134774; // - -function IsLeapYear(Year:word):Boolean; -function EncodeTime(Hour, Minute, Sec: cardinal):TDateTime; -function EncodeDate(Year, Month , Day: cardinal):TDateTime; - -function Timestamp(Year,Month,Day:cardinal;Hour:cardinal=0;Minute:cardinal=0;Sec:cardinal=0):dword; -function GetCurrentTime:dword; - -procedure UnixTimeToFileTime(ts:int_ptr; var pft:TFILETIME); -function FileTimeToUnixTime(const pft: TFILETIME):int_ptr; -function TimeStampToLocalTimeStamp(ts:int_ptr):int_ptr; -function TimestampToDateTime(ts:int_ptr):TDateTime; function TimeToInt(stime:PAnsiChar):integer; overload; function TimeToInt(stime:PWideChar):integer; overload; @@ -1333,6 +1319,46 @@ begin pc^:=#0; end; +function FormatStr(fmt:pAnsiChar; const arr:array of pAnsiChar):pAnsiChar; +var + i,len:integer; + pc:pAnsiChar; + number:integer; +begin + result:=nil; + if (fmt=nil) or (fmt^=#0) then + exit; + + // calculate length + len:=StrLen(fmt); // -2*Length(arr) + for i:=0 to HIGH(arr) do + inc(len,StrLen(arr[i])); + + // format + mGetMem(result,len+1); + pc:=result; + number:=0; + while fmt^<>#0 do + begin + if (fmt^='%') and ((fmt+1)^='s') then + begin + if number<=HIGH(arr) then + begin + pc:=StrCopyE(pc,arr[number]); + inc(number); + end; + inc(fmt,2); + end + else + begin + pc^:=fmt^; + inc(pc); + inc(fmt); + end; + end; + pc^:=#0; +end; + function FormatSimpleW(fmt:pWideChar; const arr:array of const):pWideChar; var i,len:integer; @@ -1370,7 +1396,7 @@ begin end; inc(fmt,2); end; - 'd': begin + 'd','u': begin if number<=HIGH(arr) then begin pc:=StrEndW(IntToStr(pc,arr[number].VInteger)); @@ -1393,6 +1419,126 @@ begin pc^:=#0; end; +function FormatSimple(fmt:pAnsiChar; const arr:array of const):pAnsiChar; +var + i,len:integer; + pc:pAnsiChar; + number:integer; +begin + result:=nil; + if (fmt=nil) or (fmt^=#0) then + exit; + + // calculate length + len:=StrLen(fmt); // -2*Length(arr) + for i:=0 to HIGH(arr) do + begin + case arr[i].VType of + vtInteger: inc(len,10); // max len of VInteger text + vtPChar : inc(len,StrLen(arr[i].VPChar)); + end; + end; + + // format + mGetMem(result,len+1); + pc:=result; + number:=0; + while fmt^<>#0 do + begin + if (fmt^='%') then + begin + case (fmt+1)^ of + 's': begin + if number<=HIGH(arr) then + begin + pc:=StrCopyE(pc,arr[number].VPChar); + inc(number); + end; + inc(fmt,2); + end; + 'd','u': begin + if number<=HIGH(arr) then + begin + pc:=StrEnd(IntToStr(pc,arr[number].VInteger)); + inc(number); + end; + inc(fmt,2); + end; + '%': begin + pc^:='%'; + inc(pc); + inc(fmt,2); + end; + else + pc^:=fmt^; + inc(pc); + inc(fmt); + end; + end; + end; + pc^:=#0; +end; + +function AdjustLineBreaks(S:pWideChar):pWideChar; +var + Source, Dest: PWideChar; + Extra, len: Integer; +begin + Result := nil; + len := StrLenW(S); + if len=0 then + exit; + + Source := S; + Extra := 0; + while Source^ <> #0 do + begin + case Source^ of + #10: + Inc(Extra); + #13: + if Source[1] = #10 then + Inc(Source) + else + Inc(Extra); + end; + Inc(Source); + end; + + if Extra = 0 then + begin + StrDupW(Result, S); + end + else + begin + Source := S; + mGetMem(Result, (len + Extra + 1) * SizeOf(WideChar)); + Dest := Result; + while Source^ <> #0 do + begin + case Source^ of + #10: begin + Dest^ := #13; + Inc(Dest); + Dest^ := #10; + end; + #13: begin + Dest^ := #13; + Inc(Dest); + Dest^ := #10; + if Source[1] = #10 then + Inc(Source); + end; + else + Dest^ := Source^; + end; + Inc(Dest); + Inc(Source); + end; + Dest^ := #0; + end; +end; + // ----- base string functions ----- function StrDup(var dst:PAnsiChar;src:PAnsiChar;len:cardinal=0):PAnsiChar; @@ -2118,93 +2264,6 @@ end; //----- Date and Time ----- -type - PDayTable = ^TDayTable; - TDayTable = array [0..11] of cardinal; - -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)); - -function IsLeapYear(Year:word):Boolean; -begin - Result:=(Year mod 4=0) and ((Year mod 100<>0) or (Year mod 400=0)); -end; - -function EncodeTime(Hour, Minute, Sec: cardinal): TDateTime; -begin - result := (Hour*3600 + Minute*60 + Sec) / 86400; -end; - -function EncodeDate(Year, Month, Day: cardinal):TDateTime; -var - DayTable: PDayTable; -begin - DayTable := @MonthDays[IsLeapYear(Year)]; - dec(Month); - while Month>0 do - begin - dec(Month); - inc(Day,DayTable^[Month]); - end; - - dec(Year); - result := Year * 365 + Year div 4 - Year div 100 + Year div 400 + Day - DateDelta; -end; - -function Timestamp(Year,Month,Day:cardinal;Hour:cardinal=0;Minute:cardinal=0;Sec:cardinal=0):dword; -var - t:tDateTime; -begin - t := EncodeDate(Year, Month, Day); - if t >= 0 then - t := t + EncodeTime(Hour, Minute, Sec) - else - t := t - EncodeTime(Hour, Minute, Sec); - result:=Round((t - UnixDateDelta) * SecondsPerDay); -end; - -function GetCurrentTime:dword; -var - st:tSystemTime; -begin - GetSystemTime(st); - result:=Timestamp(st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond); -end; - -procedure UnixTimeToFileTime(ts:int_ptr; var pft:TFILETIME); -var - ll:uint64; -begin - ll := (int64(WinDateDelta)*SecondsPerDay + ts) * 10000000; - pft.dwLowDateTime := dword(ll); - pft.dwHighDateTime := ll shr 32; -end; - -function FileTimeToUnixTime(const pft: TFILETIME):int_ptr; -var - ll:uint64; -begin - ll := (uint64(pft.dwHighDateTime) shl 32) or pft.dwLowDateTime; - ll := (ll div 10000000) - int64(WinDateDelta)*SecondsPerDay; - result := int_ptr(ll); -end; - -function TimeStampToLocalTimeStamp(ts:int_ptr):int_ptr; -var - ft,lft:TFileTime; -begin - UnixTimeToFileTime(ts,ft); - FileTimeToLocalFileTime(ft, lft); - result:=FileTimeToUnixTime(lft); -end; - -function TimestampToDateTime(ts:int_ptr):TDateTime; -begin - Result := UnixDateDelta + TimeStampToLocalTimeStamp(ts) / SecondsPerDay; -end; - function TimeToInt(stime:PAnsiChar):integer; var hour,minute,sec,len,i:integer; @@ -2371,9 +2430,23 @@ begin end; end; +function IntStrLen(Value:int64; base:integer=10):integer; +var + i:uint64; +begin + result:=0; + if (base=10) and (Value<0) then + inc(result); + i:=ABS(Value); + repeat + i:=i div base; + inc(result); + until i=0; +end; + function IntToStr(dst:PAnsiChar;Value:int64;Digits:integer=0):PAnsiChar; var - i:dword; + i:uint64; begin if Digits<=0 then begin @@ -2404,7 +2477,7 @@ end; function IntToStr(dst:pWideChar;Value:int64;Digits:integer=0):pWideChar; var - i:dword; + i:uint64; begin if Digits<=0 then begin diff --git a/plugins/Utils.pas/datetime.pas b/plugins/Utils.pas/datetime.pas new file mode 100644 index 0000000000..f30d186b4e --- /dev/null +++ b/plugins/Utils.pas/datetime.pas @@ -0,0 +1,174 @@ +unit DateTime; + +interface + +uses + Windows; + +const + SecondsPerDay = 24*60*60; + // 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; + // Days between Unix time_t basis (1/1/1970) and Windows timestamp (1/1/1601) + WinDateDelta = 134774; // + +function IsLeapYear(Year:word):Boolean; +function EncodeTime(Hour, Minute, Sec: cardinal):TDateTime; +function EncodeDate(Year, Month , Day: cardinal):TDateTime; + +function Timestamp(Year,Month,Day:cardinal;Hour:cardinal=0;Minute:cardinal=0;Sec:cardinal=0):dword; +function GetCurrentTimestamp:DWord; + +procedure UnixTimeToFileTime(ts:int_ptr; var pft:TFILETIME); +function FileTimeToUnixTime(const pft: TFILETIME):int_ptr; +function TimeStampToLocalTimeStamp(ts:int_ptr):int_ptr; +function TimestampToDateTime(ts:int_ptr):TDateTime; +function TimestampToSystemTime(Time:DWord; var ST:TSystemTime):PSystemTime; + +function DateTimeToStr(Time:Dword; Format:pWideChar=nil):pWideChar; +function DateToStr (Time:Dword; Format:pWideChar=nil):pWideChar; +function TimeToStr (Time:Dword; Format:pWideChar=nil):pWideChar; + +implementation + +uses + common; + +type + PDayTable = ^TDayTable; + TDayTable = array [0..11] of byte; + +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)); + +function IsLeapYear(Year:word):Boolean; +begin + Result:=(Year mod 4=0) and ((Year mod 100<>0) or (Year mod 400=0)); +end; + +function EncodeTime(Hour, Minute, Sec: cardinal): TDateTime; +begin + result := (Hour*3600 + Minute*60 + Sec) / SecondsPerDay; +end; + +function EncodeDate(Year, Month, Day: cardinal):TDateTime; +var + DayTable: PDayTable; +begin + DayTable := @MonthDays[IsLeapYear(Year)]; + dec(Month); + while Month>0 do + begin + dec(Month); + inc(Day,DayTable^[Month]); + end; + + dec(Year); + result := Year * 365 + Year div 4 - Year div 100 + Year div 400 + Day - DateDelta; +end; + +function Timestamp(Year,Month,Day:cardinal;Hour:cardinal=0;Minute:cardinal=0;Sec:cardinal=0):dword; +var + t:tDateTime; +begin + t := EncodeDate(Year, Month, Day); + if t >= 0 then + t := t + EncodeTime(Hour, Minute, Sec) + else + t := t - EncodeTime(Hour, Minute, Sec); + result:=Round((t - UnixDateDelta) * SecondsPerDay); +end; + +function GetCurrentTimestamp:dword; +var + st:tSystemTime; +begin + GetSystemTime(st); + result:=Timestamp(st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond); +end; + +procedure UnixTimeToFileTime(ts:int_ptr; var pft:TFILETIME); +var + ll:uint64; +begin + ll := (int64(WinDateDelta)*SecondsPerDay + ts) * 10000000; + pft.dwLowDateTime := dword(ll); + pft.dwHighDateTime := ll shr 32; +end; + +function FileTimeToUnixTime(const pft: TFILETIME):int_ptr; +var + ll:uint64; +begin + ll := (uint64(pft.dwHighDateTime) shl 32) or pft.dwLowDateTime; + ll := (ll div 10000000) - int64(WinDateDelta)*SecondsPerDay; + result := int_ptr(ll); +end; + +function TimeStampToLocalTimeStamp(ts:int_ptr):int_ptr; +var + ft,lft:TFileTime; +begin + UnixTimeToFileTime(ts,ft); + FileTimeToLocalFileTime(ft, lft); + result:=FileTimeToUnixTime(lft); +end; + +function TimestampToDateTime(ts:int_ptr):TDateTime; +begin + Result := UnixDateDelta + TimeStampToLocalTimeStamp(ts) / SecondsPerDay; +end; + +function TimestampToSystemTime(Time:DWord; var ST:TSystemTime):PSystemTime; +var + aft,lft:TFILETIME; +begin + UnixTimeToFileTime(Time,aft); + FileTimeToLocalFileTime(aft, lft); + FileTimeToSystemTime(lft,ST); + result:=@ST; +end; + +function DateTimeToStr(Time:Dword; Format:pWideChar=nil):pWideChar; +var + buf:array [0..300] of WideChar; + ST: TSystemTime; + pc:pWideChar; +begin + TimestampToSystemTime(Time,ST); + GetDateFormatW(LOCALE_USER_DEFAULT,0,@ST,Format,@buf,300); + if Format<>nil then + GetTimeFormatW(LOCALE_USER_DEFAULT,0,@ST,@buf,@buf,300) + else + begin + pc:=StrEndW(@buf); pc^:=' '; inc(pc); + GetTimeFormatW(LOCALE_USER_DEFAULT,0,@ST,nil,pc,300-(pc-pWideChar(@buf))) + end; + StrDupW(result,buf); +end; + +function DateToStr(Time:Dword; Format:pWideChar=nil):pWideChar; +var + buf:array [0..300] of WideChar; + ST: TSystemTime; +begin + TimestampToSystemTime(Time,ST); + GetDateFormatW(LOCALE_USER_DEFAULT,0,@ST,Format,@buf,300); + StrDupW(result,buf); +end; + +function TimeToStr(Time:Dword; Format:pWideChar=nil):pWideChar; +var + buf:array [0..300] of WideChar; + ST: TSystemTime; +begin + TimestampToSystemTime(Time,ST); + GetTimeFormatW(LOCALE_USER_DEFAULT,0,@ST,Format,@buf,300); + StrDupW(result,buf); +end; + +end. diff --git a/plugins/Utils.pas/sparam.pas b/plugins/Utils.pas/sparam.pas index 00e49ac1ab..623391769e 100644 --- a/plugins/Utils.pas/sparam.pas +++ b/plugins/Utils.pas/sparam.pas @@ -156,8 +156,11 @@ begin begin if atype=ACF_NUMBER then //!! begin - pcw:='0'; - SendMessageW(wnd,WM_SETTEXT,0,TLParam(pcw)); + if SendMessageW(wnd,WM_GETTEXTLENGTH,0,0)=0 then + begin + pcw:='0'; + SendMessageW(wnd,WM_SETTEXT,0,TLParam(pcw)); + end; end; EnableEditField(wnd,true); end; diff --git a/plugins/Watrack/proto/i_proto_dlg.inc b/plugins/Watrack/proto/i_proto_dlg.inc index 5d3b6e2bb4..d5c3f2262b 100644 --- a/plugins/Watrack/proto/i_proto_dlg.inc +++ b/plugins/Watrack/proto/i_proto_dlg.inc @@ -38,20 +38,6 @@ begin end; end; -procedure ResetListOptions(hwndList:HWND); -var - i:integer; -begin - SendMessage(hwndList,CLM_SETBKBITMAP ,0,0); - SendMessage(hwndList,CLM_SETBKCOLOR ,GetSysColor(COLOR_WINDOW),0); - SendMessage(hwndList,CLM_SETGREYOUTFLAGS,0,0); - SendMessage(hwndList,CLM_SETLEFTMARGIN ,2,0); - SendMessage(hwndList,CLM_SETINDENT ,10,0); - for i:=0 to FONTID_MAX do - SendMessage(hwndList,CLM_SETTEXTCOLOR,i,GetSysColor(COLOR_WINDOWTEXT)); - SetWindowLongPtr(hwndList,GWL_STYLE,GetWindowLongPtr(hwndList,GWL_STYLE) or CLS_SHOWHIDDEN); -end; - procedure SetHistMask(Dlg:HWND); begin CheckDlgButton(Dlg,IDC_IN_REQUEST ,ORD((HistMask and hmInRequest )<>0)); @@ -92,7 +78,7 @@ begin Changed:=DLGED_INIT; hList:=GetDlgItem(Dialog,IDC_SHARE); - ResetListOptions(hList); + SetWindowLongPtr(hList,GWL_STYLE,GetWindowLongPtr(hList,GWL_STYLE) or CLS_SHOWHIDDEN); SendMessage(hList,CLM_SETUSEGROUPS ,1,0); SendMessage(hList,CLM_SETHIDEEMPTYGROUPS,1,0); diff --git a/plugins/mRadio/rframeapi.pas b/plugins/mRadio/rframeapi.pas index 03a38ea267..b00cab0e52 100644 --- a/plugins/mRadio/rframeapi.pas +++ b/plugins/mRadio/rframeapi.pas @@ -84,6 +84,7 @@ begin hVolFrmCtrl:=0; hVolFrmMute:=0; DeleteObject(hbr); + hbr:=0; end; WM_INITDIALOG: begin -- cgit v1.2.3