summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2005-03-25 13:09:00 (GMT)
committerBrad King <brad.king@kitware.com>2005-03-25 13:09:00 (GMT)
commit4ba14d1278796d6b82f3b5bb89a6121ba4e4ff76 (patch)
treec69e409d5d60052dfacc8bfb7211a7dfb4d536d6
parent7219c7a75cd0ef640b597516d2928f085a506ce1 (diff)
downloadCMake-4ba14d1278796d6b82f3b5bb89a6121ba4e4ff76.zip
CMake-4ba14d1278796d6b82f3b5bb89a6121ba4e4ff76.tar.gz
CMake-4ba14d1278796d6b82f3b5bb89a6121ba4e4ff76.tar.bz2
ENH: Re-implemented CollapseFullPath to not need to change directories. Operation is now fully string based.
-rw-r--r--Source/kwsys/SystemTools.cxx106
1 files changed, 64 insertions, 42 deletions
diff --git a/Source/kwsys/SystemTools.cxx b/Source/kwsys/SystemTools.cxx
index fc7e216..5807111 100644
--- a/Source/kwsys/SystemTools.cxx
+++ b/Source/kwsys/SystemTools.cxx
@@ -2085,60 +2085,82 @@ void SystemTools::CheckTranslationPath(kwsys_stl::string & path)
path.erase(path.end()-1, path.end());
}
-kwsys_stl::string SystemTools::CollapseFullPath(const char* in_relative,
- const char* in_base)
+void
+SystemToolsAppendComponents(
+ kwsys_stl::vector<kwsys_stl::string>& out_components,
+ kwsys_stl::vector<kwsys_stl::string>::const_iterator first,
+ kwsys_stl::vector<kwsys_stl::string>::const_iterator last)
{
- kwsys_stl::string orig;
-
- // Change to base of relative path.
- if(in_base)
+ for(kwsys_stl::vector<kwsys_stl::string>::const_iterator i = first;
+ i != last; ++i)
{
- // Save original working directory.
- orig = SystemTools::GetCurrentWorkingDirectory();
- Chdir(in_base);
+ if(*i == "..")
+ {
+ if(out_components.begin() != out_components.end())
+ {
+ out_components.erase(out_components.end()-1, out_components.end());
+ }
+ }
+ else if(*i != "." && *i != "")
+ {
+ out_components.push_back(*i);
+ }
}
-
- kwsys_stl::string dir, file;
- SystemTools::SplitProgramPath(in_relative, dir, file, false);
- if(dir.size() == 0 &&
- in_relative && strlen(in_relative) > 0 &&
- in_relative[0] == '/')
- {
- dir = "/";
- }
+}
- // Resolve relative path.
- kwsys_stl::string newDir;
- if(!(dir == ""))
- {
- Realpath(dir.c_str(), newDir);
- }
- else
- {
- newDir = SystemTools::GetCurrentWorkingDirectory();
- }
+kwsys_stl::string SystemTools::CollapseFullPath(const char* in_path,
+ const char* in_base)
+{
+ // Collect the output path components.
+ kwsys_stl::vector<kwsys_stl::string> out_components;
- if(in_base)
- {
- // Restore original working directory.
- Chdir(orig.c_str());
- }
-
- // Construct and return the full path.
- kwsys_stl::string newPath = newDir;
- if(!(file == ""))
+ // Split the input path components.
+ kwsys_stl::vector<kwsys_stl::string> path_components;
+ SystemTools::SplitPath(in_path, path_components);
+
+ // If the input path is relative, start with a base path.
+ if(path_components[0].length() == 0)
{
- if(!(newDir.size() == 1 && newDir[0] == '/'))
+ kwsys_stl::vector<kwsys_stl::string> base_components;
+ if(in_base)
+ {
+ // Use the given base path.
+ SystemTools::SplitPath(in_base, base_components);
+ }
+ else
{
- newPath += "/";
+ // Use the current working directory as a base path.
+ char buf[2048];
+ if(const char* cwd = Getcwd(buf, 2048))
+ {
+ SystemTools::SplitPath(cwd, base_components);
+ }
+ else
+ {
+ // ??
+ }
}
- newPath += file;
+
+ // Append base path components to the output path.
+ out_components.push_back(base_components[0]);
+ SystemToolsAppendComponents(out_components,
+ base_components.begin()+1,
+ base_components.end());
}
- // Now we need to update the translation table with this potentially new path
- SystemTools::AddTranslationPath(newPath.c_str(), in_relative);
+ // Append input path components to the output path.
+ SystemToolsAppendComponents(out_components,
+ path_components.begin(),
+ path_components.end());
+
+ // Transform the path back to a string.
+ kwsys_stl::string newPath = SystemTools::JoinPath(out_components);
+
+ // Update the translation table with this potentially new path.
+ SystemTools::AddTranslationPath(newPath.c_str(), in_path);
SystemTools::CheckTranslationPath(newPath);
+ // Return the reconstructed path.
return newPath;
}