Парсинг файла .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
Листинг программы
  1.         public static void Func(string str, sectionsInfo section)
  2.         {
  3.             int pos = 0;    
  4.             string sub = String.Empty;
  5.             string childName = String.Empty;
  6.  
  7.            
  8.             while (true)
  9.             {
  10.                 str = str.Substring(pos);
  11.                 if ((pos = str.IndexOf("\n")) == -1)
  12.                     break;
  13.  
  14.                 sub = str.Substring(0, pos);
  15.                 pos += 1;
  16.  
  17.                 if (sub.Contains("{"))
  18.                 {
  19.                     var child = new sectionsInfo();
  20.                     section.sections.Add(child);
  21.                     Match match = Regex.Match(sub, @"\w+");
  22.                     if (match.Success)
  23.                     {
  24.                         childName = match.Value;
  25.                     }
  26.                     child.name = childName;
  27.                     Func(str.Substring(str.IndexOf("{") + 1), child);
  28.                     break;
  29.                 }
  30.                 else
  31.                 if (Regex.IsMatch(sub, @"^\s*\b[a-zA-Z]+\b\s*$"))
  32.                 {
  33.                     Match match = Regex.Match(sub, @"\w+");
  34.                     if (match.Success)
  35.                     {
  36.                         childName = match.Value;
  37.                     }
  38.                 }
  39.                 else
  40.                 if (Regex.IsMatch(sub, @".+=.+"))
  41.                 {
  42.                     var values = sub.Split(new[] { '=','}' },
  43.                         StringSplitOptions.RemoveEmptyEntries);
  44.                     section.values.Add(values[0], values[1]);
  45.                 }
  46.             }
  47.         }

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


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

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

10   голосов , оценка 4.3 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы