Парсинг файла .cfg - C#

Узнай цену своей работы

Формулировка задачи:

Доброго вечера! Имеем вот такой файл формата .cfg:
PART { // --- general parameters --- name = mumech_MJ2_AR202 module = Part author = r4m0n/The_Duck (code) || keptin (part) // --- asset parameters --- mesh = model.mu rescaleFactor = 0.75 // --- node definitions --- node_attach = 0.0, 0.0, 0.0, 1, 0, 0 // --- editor parameters --- TechRequired = flightControl entryCost = 15000 cost = 750 category = Control subcategory = 0 title = MechJeb 2 (AR202 case) manufacturer = Anatid Robotics description = A ruggedized version of MechJeb's neural circuits in a radial-mount case. // attachment rules: stack, srfAttach, allowStack, allowSrfAttach, allowCollision attachRules = 0,1,0,0,0 // --- standard part parameters --- mass = 0.0001 thermalMassModifier = 100 skinThermalMassModifier = 100 dragModelType = default maximum_drag = 0.0 minimum_drag = 0.0 angularDrag = 0 crashTolerance = 6 maxTemp = 2900 CrewCapacity = 0 vesselType = Probe tags = command control autopilot MODULE { name = ModuleCommand minimumCrew = 0 RESOURCE { name = ElectricCharge rate = 0.005 } } MODULE { name = MechJebCore MechJebLocalSettings { MechJebModuleCustomWindowEditor { unlockTechs = flightControl } MechJebModuleSmartASS { unlockTechs = flightControl } MechJebModuleManeuverPlanner { unlockTechs = advFlightControl } MechJebModuleNodeEditor { unlockTechs = advFlightControl } MechJebModuleTranslatron { unlockTechs = advFlightControl } MechJebModuleWarpHelper { unlockTechs = advFlightControl } MechJebModuleAttitudeAdjustment { unlockTechs = advFlightControl } MechJebModuleThrustWindow { unlockTechs = advFlightControl } MechJebModuleRCSBalancerWindow { unlockTechs = advFlightControl } MechJebModuleRoverWindow { unlockTechs = fieldScience } MechJebModuleAscentGuidance { unlockTechs = unmannedTech } MechJebModuleLandingGuidance { unlockTechs = unmannedTech } MechJebModuleSpaceplaneGuidance { unlockTechs = unmannedTech } MechJebModuleDockingGuidance { unlockTechs = advUnmanned } MechJebModuleRendezvousAutopilotWindow { unlockTechs = advUnmanned } MechJebModuleRendezvousGuidance { unlockTechs = advUnmanned } } } MODULE { name = MechJebAR202 } RESOURCE { name = ElectricCharge amount = 5 maxAmount = 5 } } PART { // --- general parameters --- name = mumech_MJ2_AR202_features1 module = Part author = r4m0n/The_Duck (code) || keptin (part) // --- asset parameters --- mesh = model.mu rescaleFactor = 0.75 // --- editor parameters --- TechRequired = advFlightControl entryCost = 15000 cost = 750 category = none subcategory = 0 title = MechJeb Features- Maneuver & Translatron manufacturer = Anatid Robotics description = Unlocks the following MechJeb windows: Maneuver Planner, Translatron, Warp Helper, Attitude Adjustment, Thrust Window, RCS Balancer. Once unlocked, all existing MechJeb modules will be automatically upgraded to latest firmware. // --- standard part parameters --- mass = 0.00001 dragModelType = default maximum_drag = 0.0 minimum_drag = 0.0 angularDrag = 0 crashTolerance = 6 maxTemp = 2900 CrewCapacity = 0 vesselType = Probe } PART { // --- general parameters --- name = mumech_MJ2_AR202_features2 module = Part author = r4m0n/The_Duck (code) || keptin (part) // --- asset parameters --- mesh = model.mu rescaleFactor = 0.75 // --- editor parameters --- TechRequired = fieldScience entryCost = 15000 cost = 750 category = none subcategory = 0 title = MechJeb Features- Rover Autopilot manufacturer = Anatid Robotics description = Unlocks the following MechJeb windows: Rover Autopilot. Once unlocked, all existing MechJeb modules will be automatically upgraded to latest firmware. // --- standard part parameters --- mass = 0.00001 dragModelType = default maximum_drag = 0.0 minimum_drag = 0.0 angularDrag = 0 crashTolerance = 6 maxTemp = 2900 CrewCapacity = 0 vesselType = Probe } PART { // --- general parameters --- name = mumech_MJ2_AR202_features3 module = Part author = r4m0n/The_Duck (code) || keptin (part) // --- asset parameters --- mesh = model.mu rescaleFactor = 0.75 // --- editor parameters --- TechRequired = unmannedTech entryCost = 15000 cost = 750 category = none subcategory = 0 title = MechJeb Features- Ascent, Landing, Spaceplane manufacturer = Anatid Robotics description = Unlocks the following MechJeb windows: Ascent Autopilot, Landing Autopilot, Spaceplane Guidance. Once unlocked, all existing MechJeb modules will be automatically upgraded to latest firmware. // --- standard part parameters --- mass = 0.00001 dragModelType = default maximum_drag = 0.0 minimum_drag = 0.0 angularDrag = 0 crashTolerance = 6 maxTemp = 2900 CrewCapacity = 0 vesselType = Probe } PART { // --- general parameters --- name = mumech_MJ2_AR202_features4 module = Part author = r4m0n/The_Duck (code) || keptin (part) // --- asset parameters --- mesh = model.mu rescaleFactor = 0.75 // --- editor parameters --- TechRequired = advUnmanned entryCost = 15000 cost = 750 category = none subcategory = 0 title = MechJeb Features- Rendezvous & Docking manufacturer = Anatid Robotics description = Unlocks the following MechJeb windows: Rendezvous Guidance, Rendezvous Autopilot, Docking Autopilot. Once unlocked, all existing MechJeb modules will be automatically upgraded to latest firmware. // --- standard part parameters --- mass = 0.00001 dragModelType = default maximum_drag = 0.0 minimum_drag = 0.0 angularDrag = 0 crashTolerance = 6 maxTemp = 2900 CrewCapacity = 0 vesselType = Probe }
Есть у кого уже готовое решение как распарсить это? Что бы после этого, я мог получить значения секция, ключ, значение? Мб кто знает какие-то библиотеки? Заранее спасибо! P.S. файл описывает деталь космического корабля игры KSP

Решение задачи: «Парсинг файла .cfg»

textual
Листинг программы
        public static void Func(string str, sectionsInfo section)
        {
            int pos = 0;     
            string sub = String.Empty;
            string childName = String.Empty;
 
            
            while (true)
            {
                str = str.Substring(pos);
                if ((pos = str.IndexOf("\n")) == -1)
                    break;
 
                sub = str.Substring(0, pos);
                pos += 1;
 
                if (sub.Contains("{"))
                {
                    var child = new sectionsInfo();
                    section.sections.Add(child);
                    Match match = Regex.Match(sub, @"\w+");
                    if (match.Success)
                    {
                        childName = match.Value;
                    }
                    child.name = childName;
                    Func(str.Substring(str.IndexOf("{") + 1), child);
                    break;
                }
                else
                if (Regex.IsMatch(sub, @"^\s*\b[a-zA-Z]+\b\s*$"))
                {
                    Match match = Regex.Match(sub, @"\w+");
                    if (match.Success)
                    {
                        childName = match.Value;
                    }
                }
                else
                if (Regex.IsMatch(sub, @".+=.+"))
                {
                    var values = sub.Split(new[] { '=','}' },
                        StringSplitOptions.RemoveEmptyEntries);
                    section.values.Add(values[0], values[1]);
                }
            }
        }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

10   голосов , оценка 4.3 из 5
Похожие ответы