{"id":528,"date":"2017-03-12T20:06:09","date_gmt":"2017-03-12T19:06:09","guid":{"rendered":"http:\/\/blog.kassebaum.eu\/?p=528"},"modified":"2017-03-13T02:39:33","modified_gmt":"2017-03-13T01:39:33","slug":"tfile-size-2-0","status":"publish","type":"post","link":"https:\/\/www.kassebaum.eu\/blog\/2017\/03\/12\/tfile-size-2-0\/","title":{"rendered":"TFile.Size 2.0"},"content":{"rendered":"<p>Yesterday I got a suggestion for a better implementation for TFile.Size from Stefan Glienke. Many thanks for it, Stefan.<\/p>\n<p>In order to make the code compatible to other functions in System.IOUtils, I modified it in the way that I&#8217;m raising an EInOutError if there is a problem.<\/p>\n<p>I could compile the code for all platforms Win32, Win64, MacOS32, iOS32, iOS64, Android32 and Linux64.<\/p>\n<pre class=\"lang:delphi decode:true \">unit System.IOUtils.Helper;\r\n\r\ninterface\r\n\r\nuses\r\n  System.IOUtils;\r\n\r\ntype\r\n  TFileHelper = record Helper for TFile\r\n  public\r\n    class function Size(const APath: string): Int64; static;\r\n  end;\r\n\r\nimplementation\r\n\r\n{$IFDEF MSWINDOWS}\r\nuses\r\n  System.SysUtils, Winapi.Windows;\r\n{$ENDIF}\r\n{$IFDEF POSIX}\r\nuses\r\n  System.SysUtils, Posix.SysStat;\r\n{$ENDIF}\r\n\r\n{ TFileHelper }\r\n\r\nclass function TFileHelper.Size(const APath: string): Int64;\r\n{$IFDEF MSWINDOWS}\r\nvar\r\n  lAttributes: TWin32FileAttributeData;\r\nbegin\r\n  if GetFileAttributesEx(PChar(APath), GetFileExInfoStandard, @lAttributes) then\r\n  begin\r\n    Int64Rec(Result).Lo := lAttributes.nFileSizeLow;\r\n    Int64Rec(Result).Hi := lAttributes.nFileSizeHigh;\r\n  end\r\n  else\r\n    raise EInOutError.Create(SysErrorMessage(GetLastError));\r\nend;\r\n{$ENDIF}\r\n{$IFDEF POSIX}\r\nvar\r\n  lStat: _stat;\r\n  lFileName: Pointer;\r\n  lMarshaller: TMarshaller;\r\n  lError: Integer;\r\nbegin\r\n  lFileName := lMarshaller.AsAnsi(APath, CP_UTF8).ToPointer;\r\n  lError := stat(lFileName, lStat);\r\n  if lError = 0 then\r\n    Result := lStat.st_size\r\n  else\r\n    raise EInOutError.Create(SysErrorMessage(lError));\r\nend;\r\n{$ENDIF}\r\n\r\nend.\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday I got a suggestion for a better implementation for TFile.Size from Stefan Glienke. Many thanks for it, Stefan. In order to make the code compatible to other functions in System.IOUtils, I modified it in the way that I&#8217;m raising &hellip; <a href=\"https:\/\/www.kassebaum.eu\/blog\/2017\/03\/12\/tfile-size-2-0\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,31,34,35,1],"tags":[],"class_list":["post-528","post","type-post","status-publish","format-standard","hentry","category-c-builder","category-delphi","category-rad-studio-10-1-berlin","category-rad-studio-10-2-tokyo","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/528","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/comments?post=528"}],"version-history":[{"count":5,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/528\/revisions"}],"predecessor-version":[{"id":536,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/528\/revisions\/536"}],"wp:attachment":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/media?parent=528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/categories?post=528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/tags?post=528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}