-// XML.cpp
-
+/*
+ * Copyright (C) 2005 Casper S. Hornstrup
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
#include "pch.h"
-#ifdef _MSC_VER
+#ifndef MAX_PATH
#define MAX_PATH _MAX_PATH
#endif
string
Path::RelativeFromWorkingDirectory ( const string& path )
{
- vector<string> vwork, vpath, vout;
- Path::Split ( vwork, working_directory, true );
+ return Path::RelativeFromDirectory ( path, working_directory );
+}
+
+string
+Path::RelativeFromDirectory (
+ const string& path,
+ const string& base_directory )
+{
+ vector<string> vbase, vpath, vout;
+ Path::Split ( vbase, base_directory, true );
Path::Split ( vpath, path, true );
#ifdef WIN32
// this squirreliness is b/c win32 has drive letters and *nix doesn't...
// not possible to do relative across different drive letters
- if ( vwork[0] != vpath[0] )
- return path;
+ {
+ char path_driveletter = (path[1] == ':') ? toupper(path[0]) : 0;
+ char base_driveletter = (base_directory[1] == ':') ? toupper(base_directory[0]) : 0;
+ if ( path_driveletter != base_driveletter )
+ return path;
+ }
#endif
size_t i = 0;
- while ( i < vwork.size() && i < vpath.size() && vwork[i] == vpath[i] )
+ while ( i < vbase.size() && i < vpath.size() && vbase[i] == vpath[i] )
++i;
- if ( i < vwork.size() )
+
+ // did we go through all of the path?
+ if ( vbase.size() == vpath.size() && i == vpath.size() )
+ return ".";
+
+ if ( i < vbase.size() )
{
- // path goes above our working directory, we will need some ..'s
- for ( size_t j = 0; j < i; j++ )
+ // path goes above our base directory, we will need some ..'s
+ for ( size_t j = i; j < vbase.size(); j++ )
vout.push_back ( ".." );
}
+
while ( i < vpath.size() )
vout.push_back ( vpath[i++] );
out.resize ( 0 );
while ( p )
{
- out.push_back ( prev );
+ if ( strcmp ( prev, "." ) )
+ out.push_back ( prev );
prev = p;
p = strtok ( NULL, "/\\" );
}
- if ( include_last )
+ if ( include_last && strcmp ( prev, "." ) )
out.push_back ( prev );
+ // special-case where path only has "."
+ // don't move this check up higher as it might miss
+ // some funny paths...
+ if ( !out.size() && !strcmp ( prev, "." ) )
+ out.push_back ( "." );
}
XMLFile::XMLFile()