Skip to content

Commit 730aafc

Browse files
committed
change delNull, is_dir_writable, and RecursiveCreateDirectory over to the pcjHelper version of each
1 parent 91e342b commit 730aafc

File tree

2 files changed

+15
-75
lines changed

2 files changed

+15
-75
lines changed

src/Classes/CollectionInterfaceClass.cpp

Lines changed: 11 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ bool CollectionInterface::downloadFileToDisk(const std::wstring& url, const std:
5757
::MessageBox(gNppMetaInfo.hwnd._nppHandle, errmsg.c_str(), L"Download Error", MB_ICONERROR);
5858
return false;
5959
}
60-
_wsDeleteTrailingNulls(expandedPath);
60+
pcjHelper::delNull(expandedPath);
6161

6262
// don't download and overwrite the file if it already exists
6363
if (!ask_overwrite_if_exists(expandedPath)) {
@@ -371,98 +371,43 @@ bool CollectionInterface::getListsFromJson(void)
371371
return true;
372372
}
373373

374-
std::wstring& CollectionInterface::_wsDeleteTrailingNulls(std::wstring& str)
375-
{
376-
str.resize(lstrlen(str.c_str()));
377-
return str;
378-
}
379-
380-
BOOL CollectionInterface::_RecursiveCreateDirectory(std::wstring wsPath)
381-
{
382-
std::wstring wsParent = wsPath;
383-
PathRemoveFileSpec(const_cast<LPWSTR>(wsParent.data()));
384-
if (!PathFileExists(wsParent.c_str())) {
385-
BOOL stat = _RecursiveCreateDirectory(wsParent);
386-
if (!stat) return stat;
387-
}
388-
return CreateDirectory(wsPath.c_str(), NULL);
389-
}
390-
391-
392-
bool CollectionInterface::_is_dir_writable(const std::wstring& path)
393-
{
394-
// first grab the directory and make sure it exists
395-
std::wstring cleanFilePath = path;
396-
_wsDeleteTrailingNulls(cleanFilePath);
397-
398-
if (!PathFileExists(cleanFilePath.c_str())) {
399-
BOOL stat = _RecursiveCreateDirectory(cleanFilePath);
400-
if (!stat) {
401-
DWORD errNum = GetLastError();
402-
if (errNum != ERROR_ACCESS_DENIED) {
403-
std::wstring errmsg = L"Could not find or create directory for \"" + path + L"\": " + std::to_wstring(GetLastError()) + L"\n";
404-
::MessageBox(NULL, errmsg.c_str(), L"Directory error", MB_ICONERROR);
405-
}
406-
return false;
407-
}
408-
}
409-
410-
// then create the tempfile name, and see if it is writable
411-
std::wstring tmpFileName = cleanFilePath + L"\\~$TMPFILE.PRYRT";
412-
413-
HANDLE hFile = CreateFile(tmpFileName.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
414-
if (hFile == INVALID_HANDLE_VALUE) {
415-
DWORD errNum = GetLastError();
416-
if (errNum != ERROR_ACCESS_DENIED) {
417-
std::wstring errmsg = L"Error when testing if \"" + path + L"\" is writeable: " + std::to_wstring(GetLastError()) + L"\n";
418-
::MessageBox(NULL, errmsg.c_str(), L"Directory error", MB_ICONERROR);
419-
}
420-
return false;
421-
}
422-
423-
// cleanup
424-
CloseHandle(hFile);
425-
DeleteFile(tmpFileName.c_str());
426-
return true;
427-
}
428-
429374
std::wstring CollectionInterface::getWritableTempDir(void)
430375
{
431376
// first try the system TEMP
432377
std::wstring tempDir(MAX_PATH + 1, L'\0');
433378
GetTempPath(MAX_PATH + 1, const_cast<LPWSTR>(tempDir.data()));
434-
_wsDeleteTrailingNulls(tempDir);
379+
pcjHelper::delNull(tempDir);
435380

436381
// if that fails, try c:\tmp or c:\temp
437-
if (!_is_dir_writable(tempDir)) {
382+
if (!pcjHelper::is_dir_writable(tempDir)) {
438383
tempDir = L"c:\\temp";
439-
_wsDeleteTrailingNulls(tempDir);
384+
pcjHelper::delNull(tempDir);
440385
}
441-
if (!_is_dir_writable(tempDir)) {
386+
if (!pcjHelper::is_dir_writable(tempDir)) {
442387
tempDir = L"c:\\tmp";
443-
_wsDeleteTrailingNulls(tempDir);
388+
pcjHelper::delNull(tempDir);
444389
}
445390

446391
// if that fails, try the %USERPROFILE%
447-
if (!_is_dir_writable(tempDir)) {
392+
if (!pcjHelper::is_dir_writable(tempDir)) {
448393
tempDir.resize(MAX_PATH + 1);
449394
if (!ExpandEnvironmentStrings(L"%USERPROFILE%", const_cast<LPWSTR>(tempDir.data()), MAX_PATH + 1)) {
450395
std::wstring errmsg = L"getWritableTempDir::ExpandEnvirontmentStrings(%USERPROFILE%) failed: " + std::to_wstring(GetLastError()) + L"\n";
451396
::MessageBox(NULL, errmsg.c_str(), L"Directory Error", MB_ICONERROR);
452397
return L"";
453398
}
454-
_wsDeleteTrailingNulls(tempDir);
399+
pcjHelper::delNull(tempDir);
455400
}
456401

457402
// last try: current directory
458-
if (!_is_dir_writable(tempDir)) {
403+
if (!pcjHelper::is_dir_writable(tempDir)) {
459404
tempDir.resize(MAX_PATH + 1);
460405
GetCurrentDirectory(MAX_PATH + 1, const_cast<LPWSTR>(tempDir.data()));
461-
_wsDeleteTrailingNulls(tempDir);
406+
pcjHelper::delNull(tempDir);
462407
}
463408

464409
// if that fails, no other ideas
465-
if (!_is_dir_writable(tempDir)) {
410+
if (!pcjHelper::is_dir_writable(tempDir)) {
466411
std::wstring errmsg = L"getWritableTempDir() cannot find any writable directory; please make sure %TEMP% is defined and writable\n";
467412
::MessageBox(NULL, errmsg.c_str(), L"Directory Error", MB_ICONERROR);
468413
return L"";

src/Classes/CollectionInterfaceClass.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ class CollectionInterface {
3131
bool getListsFromJson(void);
3232

3333
// status methods
34-
bool isUdlDirWritable(void) { return _is_dir_writable(gNppMetaInfo.dir.cfgUdl); };
35-
bool isFunctionListDirWritable(void) { return _is_dir_writable(gNppMetaInfo.dir.cfgFunctionList); };
36-
bool isAutoCompletionDirWritable(void) { return _is_dir_writable(gNppMetaInfo.dir.cfgAutoCompletion); };
37-
bool isThemesDirWritable(void) { return _is_dir_writable(gNppMetaInfo.dir.cfgThemes); };
34+
bool isUdlDirWritable(void) { return pcjHelper::is_dir_writable(gNppMetaInfo.dir.cfgUdl); };
35+
bool isFunctionListDirWritable(void) { return pcjHelper::is_dir_writable(gNppMetaInfo.dir.cfgFunctionList); };
36+
bool isAutoCompletionDirWritable(void) { return pcjHelper::is_dir_writable(gNppMetaInfo.dir.cfgAutoCompletion); };
37+
bool isThemesDirWritable(void) { return pcjHelper::is_dir_writable(gNppMetaInfo.dir.cfgThemes); };
3838
bool areListsPopulated(void) { return _areListsPopulated; };
3939

4040
// if the chosen directory isn't writable, need to be able to use a directory that _is_ writable
@@ -46,10 +46,5 @@ class CollectionInterface {
4646

4747
private:
4848
std::string _xml_unentity(const std::string& text);
49-
std::wstring& _wsDeleteTrailingNulls(std::wstring& text);
50-
bool _is_dir_writable(const std::wstring& path);
51-
BOOL _RecursiveCreateDirectory(std::wstring wsPath);
5249
bool _areListsPopulated;
53-
54-
// TODO: see if trailing nulls, is-writable, and recursive-create can be replaced by pcjHelper instances
5550
};

0 commit comments

Comments
 (0)