Advertisement
SilverTES

Simple Style Reader from txt file

Mar 16th, 2023 (edited)
595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.19 KB | Source Code | 0 0
  1.         public struct Value
  2.         {
  3.             public bool _isMap;
  4.             public bool _isNumeric;
  5.             public bool _isBoolean;
  6.             public string _string;
  7.             public float _number;
  8.             public bool _boolean;
  9.  
  10.             public Dictionary<string, Value> _childs = new Dictionary<string, Value>();
  11.  
  12.             public Value()
  13.             {
  14.                 _isMap = false;
  15.                 _isNumeric = false;
  16.                 _isBoolean = false;
  17.                 _string = string.Empty;
  18.                 _number = 0;
  19.                 _boolean = false;
  20.             }
  21.             public Value Child(string name)
  22.             {
  23.                 if (_childs.ContainsKey(name))
  24.                     return _childs[name];
  25.  
  26.                 return default(Value);
  27.             }
  28.         }
  29.  
  30.         public static Dictionary<string, Value> LoadStyle(string strStyle)
  31.         {
  32.             Dictionary<string, Value> map = new Dictionary<string, Value>();
  33.  
  34.             string reader = Regex.Replace(strStyle, @"\s+", "");
  35.  
  36.             string curExpression = "";
  37.  
  38.             bool isKey = true;
  39.             bool isMap = false;
  40.             bool isValue = false;
  41.  
  42.             string key = "";
  43.             string value = "";
  44.  
  45.             int nbOpenBracket = 0;
  46.  
  47.             for (int i = 0; i < reader.Length; i++)
  48.             {
  49.                 char c = reader[i];
  50.  
  51.                 curExpression += c;
  52.  
  53.                 if (c == '=' && isKey)
  54.                 {
  55.                     curExpression = curExpression.TrimEnd('=');
  56.  
  57.                     key = curExpression;
  58.  
  59.                     //Console.Write( key + '=');
  60.  
  61.                     curExpression = "";
  62.                     isKey = false;
  63.                     isValue = true;
  64.                 }
  65.  
  66.                 if (c == '(' && isMap)
  67.                 {
  68.                     nbOpenBracket++;
  69.                     //Console.WriteLine("nbOpenBracket = " + nbOpenBracket);
  70.                 }
  71.  
  72.                 if (c == ')' && isMap)
  73.                 {
  74.                    
  75.                     nbOpenBracket--;
  76.  
  77.                     if (nbOpenBracket==0)
  78.                     {
  79.                         curExpression = curExpression.TrimEnd(')');
  80.  
  81.                         //Console.WriteLine('(' +curExpression + ");");
  82.  
  83.                         char lastChar = curExpression[curExpression.Length - 1];
  84.  
  85.                         if (lastChar != ';') // check if forget char ';' in the mapChild or not
  86.                             curExpression += ';';
  87.                         // recursive
  88.                         var mapChild = LoadStyle(curExpression);
  89.  
  90.                         if (!map.ContainsKey(key))
  91.                             map.Add(key, new Value() { _isMap = true, _string = curExpression, _childs = mapChild });
  92.  
  93.                         curExpression = "";
  94.                         isMap = false;
  95.  
  96.                     }
  97.                 }
  98.  
  99.                 if (c == '(' && !isKey && !isMap)
  100.                 {
  101.                     nbOpenBracket = 1;
  102.                     curExpression = "";
  103.                     isMap = true;
  104.                 }
  105.  
  106.                 if (c == ';' )//&& !isKey && !isMap)
  107.                 {
  108.                     if (isValue && !isKey && !isMap)
  109.                     {
  110.                         curExpression = curExpression.TrimEnd(';');
  111.  
  112.                         value = curExpression;
  113.  
  114.                         bool isNumeric = float.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out float number);
  115.                         bool isBoolean = bool.TryParse(value, out bool boolean);
  116.                         if (isNumeric)
  117.                         {
  118.                             if (!map.ContainsKey(key))
  119.                                 map.Add(key, new Value() { _isNumeric = true, _string = value, _number = number });
  120.                         }
  121.                         else if(isBoolean)
  122.                         {
  123.                             if (!map.ContainsKey(key))
  124.                                 map.Add(key, new Value() { _isBoolean = true, _string = value, _boolean = boolean });
  125.                         }
  126.                         else
  127.                         {
  128.                             if (!map.ContainsKey(key))
  129.                                 map.Add(key, new Value() { _string = value });
  130.                         }
  131.                         //if (value.Length > 0)
  132.                         //    Console.WriteLine(curExpression + ';');
  133.                        
  134.                         isValue = false;
  135.                         isKey = true;
  136.                         curExpression = "";
  137.                     }
  138.                     else if (!isValue && isKey && !isMap) // Error key don't have value
  139.                     {
  140.                         Console.WriteLine("..Error Key don't have value..");
  141.  
  142.                         isKey = true;
  143.                         curExpression = "";
  144.                     }
  145.                     else if (!isValue && !isKey && isMap) // Out of mapchild
  146.                     {
  147.                         isKey = true;
  148.                         curExpression = "";
  149.                     }
  150.                 }
  151.             }
  152.             return map;
  153.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement