ksString.pas 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. {*******************************************************}
  2. { }
  3. { Methods of Sting }
  4. { }
  5. { 版权所有 (C) 2020 KngStr }
  6. { }
  7. {*******************************************************}
  8. unit ksString;
  9. interface
  10. /// <summary>
  11. /// 创建空白文件
  12. /// </summary>
  13. function CreateBlankFile(S: string): Boolean;
  14. /// <summary>
  15. /// 自动为已存在文件名加数字
  16. /// </summary>
  17. function AutoRenameFileName(AFile: string): string;
  18. /// <summary>
  19. /// 自动替换文件名中的非法字符为
  20. /// </summary>
  21. procedure ReplaceInvalidFileNameChars(var FileName: string; AChar: Char = '_');
  22. /// <summary>
  23. /// 格式化秒为时间,冒号分割
  24. /// </summary>
  25. function FormatSeconds(const AValue: Int64): string; overload;
  26. /// <summary>
  27. /// 格式化秒为时间,冒号分割
  28. /// </summary>
  29. function FormatSeconds(const AValue: Integer): string; overload;
  30. /// <summary>
  31. /// 格式化秒为时间,冒号分割
  32. /// </summary>
  33. function FormatSeconds(const AValue: Double): string; overload;
  34. implementation
  35. uses
  36. System.Classes, System.SysUtils, System.IOUtils;
  37. function CreateBlankFile(S: string): Boolean;
  38. var
  39. FStrm: TFileStream;
  40. begin
  41. Result := False;
  42. try
  43. FStrm := TFileStream.Create(S, fmCreate);
  44. if FStrm = nil then
  45. Exit;
  46. Result := True;
  47. FreeAndNil(FStrm);
  48. except
  49. FStrm := nil;
  50. end;
  51. end;
  52. function AutoRenameFileName(AFile: string): string;
  53. var
  54. I: Integer;
  55. sPath, sExt: string;
  56. begin
  57. Result := AFile;
  58. I := 0;
  59. sPath := ChangeFileExt(Result, '');
  60. sExt := ExtractFileExt(Result);
  61. while FileExists(Result) do begin
  62. Inc(I);
  63. Result := Format('%s(%d)%s', [sPath, I, sExt]);
  64. end;
  65. end;
  66. procedure ReplaceInvalidFileNameChars(var FileName: string; AChar: Char);
  67. var
  68. PFileName: PChar;
  69. FileNameLen: Integer;
  70. Ch: Char;
  71. I: Integer;
  72. begin
  73. I := 0;
  74. PFileName := PChar(FileName);
  75. FileNameLen := Length(FileName);
  76. while I < FileNameLen do begin
  77. Ch := PFileName[I];
  78. if not TPath.IsValidFileNameChar(Ch) then
  79. PFileName[I] := AChar
  80. else
  81. Inc(I);
  82. end;
  83. end;
  84. function FormatSeconds(const AValue: Double): string; overload;
  85. var
  86. h, m, s, ms: Integer;
  87. begin
  88. ms := Trunc(Frac(AValue) * 1000);
  89. s := Trunc(AValue);
  90. m := s div 60;
  91. h := m div 60;
  92. if h > 0 then
  93. Result := Format('%.2d:%.2d:%.2d.%.3d', [h, m mod 60, s mod 60, ms])
  94. else
  95. Result := Format('%.2d:%.2d.%.3d', [m, s mod 60, ms]);
  96. end;
  97. function FormatSeconds(const AValue: Int64): string;
  98. var
  99. h, m, s: Integer;
  100. begin
  101. s := AValue;
  102. m := s div 60;
  103. h := m div 60;
  104. if h > 0 then
  105. Result := Format('%.2d:%.2d:%.2d', [h, m mod 60, s mod 60])
  106. else
  107. Result := Format('%.2d:%.2d', [m, s mod 60]);
  108. end;
  109. function FormatSeconds(const AValue: Integer): string; overload;
  110. begin
  111. FormatSeconds(Int64(AValue));
  112. end;
  113. end.