Discovering Delphi’s IOUtils and TPath

Over the last couple of weeks we have been working on getting the next generation of our standalone Unit Conversion Software, ESBUnitConv Pro, ready for beta release (and you read more about that on our other blog http://esbcalc.com/wordpress ). One of the tasks I’ve been involved in has been making it as dependent on ESBDevLib as possible (rather than using ESBPCS for VCL) – since this is a good test of the visual side (VCL/Win32) of our new library as well as a good way to prioritise items to port/update.

I quickly discovered that we had not yet moved anything from the ESBPCSFileIO and ESBPCSShellIO units – and I needed the “special folder” support for things like Application Data Folder, My Documents Folder, Temporary Directory etc… And then it dawned on me – these two units (and especially the latter one) are heavily based on MS Windows – which of course is fine for ESBPCS for VCL – but ESBDevLib needs to be happy in FireMonkey and we especially want it to be happy with FireMonkey/MacOS (we realise that iOS support will require other adjustments, as will future Android support). Now whilst my first computer was an Apple II – I have been a PC developer since the mid-1980s – and though I am reasonably comfortable on our MacBook, I don’t have the “tech knowhow” on that platform.

So to Google I go, to see about such things as what I should do for “My Documents” on a Mac… First, I came across a very nice article by a friend of mine, Malcolm Groves – Cross-platform Special Folder in FireMonkey  – this was what I was after! – Thanks, Malcolm.

This lead me to a Delphi Unit that I had never really explored – IOUtils – as this already supplies a nice cross platform way to get the Temporary Directory and the Application Data (ie Home) Directory, it definitely warranted some time exploring – and some Google searching on its use.

So we set about using TPath wherever we could during our port of methods from the above units – since this should also make things “easier” when Android support is added.

Whilst we are currently doing development work for ESBDevLib in Delphi XE2, ESBUnitConv Pro v9 is being developed in Delphi XE4 – and thus gives us a good opportunity to compare the changes from XE2 -> XE3 -> XE4 – so did TPath change? Well one nice addition (similar to what Malcolm did in his article) they added TPath.GetDocumentsPath – so I added a few conditional compilation directives and we now have:

{$REGION 'GetMyDocumentsDir'}
{$IFDEF D18andAbove}
  function GetMyDocumentsDir: string;
  begin
    Result := TPath.GetDocumentsPath;
    AdjustPath (Result);
  end;
{$ELSE}
  {$IFDEF MACOS}
    function GetMyDocumentsDir: string;
var
      nsStr: NSString;
      nsFile: NSFileManager;
    begin
      nsFile := TNSFileManager.Wrap (TNSFileManager.OCClass.defaultManager);
      nsStr := nsFile.URLForDirectory (NSDocumentDirectory, NSUserDomainMask, nil, true, nil).path;
      Result := UTF8ToString (nsstr.UTF8String);
      AdjustPath (Result);
    end;
  {$ENDIF MACOS}
  {$IFDEF MSWINDOWS}
    function GetMyDocumentsDir: string;
    begin
      Result := GetSpecialFolder (CSIDL_PERSONAL);
      AdjustPath (Result);
    end;
  {$ENDIF MSWINDOWS}
{$ENDIF ELSE}
{$ENDREGION}

Now as far as I can see MacOS doesn’t have “special” folders for “My Pictures”, “My Music”, “My Videos” let alone the All Users (Public) versions – so for now we are just directing them to the Document Directory – if anyone knows of better options please let me know 🙂

Anyway – need to get back into the Date / Time Formatter Classes and adding them to the Formatter Component so that I can have Date and Calendar Edits for our in-house Apps 🙂

About esbglenn

Software Developer working at our Family owned business, ESB Consultancy, which is located in Kalgoorlie-Boulder, in the Eastern Goldfields of Western Australia.
This entry was posted in Beta Development, FireMonkey and tagged , , , , , , . Bookmark the permalink.

Leave a Reply