/* * P4.Net * Copyright (c) 2007-2009 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; public P4Map() { // always use UTF8 _map = new P4MapMaker(Encoding.UTF8); } public P4Map(params string[] lines) { // always use UTF8 _map = new P4MapMaker(Encoding.UTF8); Insert(lines); } private P4Map(P4MapMaker mapmaker) { _map = mapmaker; } public static P4Map Join(P4Map left, P4Map right) { return new P4Map(P4MapMaker.Join(left._map, right._map)); } public void Clear() { _map.Clear(); } public int Count { get { return _map.Count(); } } public bool IsEmpty() { return (_map.Count() == 0); } public void Insert(params string[] lines) { foreach (string line in lines) { _map.Insert(line); } } public void Insert2(string left, string right) { _map.Insert(left, right); } 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]); } } public string Translate(string path) { return Translate(path, true); } public IList Translate(bool forward, params string[] paths) { List output = new List(); foreach (string path in paths) { string t = Translate(path, forward); if (t != null) output.Add(t); } return output; } public IList Translate(params string[] paths) { return Translate(true, paths); } public string Translate(string path, bool forward) { return _map.Translate(path, forward); } public bool Includes(string path) { return (Translate(path) != null); } public bool IncludesAny(params string[] paths) { foreach (string path in paths) { if (Translate(path) != null) return true; } return false; } public bool IncludesAll(params string[] paths) { foreach (string path in paths) { if (Translate(path) == null) return false; } return true; } public P4Map Reverse() { P4Map map = new P4Map(); map.Insert(_map.ToA()); map._map.Reverse(); return map; } public IList Lhs() { return _map.Lhs(); } public IList Rhs() { return _map.Rhs(); } public IList ToArray() { return _map.ToA(); } public void Dispose() { _map.Dispose(); GC.SuppressFinalize(this); } public override string ToString() { return _map.Inspect(); } ~P4Map() { _map.Dispose(); } } }