123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- {*******************************************************}
- { }
- { Methods of Sting }
- { }
- { 版权所有 (C) 2020 KngStr }
- { }
- {*******************************************************}
- unit ksString;
- interface
- /// <summary>
- /// 创建空白文件
- /// </summary>
- function CreateBlankFile(S: string): Boolean;
- /// <summary>
- /// 自动为已存在文件名加数字
- /// </summary>
- function AutoRenameFileName(AFile: string): string;
- /// <summary>
- /// 自动替换文件名中的非法字符为
- /// </summary>
- procedure ReplaceInvalidFileNameChars(var FileName: string; AChar: Char = '_');
- /// <summary>
- /// 格式化秒为时间,冒号分割
- /// </summary>
- function FormatSeconds(const AValue: Int64): string; overload;
- /// <summary>
- /// 格式化秒为时间,冒号分割
- /// </summary>
- function FormatSeconds(const AValue: Integer): string; overload;
- /// <summary>
- /// 格式化秒为时间,冒号分割
- /// </summary>
- function FormatSeconds(const AValue: Double): string; overload;
- implementation
- uses
- System.Classes, System.SysUtils, System.IOUtils;
- function CreateBlankFile(S: string): Boolean;
- var
- FStrm: TFileStream;
- begin
- Result := False;
- try
- FStrm := TFileStream.Create(S, fmCreate);
- if FStrm = nil then
- Exit;
- Result := True;
- FreeAndNil(FStrm);
- except
- FStrm := nil;
- end;
- end;
- function AutoRenameFileName(AFile: string): string;
- var
- I: Integer;
- sPath, sExt: string;
- begin
- Result := AFile;
- I := 0;
- sPath := ChangeFileExt(Result, '');
- sExt := ExtractFileExt(Result);
- while FileExists(Result) do begin
- Inc(I);
- Result := Format('%s(%d)%s', [sPath, I, sExt]);
- end;
- end;
- procedure ReplaceInvalidFileNameChars(var FileName: string; AChar: Char);
- var
- PFileName: PChar;
- FileNameLen: Integer;
- Ch: Char;
- I: Integer;
- begin
- I := 0;
- PFileName := PChar(FileName);
- FileNameLen := Length(FileName);
- while I < FileNameLen do begin
- Ch := PFileName[I];
- if not TPath.IsValidFileNameChar(Ch) then
- PFileName[I] := AChar
- else
- Inc(I);
- end;
- end;
- function FormatSeconds(const AValue: Double): string; overload;
- var
- h, m, s, ms: Integer;
- begin
- ms := Trunc(Frac(AValue) * 1000);
- s := Trunc(AValue);
- m := s div 60;
- h := m div 60;
- if h > 0 then
- Result := Format('%.2d:%.2d:%.2d.%.3d', [h, m mod 60, s mod 60, ms])
- else
- Result := Format('%.2d:%.2d.%.3d', [m, s mod 60, ms]);
- end;
- function FormatSeconds(const AValue: Int64): string;
- var
- h, m, s: Integer;
- begin
- s := AValue;
- m := s div 60;
- h := m div 60;
- if h > 0 then
- Result := Format('%.2d:%.2d:%.2d', [h, m mod 60, s mod 60])
- else
- Result := Format('%.2d:%.2d', [m, s mod 60]);
- end;
- function FormatSeconds(const AValue: Integer): string; overload;
- begin
- FormatSeconds(Int64(AValue));
- end;
- end.
|