FileImporter.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. namespace SketchAssistant
  9. {
  10. public class FileImporter
  11. {
  12. /// <summary>
  13. /// parses a drawing consisting of line objects, given as a file in the application specific .isad format
  14. /// </summary>
  15. /// <param name="fileName">the path of the input file</param>
  16. /// <returns>the width and height of the left canvas and the parsed picture as a list of lines</returns>
  17. public (int, int, List<Line>) ParseISADInputFile(String fileName)
  18. {
  19. return ParseISADInput(System.IO.File.ReadAllLines(fileName));
  20. }
  21. /// <summary>
  22. /// parses a drawing consisting of line objects, given as the content of a .isad file, seperated into lines
  23. /// </summary>
  24. /// <param name="allLines">an array holding all lines of the input file</param>
  25. /// <returns>the width and height of the left canvas and the parsed picture as a list of lines</returns>
  26. private (int, int, List<Line>) ParseISADInput(String[] allLines)
  27. {
  28. if (allLines.Length == 0)
  29. {
  30. throw new FileImporterException("file is empty", "", -1);
  31. }
  32. if (!"drawing".Equals(allLines[0]))
  33. {
  34. throw new FileImporterException("file is not an interactive sketch assistant drawing", ".isad files have to start with the 'drawing' token", 1);
  35. }
  36. if (!"enddrawing".Equals(allLines[allLines.Length - 1]))
  37. {
  38. throw new FileImporterException("unterminated drawing definition", ".isad files have to end with the 'enddrawing' token", allLines.Length);
  39. }
  40. (int, int) dimensions = ParseISADHeader(allLines);
  41. List<Line> picture = ParseISADBody(allLines, dimensions.Item1, dimensions.Item2);
  42. return (dimensions.Item1, dimensions.Item2, picture);
  43. }
  44. /// <summary>
  45. /// parses the first two lines of an input file in .isad format
  46. /// </summary>
  47. /// <param name="allLines">the input file as an array of lines</param>
  48. /// <returns>the width and height of the left canvas</returns>
  49. private (int, int) ParseISADHeader(String[] allLines)
  50. {
  51. int width;
  52. int height;
  53. if (!(allLines.Length > 1) || !Regex.Match(allLines[1], @"^\d+x?\d+$", RegexOptions.None).Success)
  54. {
  55. throw new FileImporterException("invalid or missing canvas size definition", "format: [width]x[heigth]", 2);
  56. }
  57. String[] size = allLines[1].Split('x');
  58. width = Convert.ToInt32(size[0]);
  59. height = Convert.ToInt32(size[1]);
  60. return (width, height);
  61. }
  62. /// <summary>
  63. /// parses all line entries of an input file in .isad format
  64. /// </summary>
  65. /// <param name="allLines">the input file as an array of lines</param>
  66. /// <returns>the parsed picture as a list of lines</returns>
  67. private List<Line> ParseISADBody(String[] allLines, int width, int height)
  68. {
  69. String lineStartString = "line";
  70. String lineEndString = "endline";
  71. List<Line> drawing = new List<Line>();
  72. //number of the line currently being parsed, enumeration starting at 0, body starts at the third line, therefore lin number 2
  73. int i = 2;
  74. //parse 'line' token and complete line definition
  75. int lineStartPointer = i;
  76. //holds the line number of the next expected beginning of a line definition, or of the enddrawing token
  77. while (lineStartString.Equals(allLines[i]))
  78. {
  79. //start parsing next line
  80. i++;
  81. List<Point> newLine = new List<Point>();
  82. while (!lineEndString.Equals(allLines[i]))
  83. {
  84. if (i == allLines.Length)
  85. {
  86. throw new FileImporterException("unterminated line definition", null, (i + 1));
  87. }
  88. //parse single point definition
  89. if (!Regex.Match(allLines[i], @"^\d+;\d+$", RegexOptions.None).Success)
  90. {
  91. throw new FileImporterException("invalid Point definition: wrong format", "format: [xCoordinate];[yCoordinate]", (i + 1) );
  92. }
  93. String[] coordinates = allLines[i].Split(';');
  94. //no errors possible, convertability to int already checked above
  95. int xCoordinate = Convert.ToInt32(coordinates[0]);
  96. int yCoordinate = Convert.ToInt32(coordinates[1]);
  97. if (xCoordinate < 0 || yCoordinate < 0 || xCoordinate > width - 1 || yCoordinate > height - 1)
  98. {
  99. throw new FileImporterException("invalid Point definition: point out of bounds", null, (i + 1) );
  100. }
  101. newLine.Add(new Point(xCoordinate, yCoordinate));
  102. //start parsing next line
  103. i++;
  104. }
  105. //"parse" 'endline' token, syntax already checked at the beginning, and start parsing next line
  106. i++;
  107. //add line to drawing
  108. drawing.Add(new Line(newLine));
  109. //update lineStartPointer to the presumable start of the next line
  110. lineStartPointer = i;
  111. }
  112. //check if end of body is reached after there are no more line definitions
  113. if(i != allLines.Length - 1)
  114. {
  115. throw new FileImporterException("missing or invalid line definition token", "line definitions start with the 'line' token", (i + 1));
  116. }
  117. //return parsed picture
  118. return drawing;
  119. }
  120. /// <summary>
  121. /// connection point for testing use only: calls ParseISADInput(String[] allLines) and directly passes the given argument (effectively bypassing the File Input functionality)
  122. /// </summary>
  123. /// <param name="allLines">an array holding all lines of the input file</param>
  124. /// <returns>the width and height of the left canvas and the parsed picture as a list of lines</returns>
  125. public (int, int, List<Line>) ParseISADInputForTesting(String[] allLines)
  126. {
  127. return ParseISADInput(allLines);
  128. }
  129. }
  130. }