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
{
///
/// Default Constructor
///
protected PathSpec() { }
///
/// Construct a PathSpec from a string
///
/// path string
public PathSpec(string path)
{
Path = path;
}
///
/// Property to access Path string
///
public string Path
{ get; protected set; }
///
/// Return a string from a PathSpec
///
/// string path
public override string ToString()
{
return Path;
}
///
/// Compare one PathSpec with another
///
/// PathSpec to compare to
/// true if equal
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;
}
///
/// Get HashCode for PathSpec
///
/// hashcode
public override int GetHashCode()
{
return Path.GetHashCode();
}
///
/// Normalize a depot Path
/// Removes quotes around it.
///
/// path to normalize
/// normalized path
protected string NormalizeDepot(string path)
{
string val = "";
if (path != null)
{
val = path.Trim('"');
}
return val;
}
///
/// Normalize a Local Path
/// Remove quotes, insure backslashes instead of forward slashes
///
/// path to normalize
/// normalized path
protected string NormalizeLocal(string path)
{
string val = "";
if (path != null)
{
val = path.Trim('"', ' ');
val = val.Replace('/', '\\');
}
return val;
}
///
/// Return the filename from a path
///
/// path to parse
/// filename
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;
}
///
/// Get just the Directory path from a path
/// Exclude the Filename
///
/// path to parse
/// directory path
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;
}
///
/// Return the File name from this PathSpec
///
/// filename
public string GetFileName()
{
return GetFileName(Path);
}
///
/// Return the Path Name from
///
///
public string GetDirectoryName()
{
return GetDirectoryName(Path);
}
///
/// Operator to Create a FileSpec from a PathSpec
///
/// PathSpec
/// FileSpec
public static implicit operator FileSpec(PathSpec s)
{
return new FileSpec(s,null);
}
///
/// Unescape a string path
/// UnEscapes the Perforce Special characters * # @ and %
///
/// Path to unescape
/// unescaped path
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
/// Escapes the Perforce Special characters * # @ and %
///
/// Path to escape
/// escaped path
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 string paths.
///
/// Path to escape
/// List of Escaped paths
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 string paths
///
/// Array of Paths to escape
/// Array of Escaped paths
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 string paths
///
/// Paths to process
/// List of Unescaped paths
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 string paths
///
/// Paths to unescape
/// array of unescaped path strings
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
{
///
/// Construct a DepotPath from a string path
///
/// string path
/// A Depot Path
public DepotPath(string path)
{
Path = NormalizeDepot(path);
}
public override bool Equals(object obj) { return base.Equals(obj); }
public override int GetHashCode() { return base.GetHashCode(); }
}
///
/// A path spec in client syntax.
///
public class ClientPath : PathSpec
{
///
/// Construct a ClientPath from a string path
///
/// string path
/// A Client Path
public ClientPath(string path)
{
Path = NormalizeDepot(path);
}
public override bool Equals(object obj) { return base.Equals(obj); }
public override int GetHashCode() { return base.GetHashCode(); }
}
///
/// A path spec in local syntax.
///
public class LocalPath : PathSpec
{
///
/// Construct a LocalPath from a string path
///
/// string path
/// A Local Path
public LocalPath(string path)
{
Path = NormalizeLocal(path);
}
public override bool Equals(object obj) { return base.Equals(obj); }
public override int GetHashCode() { return base.GetHashCode(); }
}
}