std::string CVSStatus::CVSRootForPath(std::string path) { TDEBUG_ENTER("CVSStatus::CVSRootForPath"); TDEBUG_TRACE("path: " << path); // We must be a directory with a CVS dir path = GetDirectoryPart(path); path = EnsureTrailingDelimiter(path); if (!CVSDirectoryHere(path)) { TDEBUG_TRACE("No CVS directory found in " << path); return ""; } std::string root; std::string rootFile = path + "CVS/Root"; std::ifstream in(rootFile.c_str(), std::ios::in); if (!in.good()) { TDEBUG_TRACE("Error opening " << rootFile); return ""; } std::getline(in, root); return root; }
const char *FindCaseInsensitive ( const char *_fullPath ) { if ( !_fullPath ) return NULL; static char retval[PATH_MAX]; char *dir = NULL, *file = NULL; if ( (dir = GetDirectoryPart(_fullPath)) != NULL ) { // Make our own copy of the result, since GetDirectoryPart // and GetFilenamePart use the same variable for temp // storage. dir = newStr(dir); } if ( !dir ) { // No directory provided. Assume working directory. file = newStr(_fullPath); } else { // Kill the last slash dir[strlen(dir) - 1] = '\0'; file = newStr(GetFilenamePart(_fullPath)); } LList <char *> *files = ListDirectory(dir, file); delete [] dir; delete [] file; dir = file = NULL; // We shouldn't have found more than one match. AppAssert(files->Size() <= 1); // No results, so maybe the file does not exist. if ( files->Size() == 0 ) return _fullPath; // Copy the corrected path back, and prepare to return it. memset ( retval, 0, sizeof ( retval ) ); AppAssert ( strlen ( files->GetData(0) ) < PATH_MAX ); strcpy ( retval, files->GetData(0) ); // Negate the possibility of a memory access violation. // This way, we can simply strcpy the result inline without // worrying about a buffer overflow. AppAssert(strlen(retval) == strlen(_fullPath)); while ( files->Size() ) { char *data = files->GetData(0); files->RemoveData(0); delete [] data; } delete files; return retval; }
std::string CVSStatus::CVSRepositoryForPath(std::string path) { TDEBUG_ENTER("CVSStatus::CVSRepositoryForPath"); TDEBUG_TRACE(" path: '" << path << "')"); // We must be a directory with a CVS dir path = GetDirectoryPart(path); path = EnsureTrailingDelimiter(path); if (!CVSDirectoryHere(path)) return ""; std::string rootFile = path + "CVS/Repository"; std::ifstream in(rootFile.c_str(), std::ios::in); if (!in.good()) return ""; std::string repository; std::getline(in, repository); // Apparently, the path in CVS/Repository may be either absolute or relative. // So if it is absolute, we hack it to be relative. TDEBUG_TRACE(" Repository is '" << repository << "')"); if (repository[0] == '/' || (repository.length() > 2 && repository[1] == ':')) { CVSRoot cvsroot(CVSRootForPath(path)); std::string root = cvsroot.GetDirectory(); TDEBUG_TRACE(" Root is '" << root << "')"); unsigned int i = 0; // Find out how many leading chars match while (i < root.length() && i < repository.length()) { if (root[i] == repository[i] || (root[i] == '\\' && repository[i] == '/') || (root[i] == '/' && repository[i] == '\\')) { i++; continue; } else { break; } } // Also kill final slash if (repository[i] == '/' || repository[i] == '\\') ++i; repository = repository.substr(i); } return repository; }
TEST(USystemTest,GetDirectoryPart_ValidPath_ReturnsDirectoryPart) { ASSERT_EQ(L"C:\\temp\\",GetDirectoryPart(L"C:\\temp\\a.txt")); }
TEST(USystemTest,GetDirectoryPart_EmptyPath_ReturnsEmptyString) { ASSERT_EQ(L"",GetDirectoryPart(L"")); }
// Finds all the filenames in the specified directory that match the specified // filter. Directory should be like "textures" or "textures/". // Filter can be NULL or "" or "*.bmp" or "map_*" or "map_*.txt" // Set _longResults to true if you want results like "textures/blah.bmp" // or false for "blah.bmp" LList<char *> *FileSystem::ListArchive(char *_dir, char *_filter, bool fullFilename) { LList<char *> *results = NULL; // // List the search directories for( int i = 0; i < m_searchPath.Size(); ++i ) { char searchPathFilename[512]; snprintf( searchPathFilename, sizeof(searchPathFilename), "%s%s", m_searchPath[i], _dir ); searchPathFilename[ sizeof(searchPathFilename) - 1 ] = '\x0'; LList<char *> *results2 = ListDirectory( searchPathFilename, _filter, fullFilename ); if( !results ) { results = results2; } else { ListArchiveFilterResults( results, results2 ); results2->EmptyAndDelete(); delete results2; } } // // List the localisation directory char localisationFilename[512]; snprintf( localisationFilename, sizeof(localisationFilename), "localisation/%s", _dir ); localisationFilename[ sizeof(localisationFilename) - 1 ] = '\0'; LList<char *> *results3 = ListDirectory( localisationFilename, _filter, fullFilename ); if( !results ) { results = results3; } else { ListArchiveFilterResults( results, results3 ); results3->EmptyAndDelete(); delete results3; } // // List the base data directory LList<char *> *results4 = ListDirectory( _dir, _filter, fullFilename ); ListArchiveFilterResults( results, results4 ); results4->EmptyAndDelete(); delete results4; // // List the pre-loaded resource files if (m_archiveFiles.Size() > 0) { if(_filter == NULL || _filter[0] == '\0') { _filter = "*"; } DArray <char *> *unfilteredResults = m_archiveFiles.ConvertIndexToDArray(); for (int i = 0; i < unfilteredResults->Size(); ++i) { if (!unfilteredResults->ValidIndex(i)) continue; char *fullname = unfilteredResults->GetData(i); char *dirPart = (char *) GetDirectoryPart( fullname ); if( stricmp( dirPart, _dir ) == 0 ) { char *filePart = (char *) GetFilenamePart( fullname ); int result = WildCmp(_filter, filePart); if (result != 0) { if( !fullFilename ) OrderedInsert(results, filePart); else OrderedInsert(results, fullname); } } } delete unfilteredResults; } return results; }
bool ComputeCommitSelection(const CommitDialog::SelectedFiles& userSelection, const FilesWithBugnumbers* modified, const FilesWithBugnumbers* added, const FilesWithBugnumbers* removed, const FilesWithBugnumbers* renamed, std::vector<std::string>& result) { #if 0 // Optimization: If all files are selected, we can simply do a 'cvs commit' in the top-level directories if (Includes(userSelection, modified) && Includes(userSelection, added) && Includes(userSelection, removed) && Includes(userSelection, renamed)) { std::set<std::string> modifiedDirs; if (modified) modifiedDirs = GetTopLevelDirs(*modified); std::set<std::string> addedDirs; if (addedDirs) addedDirs = GetTopLevelDirs(*added); std::set<std::string> removedDirs; if (removedDirs) removedDirs = GetTopLevelDirs(*removed); std::set<std::string> renamedDirs; if (renamedDirs) renamedDirs = GetTopLevelDirs(*renamed); std::set<std::string> dirs1; std::set_union(modifiedDirs.begin(), modifiedDirs.end(), addedDirs.begin(), addedDirs.end(), std::inserter(dirs1, dirs1.begin())); std::set<std::string> dirs2; std::set_union(removedDirs.begin(), removedDirs.end(), renamedDirs.begin(), renamedDirs.end(), std::inserter(dirs2, dirs2.begin())); std::set<std::string> dirs; std::set_union(dirs1.begin(), dirs1.end(), dirs2.begin(), dirs2.end(), std::inserter(dirs, dirs.begin())); result.clear(); for (std::set<std::string>::iterator it = dirs.begin(); it != dirs.end(); ++it) result.push_back(*it); return true; } else #endif { // Get list of non-renamed files result.clear(); for (CommitDialog::SelectedFiles::const_iterator it = userSelection.begin(); it != userSelection.end(); ++it) result.push_back(it->first); // Check if any renames are to be committed std::vector<std::string> renameDirs; for (CommitDialog::SelectedFiles::const_iterator it = userSelection.begin(); it != userSelection.end(); ++it) if (it->second == CVSStatus::STATUS_RENAMED) renameDirs.push_back(GetDirectoryPart(it->first)); // For each directory containing renamed files for (std::vector<std::string>::iterator it = renameDirs.begin(); it != renameDirs.end(); ++it) { // Check if this directory contains any modified files that the user did not select std::vector<std::string> files; if (modified) files = GetFilesInFolder(*modified, *it); for (std::vector<std::string>::iterator fileIter = files.begin(); fileIter != files.end(); ++fileIter) if (find(result.begin(), result.end(), *fileIter) == result.end()) // This file is not selected return false; // Same for added files.clear(); if (added) files = GetFilesInFolder(*added, *it); for (std::vector<std::string>::iterator fileIter = files.begin(); fileIter != files.end(); ++fileIter) if (find(result.begin(), result.end(), *fileIter) == result.end()) // This file is not selected return false; // Same for removed files.clear(); if (removed) files = GetFilesInFolder(*removed, *it); for (std::vector<std::string>::iterator fileIter = files.begin(); fileIter != files.end(); ++fileIter) if (find(result.begin(), result.end(), *fileIter) == result.end()) // This file is not selected return false; // Same for renamed files.clear(); if (renamed) files = GetFilesInFolder(*renamed, *it); for (std::vector<std::string>::iterator fileIter = files.begin(); fileIter != files.end(); ++fileIter) if (find(result.begin(), result.end(), *fileIter) == result.end()) // This file is not selected return false; } // Remove all files from the directories where renames are being committed result.clear(); for (CommitDialog::SelectedFiles::const_iterator it = userSelection.begin(); it != userSelection.end(); ++it) { std::string dir = GetDirectoryPart(it->first); if (find(renameDirs.begin(), renameDirs.end(), dir) == renameDirs.end()) result.push_back(it->first); } // Add directories where renames are being committed for (std::vector<std::string>::iterator it = renameDirs.begin(); it != renameDirs.end(); ++it) result.push_back(*it); } return true; }