/* * P4.Net * Copyright (c) 2007-2010 Shawn Hladky Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ using System; using System.Collections.Generic; using System.Text; using p4dn; namespace P4API { /// /// Represents a Perforce Map object. /// public class P4Map : IDisposable { private P4MapMaker _map; /// /// Creates an empty Map. /// public P4Map() { // always use UTF8 _map = new P4MapMaker(Encoding.UTF8); } /// /// Creates a P4Map object populated with the view defined by lines. /// /// Lines in the Map view. public P4Map(params string[] lines) { // always use UTF8 _map = new P4MapMaker(Encoding.UTF8); Insert(lines); } private P4Map(P4MapMaker mapmaker) { _map = mapmaker; } /// /// Joins two map objects to form the intersection of the two views. /// /// Left-hand map. /// Right-hand map. /// P4Map object of the intersection of the two maps. public static P4Map Join(P4Map left, P4Map right) { return new P4Map(P4MapMaker.Join(left._map, right._map)); } /// /// Clears all view lines from the map. /// public void Clear() { _map.Clear(); } /// /// Returns the number of view lines in the map. /// public int Count { get { return _map.Count(); } } /// /// Returns true if the map has no lines. /// /// True if there are no view lines. public bool IsEmpty() { return (_map.Count() == 0); } /// /// Inserts a new view line at the end of the map. /// /// public void Insert(params string[] lines) { foreach (string line in lines) { _map.Insert(line); } } /// /// Inserts a new view line at the end of the map. /// /// Left-hand path of the view line. /// Right-hand path of the view line. public void Insert2(string left, string right) { _map.Insert(left, right); } /// /// Inserts a new view line at the end of the map. /// /// Left-hand paths of the view lines. /// Right-hand paths of the view lines. public void Insert2(IList left, IList right) { if (left.Count != right.Count) throw new ArgumentException("Left and Right arguments must be the same length."); for (int i = 0; i < left.Count; i++) { _map.Insert(left[i], right[i]); } } /// /// Translates a list of paths through the view. /// /// Paths to be translated. /// Translated paths. public IList Translate(params string[] paths) { List output = new List(); foreach (string path in paths) { string t = Translate(path); if (t != null) output.Add(t); } return output; } /// /// Translates a list of paths through the reverse view (left-to-right). /// /// Paths to be translated. /// Translated paths. public IList TranslateReverse(params string[] paths) { List output = new List(); foreach (string path in paths) { string t = TranslateReverse(path); if (t != null) output.Add(t); } return output; } /// /// Translates a path through the view. /// /// Path to be translated. /// Translated path. public string Translate(string path) { return _map.Translate(path, true); } /// /// Translates a path through the reverse view (left-to-right). /// /// Path to be translated. /// Translated path. public string TranslateReverse(string path) { return _map.Translate(path, false); } /// /// Determines if the path is contained in the view. /// /// Path to check. /// True when the view includes the path. public bool Includes(string path) { return (Translate(path) != null); } /// /// Determines if the view contains any of the specified paths. /// /// Paths to check. /// True if any of the paths are contained in the view. public bool IncludesAny(params string[] paths) { foreach (string path in paths) { if (Translate(path) != null) return true; } return false; } /// /// Determines if the view contains all of the specified paths. /// /// Paths to check. /// True if all of the paths are contained in the view. public bool IncludesAll(params string[] paths) { foreach (string path in paths) { if (Translate(path) == null) return false; } return true; } /// /// Reverses the view lines. /// /// A P4Map object containing the reversed view. public P4Map Reverse() { P4Map map = new P4Map(); map.Insert(_map.ToA()); map._map.Reverse(); return map; } /// /// Gets the left-hand side of the view lines. /// /// List containing the left-hand side of the view. public IList Lhs() { return _map.Lhs(); } /// /// Gets the right-hand side of the view lines. /// /// List containing the right-hand side of the view. public IList Rhs() { return _map.Rhs(); } /// /// Gets a list of the view lines. /// /// List fo strings containing each view line. public IList ToArray() { return _map.ToA(); } /// /// Cleans up unmanaged resources. /// public void Dispose() { _map.Dispose(); GC.SuppressFinalize(this); } /// /// Returns string representation of the view. /// /// String representing the view. public override string ToString() { return _map.Inspect(); } /// /// Calls dispose method if not already disposed. /// ~P4Map() { _map.Dispose(); } } }