Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

Kannst du mir helfen einen Parser für GDT-Dateien in C# zu schreiben?

  1. User

    Kannst du mir helfen einen Parser für GDT-Dateien in C# zu schreiben?

  2. Assistant

    Ja, ich kann Ihnen dabei helfen, einen Parser für GDT-Dateien in C# zu schreiben. Hier ist ein Beispielcode, der Ihnen als Ausgangspunkt dienen kann:

    using System;
    using System.Collections.Generic;
    using System.IO;
    
    public class GdtParser
    {
        public List<GdtData> ParseGdtFile(string filePath)
        {
            List<GdtData> gdtDataList = new List<GdtData>();
    
            using (StreamReader sr = new StreamReader(filePath))
            {
                string line;
                int lineCount = 0;
                string currentDataBlockName = "";
                GdtData currentGdtData = null;
    
                while ((line = sr.ReadLine()) != null)
                {
                    lineCount++;
    
                    if (line.StartsWith("MST"))
                    {
                        if (currentGdtData != null)
                        {
                            gdtDataList.Add(currentGdtData);
                        }
    
                        currentGdtData = new GdtData();
                        currentGdtData.MstLine = line;
                    }
                    else if (line.StartsWith("STB"))
                    {
                        currentDataBlockName = line.Substring(0, 20).Trim();
    
                        if (currentDataBlockName.Length == 0)
                        {
                            throw new Exception($"Error: Data block name is missing in line {lineCount}");
                        }
    
                        if (currentGdtData.DataBlocks.ContainsKey(currentDataBlockName))
                        {
                            throw new Exception($"Error: Duplicate data block name '{currentDataBlockName}' found in line {lineCount}");
                        }
    
                        currentGdtData.DataBlocks.Add(currentDataBlockName, new DataBlock(currentDataBlockName));
                    }
                    else if (line.StartsWith("SED"))
                    {
                        currentDataBlockName = "";
                    }
                    else if (line.StartsWith("SES"))
                    {
                        currentDataBlockName = line.Substring(0, 20).Trim();
    
                        if (currentDataBlockName.Length == 0)
                        {
                            throw new Exception($"Error: Data block name is missing in line {lineCount}");
                        }
    
                        if (currentGdtData.SubDataBlocks.ContainsKey(currentDataBlockName))
                        {
                            throw new Exception($"Error: Duplicate sub data block name '{currentDataBlockName}' found in line {lineCount}");
                        }
    
                        currentGdtData.SubDataBlocks.Add(currentDataBlockName, new SubDataBlock(currentDataBlockName));
                    }
                    else if (line.StartsWith("SEE"))
                    {
                        currentDataBlockName = "";
                    }
                    else if (line.StartsWith("SGD"))
                    {
                        if (currentDataBlockName.Length == 0)
                        {
                            throw new Exception($"Error: Data block name is missing in line {lineCount}");
                        }
    
                        if (!currentGdtData.DataBlocks.ContainsKey(currentDataBlockName))
                        {
                            throw new Exception($"Error: Invalid data block name '{currentDataBlockName}' found in line {lineCount}");
                        }
    
                        DataBlock dataBlock = currentGdtData.DataBlocks[currentDataBlockName];
                        string[] values = line.Split(new string[] { "SGD" }, StringSplitOptions.None)[1].Trim().Split(' ');
    
                        foreach (string value in values)
                        {
                            dataBlock.Values.Add(decimal.Parse(value));
                        }
                    }
                    else if (line.StartsWith("SEG"))
                    {
                        if (currentDataBlockName.Length == 0)
                        {
                            throw new Exception($"Error: Sub data block name is missing in line {lineCount}");
                        }
    
                        if (!currentGdtData.SubDataBlocks.ContainsKey(currentDataBlockName))
                        {
                            throw new Exception($"Error: Invalid sub data block name '{currentDataBlockName}' found in line {lineCount}");
                        }
    
                        SubDataBlock subDataBlock = currentGdtData.SubDataBlocks[currentDataBlockName];
                        string[] values = line.Split(new string[] { "SEG" }, StringSplitOptions.None)[1].Trim().Split(' ');
    
                        foreach (string value in values)
                        {
                            subDataBlock.Values.Add(decimal.Parse(value));
                        }
                    }
                }
    
                if (currentGdtData != null)
                {
                    gdtDataList.Add(currentGdtData);
                }
            }
    
            return gdtDataList;
        }
    }
    
    public class GdtData
    {
        public string MstLine { get; set; }
        public Dictionary<string, DataBlock> DataBlocks { get; set; }
        public Dictionary<string, SubDataBlock> SubDataBlocks { get; set; }
    
        public GdtData()
        {
            DataBlocks = new Dictionary<string, DataBlock>();
            SubDataBlocks = new Dictionary<string, SubDataBlock>();
        }
    }
    
    public class DataBlock
    {
        public string Name { get; set; }
        public List<decimal> Values { get; set; }
    
        public DataBlock(string name)
        {
            Name = name;
            Values = new List<decimal>();
        }
    }
    
    public class SubDataBlock
    {
        public string Name { get; set; }
        public List<decimal> Values { get; set; }
    
        public SubDataBlock(string name)
        {
            Name = name;
            Values = new List<decimal>();
        }
    }
    

    Um den Parser zu verwenden, erstellen Sie eine Instanz der Klasse GdtParser und rufen Sie die Methode ParseGdtFile mit dem Pfad zur GDT-Datei als Argument auf. Das Ergebnis ist eine Liste von GdtData-Objekten, die die analysierten Daten der GDT-Datei enthalten.

    Bitte beachten Sie, dass dies nur ein grundlegender Parser ist und möglicherweise nicht alle Arten von GDT-Dateien unterstützt oder spezifische Anforderungen erfüllt. Es kann erforderlich sein, den Code entsprechend anzupassen, um spezifische Anforderungen zu erfüllen.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.