FileImporter.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /// pointer to the running instance of main program
  14. /// </summary>
  15. Form1 program;
  16. public FileImporter(Form1 newProgram)
  17. {
  18. program = newProgram;
  19. }
  20. /// <summary>
  21. /// parses a drawing consisting of line objects, given in the application specific .isad format
  22. /// </summary>
  23. /// <param name="fileName">the path of the input file</param>
  24. /// <returns>the width and height of the left canvas and the parsed picture as a list of lines</returns>
  25. public (int, int, List<Line>) ParseISADInput(String fileName)
  26. {
  27. String[] allLines = System.IO.File.ReadAllLines(fileName);
  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?\d*x?\d?\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. int i = 2;
  73. //parse 'line' token and complete line definition
  74. while (lineStartString.Equals(allLines[i]))
  75. {
  76. i++;
  77. List<Point> newLine = new List<Point>();
  78. while (!lineEndString.Equals(allLines[i]))
  79. {
  80. if (i == allLines.Length)
  81. {
  82. throw new FileImporterException("unterminated line definition", null, (i + 1));
  83. }
  84. //parse single point definition
  85. if (!Regex.Match(allLines[i], @"(\d?\d*;?\d?\d*?)?", RegexOptions.None).Success)
  86. {
  87. throw new FileImporterException("invalid Point definition: wrong format", "format: [xCoordinate];[yCoordinate]", (i + 1) );
  88. }
  89. String[] coordinates = allLines[i].Split(';');
  90. //no errors possible, convertability to string already checked above
  91. int xCoordinate = Convert.ToInt32(coordinates[0]);
  92. int yCoordinate = Convert.ToInt32(coordinates[1]);
  93. if (xCoordinate < 0 || yCoordinate < 0 || xCoordinate > width - 1 || yCoordinate > height - 1)
  94. {
  95. throw new FileImporterException("invalid Point definition: point out of bounds", null, (i + 1) );
  96. }
  97. newLine.Add(new Point(xCoordinate, yCoordinate));
  98. i++;
  99. }
  100. //parse 'endline' token
  101. i++;
  102. //add line to drawing
  103. drawing.Add(new Line(newLine));
  104. }
  105. //return parsed picture
  106. return drawing;
  107. }
  108. }
  109. }