Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public struct Value
- {
- public bool _isMap;
- public bool _isNumeric;
- public bool _isBoolean;
- public string _string;
- public float _number;
- public bool _boolean;
- public Dictionary<string, Value> _childs = new Dictionary<string, Value>();
- public Value()
- {
- _isMap = false;
- _isNumeric = false;
- _isBoolean = false;
- _string = string.Empty;
- _number = 0;
- _boolean = false;
- }
- public Value Child(string name)
- {
- if (_childs.ContainsKey(name))
- return _childs[name];
- return default(Value);
- }
- }
- public static Dictionary<string, Value> LoadStyle(string strStyle)
- {
- Dictionary<string, Value> map = new Dictionary<string, Value>();
- string reader = Regex.Replace(strStyle, @"\s+", "");
- string curExpression = "";
- bool isKey = true;
- bool isMap = false;
- bool isValue = false;
- string key = "";
- string value = "";
- int nbOpenBracket = 0;
- for (int i = 0; i < reader.Length; i++)
- {
- char c = reader[i];
- curExpression += c;
- if (c == '=' && isKey)
- {
- curExpression = curExpression.TrimEnd('=');
- key = curExpression;
- //Console.Write( key + '=');
- curExpression = "";
- isKey = false;
- isValue = true;
- }
- if (c == '(' && isMap)
- {
- nbOpenBracket++;
- //Console.WriteLine("nbOpenBracket = " + nbOpenBracket);
- }
- if (c == ')' && isMap)
- {
- nbOpenBracket--;
- if (nbOpenBracket==0)
- {
- curExpression = curExpression.TrimEnd(')');
- //Console.WriteLine('(' +curExpression + ");");
- char lastChar = curExpression[curExpression.Length - 1];
- if (lastChar != ';') // check if forget char ';' in the mapChild or not
- curExpression += ';';
- // recursive
- var mapChild = LoadStyle(curExpression);
- if (!map.ContainsKey(key))
- map.Add(key, new Value() { _isMap = true, _string = curExpression, _childs = mapChild });
- curExpression = "";
- isMap = false;
- }
- }
- if (c == '(' && !isKey && !isMap)
- {
- nbOpenBracket = 1;
- curExpression = "";
- isMap = true;
- }
- if (c == ';' )//&& !isKey && !isMap)
- {
- if (isValue && !isKey && !isMap)
- {
- curExpression = curExpression.TrimEnd(';');
- value = curExpression;
- bool isNumeric = float.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out float number);
- bool isBoolean = bool.TryParse(value, out bool boolean);
- if (isNumeric)
- {
- if (!map.ContainsKey(key))
- map.Add(key, new Value() { _isNumeric = true, _string = value, _number = number });
- }
- else if(isBoolean)
- {
- if (!map.ContainsKey(key))
- map.Add(key, new Value() { _isBoolean = true, _string = value, _boolean = boolean });
- }
- else
- {
- if (!map.ContainsKey(key))
- map.Add(key, new Value() { _string = value });
- }
- //if (value.Length > 0)
- // Console.WriteLine(curExpression + ';');
- isValue = false;
- isKey = true;
- curExpression = "";
- }
- else if (!isValue && isKey && !isMap) // Error key don't have value
- {
- Console.WriteLine("..Error Key don't have value..");
- isKey = true;
- curExpression = "";
- }
- else if (!isValue && !isKey && isMap) // Out of mapchild
- {
- isKey = true;
- curExpression = "";
- }
- }
- }
- return map;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement