2009-02-22 10:43:35 +00:00
|
|
|
#region --- License ---
|
|
|
|
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
|
|
|
|
* See license.txt for license info
|
|
|
|
*/
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
2009-10-14 22:49:04 +00:00
|
|
|
using System.Linq;
|
2009-08-17 12:28:22 +00:00
|
|
|
using System.Text;
|
2009-08-11 14:12:20 +00:00
|
|
|
using System.Xml.XPath;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
namespace Bind.Structures
|
|
|
|
{
|
|
|
|
#region class Enum
|
|
|
|
|
|
|
|
public class Enum
|
|
|
|
{
|
|
|
|
static StringBuilder translator = new StringBuilder();
|
2009-10-09 05:48:10 +00:00
|
|
|
string _name, _type;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2009-05-05 21:00:48 +00:00
|
|
|
// Returns true if the enum contains a collection of flags, i.e. 1, 2, 4, 8, ...
|
|
|
|
public bool IsFlagCollection
|
|
|
|
{
|
2013-10-27 16:35:36 +00:00
|
|
|
get; set;
|
2009-05-05 21:00:48 +00:00
|
|
|
}
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
public string Name
|
|
|
|
{
|
2009-10-09 05:48:10 +00:00
|
|
|
get { return _name ?? ""; }
|
2009-02-22 10:43:35 +00:00
|
|
|
set { _name = value; }
|
|
|
|
}
|
2009-10-09 05:48:10 +00:00
|
|
|
|
|
|
|
// Typically 'long' or 'int'. Default is 'int'.
|
|
|
|
public string Type
|
|
|
|
{
|
|
|
|
get { return String.IsNullOrEmpty(_type) ? "int" : _type; }
|
|
|
|
set { _type = value; }
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2013-10-28 13:08:55 +00:00
|
|
|
SortedDictionary<string, Constant> _constant_collection = new SortedDictionary<string, Constant>();
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2009-02-28 18:50:16 +00:00
|
|
|
public IDictionary<string, Constant> ConstantCollection
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
|
|
|
get { return _constant_collection; }
|
2010-10-12 16:46:08 +00:00
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == null)
|
|
|
|
throw new ArgumentNullException("value");
|
|
|
|
|
|
|
|
_constant_collection.Clear();
|
|
|
|
foreach (var item in value)
|
|
|
|
{
|
|
|
|
_constant_collection.Add(item.Key, item.Value);
|
|
|
|
}
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
2011-07-20 10:08:27 +00:00
|
|
|
[Obsolete("This code belongs to the various language-specific ISpecWriter implementations")]
|
2009-02-22 10:43:35 +00:00
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2009-02-28 18:50:16 +00:00
|
|
|
List<Constant> constants = new List<Constant>(ConstantCollection.Values);
|
|
|
|
constants.Sort(delegate(Constant c1, Constant c2)
|
|
|
|
{
|
|
|
|
int ret = String.Compare(c1.Value, c2.Value);
|
|
|
|
if (ret == 0)
|
|
|
|
return String.Compare(c1.Name, c2.Name);
|
|
|
|
return ret;
|
|
|
|
});
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2009-05-05 21:00:48 +00:00
|
|
|
if (IsFlagCollection)
|
|
|
|
sb.AppendLine("[Flags]");
|
2009-10-09 05:48:10 +00:00
|
|
|
sb.Append("public enum ");
|
|
|
|
sb.Append(Name);
|
|
|
|
sb.Append(" : ");
|
|
|
|
sb.AppendLine(Type);
|
2009-02-22 10:43:35 +00:00
|
|
|
sb.AppendLine("{");
|
2009-02-28 18:50:16 +00:00
|
|
|
|
|
|
|
foreach (Constant c in constants)
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
|
|
|
sb.Append(" ");
|
|
|
|
sb.Append(c.ToString());
|
|
|
|
if (!String.IsNullOrEmpty(c.ToString()))
|
|
|
|
sb.AppendLine(",");
|
|
|
|
}
|
|
|
|
sb.Append("}");
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
}
|
2013-10-27 16:35:36 +00:00
|
|
|
|
|
|
|
public void Add(Constant constant)
|
|
|
|
{
|
|
|
|
ConstantCollection.Add(constant.Name, constant);
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region class EnumCollection
|
|
|
|
|
2009-05-29 15:57:01 +00:00
|
|
|
public class EnumCollection : SortedDictionary<string, Enum>
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
|
|
|
internal void AddRange(EnumCollection enums)
|
|
|
|
{
|
|
|
|
foreach (Enum e in enums.Values)
|
|
|
|
Utilities.Merge(this, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return -1 for ext1, 1 for ext2 or 0 if no preference.
|
|
|
|
int OrderOfPreference(string ext1, string ext2)
|
|
|
|
{
|
2009-02-28 18:50:16 +00:00
|
|
|
// If one is empty and the other not, prefer the empty one (empty == core)
|
2009-02-22 10:43:35 +00:00
|
|
|
// Otherwise check for Arb and Ext. To reuse the logic for the
|
|
|
|
// empty check, let's try to remove first Arb, then Ext from the strings.
|
2009-02-28 18:50:16 +00:00
|
|
|
int ret = PreferEmpty(ext1, ext2);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
ext1 = ext1.Replace("Arb", ""); ext2 = ext2.Replace("Arb", "");
|
2009-02-28 18:50:16 +00:00
|
|
|
ret = PreferEmpty(ext1, ext2);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
ext1 = ext1.Replace("Ext", ""); ext2 = ext2.Replace("Ext", "");
|
2009-02-28 18:50:16 +00:00
|
|
|
return PreferEmpty(ext1, ext2);
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prefer the empty string over the non-empty.
|
2009-02-28 18:50:16 +00:00
|
|
|
int PreferEmpty(string ext1, string ext2)
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
|
|
|
if (String.IsNullOrEmpty(ext1) && !String.IsNullOrEmpty(ext2))
|
|
|
|
return -1;
|
|
|
|
else if (String.IsNullOrEmpty(ext2) && !String.IsNullOrEmpty(ext1))
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
new bool TryGetValue(string key, out Enum value)
|
|
|
|
{
|
|
|
|
return base.TryGetValue(key, out value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|