Created
April 13, 2017 20:48
-
-
Save jpluimers/a2d620f6d3fe6aabb36534df36a01d1e to your computer and use it in GitHub Desktop.
One day I will clean up this code to make %...% and $(...) expansion for Delphi programs easier.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit ConfigExpanderUnit; | |
interface | |
type | |
TConfigExpander = class(TObject) | |
public | |
procedure ExpandConfigValue(var ConfigValue: string); | |
function ExpandCSIDL(var Value: string): Boolean; | |
function ExpandEnvironmentVar(var Value: string): Boolean; | |
function ExpandExePath(var Value: string): Boolean; | |
end; | |
implementation | |
uses | |
CSIDLsUnit, JclShell; | |
procedure TConfigExpander.ExpandConfigValue(var ConfigValue: string); | |
begin | |
if Pos('%', ConfigValue) <> 0 then | |
ExpandEnvironmentVar(ConfigValue); // so we can put %APPDATA% in it | |
if Pos('$(', ConfigValue) <> 0 then | |
begin | |
ExpandCSIDL(ConfigValue); // so we can put $(CSIDL_COMMON_APPDATA) in it | |
ExpandExePath(ConfigValue); // so we can put $(ExePath) in it | |
end; | |
end; | |
function TConfigExpander.ExpandCSIDL(var Value: string): Boolean; | |
var | |
Index: TCSIDL; | |
CSIDL: Word; | |
CSIDLString: string; | |
CSIDLPath: string; | |
CSIDLMacro: string; | |
begin | |
Result := False; | |
for Index := Low(TCSIDL) to High(TCSIDL) do | |
begin | |
CSIDL := GetCSIDL(Index); | |
CSIDLString := GetCSIDLstring(Index); | |
CSIDLMacro := Format('$(%s)', [CSIDLString]); | |
if Pos(CSIDLMacro, Value) <> 0 then | |
begin | |
CSIDLPath := GetSpecialFolderLocation(CSIDL); | |
Value := StringReplace(Value, CSIDLMacro, CSIDLPath, [rfReplaceAll, rfIgnoreCase]); | |
Result := True; | |
end; | |
end; | |
// shfolder | |
// JvBrowseFolder TFromDirectory | |
// ShlObj | |
// SHGetSpecialFolderLocation versus SHGetFolderPath | |
// JclShell | |
end; | |
function TConfigExpander.ExpandEnvironmentVar(var Value: string): Boolean; | |
var | |
R: Integer; | |
Expanded: string; | |
begin | |
SetLength(Expanded, 1); | |
R := ExpandEnvironmentStrings(PChar(Value), PChar(Expanded), 0); | |
SetLength(Expanded, R); | |
Result := ExpandEnvironmentStrings(PChar(Value), PChar(Expanded), R) <> 0; | |
if Result then | |
begin | |
StrResetLength(Expanded); | |
Value := Expanded; | |
end; | |
end; | |
function TConfigExpander.ExpandExePath(var Value: string): Boolean; | |
var | |
// Index: TCSIDL; | |
// CSIDL: Word; | |
ExeName: string; | |
ExePath: string; | |
SpecialKeyName: string; | |
SpecialKeyPath: string; | |
SpecialMacro: string; | |
begin | |
Result := False; | |
// for Index := Low(TCSIDL) to High(TCSIDL) do | |
// begin | |
// CSIDL := GetCSIDL(Index); | |
SpecialKeyName := 'ExePath'; // GetCSIDLstring(Index); | |
SpecialMacro := Format('$(%s)', [SpecialKeyName]); | |
if Pos(SpecialMacro, Value) <> 0 then | |
begin | |
ExeName := ParamStr(0); | |
ExePath := ExtractFileDir(ExeName); | |
SpecialKeyPath := ExePath; // GetSpecialFolderLocation(CSIDL); | |
Value := StringReplace(Value, SpecialMacro, SpecialKeyPath, [rfReplaceAll, rfIgnoreCase]); | |
Result := True; | |
end; | |
// end; | |
end; | |
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit CSIDLsUnit; | |
interface | |
uses | |
ShlObj; | |
{ See these URLs: | |
http://msdn.microsoft.com/en-us/library/bb762494(VS.85).aspx | |
http://msdn.microsoft.com/en-us/library/bb762584(VS.85).aspxg | |
http://msdn.microsoft.com/en-us/library/bb762203(VS.85).aspx | |
http://msdn.microsoft.com/en-us/library/bb762204(VS.85).aspx | |
http://msdn.microsoft.com/en-us/library/bb762181(VS.85).aspx | |
} | |
type | |
TCSIDL = ( | |
csidlADMINTOOLS, | |
csidlALTSTARTUP, | |
csidlAPPDATA, | |
csidlBITBUCKET, | |
csidlCDBURN_AREA, | |
csidlCOMMON_ADMINTOOLS, | |
csidlCOMMON_ALTSTARTUP, | |
csidlCOMMON_APPDATA, | |
csidlCOMMON_DESKTOPDIRECTORY, | |
csidlCOMMON_DOCUMENTS, | |
csidlCOMMON_FAVORITES, | |
csidlCOMMON_MUSIC, | |
csidlCOMMON_OEM_LINKS, | |
csidlCOMMON_PICTURES, | |
csidlCOMMON_PROGRAMS, | |
csidlCOMMON_STARTMENU, | |
csidlCOMMON_STARTUP, | |
csidlCOMMON_TEMPLATES, | |
csidlCOMMON_VIDEO, | |
csidlCOMPUTERSNEARME, | |
csidlCONNECTIONS, | |
csidlCONTROLS, | |
csidlCOOKIES, | |
csidlDESKTOP, | |
csidlDESKTOPDIRECTORY, | |
csidlDRIVES, | |
csidlFAVORITES, | |
csidlFONTS, | |
csidlHISTORY, | |
csidlINTERNET, | |
csidlINTERNET_CACHE, | |
csidlLOCAL_APPDATA, | |
csidlMYDOCUMENTS, | |
csidlMYMUSIC, | |
csidlMYPICTURES, | |
csidlMYVIDEO, | |
csidlNETHOOD, | |
csidlNETWORK, | |
csidlPERSONAL, | |
csidlPRINTERS, | |
csidlPRINTHOOD, | |
csidlPROFILE, | |
csidlPROGRAM_FILES, | |
csidlPROGRAM_FILESX86, | |
csidlPROGRAM_FILES_COMMON, | |
csidlPROGRAM_FILES_COMMONX86, | |
csidlPROGRAMS, | |
csidlRECENT, | |
csidlRESOURCES, | |
csidlRESOURCES_LOCALIZED, | |
csidlSENDTO, | |
csidlSTARTMENU, | |
csidlSTARTUP, | |
csidlSYSTEM, | |
csidlSYSTEMX86, | |
csidlTEMPLATES, | |
csidlWINDOWS | |
); | |
function GetCSIDL(const Value: TCSIDL): Word; | |
function GetCSIDLstring(const Value: TCSIDL): string; | |
function GetDescription(const Value: TCSIDL): string; | |
function GetExplanation(const Value: TCSIDL): string; | |
implementation | |
uses | |
SysUtils, TypInfo; | |
type | |
TCSIDLMappingArray = array[TCSIDL] of Word; | |
TCSIDLMappingStringArray = array[TCSIDL] of string; | |
const | |
CSIDLMappingArray: TCSIDLMappingArray = | |
(CSIDL_ADMINTOOLS, | |
CSIDL_ALTSTARTUP, | |
CSIDL_APPDATA, | |
CSIDL_BITBUCKET, | |
CSIDL_CDBURN_AREA, | |
CSIDL_COMMON_ADMINTOOLS, | |
CSIDL_COMMON_ALTSTARTUP, | |
CSIDL_COMMON_APPDATA, | |
CSIDL_COMMON_DESKTOPDIRECTORY, | |
CSIDL_COMMON_DOCUMENTS, | |
CSIDL_COMMON_FAVORITES, | |
CSIDL_COMMON_MUSIC, | |
CSIDL_COMMON_OEM_LINKS, | |
CSIDL_COMMON_PICTURES, | |
CSIDL_COMMON_PROGRAMS, | |
CSIDL_COMMON_STARTMENU, | |
CSIDL_COMMON_STARTUP, | |
CSIDL_COMMON_TEMPLATES, | |
CSIDL_COMMON_VIDEO, | |
CSIDL_COMPUTERSNEARME, | |
CSIDL_CONNECTIONS, | |
CSIDL_CONTROLS, | |
CSIDL_COOKIES, | |
CSIDL_DESKTOP, | |
CSIDL_DESKTOPDIRECTORY, | |
CSIDL_DRIVES, | |
CSIDL_FAVORITES, | |
CSIDL_FONTS, | |
CSIDL_HISTORY, | |
CSIDL_INTERNET, | |
CSIDL_INTERNET_CACHE, | |
CSIDL_LOCAL_APPDATA, | |
CSIDL_MYDOCUMENTS, | |
CSIDL_MYMUSIC, | |
CSIDL_MYPICTURES, | |
CSIDL_MYVIDEO, | |
CSIDL_NETHOOD, | |
CSIDL_NETWORK, | |
CSIDL_PERSONAL, | |
CSIDL_PRINTERS, | |
CSIDL_PRINTHOOD, | |
CSIDL_PROFILE, | |
CSIDL_PROGRAM_FILES, | |
CSIDL_PROGRAM_FILESX86, | |
CSIDL_PROGRAM_FILES_COMMON, | |
CSIDL_PROGRAM_FILES_COMMONX86, | |
CSIDL_PROGRAMS, | |
CSIDL_RECENT, | |
CSIDL_RESOURCES, | |
CSIDL_RESOURCES_LOCALIZED, | |
CSIDL_SENDTO, | |
CSIDL_STARTMENU, | |
CSIDL_STARTUP, | |
CSIDL_SYSTEM, | |
CSIDL_SYSTEMX86, | |
CSIDL_TEMPLATES, | |
CSIDL_WINDOWS | |
); | |
const | |
FOLDERIDMappingArray: TCSIDLMappingStringArray = ( | |
'FOLDERID_AdminTools', | |
'FOLDERID_Startup', | |
'FOLDERID_RoamingAppData', | |
'FOLDERID_RecycleBinFolder', | |
'FOLDERID_CDBurning', | |
'FOLDERID_CommonAdminTools', | |
'FOLDERID_CommonStartup', | |
'FOLDERID_ProgramData', | |
'FOLDERID_PublicDesktop', | |
'FOLDERID_PublicDocuments', | |
'FOLDERID_Favorites', | |
'FOLDERID_PublicMusic', | |
'FOLDERID_CommonOEMLinks', | |
'FOLDERID_PublicPictures', | |
'FOLDERID_CommonPrograms', | |
'FOLDERID_CommonStartMenu', | |
'FOLDERID_CommonStartup', | |
'FOLDERID_CommonTemplates', | |
'FOLDERID_PublicVideos', | |
'FOLDERID_NetworkFolder', | |
'FOLDERID_ConnectionsFolder', | |
'FOLDERID_ControlPanelFolder', | |
'FOLDERID_Cookies', | |
'FOLDERID_Desktop', | |
'FOLDERID_Desktop', | |
'FOLDERID_ComputerFolder', | |
'FOLDERID_Favorites', | |
'FOLDERID_Fonts', | |
'FOLDERID_History', | |
'FOLDERID_InternetFolder', | |
'FOLDERID_InternetCache', | |
'FOLDERID_LocalAppData', | |
'FOLDERID_Documents', | |
'FOLDERID_Music', | |
'FOLDERID_Pictures', | |
'FOLDERID_Videos', | |
'FOLDERID_NetHood', | |
'FOLDERID_NetworkFolder', | |
'FOLDERID_Documents', | |
'FOLDERID_PrintersFolder', | |
'FOLDERID_PrintHood', | |
'FOLDERID_Profile', | |
'FOLDERID_ProgramFiles', | |
'FOLDERID_ProgramFilesX86', | |
'FOLDERID_ProgramFilesCommon', | |
'FOLDERID_ProgramFilesCommonX86', | |
'FOLDERID_Programs', | |
'FOLDERID_Recent', | |
'FOLDERID_ResourceDir', | |
'FOLDERID_LocalizedResourcesDir', | |
'FOLDERID_SendTo', | |
'FOLDERID_StartMenu', | |
'FOLDERID_Startup', | |
'FOLDERID_System', | |
'FOLDERID_SystemX86', | |
'FOLDERID_Templates', | |
'FOLDERID_Windows' | |
); | |
const | |
DescriptionMappingArray: TCSIDLMappingStringArray = ( | |
'Version 5.0. The file system directory that is used to store administrative tools for an individual user.', | |
'The file system directory that corresponds to the user''s nonlocalized Startup program group.', | |
'Version 4.71. The file system directory that serves as a common repository for application-specific data.', | |
'The virtual folder that contains the objects in the user''s Recycle Bin.', | |
'Version 6.0. The file system directory that acts as a staging area for files waiting to be written to a CD.', | |
'Version 5.0. The file system directory that contains administrative tools for all users of the computer.', | |
'The file system directory that corresponds to the nonlocalized Startup program group for all users.', | |
'Version 5.0. The file system directory that contains application data for all users.', | |
'The file system directory that contains files and folders that appear on the desktop for all users.', | |
'The file system directory that contains documents that are common to all users.', | |
'The file system directory that serves as a common repository for favorite items common to all users. Valid only for Windows NT systems.', | |
'Version 6.0. The file system directory that serves as a repository for music files common to all users.', | |
'This value is recognized in Windows Vista for backward compatibility, but the folder itself is no longer used.', | |
'Version 6.0. The file system directory that serves as a repository for image files common to all users.', | |
'The file system directory that contains the directories for the common program groups that appear on the Start menu for all users.', | |
'The file system directory that contains the programs and folders that appear on the Start menu for all users.', | |
'The file system directory that contains the programs that appear in the Startup folder for all users.', | |
'The file system directory that contains the templates that are available to all users.', | |
'Version 6.0. The file system directory that serves as a repository for video files common to all users.', | |
'The folder that represents other computers in your workgroup.', | |
'The virtual folder that represents Network Connections, that contains network and dial-up connections.', | |
'The virtual folder that contains icons for the Control Panel applications.', | |
'The file system directory that serves as a common repository for Internet cookies.', | |
'The virtual folder that represents the Windows desktop, the root of the namespace.', | |
'The file system directory used to physically store file objects on the desktop (not to be confused with the desktop folder itself).', | |
'The virtual folder that represents My Computer, containing everything on the local computer: storage devices, printers, and Control Panel. The folder can also contain mapped network drives.', | |
'The file system directory that serves as a common repository for the user''s favorite items.', | |
'A virtual folder that contains fonts.', | |
'The file system directory that serves as a common repository for Internet history items.', | |
'A virtual folder for Internet Explorer.', | |
'Version 4.72. The file system directory that serves as a common repository for temporary Internet files.', | |
'Version 5.0. The file system directory that serves as a data repository for local (nonroaming) applications.', | |
'Version 6.0. The virtual folder that represents the My Documents desktop item.', | |
'The file system directory that serves as a common repository for music files.', | |
'Version 5.0. The file system directory that serves as a common repository for image files.', | |
'Version 6.0. The file system directory that serves as a common repository for video files.', | |
'A file system directory that contains the link objects that may exist in the My Network Places virtual folder. It is not the same as CSIDL_NETWORK, which represents the network namespace root.', | |
'A virtual folder that represents Network Neighborhood, the root of the network namespace hierarchy.', | |
'Version 6.0. The virtual folder that represents the My Documents desktop item. This is equivalent to CSIDL_MYDOCUMENTS. Previous to Version 6.0. The file system directory used to physically store a user''s common repository of documents.', | |
'The virtual folder that contains installed printers.', | |
'The file system directory that contains the link objects that can exist in the Printers virtual folder.', | |
'Version 5.0. The user''s profile folder.', | |
'Version 5.0. The Program Files folder.', | |
'Version 5.0. The Program Files folder.', | |
'Version 5.0. A folder for components that are shared across applications.', | |
'Version 5.0. A folder for components that are shared across applications.', | |
'The file system directory that contains the user''s program groups (which are themselves file system directories).', | |
'The file system directory that contains shortcuts to the user''s most recently used documents.', | |
'Windows Vista. The file system directory that contains resource data.', | |
'Windows Vista. The file system directory that contains resource data.', | |
'The file system directory that contains Send To menu items.', | |
'The file system directory that contains Start menu items.', | |
'The file system directory that corresponds to the user''s Startup program group. The system starts these programs whenever any user logs onto Windows NT or starts Windows 95.', | |
'Version 5.0. The Windows System folder.', | |
'Version 5.0. The Windows System folder.', | |
'The file system directory that serves as a common repository for document templates. A typical path is C:\Documents and Settings\username\Templates.', | |
'Version 5.0. The Windows directory or SYSROOT. This corresponds to the %windir% or %SYSTEMROOT% environment variables.' | |
); | |
ExplanationMappingArray: TCSIDLMappingStringArray = ( | |
'The Microsoft Management Console (MMC) will save customized consoles to this directory, and it will roam with the user.', | |
'This value is recognized in Windows Vista for backward compatibility, but the folder itself no longer exists.', | |
'A typical path is C:\Documents and Settings\username\Application Data. This CSIDL is supported by the redistributable Shfolder.dll for systems that do not have the Microsoft Internet Explorer 4.0 integrated Shell installed.', | |
'', | |
'A typical path is C:\Documents and Settings\username\Local Settings\Application Data\Microsoft\CD Burning.', | |
'', | |
'Valid only for Microsoft Windows NT systems. This value is recognized in Windows Vista for backward compatibility, but the folder itself no longer exists.', | |
'', | |
'A typical path is C:\Documents and Settings\All Users\Desktop. Valid only for Windows NT systems.', | |
'A typical paths is C:\Documents and Settings\All Users\Documents. Valid for Windows NT systems and Microsoft Windows 95 and Windows 98 systems with Shfolder.dll installed.', | |
'Valid only for Windows NT systems.', | |
'A typical path is C:\Documents and Settings\All Users\Documents\My Music.', | |
'', | |
'A typical path is C:\Documents and Settings\All Users\Documents\My Pictures.', | |
'A typical path is C:\Documents and Settings\All Users\Start Menu\Programs. Valid only for Windows NT systems.', | |
'A typical path is C:\Documents and Settings\All Users\Start Menu. Valid only for Windows NT systems.', | |
'A typical path is C:\Documents and Settings\All Users\Start Menu\Programs\Startup. Valid only for Windows NT systems.', | |
'A typical path is C:\Documents and Settings\All Users\Templates. Valid only for Windows NT systems.', | |
'A typical path is C:\Documents and Settings\All Users\Documents\My Videos.', | |
'', | |
'', | |
'', | |
'A typical path is C:\Documents and Settings\username\Cookies.', | |
'', | |
'A typical path is C:\Documents and Settings\username\Desktop.', | |
'', | |
'A typical path is C:\Documents and Settings\username\Favorites.', | |
'A typical path is C:\Windows\Fonts.', | |
'The file system directory that serves as a common repository for Internet history items.', | |
'', | |
'A typical path is C:\Documents and Settings\username\Local Settings\Temporary Internet Files.', | |
'A typical path is C:\Documents and Settings\username\Local Settings\Application Data.', | |
'This value is equivalent to CSIDL_PERSONAL.', | |
'A typical path is C:\Documents and Settings\User\My Documents\My Music.', | |
'A typical path is C:\Documents and Settings\username\My Documents\My Pictures.', | |
'A typical path is C:\Documents and Settings\username\My Documents\My Videos.', | |
'A typical path is C:\Documents and Settings\username\NetHood.', | |
'A virtual folder that represents Network Neighborhood, the root of the network namespace hierarchy.', | |
'A typical path is C:\Documents and Settings\username\My Documents. This should be distinguished from the virtual My Documents folder in the namespace. To access that virtual folder, use SHGetFolderLocation.', | |
'', | |
'A typical path is C:\Documents and Settings\username\PrintHood.', | |
'', | |
'A typical path is C:\Program Files.', | |
'A typical path is C:\Program Files.', | |
'A typical path is C:\Program Files\Common. Valid only for Windows NT, Windows 2000, and Windows XP systems. Not valid for Windows Millennium Edition (Windows Me).', | |
'A typical path is C:\Program Files\Common. Valid only for Windows NT, Windows 2000, and Windows XP systems. Not valid for Windows Millennium Edition (Windows Me).', | |
'A typical path is C:\Documents and Settings\username\Start Menu\Programs.', | |
'', | |
'A typical path is C:\Windows\Resources.', | |
'A typical path is C:\Windows\Resources.', | |
'A typical path is C:\Documents and Settings\username\SendTo.', | |
'A typical path is C:\Documents and Settings\username\Start Menu.', | |
'A typical path is C:\Documents and Settings\username\Start Menu\Programs\Startup.', | |
'A typical path is C:\Windows\System32.', | |
'A typical path is C:\Windows\System32.', | |
'A typical path is C:\Documents and Settings\username\Templates.', | |
'A typical path is C:\Windows.' | |
); | |
function GetCSIDL(const Value: TCSIDL): Word; | |
begin | |
Result := CSIDLMappingArray[Value]; | |
end; | |
function GetCSIDLstring(const Value: TCSIDL): string; | |
begin | |
Result := GetEnumName(TypeInfo(TCSIDL), Ord(Value)); | |
Result := StringReplace(Result, 'csidl', 'CSIDL_', []); | |
end; | |
function GetDescription(const Value: TCSIDL): string; | |
begin | |
Result := DescriptionMappingArray[Value]; | |
end; | |
function GetExplanation(const Value: TCSIDL): string; | |
begin | |
Result := ExplanationMappingArray[Value]; | |
end; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment