using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
public static void Main() {
public bool Format { get; set; }
public string Identifier { get; set; }
public string Assignment { get; set; }
public string Comment { get; set; }
get { return GetUInt64Value(); }
private long GetInt64Value() {
if (Assignment.StartsWith("0x")) return long.Parse(Assignment.Substring(2), NumberStyles.HexNumber);
return long.Parse(Assignment);
private ulong GetUInt64Value() {
if (Assignment.StartsWith("-")) return (ulong) GetInt64Value();
if (Assignment.StartsWith("0x")) return ulong.Parse(Assignment.Substring(2), NumberStyles.HexNumber);
return ulong.Parse(Assignment);
public static void FormatEnum(string enumCode, string enumType = null, bool formatValue = false, bool hexOutput = false) {
formatValue |= hexOutput;
var output = new List<Line>();
for (var i = 0; i < lines.Length; i++) {
var line = lines[i].TrimStart();
var enumDefinition = Regex.Match(line, @"^(?:(?!\/\/).)*enum\s+(\w+)\s*(?::\s*(\w+))?\s*{?\s*$");
if (!enumDefinition.Success)
enumType = enumDefinition.Groups[2].Success ? enumDefinition.Groups[2].Value : "int";
output.Add(new Line { Comment = line });
if (line.StartsWith("{") || line.StartsWith("}")) {
output.Add(new Line { Comment = line });
var match = Regex.Match(line, @"(\w*)[ \t]*=[ \t]*([\~\-0xXa-fA-F0-9]*)[, \t]*(.*)");
output.Add(new Line { Comment = " " + line });
var identifier = match.Groups[1].Value;
var assignment = match.Groups[2].Value;
var comment = match.Groups[3].Value;
var format = GetFormatString(enumType, hexOutput);
var size = GetSize(enumType);
var signed = GetSigned(enumType);
var negativeBit = 1ul << ((size * 8) - 1);
var mask = (1ul << (size * 8)) - 1;
if (mask == 0) mask = ulong.MaxValue;
foreach (var line in output.Where(l => l.Format)) {
if (signed && hexOutput && (@value & negativeBit) != 0) {
lineFormat += " | (1 << " + ((size * 8) - 1) + ")";
line.Assignment = string.Format(lineFormat, signed ?
(object)(long) (@value & mask) :
(object)(ulong)(@value & mask));
var longestIdentifier = output
.Max(l => l.Identifier.Length);
var longestAssignment = output
.Max(l => l.Assignment.Length);
foreach (var line in output) {
Console.WriteLine(line.Comment);
var identifier = line.Identifier.PadRight(longestIdentifier);
var assignment = line.Comment.Length > 0 ? line.Assignment.PadRight(longestAssignment + 1) : line.Assignment;
var comment = line.Comment;
Console.WriteLine(" {0} = {1}{2},", identifier, assignment, comment);
public static bool GetSigned(string type) {
case "sbyte": return true;
case "short": return true;
case "long": return true;
public static int GetSize(string type) {
case "byte": case "sbyte": return 1;
case "ushort": case "short": return 2;
case "uint": case "int": return 4;
case "ulong": case "long": return 8;
public static string GetFormatString(string type, bool hex) {
return hex ? "0x{0:X" + GetSize(type) * 2 + "}" : "{0}";
public const string _enum = @"
MediaPreviousTrack = 177,
LaunchApplication1 = 182,
LaunchApplication2 = 183,