Advertisement
Guest User

Parse 3.0

a guest
Feb 4th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.54 KB | None | 0 0
  1.         Node ParseTextStream(string path)
  2.         {
  3.             List<Node> last = new List<Node>();
  4.             Node cur = new Node();
  5.             string buf = "";
  6.             bool quotes = false;
  7.  
  8.             var reader = new StreamReader(path);
  9.             char? lastc = null;
  10.             do
  11.             {
  12.                 char c = (char)reader.Read();
  13.                 if (c == '{')
  14.                 {
  15.                     last.Add(cur);
  16.                     cur = new Node();
  17.                     continue;
  18.                 }
  19.                 if (c == '}')
  20.                 {
  21.                     if (!(lastc == '}'))
  22.                     {
  23.                         cur.Nodes.Add(new Node() { Value = buf });
  24.                         buf = "";
  25.                     }
  26.                     last.Last().Nodes.Add(cur);
  27.                     cur = last.Last();
  28.                     last.Remove(cur);
  29.                     continue;
  30.                 }
  31.                 if (c == '"')
  32.                     quotes = !quotes;
  33.                 if (!(lastc == '}'))
  34.                 {
  35.                     if (c == ',' && !quotes)
  36.                     {
  37.                         cur.Nodes.Add(new Node() { Value = buf });
  38.                         buf = "";
  39.                     }
  40.                     else
  41.                     {
  42.                         buf += c;
  43.                     }
  44.                 }
  45.                 lastc = c;
  46.             } while (!reader.EndOfStream);
  47.             reader.Close();
  48.             reader.Dispose();
  49.  
  50.             return cur;
  51.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement