void inputFilename(char* ior_fileName, int i_maxLength) { int okFlag = FALSE; char* tempInputPtr; tempInputPtr = malloc(sizeof(char) * i_maxLength); if (tempInputPtr == NULL) { systemAbort("Out of memory"); } printf_s("Hit Enter to use Current File Name: (\"%s\") ", ior_fileName); printf_s("or input New File Name!\n)"); do { printf_s("Enter File Name = %s = ", ior_fileName); okFlag = readLine(tempInputPtr, i_maxLength); if (okFlag == FALSE) { printf_s("*** Input not accepted! Please try again! ***\n"); } } while (!okFlag); strcpy_s(ior_fileName, i_maxLength, tempInputPtr); free(tempInputPtr); }
string convertToMultiByte(const wstring& wideStr) // convert wchar_t to a multibyte string using the currently assigned locale { // get length of the output excluding the NULL and validate the parameters size_t mbLen = wcstombs(NULL, wideStr.c_str(), 0); if (mbLen == string::npos) systemAbort("Bad char in wide character string"); // convert the characters char* mbStr = new(nothrow) char[mbLen + 1]; if (mbStr == NULL) systemAbort("Bad memory alloc for multi-byte string"); wcstombs(mbStr, wideStr.c_str(), mbLen + 1); // return the string string returnStr = mbStr; delete [] mbStr; return returnStr; }
void cleanTestDirectory(const wstring& directory) // Windows remove files and sub directories from the test directory { WIN32_FIND_DATAW FindFileData; // Find the first file in the directory // Find will get at least "." and "..". wstring firstFile = directory + L"\\*"; HANDLE hFind = ::FindFirstFileW(firstFile.c_str(), &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { displayLastError(); systemAbort(L"Cannot open directory for clean: " + directory); } // remove files and sub directories do { // skip these if (wcscmp(FindFileData.cFileName, L".") == 0 || wcscmp(FindFileData.cFileName, L"..") == 0) continue; // clean and remove sub directories if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { wstring subDirectoryPath = directory + L"\\" + FindFileData.cFileName; cleanTestDirectory(subDirectoryPath); BOOL isRemoved = ::RemoveDirectoryW(subDirectoryPath.c_str()); if (!isRemoved) retryRemoveDirectory(subDirectoryPath); continue; } // remove the file wstring filePathName = directory + L"\\" + FindFileData.cFileName; BOOL isRemoved = ::DeleteFileW(filePathName.c_str()); if (!isRemoved) { displayLastError(); systemAbort(L"Cannot remove file for clean: " + filePathName); } } while (::FindNextFileW(hFind, &FindFileData) != 0); // check for processing error FindClose(hFind); DWORD dwError = GetLastError(); if (dwError != ERROR_NO_MORE_FILES) systemAbort(L"Error processing directory for clean: " + directory); }
void renameDefaultOptionsFile() // Rename a default options file so test functions will not use or overwrite it. { string oldPath = getDefaultOptionsFilePath(); standardizeFileSeparators(oldPath); string newPath = oldPath + ".orig"; int result = rename(oldPath.c_str(), newPath.c_str()); if (result && errno != ENOENT) { string errMessage = "Error renaming default options file: "; errMessage.append(strerror(errno)); systemAbort(errMessage); } }
string getDefaultOptionsFilePath() // Return the path of the default options file for the platform. { #ifdef _WIN32 char* env = getenv("APPDATA"); char name[] = "/astylerc"; #else char* env = getenv("HOME"); char name[] = "/.astylerc"; #endif if (env == nullptr) systemAbort("Cannot get $HOME directory"); return string(env) + name; }
void restoreDefaultOptionsFile() // Restore the original default options file. { string newPath = getDefaultOptionsFilePath(); standardizeFileSeparators(newPath); string oldPath = newPath + ".orig"; int result = rename(oldPath.c_str(), newPath.c_str()); if (result && errno != ENOENT) { string errMessage = "Error restoring default options file: "; errMessage.append(strerror(errno)); systemAbort(errMessage); } }
void retryRemoveDirectory(const wstring& directory) // WINDOWS wait for files and sub-directories to be removed { // sleep a max of 20 seconds for the remove for (size_t seconds = 1; seconds <= 20; seconds++) { sleep(1); BOOL isRemoved = ::RemoveDirectoryW(directory.c_str()); if (isRemoved) { // cout << "remove retry: " << seconds << " seconds" << endl; return; } } displayLastError(); systemAbort(L"Cannot remove file for clean: " + directory); }
void retryCreateDirectory(const string& directory) // WINDOWS wait for directories to be removed { // sleep a max of 20 seconds for the remove for (size_t seconds = 1; seconds <= 20; seconds++) { sleep(1); BOOL ok = ::CreateDirectory(directory.c_str(), NULL); if (ok || GetLastError() == ERROR_ALREADY_EXISTS) { // cout << "create retry: " << seconds << " seconds" << endl; return; } } displayLastError(); systemAbort("Cannot create directory: " + directory); }