<RuntimeDirectory>\System.Drawing.dll <RuntimeDirectory>\System.IO.dll <RuntimeDirectory>\System.Net.dll <RuntimeDirectory>\System.Numerics.dll <RuntimeDirectory>\System.Numerics.Vectors.dll <RuntimeDirectory>\System.Security.dll System.Collections System.Collections.Concurrent System.Collections.Generic System.Collections.Specialized System.Drawing System.Drawing.Imaging System.IO System.IO.MemoryMappedFiles System.IO.Pipes System.IO.Ports System.Net System.Net.Sockets System.Numerics System.Runtime.InteropServices System.Runtime.InteropServices System.Runtime.Serialization System.Runtime.Serialization.Formatters.Binary System.Security System.Security.AccessControl System.Security.Cryptography System.Security.Principal System.Text System.Threading System.Threading.Tasks /* Script to turn a struct definition into C++ code that prints out its contents. So far it supports USHORT, ULONG, and UCHAR. This was written specifically for IDENTIFY_DEVICE_DATA in order to speed up writing ATAIdentifyDump. This can probably be used for similar structs too, so it might be useful elsewhere. */ const string SourceFile = @"C:\Users\Graham\Source\Repos\al-khaser\Tools\ATAIdentifyDump\IdentifyDeviceData.h"; const string StructVar = "idd"; const bool SwapStringEndian = true; // for IDENTIFY_DEVICE_DATA void Main() { string[] lines = File.ReadAllLines(SourceFile); bool foundStart = false; bool inStruct = false; var structLines = new List(); var output = new StringBuilder(); foreach (string rawLine in lines) { var line = rawLine.Trim().TrimEnd(';'); if (!foundStart) { if (line.StartsWith("typedef struct")) { foundStart = true; } continue; } if (line.StartsWith("struct {")) { if (inStruct) { throw new InvalidDataException(); } // we're starting a nested structure inStruct = true; structLines.Clear(); continue; } if (line.StartsWith("}")) { if (!inStruct) { Console.WriteLine("// end"); break; } // we're ending a nested structure var structNameMatch = Regex.Match(line, "^}\\s+([a-zA-Z0-9]+)$"); if (!structNameMatch.Success) { throw new InvalidDataException(); } string structName = structNameMatch.Groups[1].Value; inStruct = false; foreach (string structLine in structLines) { ProcessLine(StructVar, structLine, structName); } continue; } if (inStruct) { structLines.Add(line); continue; } ProcessLine(StructVar, line); } } void ProcessLine(string structvar, string line, string structname = null) { string[] parts = line.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); string fieldType = null; string fieldName = null; int arraySize = 0; if (parts.Length == 2) { fieldType = parts[0]; if (parts[1].Contains("[")) { var arrayMatch = Regex.Match(parts[1], "^([a-zA-Z0-9]+)\\[(\\d+)\\]$"); if (!arrayMatch.Success) { throw new InvalidDataException(); } fieldName = arrayMatch.Groups[1].Value; arraySize = int.Parse(arrayMatch.Groups[2].Value); } else { fieldName = parts[1]; } } else { if (!parts.Contains(":")) { throw new InvalidDataException(); } fieldType = parts[0]; fieldName = parts[1]; } if (structname != null) { structname += "."; } if (fieldType == "USHORT") { if (arraySize == 0) { Console.WriteLine($"printf(\"{structname ?? ""}{fieldName} = %hu\\r\\n\", {structvar}->{structname ?? ""}{fieldName});"); } else { for (int i = 0; i < arraySize; i++) { Console.WriteLine($"printf(\"{structname ?? ""}{fieldName}[{i}] = %hu\\r\\n\", {structvar}->{structname ?? ""}{fieldName}[{i}]);"); } } } else if (fieldType == "ULONG") { if (arraySize == 0) { Console.WriteLine($"printf(\"{structname ?? ""}{fieldName} = %lu\\r\\n\", {structvar}->{structname ?? ""}{fieldName});"); } else { for (int i = 0; i < arraySize; i++) { Console.WriteLine($"printf(\"{structname ?? ""}{fieldName}[{i}] = %lu\\r\\n\", {structvar}->{structname ?? ""}{fieldName}[{i}]);"); } } } else if (fieldType == "UCHAR") { if (arraySize == 0) { Console.WriteLine($"printf(\"{structname ?? ""}{fieldName} = %d\\r\\n\", {structvar}->{structname ?? ""}{fieldName});"); } else { string format = string.Concat(Enumerable.Repeat("%c", arraySize)); Console.Write($"printf(\"{structname ?? ""}{fieldName} = \\\"{format}\\\"\\r\\n\""); for (int i = 0; i < arraySize; i++) { int ni = i; if (SwapStringEndian) { ni = (i - (i % 2)) + (1 - (i % 2)); } Console.Write($", {structvar}->{structname ?? ""}{fieldName}[{ni}]"); } Console.WriteLine(");"); } } else { throw new InvalidDataException(); } }