P4Record.cs #1

  • //
  • guest/
  • erik_purins/
  • P4.Net/
  • release/
  • 0.9/
  • src/
  • P4API/
  • P4Record.cs
  • View
  • Commits
  • Open Download .zip Download (6 KB)
/*
 * P4.Net *
Copyright (c) 2006 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.Text;
using System.Collections;
using System.Collections.Specialized;

namespace P4API
{
    /// <summary>
    /// P4Record is a dictionary-like object that provides a means of interpreting Perforce results.
    /// </summary>
    /// <remarks>
    /// The Perforce api provides "parsed" output in the form of key-value pairs.  
    /// Keys are always strings (case sensitive).  From the raw api, the values come in 3 flavors:
    /// <list>
    ///     <li>Simple strings.</li>
    ///     <li>List of strings.</li>
    ///     <li>Sparse multi-demensional list of strings.</li>    
    /// </list>
    /// P4.Net does not yet handle the third type (which only comes from certain output of p4 filelog).
    /// <para>To access simple string values by key, use the Fields property (or the default indexer).</para>
    /// <para>To access lists of strings by key, use the ArrayFields property.</para>
    /// </remarks>
    public class P4Record
    {
        internal FieldDictionary _Fields = null;
        internal ArrayFieldDictionary _ArrayFields = null;

        internal P4Record(Hashtable sd)
        {
            _Fields = new FieldDictionary();
            _ArrayFields = new ArrayFieldDictionary();

            // 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))
                {
                    string keyBaseName = s;

                    int lastNumericCharacter = s.Length;

                    // look to see if the variable is an array
                    // we define an array as:
                    // there must exist a key that is all alpha followed by exactly one '0'
                    // if that key exists, all other keys with that base alpha name followed by a number
                    // will be an element in the array indexed by said number.
                    while (s[lastNumericCharacter - 1] >= '0' && s[lastNumericCharacter - 1]<= '9')
                    {
                        lastNumericCharacter--;
                    }
                    if (lastNumericCharacter != s.Length)
                    {
                        keyBaseName = s.Substring(0, lastNumericCharacter);

                        if (ht.ContainsKey(string.Format("{0}0", keyBaseName)))
                        {
                            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(s, (string)ht[s]);
                    }                    
                }
            }
        }

        /// <summary>
        /// Gets the FieldDictionary returned from the Perforce command.
        /// </summary>
        public FieldDictionary Fields
        {
            get
            {
                return _Fields;
            }
        }

        /// <summary>
        /// Gets the ArrayFieldDictionary returned from the Perforce command.
        /// </summary>
        public ArrayFieldDictionary ArrayFields
        {
            get
            {
                return _ArrayFields;
            }
        }

        /// <summary>
        /// Returns the value of of the Field by key.  This is the same as Fields[key].
        /// </summary>
        /// <param name="key">The key for </param>
        /// <returns>String value for the associated key.</returns>
        public string this[string key]
        {
            get
            {
                return _Fields[key];
            }
            set
            {
                _Fields[key] = 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;
            }

        }


    }
}
# Change User Description Committed
#1 7341 Erik Purins p4.net
---
pull p4.net#head
//guest/shawn_hladky/P4.Net/release/0.9/src/P4API/P4Record.cs
#1 5831 Shawn Hladky P4.Net: Branch release 0.9 and delete a few files missed last time
//guest/shawn_hladky/P4.Net/main/src/P4API/P4Record.cs
#1 5830 Shawn Hladky P4.Net: reorg to support release branches
//guest/shawn_hladky/P4.Net/src/P4API/P4Record.cs
#7 5824 Shawn Hladky P4.Net: Last bits of documentation for 0.9
#6 5812 Shawn Hladky P4.Net: More documentation.
#5 5798 Shawn Hladky P4.Net...  still not ready for beta
Added license to all files
Added several doc files
Misc bugs
#4 5636 Shawn Hladky 1.
 Added test harness framework, and some initial tests
2.  Fixed many bugs (oddly enough identified by the unit tests)
3.  Fixes so will build 1.1 Framework (and build batch files actually work)
4.  Pathetic attempt at documentation
#3 5438 Shawn Hladky refactoring, and documentation code
#2 5433 Shawn Hladky P4.Net More refactoring
#1 5432 Shawn Hladky Refactoring step 2 -- rename source files
//guest/shawn_hladky/P4.Net/src/P4API/P4ResultRecord.cs
#4 5431 Shawn Hladky Refactoring...
step 1.
#3 5411 Shawn Hladky WIP -- forms are working now.
#2 5373 Shawn Hladky P4.Net: Still WIP, but some things starting to work
#1 5362 Shawn Hladky Chipping away at the API changes