{*******************************************************}
{                                                       }
{       Tocsg.Path                                      }
{                                                       }
{       Copyright (C) 2022 kkuzil                       }
{                                                       }
{*******************************************************}

unit Tocsg.Path;

interface

function CutFileExt(sFName: String): String; {$IFDEF RELEASE} inline; {$ENDIF}
function GetFileExt(sFName: String): String; {$IFDEF RELEASE} inline; {$ENDIF}
function GetRunExePath: String; inline;
function GetRunExePathDir: String; inline;
function GetRunExeName: String; inline;

function GetRecentDir: String;
function GetDesktopDir: String;
function GetCommonDesktopDir: String;
function GetWindowsDir: String;
function GetSystemDir: String;
function GetProgramFilesDir: String;
function GetUserDir: String;
function GetProgramsDir: String;
function GetDocumentDir: String;
function GetCommonDocumentDir: String;
function GetCommonDataDir: String;
function GetInternetTempDir: String;
function GetInternetCookiesDir: String;

implementation

uses
  System.SysUtils, Winapi.ShlObj, Winapi.Windows;

function CutFileExt(sFName: String): String;
var
  i: Integer;
begin
  i := sFName.LastIndexOf('.');
  if i <> -1 then
    SetLength(sFName, i);
  Result := sFName
end;

function GetFileExt(sFName: String): String;
var
  i: Integer;
begin
  i := sFName.LastIndexOf('.');
  if i <> -1 then
    Result := Copy(sFName, i + 2, sFName.Length - i)
  else
    Result := '';
end;

function GetRunExePath: String;
begin
  Result := ParamStr(0);
end;

function GetRunExePathDir: String;
begin
  Result := ExtractFilePath(GetRunExePath);
end;

function GetRunExeName: String; inline;
begin
  Result := ExtractFileName(GetRunExePath);
end;

function GetSpecialDir(nFolder: Integer): String;
var
  pidl: PItemIDList;
  hRes: HRESULT;
  bSuccess: Boolean;
  sRealPath: array [0..MAX_PATH] of {$IFDEF UNICODE} WideChar {$ELSE} AnsiChar {$ENDIF};
begin
  Result := '';
  bSuccess := false;

  pidl := nil;
  hRes := SHGetSpecialFolderLocation(0, nFolder, pidl);
  if hRes = NO_ERROR then
  begin
    try
      if SHGetPathFromIDList(pidl, sRealPath) then
        Result := IncludeTrailingPathDelimiter(sRealPath);
    finally
      if pidl <> nil then
        ILFree(pidl);
    end;
  end;
end;

function GetRecentDir: String;
begin
  Result := GetSpecialDir(CSIDL_RECENT);
end;

function GetDesktopDir: String;
begin
  Result := GetSpecialDir(CSIDL_DESKTOPDIRECTORY);
end;

function GetCommonDesktopDir: String;
begin
  Result := GetSpecialDir(CSIDL_COMMON_DESKTOPDIRECTORY);
end;

function GetWindowsDir: String;
begin
  Result := GetSpecialDir(CSIDL_WINDOWS);
end;

function GetSystemDir: String;
begin
  Result := GetSpecialDir(CSIDL_SYSTEM);
end;

function GetProgramFilesDir: String;
begin
  Result := GetSpecialDir(CSIDL_PROGRAM_FILES);
end;

function GetUserDir: String;
begin
  Result := GetSpecialDir(CSIDL_PROFILE);
end;

function GetProgramsDir: String;
begin
  Result := GetSpecialDir(CSIDL_PROGRAMS);
end;

function GetDocumentDir: String;
begin
  Result := GetSpecialDir(CSIDL_PERSONAL);
end;

function GetCommonDocumentDir: String;
begin
  Result := GetSpecialDir(CSIDL_COMMON_DOCUMENTS);
end;

function GetCommonDataDir: String;
begin
  Result := GetSpecialDir(CSIDL_COMMON_APPDATA);
end;

function GetInternetTempDir: String;
begin
  Result := GetSpecialDir(CSIDL_INTERNET_CACHE);
end;

function GetInternetCookiesDir: String;
begin
  Result := GetSpecialDir(CSIDL_COOKIES);
end;

end.
