2
0

futils.pp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. unit futils;
  2. {$mode objfpc}
  3. {$h+}
  4. Interface
  5. Const
  6. {$ifdef win32}
  7. PathSeparator='\';
  8. {$else}
  9. PathSeparator='/';
  10. {$endif}
  11. Function StripTrailingSeparator(Const Dir : String) : String;
  12. Function AddTrailingSeparator(Const Dir : String) : String;
  13. Function FileSizeToString(Size: Int64) : String;
  14. Function FileAttrsToString(FileAttrs : Integer) : String;
  15. Implementation
  16. Uses sysutils;
  17. Function StripTrailingSeparator(Const Dir : String) : String;
  18. Var
  19. L : Integer;
  20. begin
  21. Result:=Dir;
  22. L:=Length(result);
  23. If (L>1) and (Result[l]=PathSeparator) then
  24. SetLength(Result,L-1);
  25. end;
  26. Function AddTraiLingSeparator(Const Dir : String) : String;
  27. Var
  28. L : Integer;
  29. begin
  30. Result:=Dir;
  31. L:=Length(Result);
  32. If (L>0) and (Result[l]<>PathSeparator) then
  33. Result:=Result+PathSeparator;
  34. end;
  35. Function FileSizeToString(Size: Int64) : String;
  36. Const
  37. Sizes : Array [0..4] of String =
  38. ('Bytes','Kb','Mb','Gb','Tb');
  39. Var
  40. F : Double;
  41. I : longint;
  42. begin
  43. If Size>1024 Then
  44. begin
  45. F:=Size;
  46. I:=0;
  47. While (F>1024) and (I<4) do
  48. begin
  49. F:=F / 1024;
  50. Inc(i);
  51. end;
  52. Result:=Format('%4.2f %s',[F,Sizes[i]]);
  53. end
  54. else
  55. Result:=Format('%d %s',[Size,Sizes[0]]);
  56. end;
  57. Function FileAttrsToString(FileAttrs : Integer) : String;
  58. Const
  59. Attrs : Array[1..4] of integer =
  60. (faArchive,faReadOnly,faHidden,faSysfile);
  61. AttrChars : Array[1..4] of char =
  62. ('A','R','H','S');
  63. Var
  64. i : longint;
  65. begin
  66. Result:='';
  67. For I:=1 to 4 do
  68. If (Attrs[i] and FileAttrs)=Attrs[i] then
  69. Result:=Result+AttrChars[i];
  70. end;
  71. end.