Skip to content

Commit

Permalink
Remove some unnecessary path manipulation helpers (#77301)
Browse files Browse the repository at this point in the history
  • Loading branch information
elinor-fung authored Oct 21, 2022
1 parent 77331bb commit 2f9418d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 289 deletions.
26 changes: 0 additions & 26 deletions src/coreclr/inc/utilcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -958,25 +958,6 @@ void SplitPathInterior(
_Out_opt_ LPCWSTR *pwszExt, _Out_opt_ size_t *pcchExt);


void MakePath(_Out_ CQuickWSTR &path,
_In_ LPCWSTR drive,
_In_ LPCWSTR dir,
_In_ LPCWSTR fname,
_In_ LPCWSTR ext);

WCHAR * FullPath(_Out_writes_ (maxlen) WCHAR *UserBuf, const WCHAR *path, size_t maxlen);

//*****************************************************************************
//
// SString version of the path functions.
//
//*****************************************************************************
void SplitPath(_In_ SString const &path,
__inout_opt SString *drive,
__inout_opt SString *dir,
__inout_opt SString *fname,
__inout_opt SString *ext);

#include "ostype.h"

#define CLRGetTickCount64() GetTickCount64()
Expand Down Expand Up @@ -4433,13 +4414,6 @@ inline T* InterlockedCompareExchangeT(
// Returns the directory for clr module. So, if path was for "C:\Dir1\Dir2\Filename.DLL",
// then this would return "C:\Dir1\Dir2\" (note the trailing backslash).
HRESULT GetClrModuleDirectory(SString& wszPath);
HRESULT CopySystemDirectory(const SString& pPathString, SString& pbuffer);

HMODULE LoadLocalizedResourceDLLForSDK(_In_z_ LPCWSTR wzResourceDllName, _In_opt_z_ LPCWSTR modulePath=NULL, bool trySelf=true);
// This is a slight variation that can be used for anything else
typedef void* (__cdecl *LocalizedFileHandler)(LPCWSTR);
void* FindLocalizedFile(_In_z_ LPCWSTR wzResourceDllName, LocalizedFileHandler lfh, _In_opt_z_ LPCWSTR modulePath=NULL);


namespace Clr { namespace Util
{
Expand Down
9 changes: 5 additions & 4 deletions src/coreclr/md/enc/stgio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,14 @@ HRESULT StgIO::Open( // Return code.
m_fFlags = fFlags;
if ((szName != NULL) && (*szName != 0))
{
WCHAR rcExt[_MAX_PATH];
SplitPath(szName, NULL, 0, NULL, 0, NULL, 0, rcExt, _MAX_PATH);
if (SString::_wcsicmp(rcExt, W(".obj")) == 0)
LPCWSTR ext;
size_t extSize;
SplitPathInterior(szName, NULL, 0, NULL, 0, NULL, 0, &ext, &extSize);
if (SString::_wcsicmp(ext, W(".obj")) == 0)
{
m_FileType = FILETYPE_NTOBJ;
}
else if (SString::_wcsicmp(rcExt, W(".tlb")) == 0)
else if (SString::_wcsicmp(ext, W(".tlb")) == 0)
{
m_FileType = FILETYPE_TLB;
}
Expand Down
166 changes: 3 additions & 163 deletions src/coreclr/utilcode/makepath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,147 +10,9 @@
*
*******************************************************************************/
#include "stdafx.h"
#include "winwrap.h"
#include "utilcode.h"
#include "ex.h"


/***
*void Makepath() - build path name from components
*
*Purpose:
* create a path name from its individual components
*
*Entry:
* CQuickWSTR &szPath - Buffer for constructed path
* WCHAR *drive - pointer to drive component, may or may not contain
* trailing ':'
* WCHAR *dir - pointer to subdirectory component, may or may not include
* leading and/or trailing '/' or '\' characters
* WCHAR *fname - pointer to file base name component
* WCHAR *ext - pointer to extension component, may or may not contain
* a leading '.'.
*
*Exit:
* path - pointer to constructed path name
*
*Exceptions:
*
*******************************************************************************/

void MakePath (
_Out_ CQuickWSTR &szPath,
_In_ LPCWSTR drive,
_In_ LPCWSTR dir,
_In_ LPCWSTR fname,
_In_ LPCWSTR ext
)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END

SIZE_T maxCount = 4 // Possible separators between components, plus null terminator
+ (drive != nullptr ? 2 : 0)
+ (dir != nullptr ? wcslen(dir) : 0)
+ (fname != nullptr ? wcslen(fname) : 0)
+ (ext != nullptr ? wcslen(ext) : 0);
LPWSTR path = szPath.AllocNoThrow(maxCount);

const WCHAR *p;
DWORD count = 0;

/* we assume that the arguments are in the following form (although we
* do not diagnose invalid arguments or illegal filenames (such as
* names longer than 8.3 or with illegal characters in them)
*
* drive:
* A ; or
* A:
* dir:
* \top\next\last\ ; or
* /top/next/last/ ; or
* either of the above forms with either/both the leading
* and trailing / or \ removed. Mixed use of '/' and '\' is
* also tolerated
* fname:
* any valid file name
* ext:
* any valid extension (none if empty or null )
*/

/* copy drive */

if (drive && *drive) {
*path++ = *drive;
*path++ = _T(':');
count += 2;
}

/* copy dir */

if ((p = dir)) {
while (*p) {
*path++ = *p++;
count++;

_ASSERTE(count < maxCount);
}

// suppress warning for the following line; this is safe but would require significant code
// delta for prefast to understand.
#ifdef _PREFAST_
#pragma warning( suppress: 26001 )
#endif
if (*(p-1) != _T('/') && *(p-1) != _T('\\')) {
*path++ = _T('\\');
count++;

_ASSERTE(count < maxCount);
}
}

/* copy fname */

if ((p = fname)) {
while (*p) {
*path++ = *p++;
count++;

_ASSERTE(count < maxCount);
}
}

/* copy ext, including 0-terminator - check to see if a '.' needs
* to be inserted.
*/

if ((p = ext)) {
if (*p && *p != _T('.')) {
*path++ = _T('.');
count++;

_ASSERTE(count < maxCount);
}

while ((*path++ = *p++)) {
count++;

_ASSERTE(count < maxCount);
}
}
else {
/* better add the 0-terminator */
*path = _T('\0');
}

szPath.Shrink(count + 1);
}


// Returns the directory for clr module. So, if path was for "C:\Dir1\Dir2\Filename.DLL",
// then this would return "C:\Dir1\Dir2\" (note the trailing backslash).HRESULT GetClrModuleDirectory(SString& wszPath)
HRESULT GetClrModuleDirectory(SString& wszPath)
Expand All @@ -170,36 +32,14 @@ HRESULT GetClrModuleDirectory(SString& wszPath)
return HRESULT_FROM_GetLastError();
}

CopySystemDirectory(wszPath, wszPath);
return S_OK;
}

//
// Returns path name from a file name.
// Example: For input "C:\Windows\System.dll" returns "C:\Windows\".
// Warning: The input file name string might be destroyed.
//
// Arguments:
// pPathString - [in] SString with file name
//
// pBuffer - [out] SString .
//
// Return Value:
// S_OK - Output buffer contains path name.
// other errors - If Sstring throws.
//
HRESULT CopySystemDirectory(const SString& pPathString,
SString& pbuffer)
{
HRESULT hr = S_OK;
EX_TRY
{
pbuffer.Set(pPathString);
SString::Iterator iter = pbuffer.End();
if (pbuffer.FindBack(iter,DIRECTORY_SEPARATOR_CHAR_W))
SString::Iterator iter = wszPath.End();
if (wszPath.FindBack(iter,DIRECTORY_SEPARATOR_CHAR_W))
{
iter++;
pbuffer.Truncate(iter);
wszPath.Truncate(iter);
}
else
{
Expand Down
103 changes: 7 additions & 96 deletions src/coreclr/utilcode/splitpath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@
#include "stdafx.h"
#include "winwrap.h"
#include "utilcode.h"
#include "sstring.h"


/***
*SplitPath() - split a path name into its individual components
*SplitPathInterior()
*
*Purpose:
* to split a path name into its individual components
* Split a path name into its individual components
* Just points to each section of the string.
*
*Entry:
* path - pointer to path name to be parsed
* drive - pointer to buffer for drive component, if any
* dir - pointer to buffer for subdirectory component, if any
* fname - pointer to buffer for file base name component, if any
* ext - pointer to buffer for file name extension component, if any
* drive - pointer to set to drive component, if any
* dir - pointer to set to subdirectory component, if any
* fname - pointer to set to file base name component, if any
* ext - pointer to set to file name extension component, if any
*
*Exit:
* drive - pointer to drive string. Includes ':' if a drive was given.
Expand All @@ -39,41 +38,6 @@
*Exceptions:
*
*******************************************************************************/

void SplitPath(
const WCHAR *path,
__inout_z __inout_ecount_opt(driveSizeInWords) WCHAR *drive, int driveSizeInWords,
__inout_z __inout_ecount_opt(dirSizeInWords) WCHAR *dir, int dirSizeInWords,
__inout_z __inout_ecount_opt(fnameSizeInWords) WCHAR *fname, size_t fnameSizeInWords,
__inout_z __inout_ecount_opt(extSizeInWords) WCHAR *ext, size_t extSizeInWords)
{
WRAPPER_NO_CONTRACT;

LPCWSTR _wszDrive, _wszDir, _wszFileName, _wszExt;
size_t _cchDrive, _cchDir, _cchFileName, _cchExt;

SplitPathInterior(path,
&_wszDrive, &_cchDrive,
&_wszDir, &_cchDir,
&_wszFileName, &_cchFileName,
&_wszExt, &_cchExt);

if (drive && _wszDrive)
wcsncpy_s(drive, driveSizeInWords, _wszDrive, min(_cchDrive, _MAX_DRIVE));

if (dir && _wszDir)
wcsncpy_s(dir, dirSizeInWords, _wszDir, min(_cchDir, _MAX_DIR));

if (fname && _wszFileName)
wcsncpy_s(fname, fnameSizeInWords, _wszFileName, min(_cchFileName, _MAX_FNAME));

if (ext && _wszExt)
wcsncpy_s(ext, extSizeInWords, _wszExt, min(_cchExt, _MAX_EXT));
}

//*******************************************************************************
// A much more sensible version that just points to each section of the string.
//*******************************************************************************
void SplitPathInterior(
_In_ LPCWSTR wszPath,
_Out_opt_ LPCWSTR *pwszDrive, _Out_opt_ size_t *pcchDrive,
Expand Down Expand Up @@ -203,56 +167,3 @@ void SplitPathInterior(
}
}
}

/***
*SplitPath() - split a path name into its individual components
*
*Purpose:
* to split a path name into its individual components
*
*Entry:
* path - SString representing the path name to be parsed
* drive - Out SString for drive component
* dir - Out SString for subdirectory component
* fname - Out SString for file base name component
* ext - Out SString for file name extension component
*
*Exit:
* drive - Drive string. Includes ':' if a drive was given.
* dir - Subdirectory string. Includes leading and trailing
* '/' or '\', if any.
* fname - File base name
* ext - File extension, if any. Includes leading '.'.
*
*Exceptions:
*
*******************************************************************************/

void SplitPath(_In_ SString const &path,
__inout_opt SString *drive,
__inout_opt SString *dir,
__inout_opt SString *fname,
__inout_opt SString *ext)
{
LPCWSTR wzDrive, wzDir, wzFname, wzExt;
size_t cchDrive, cchDir, cchFname, cchExt;

SplitPathInterior(path,
&wzDrive, &cchDrive,
&wzDir, &cchDir,
&wzFname, &cchFname,
&wzExt, &cchExt);

if (drive != NULL)
drive->Set(wzDrive, (COUNT_T)cchDrive);

if (dir != NULL)
dir->Set(wzDir, (COUNT_T)cchDir);

if (fname != NULL)
fname->Set(wzFname, (COUNT_T)cchFname);

if (ext != NULL)
ext->Set(wzExt, (COUNT_T)cchExt);
}

0 comments on commit 2f9418d

Please sign in to comment.