-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathFileReferenceResolver.cs
More file actions
84 lines (76 loc) · 2.69 KB
/
FileReferenceResolver.cs
File metadata and controls
84 lines (76 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.IO;
namespace Xbim.IO
{
class FileReferenceResolver
{
internal static string EvaluateRelativePath(string mainDirPath, string absoluteFilePath)
{
var
firstPathParts = mainDirPath.Trim(Path.DirectorySeparatorChar).Split(Path.DirectorySeparatorChar);
var
secondPathParts = absoluteFilePath.Trim(Path.DirectorySeparatorChar).Split(Path.DirectorySeparatorChar);
var sameCounter = 0;
for (var i = 0; i < Math.Min(firstPathParts.Length,
secondPathParts.Length); i++)
{
if (
!firstPathParts[i].ToLower().Equals(secondPathParts[i].ToLower()))
{
break;
}
sameCounter++;
}
if (sameCounter == 0)
{
return absoluteFilePath;
}
var newPath = String.Empty;
for (var i = sameCounter; i < firstPathParts.Length; i++)
{
if (i > sameCounter)
{
newPath += Path.DirectorySeparatorChar;
}
newPath += "..";
}
if (newPath.Length == 0)
{
newPath = ".";
}
for (var i = sameCounter; i < secondPathParts.Length; i++)
{
newPath += Path.DirectorySeparatorChar;
newPath += secondPathParts[i];
}
return newPath;
}
internal static List<string> ResourceAlternatives(string ResFileName, string ProjFilePrev, string ProjFileCurr)
{
var _ret = new List<string>();
if (File.Exists(ResFileName))
_ret.Add(ResFileName);
try
{
var ProjFolderCurr = Path.GetDirectoryName(ProjFileCurr);
var ProjFolderPrev = Path.GetDirectoryName(ProjFilePrev);
// if project folder has not changed then just return the first option
if (ProjFolderCurr == ProjFolderPrev)
{
return _ret;
}
var RelativeToPrev = EvaluateRelativePath(ProjFolderPrev, ResFileName);
var AbsoluteToCurr = Path.GetFullPath(Path.Combine(ProjFolderCurr, RelativeToPrev));
if (AbsoluteToCurr == ResFileName || !File.Exists(AbsoluteToCurr))
return _ret;
_ret.Add(AbsoluteToCurr);
}
catch
{
// nothing to do
}
return _ret;
}
}
}