using System; using System.Text; using System.Collections; using System.Collections.Specialized; namespace P4API { /// /// Subclass of Hashtable which provides additional methods for dealing with the psuedo-arrays passed from Perforce. /// public class P4Record { protected Record.FieldCollection _Fields = null; protected Record.ArrayFieldCollection _ArrayFields = null; internal P4Record(Hashtable sd) { _Fields = new Record.FieldCollection(); _ArrayFields = new Record.ArrayFieldCollection(); // first copy the Hashtable Hashtable ht = (Hashtable)sd.Clone(); ArrayList keys = new ArrayList(ht.Keys); foreach (string s in keys) { // the key may have been removed if it was an array, // so check here for that if (ht.ContainsKey(s)) { int lastNumber; bool isVariableArray = false; string keyBaseName = s; if (int.TryParse(s[s.Length - 1].ToString(), out lastNumber)) { // get the base variable name int lastNumericCharacter = s.Length; for (int j = lastNumericCharacter - 1; j > 0; j--) { int parsedNumber = 0; if (!int.TryParse(s[j].ToString(), out parsedNumber)) { break; } lastNumericCharacter--; } keyBaseName = s.Substring(0, lastNumericCharacter); if (ht.ContainsKey(string.Format("{0}0", keyBaseName))) { isVariableArray = true; } } if (isVariableArray) { int i = 0; ArrayList list = new ArrayList(); //spin the array string indexKey = string.Format("{0}0", keyBaseName); do { list.Add((string)ht[indexKey]); // remove it from the hashtable so we don't hit it again ht.Remove(indexKey); i++; indexKey = string.Format("{0}{1}", keyBaseName, i); } while (ht.ContainsKey(indexKey)); string[] sList = (string[])list.ToArray(typeof(string)); _ArrayFields.Add(keyBaseName, sList); } else { _Fields.Add(keyBaseName, (string)ht[keyBaseName]); } } } } public Record.FieldCollection Fields { get { return _Fields; } } public Record.ArrayFieldCollection ArrayFields { get { return _ArrayFields; } } // default indexer is the Fields collection public string this[string index] { get { return _Fields[index]; } set { _Fields[index] = value; } } internal Hashtable AllFieldDictionary { get { Hashtable ret = new Hashtable(); // Add the scalar fields foreach (string key in _Fields.Keys) { ret.Add(key, _Fields[key]); } // Add the array fields foreach (string key in _ArrayFields.Keys) { for (int i = 0; i < _ArrayFields[key].Length; i++) { ret.Add(string.Format("{0}{1}", key, i), _ArrayFields[key][i]); } } return ret; } } } }