MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / SplitPath

Function SplitPath

source/script2.cpp:6076–6223  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6074
6075
6076ResultType SplitPath(LPCTSTR aFileSpec, Var *output_var_name, Var *output_var_dir, Var *output_var_ext, Var *output_var_name_no_ext, Var *output_var_drive)
6077{
6078 // For URLs, "drive" is defined as the server name, e.g. http://somedomain.com
6079 LPCTSTR name = _T(""), name_delimiter = NULL, drive_end = NULL; // Set defaults to improve maintainability.
6080 LPCTSTR drive = omit_leading_whitespace(aFileSpec); // i.e. whitespace is considered for everything except the drive letter or server name, so that a pathless filename can have leading whitespace.
6081 LPCTSTR colon_double_slash = _tcsstr(aFileSpec, _T("://"));
6082
6083 if (colon_double_slash) // This is a URL such as ftp://... or http://...
6084 {
6085 if ( !(drive_end = _tcschr(colon_double_slash + 3, '/')) )
6086 {
6087 if ( !(drive_end = _tcschr(colon_double_slash + 3, '\\')) ) // Try backslash so that things like file://C:\Folder\File.txt are supported.
6088 drive_end = colon_double_slash + _tcslen(colon_double_slash); // Set it to the position of the zero terminator instead.
6089 // And because there is no filename, leave name and name_delimiter set to their defaults.
6090 //else there is a backslash, e.g. file://C:\Folder\File.txt, so treat that backslash as the end of the drive name.
6091 }
6092 name_delimiter = drive_end; // Set default, to be possibly overridden below.
6093 // Above has set drive_end to one of the following:
6094 // 1) The slash that occurs to the right of the doubleslash in a URL.
6095 // 2) The backslash that occurs to the right of the doubleslash in a URL.
6096 // 3) The zero terminator if there is no slash or backslash to the right of the doubleslash.
6097 if (*drive_end) // A slash or backslash exists to the right of the server name.
6098 {
6099 if (*(drive_end + 1))
6100 {
6101 // Find the rightmost slash. At this stage, this is known to find the correct slash.
6102 // In the case of a file at the root of a domain such as http://domain.com/root_file.htm,
6103 // the directory consists of only the domain name, e.g. http://domain.com. This is because
6104 // the directory always the "drive letter" by design, since that is more often what the
6105 // caller wants. A script can use StringReplace to remove the drive/server portion from
6106 // the directory, if desired.
6107 name_delimiter = _tcsrchr(aFileSpec, '/');
6108 if (name_delimiter == colon_double_slash + 2) // To reach this point, it must have a backslash, something like file://c:\folder\file.txt
6109 name_delimiter = _tcsrchr(aFileSpec, '\\'); // Will always be found.
6110 name = name_delimiter + 1; // This will be the empty string for something like http://domain.com/dir/
6111 }
6112 //else something like http://domain.com/, so leave name and name_delimiter set to their defaults.
6113 }
6114 //else something like http://domain.com, so leave name and name_delimiter set to their defaults.
6115 }
6116 else // It's not a URL, just a file specification such as c:\my folder\my file.txt, or \\server01\folder\file.txt
6117 {
6118 // Differences between _splitpath() and the method used here:
6119 // _splitpath() doesn't include drive in output_var_dir, it includes a trailing
6120 // backslash, it includes the . in the extension, it considers ":" to be a filename.
6121 // _splitpath(pathname, drive, dir, file, ext);
6122 //char sdrive[16], sdir[MAX_PATH], sname[MAX_PATH], sext[MAX_PATH];
6123 //_splitpath(aFileSpec, sdrive, sdir, sname, sext);
6124 //if (output_var_name_no_ext)
6125 // output_var_name_no_ext->Assign(sname);
6126 //strcat(sname, sext);
6127 //if (output_var_name)
6128 // output_var_name->Assign(sname);
6129 //if (output_var_dir)
6130 // output_var_dir->Assign(sdir);
6131 //if (output_var_ext)
6132 // output_var_ext->Assign(sext);
6133 //if (output_var_drive)

Callers 1

BIF_DECLFunction · 0.85

Calls 2

omit_leading_whitespaceFunction · 0.85
AssignMethod · 0.45

Tested by

no test coverage detected