Advertisement
encodedream

Fun with String parsing à la ref

Apr 28th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. void Main()
  2. {
  3.     var response = "B01 S01 D30 S02 D127 P01 D1 B02 S01 DScott\\ Morris S02 D145 P01 D0";
  4.    
  5.     var boards = new List<Board>();
  6.     var board = (Board)null;
  7.  
  8.     for (int i = 0; i < response.Length; i++)
  9.     {
  10.         if (response[i] == 'B')
  11.         {
  12.             board = new Board()
  13.             {
  14.                 Address = int.Parse(ParseValue(response, ref i))
  15.             };
  16.             boards.Add(board);
  17.         }
  18.         else if(response[i] == 'S' && board != null)
  19.         {
  20.             var sensor = new Sensor()
  21.             {
  22.                 Address = int.Parse(ParseValue(response, ref i)),
  23.                 Data = (response[++i] == 'D') ? ParseValue(response, ref i) : null
  24.             };
  25.            
  26.             if(sensor.Data == null) // Correct the position of i for the next value not being data
  27.                 i--;
  28.                
  29.             if (board.Sensors == null)
  30.                 board.Sensors = new List<Sensor>();
  31.             board.Sensors.Add(sensor);
  32.         }
  33.         else if(response[i] == 'P' && board != null)
  34.         {
  35.             var peripheral = new Peripheral()
  36.             {
  37.                 Address = int.Parse(ParseValue(response, ref i)),
  38.                 Data = (response[++i] == 'D') ? ParseValue(response, ref i) : null
  39.             };
  40.             if(peripheral.Data == null) // Correct the position of i for the next value not being data
  41.                 i--;
  42.                
  43.             if (board.Peripherals == null)
  44.                 board.Peripherals = new List<Peripheral>();
  45.                
  46.             board.Peripherals.Add(peripheral);         
  47.         }
  48.     }
  49.    
  50.     boards.Dump();
  51. }
  52.  
  53. public string ParseValue(string response, ref int i) {
  54.     var dataString = string.Empty;
  55.     for (int y = i + 1; y < response.Length; y++)
  56.     {
  57.         if(response[y] == '\\')
  58.             y++;
  59.         //Might have issues with parsing at the beinging (y-1 <= 0).
  60.         if (response[y-1] != '\\' && response[y] == ' ')
  61.         {
  62.             i = y;
  63.             break;
  64.         }
  65.         dataString += response[y];
  66.     }
  67.    
  68.     return dataString;
  69. }
  70.  
  71. // Define other methods and classes here
  72. public class Board
  73. {
  74.     public int Address { get; set; }
  75.     public List<Sensor> Sensors { get; set;}
  76.     public List<Peripheral> Peripherals { get; set; }
  77. }
  78.  
  79. public abstract class Component
  80. {
  81.     public int Address { get; set; }
  82.     public string Data { get; set; }
  83. }
  84.  
  85. public class Sensor : Component {}
  86.  
  87. public class Peripheral : Component {}
  88.  
  89. /*
  90. Pared Output
  91.  
  92. |Address    |Sensors                     |Peripherals        |
  93. |       1   ||Address   |Data           |||Address  |Data   ||
  94. |           ||      1   |   30          |||     1   |   1   ||
  95. |           ||      2   |   127         ||                   |
  96. |------------------------------------------------------------|
  97. |       2   ||Address   |Data           |||Address  |Data    |
  98. |           ||      1   |Scott Morris   |||     1   |   0   ||
  99. |           ||      2   |145            ||                   |
  100. |------------------------------------------------------------|
  101. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement