void setTestDirectory() // set the global variable g_testDirectory { // define unicode and multi-byte variables #ifdef _WIN32 string envVar = "%USERPROFILE%"; string varName = envVar.substr(1, envVar.length() - 2); #else string envVar = "$HOME"; string varName = envVar.substr(1, envVar.length() - 1); #endif // get replacement for environment variable char* envPath = getenv(varName.c_str()); if (envPath == NULL) ASTYLE_ABORT("Bad char in wide character string: " + envVar); // replace the environment variable size_t iEnv = g_testDirectory.find(envVar.c_str()); if (iEnv == string::npos) ASTYLE_ABORT("Cannot find environmental variable: " + envVar); g_testDirectory.replace(iEnv, envVar.length(), envPath); // check that the primary directory exists size_t iPrim = g_testDirectory.find_last_of("\\/"); if (iPrim == string::npos) ASTYLE_ABORT("Cannot find ending separator: " + g_testDirectory); string primaryDirectory = g_testDirectory.substr(0, iPrim); struct stat stBuf; if (stat(primaryDirectory.c_str(), &stBuf) == -1) ASTYLE_ABORT("Primary directory does not exist: " + primaryDirectory); }
void createTestFile(const string& testFilePath, const char* testFileText, int size /*0*/) // write a test file to the test directory // the size option is for 16 and 32 bit UTF files with NULs in the text { // verify test directory string testDir = getTestDirectory(); if (testFilePath.compare(0, testDir.length(), testDir) != 0 || !(testFilePath[testDir.length()] == '/' || testFilePath[testDir.length()] == '\\')) ASTYLE_ABORT("File not written to test directory: " + testFilePath); // write the output file ofstream fout(testFilePath.c_str(), ios::binary | ios::trunc); if (!fout) { #ifdef _WIN32 displayLastError(); #endif ASTYLE_ABORT("Cannot open output file: " + testFilePath); } if (size == 0) fout << testFileText; else fout.write(testFileText, size); fout.close(); }
void cleanTestDirectory(const string& directory) // LINUX remove files and sub directories from the test directory { struct dirent* entry; // entry from readdir() struct stat statbuf; // entry from stat() vector<string> subDirectory; // sub directories of this directory // errno is defined in <errno.h> and is set for errors in opendir, readdir, or stat errno = 0; // open directory stream DIR* dp = opendir(directory.c_str()); if (errno) ASTYLE_ABORT(string(strerror(errno)) + "\nCannot open directory for clean: " + directory); // remove files and sub directories while ((entry = readdir(dp)) != NULL) { // get file status string entryFilepath = directory + '/' + entry->d_name; stat(entryFilepath.c_str(), &statbuf); if (errno) ASTYLE_ABORT(string(strerror(errno)) + "\nError getting file status for clean: " + directory); // skip these if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; // clean and remove sub directories if (S_ISDIR(statbuf.st_mode)) { string subDirectoryPath = directory + '/' + entry->d_name; cleanTestDirectory(subDirectoryPath); rmdir(subDirectoryPath.c_str()); if (errno) ASTYLE_ABORT(string(strerror(errno)) + "\nCannot remove directory for clean: " + subDirectoryPath); continue; } // remove the file if (S_ISREG(statbuf.st_mode)) { string filePathName = directory + '/' + entry->d_name; remove(filePathName.c_str()); if (errno) ASTYLE_ABORT(string(strerror(errno)) + "\nCannot remove file for clean: " + filePathName); } } closedir(dp); if (errno) ASTYLE_ABORT(string(strerror(errno)) + "\nError processing directory for clean: " + directory); }
wstring convertToWideChar(const string& mbStr) // convert multibyte to wchar_t using the currently assigned locale { // get length of the output excluding the NULL and validate the parameters size_t wcLen = mbstowcs(NULL, mbStr.c_str(), 0); if (wcLen == string::npos) ASTYLE_ABORT("Bad char in multi-byte string"); // convert the characters wchar_t* wcStr = new(nothrow) wchar_t[wcLen + 1]; if (wcStr == NULL) ASTYLE_ABORT("Bad memory alloc for wide character string"); mbstowcs(wcStr, mbStr.c_str(), wcLen + 1); // return the string wstring returnStr = wcStr; delete [] wcStr; return returnStr; }
void removeTestFile(const string& testFileName) // remove a test file { errno = 0; remove(testFileName.c_str()); if (errno) ASTYLE_ABORT(string(strerror(errno)) + "\nCannot remove test file: " + testFileName); }
string getCurrentDirectory() // get the current working directory { bool gotDirectory = true; string currentDirectory; #ifdef _WIN32 char currdir[MAX_PATH]; currdir[0] = '\0'; if (GetCurrentDirectory(sizeof(currdir), currdir)) currentDirectory = currdir; else gotDirectory = false; #else char* currdir = getenv("PWD"); if (currdir != NULL) currentDirectory = currdir; else gotDirectory = false; #endif if (!gotDirectory) ASTYLE_ABORT("Cannot get current directory"); return currentDirectory; }