using System; using System.Collections.Generic; using System.Text; namespace Perforce.P4 { /// /// Describes the path and path type in a file spec. /// public abstract class PathSpec { protected PathSpec() { } public PathSpec(string path) { Path = path; } private string _path; public string Path { get; protected set; } public override string ToString() { return Path; } public override bool Equals(object obj) { if (obj == null) { return false; } if (obj is PathSpec) { PathSpec o = obj as PathSpec; return ((this.GetType() == o.GetType()) && (this.Path == o.Path)); } return false; } public override int GetHashCode() { return Path.GetHashCode(); } protected string NormalizeDepot(string path) { string val = ""; if (path != null) { val = path.Trim('"'); } return val; } protected string NormalizeLocal(string path) { string val = ""; if (path != null) { val = path.Trim('"', ' '); val = val.Replace('/', '\\'); } return val; } private static string GetFileName(string path) { String name = path; if (!String.IsNullOrEmpty(path)) { int idx = path.LastIndexOf('/'); if (idx >= 0) { name = path.Substring(idx + 1); } else { idx = path.LastIndexOf('\\'); if (idx >= 0) { name = path.Substring(idx + 1); } } } return name; } private static string GetDirectoryName(string path) { String name = path; if (!String.IsNullOrEmpty(path)) { int idx = path.LastIndexOf('/'); if (idx >= 0) { name = path.Substring(0, idx); } else { idx = path.LastIndexOf('\\'); if (idx >= 0) { name = path.Substring(0, idx); } } } return name; } public string GetFileName() { return GetFileName(Path); } public string GetDirectoryName() { return GetDirectoryName(Path); } public static implicit operator FileSpec(PathSpec s) { return new FileSpec(s,null); } /// /// Unescape a string. /// /// Paths to unescape public static string UnescapePath(string p) { if (p==null) { return p; } string v = p.Replace("%2A", "*"); v = v.Replace("%23", "#"); v = v.Replace("%40", "@"); v = v.Replace("%25", "%"); return v; } /// /// Escape a string. /// /// Path to escape public static string EscapePath(string p) { if (p == null) { return p; } string v = p.Replace("%", "%25"); v = v.Replace("#", "%23"); v = v.Replace("@", "%40"); return v; } /// /// Escape a list of strings. /// /// Path to escape public static IList EscapePaths(IList Paths) { List v = new List(); foreach (string p in Paths) { v.Add(EscapePath(p)); } return v; } /// /// Escape an array of strings. /// /// Paths to escape public static string[] EscapePaths(string[] Paths) { string[] v = new string[Paths.Length]; for (int idx = 0; idx < Paths.Length; idx++) { v[idx] = EscapePath(Paths[idx]); } return v; } /// /// Unescape a list of strings. /// /// Perforce server public static IList UnescapePaths(IList Paths) { List v = new List(); foreach (string p in Paths) { v.Add(UnescapePath(p)); } return v; } /// /// Unescape an array of strings. /// /// Paths to unescape public static string[] UnescapePaths(string[] Paths) { string[] v = new string[Paths.Length]; for (int idx = 0; idx < Paths.Length; idx++) { v[idx] = UnescapePath(Paths[idx]); } return v; } } /// /// A path spec in depot syntax. /// public class DepotPath : PathSpec { public DepotPath(string path) { Path = NormalizeDepot(path); } public override bool Equals(object obj) { return base.Equals(obj); } } /// /// A path spec in client syntax. /// public class ClientPath : PathSpec { public ClientPath(string path) { Path = NormalizeDepot(path); } public override bool Equals(object obj) { return base.Equals(obj); } } /// /// A path spec in local syntax. /// public class LocalPath : PathSpec { public LocalPath(string path) { Path = NormalizeLocal(path); } public override bool Equals(object obj) { return base.Equals(obj); } } }